EmailRegistrationPlugin.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Email-based registration, as on the StatusNet OnDemand service
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Email registration
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Email based registration plugin
  37. *
  38. * @category Email registration
  39. * @package StatusNet
  40. * @author Brion Vibber <brionv@status.net>
  41. * @author Evan Prodromou <evan@status.net>
  42. * @copyright 2011 StatusNet, Inc.
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  44. * @link http://status.net/
  45. */
  46. class EmailRegistrationPlugin extends Plugin
  47. {
  48. const CONFIRMTYPE = 'register';
  49. function onArgsInitialize(&$args)
  50. {
  51. if (array_key_exists('action', $args) && $args['action'] == 'register') {
  52. // YOINK!
  53. $args['action'] = 'emailregister';
  54. }
  55. return true;
  56. }
  57. function onLoginAction($action, &$login)
  58. {
  59. if ($action == 'emailregister') {
  60. $login = true;
  61. return false;
  62. }
  63. return true;
  64. }
  65. function onStartLoadDoc(&$title, &$output)
  66. {
  67. $dir = dirname(__FILE__);
  68. // @todo FIXME: i18n issue.
  69. $docFile = DocFile::forTitle($title, $dir.'/doc-src/');
  70. if (!empty($docFile)) {
  71. $output = $docFile->toHTML();
  72. return false;
  73. }
  74. return true;
  75. }
  76. static function registerEmail($email)
  77. {
  78. $old = User::getKV('email', $email);
  79. if (!empty($old)) {
  80. // TRANS: Error text when trying to register with an already registered e-mail address.
  81. // TRANS: %s is the URL to recover password at.
  82. throw new ClientException(sprintf(_m('A user with that email address already exists. You can use the '.
  83. '<a href="%s">password recovery</a> tool to recover a missing password.'),
  84. common_local_url('recoverpassword')));
  85. }
  86. $valid = false;
  87. if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
  88. $valid = Validate::email($email, common_config('email', 'check_domain'));
  89. Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
  90. }
  91. if (!$valid) {
  92. // TRANS: Error text when trying to register with an invalid e-mail address.
  93. throw new ClientException(_m('Not a valid email address.'));
  94. }
  95. try {
  96. $confirm = Confirm_address::getByAddress($email, self::CONFIRMTYPE);
  97. } catch (NoResultException $e) {
  98. $confirm = Confirm_address::saveNew(null, $email, 'register');
  99. }
  100. return $confirm;
  101. }
  102. static function nicknameFromEmail($email)
  103. {
  104. $parts = explode('@', $email);
  105. $nickname = $parts[0];
  106. $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
  107. $nickname = Nickname::normalize($nickname);
  108. $original = $nickname;
  109. $n = 0;
  110. while (User::getKV('nickname', $nickname)) {
  111. $n++;
  112. $nickname = $original . $n;
  113. }
  114. return $nickname;
  115. }
  116. static function sendConfirmEmail($confirm, $title=null)
  117. {
  118. $sitename = common_config('site', 'name');
  119. $recipients = array($confirm->address);
  120. $headers['From'] = mail_notify_from();
  121. $headers['To'] = trim($confirm->address);
  122. // TRANS: Subject for confirmation e-mail.
  123. // TRANS: %s is the StatusNet sitename.
  124. $headers['Subject'] = sprintf(_m('Welcome to %s'), $sitename);
  125. $headers['Content-Type'] = 'text/html; charset=UTF-8';
  126. $confirmUrl = common_local_url('register', array('code' => $confirm->code));
  127. if (empty($title)) {
  128. $title = 'confirmemailreg';
  129. }
  130. $confirmTemplate = DocFile::forTitle($title, DocFile::mailPaths());
  131. $body = $confirmTemplate->toHTML(array('confirmurl' => $confirmUrl));
  132. mail_send($recipients, $headers, $body);
  133. }
  134. function onEndDocFileForTitle($title, $paths, &$filename)
  135. {
  136. if ($title == 'confirmemailreg' && empty($filename)) {
  137. $filename = dirname(__FILE__).'/mail-src/'.$title;
  138. return false;
  139. }
  140. return true;
  141. }
  142. function onPluginVersion(array &$versions)
  143. {
  144. $versions[] = array('name' => 'EmailRegistration',
  145. 'version' => GNUSOCIAL_VERSION,
  146. 'author' => 'Evan Prodromou',
  147. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/EmailRegistration',
  148. 'rawdescription' =>
  149. // TRANS: Plugin description.
  150. _m('Use email only for registration.'));
  151. return true;
  152. }
  153. }