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. $confirm = Confirm_address::getAddress($email, self::CONFIRMTYPE);
  96. if (empty($confirm)) {
  97. $confirm = Confirm_address::saveNew(null, $email, 'register');
  98. }
  99. return $confirm;
  100. }
  101. static function nicknameFromEmail($email)
  102. {
  103. $parts = explode('@', $email);
  104. $nickname = $parts[0];
  105. $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
  106. $nickname = Nickname::normalize($nickname);
  107. $original = $nickname;
  108. $n = 0;
  109. while (User::getKV('nickname', $nickname)) {
  110. $n++;
  111. $nickname = $original . $n;
  112. }
  113. return $nickname;
  114. }
  115. static function sendConfirmEmail($confirm, $title=null)
  116. {
  117. $sitename = common_config('site', 'name');
  118. $recipients = array($confirm->address);
  119. $headers['From'] = mail_notify_from();
  120. $headers['To'] = trim($confirm->address);
  121. // TRANS: Subject for confirmation e-mail.
  122. // TRANS: %s is the StatusNet sitename.
  123. $headers['Subject'] = sprintf(_m('Welcome to %s'), $sitename);
  124. $headers['Content-Type'] = 'text/html; charset=UTF-8';
  125. $confirmUrl = common_local_url('register', array('code' => $confirm->code));
  126. if (empty($title)) {
  127. $title = 'confirmemailreg';
  128. }
  129. $confirmTemplate = DocFile::forTitle($title, DocFile::mailPaths());
  130. $body = $confirmTemplate->toHTML(array('confirmurl' => $confirmUrl));
  131. mail_send($recipients, $headers, $body);
  132. }
  133. function onEndDocFileForTitle($title, $paths, &$filename)
  134. {
  135. if ($title == 'confirmemailreg' && empty($filename)) {
  136. $filename = dirname(__FILE__).'/mail-src/'.$title;
  137. return false;
  138. }
  139. return true;
  140. }
  141. function onPluginVersion(&$versions)
  142. {
  143. $versions[] = array('name' => 'EmailRegistration',
  144. 'version' => GNUSOCIAL_VERSION,
  145. 'author' => 'Evan Prodromou',
  146. 'homepage' => 'http://status.net/wiki/Plugin:EmailRegistration',
  147. 'rawdescription' =>
  148. // TRANS: Plugin description.
  149. _m('Use email only for registration.'));
  150. return true;
  151. }
  152. }