EmailRegistrationPlugin.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 PLUGIN_VERSION = '2.0.0';
  49. const CONFIRMTYPE = 'register';
  50. function onArgsInitialize(&$args)
  51. {
  52. if (array_key_exists('action', $args) && $args['action'] == 'register') {
  53. // YOINK!
  54. $args['action'] = 'emailregister';
  55. }
  56. return true;
  57. }
  58. function onLoginAction($action, &$login)
  59. {
  60. if ($action == 'emailregister') {
  61. $login = true;
  62. return false;
  63. }
  64. return true;
  65. }
  66. function onStartLoadDoc(&$title, &$output)
  67. {
  68. $dir = dirname(__FILE__);
  69. // @todo FIXME: i18n issue.
  70. $docFile = DocFile::forTitle($title, $dir.'/doc-src/');
  71. if (!empty($docFile)) {
  72. $output = $docFile->toHTML();
  73. return false;
  74. }
  75. return true;
  76. }
  77. static function registerEmail($email)
  78. {
  79. $old = User::getKV('email', $email);
  80. if (!empty($old)) {
  81. // TRANS: Error text when trying to register with an already registered e-mail address.
  82. // TRANS: %s is the URL to recover password at.
  83. throw new ClientException(sprintf(_m('A user with that email address already exists. You can use the '.
  84. '<a href="%s">password recovery</a> tool to recover a missing password.'),
  85. common_local_url('recoverpassword')));
  86. }
  87. $valid = false;
  88. if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
  89. $valid = Validate::email($email, common_config('email', 'check_domain'));
  90. Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
  91. }
  92. if (!$valid) {
  93. // TRANS: Error text when trying to register with an invalid e-mail address.
  94. throw new ClientException(_m('Not a valid email address.'));
  95. }
  96. try {
  97. $confirm = Confirm_address::getByAddress($email, self::CONFIRMTYPE);
  98. } catch (NoResultException $e) {
  99. $confirm = Confirm_address::saveNew(null, $email, 'register');
  100. }
  101. return $confirm;
  102. }
  103. static function nicknameFromEmail($email)
  104. {
  105. $parts = explode('@', $email);
  106. $nickname = $parts[0];
  107. $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
  108. $nickname = Nickname::normalize($nickname);
  109. $original = $nickname;
  110. $n = 0;
  111. while (User::getKV('nickname', $nickname)) {
  112. $n++;
  113. $nickname = $original . $n;
  114. }
  115. return $nickname;
  116. }
  117. static function sendConfirmEmail($confirm, $title=null)
  118. {
  119. $sitename = common_config('site', 'name');
  120. $recipients = array($confirm->address);
  121. $headers['From'] = mail_notify_from();
  122. $headers['To'] = trim($confirm->address);
  123. // TRANS: Subject for confirmation e-mail.
  124. // TRANS: %s is the StatusNet sitename.
  125. $headers['Subject'] = sprintf(_m('Welcome to %s'), $sitename);
  126. $headers['Content-Type'] = 'text/html; charset=UTF-8';
  127. $confirmUrl = common_local_url('register', array('code' => $confirm->code));
  128. if (empty($title)) {
  129. $title = 'confirmemailreg';
  130. }
  131. $confirmTemplate = DocFile::forTitle($title, DocFile::mailPaths());
  132. $body = $confirmTemplate->toHTML(array('confirmurl' => $confirmUrl));
  133. mail_send($recipients, $headers, $body);
  134. }
  135. function onEndDocFileForTitle($title, $paths, &$filename)
  136. {
  137. if ($title == 'confirmemailreg' && empty($filename)) {
  138. $filename = dirname(__FILE__).'/mail-src/'.$title;
  139. return false;
  140. }
  141. return true;
  142. }
  143. public function onPluginVersion(array &$versions): bool
  144. {
  145. $versions[] = array('name' => 'EmailRegistration',
  146. 'version' => self::PLUGIN_VERSION,
  147. 'author' => 'Evan Prodromou',
  148. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/EmailRegistration',
  149. 'rawdescription' =>
  150. // TRANS: Plugin description.
  151. _m('Use email only for registration.'));
  152. return true;
  153. }
  154. }