RequireValidatedEmailPlugin.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Plugin that requires the user to have a validated email address before they
  6. * can post notices
  7. *
  8. * PHP version 5
  9. *
  10. * LICENCE: 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 Plugin
  24. * @package StatusNet
  25. * @author Craig Andrews <candrews@integralblue.com>
  26. * @author Brion Vibber <brion@status.net>
  27. * @author Evan Prodromou <evan@status.net>
  28. * @copyright 2011 StatusNet Inc. http://status.net/
  29. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  30. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  31. * @link http://status.net/
  32. */
  33. if (!defined('STATUSNET') && !defined('LACONICA')) {
  34. exit(1);
  35. }
  36. /**
  37. * Plugin for requiring a validated email before posting.
  38. *
  39. * Enable this plugin using addPlugin('RequireValidatedEmail');
  40. *
  41. * @category Plugin
  42. * @package StatusNet
  43. * @author Craig Andrews <candrews@integralblue.com>
  44. * @author Brion Vibber <brion@status.net>
  45. * @author Evan Prodromou <evan@status.net>
  46. * @author Mikael Nordfeldth <mmn@hethane.se>
  47. * @copyright 2009-2013 Free Software Foundation, Inc http://www.fsf.org
  48. * @copyright 2009-2010 StatusNet, Inc.
  49. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  50. * @link http://status.net/
  51. */
  52. class RequireValidatedEmailPlugin extends Plugin
  53. {
  54. /**
  55. * Users created before this time will be grandfathered in
  56. * without the validation requirement.
  57. */
  58. public $grandfatherCutoff = null;
  59. /**
  60. * If OpenID plugin is installed, users with a verified OpenID
  61. * association whose provider URL matches one of these regexes
  62. * will be considered to be sufficiently valid for our needs.
  63. *
  64. * For example, to trust WikiHow and Wikipedia OpenID users:
  65. *
  66. * addPlugin('RequireValidatedEmailPlugin', array(
  67. * 'trustedOpenIDs' => array(
  68. * '!^http://\w+\.wikihow\.com/!',
  69. * '!^http://\w+\.wikipedia\.org/!',
  70. * ),
  71. * ));
  72. */
  73. public $trustedOpenIDs = array();
  74. /**
  75. * Whether or not to disallow login for unvalidated users.
  76. */
  77. public $disallowLogin = false;
  78. function onRouterInitialized($m)
  79. {
  80. $m->connect('main/confirmfirst/:code',
  81. array('action' => 'confirmfirstemail'));
  82. return true;
  83. }
  84. /**
  85. * Event handler for notice saves; rejects the notice
  86. * if user's address isn't validated.
  87. *
  88. * @param Notice $notice The notice being saved
  89. *
  90. * @return bool hook result code
  91. */
  92. function onStartNoticeSave($notice)
  93. {
  94. $user = User::getKV('id', $notice->profile_id);
  95. if (!empty($user)) { // it's a remote notice
  96. if (!$this->validated($user)) {
  97. // TRANS: Client exception thrown when trying to post notices before validating an e-mail address.
  98. $msg = _m('You must validate your email address before posting.');
  99. throw new ClientException($msg);
  100. }
  101. }
  102. return true;
  103. }
  104. /**
  105. * Event handler for registration attempts; rejects the registration
  106. * if email field is missing.
  107. *
  108. * @param Action $action Action being executed
  109. *
  110. * @return bool hook result code
  111. */
  112. function onStartRegisterUser(&$user, &$profile)
  113. {
  114. $email = $user->email;
  115. if (empty($email)) {
  116. // TRANS: Client exception thrown when trying to register without providing an e-mail address.
  117. throw new ClientException(_m('You must provide an email address to register.'));
  118. }
  119. return true;
  120. }
  121. /**
  122. * Check if a user has a validated email address or has been
  123. * otherwise grandfathered in.
  124. *
  125. * @param User $user User to valide
  126. *
  127. * @return bool
  128. */
  129. protected function validated($user)
  130. {
  131. // The email field is only stored after validation...
  132. // Until then you'll find them in confirm_address.
  133. $knownGood = !empty($user->email) ||
  134. $this->grandfathered($user) ||
  135. $this->hasTrustedOpenID($user);
  136. // Give other plugins a chance to override, if they can validate
  137. // that somebody's ok despite a non-validated email.
  138. // @todo FIXME: This isn't how to do it! Use Start*/End* instead
  139. Event::handle('RequireValidatedEmailPlugin_Override',
  140. array($user, &$knownGood));
  141. return $knownGood;
  142. }
  143. /**
  144. * Check if a user was created before the grandfathering cutoff.
  145. * If so, we won't need to check for validation.
  146. *
  147. * @param User $user User to check
  148. *
  149. * @return bool true if user is grandfathered
  150. */
  151. protected function grandfathered($user)
  152. {
  153. if ($this->grandfatherCutoff) {
  154. $created = strtotime($user->created . " GMT");
  155. $cutoff = strtotime($this->grandfatherCutoff);
  156. if ($created < $cutoff) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. /**
  163. * Override for RequireValidatedEmail plugin. If we have a user who's
  164. * not validated an e-mail, but did come from a trusted provider,
  165. * we'll consider them ok.
  166. *
  167. * @param User $user User to check
  168. *
  169. * @return bool true if user has a trusted OpenID.
  170. */
  171. function hasTrustedOpenID($user)
  172. {
  173. if ($this->trustedOpenIDs && class_exists('User_openid')) {
  174. foreach ($this->trustedOpenIDs as $regex) {
  175. $oid = new User_openid();
  176. $oid->user_id = $user->id;
  177. $oid->find();
  178. while ($oid->fetch()) {
  179. if (preg_match($regex, $oid->canonical)) {
  180. return true;
  181. }
  182. }
  183. }
  184. }
  185. return false;
  186. }
  187. /**
  188. * Add version information for this plugin.
  189. *
  190. * @param array &$versions Array of associative arrays of version data
  191. *
  192. * @return boolean hook value
  193. */
  194. function onPluginVersion(&$versions)
  195. {
  196. $versions[] =
  197. array('name' => 'Require Validated Email',
  198. 'version' => GNUSOCIAL_VERSION,
  199. 'author' => 'Craig Andrews, '.
  200. 'Evan Prodromou, '.
  201. 'Brion Vibber',
  202. 'homepage' =>
  203. 'http://status.net/wiki/Plugin:RequireValidatedEmail',
  204. 'rawdescription' =>
  205. // TRANS: Plugin description.
  206. _m('Disables posting without a validated email address.'));
  207. return true;
  208. }
  209. /**
  210. * Show an error message about validating user email before posting
  211. *
  212. * @param string $tag Current tab tag value
  213. * @param Action $action action being shown
  214. * @param Form $form object producing the form
  215. *
  216. * @return boolean hook value
  217. */
  218. function onStartMakeEntryForm($tag, $action, &$form)
  219. {
  220. $user = common_current_user();
  221. if (!empty($user)) {
  222. if (!$this->validated($user)) {
  223. $action->element('div', array('class'=>'error'), _m('You must validate an email address before posting!'));
  224. }
  225. }
  226. return true;
  227. }
  228. /**
  229. * Prevent unvalidated folks from creating spam groups.
  230. *
  231. * @param Profile $profile User profile we're checking
  232. * @param string $right rights key
  233. * @param boolean $result if overriding, set to true/false has right
  234. * @return boolean hook result value
  235. */
  236. function onUserRightsCheck(Profile $profile, $right, &$result)
  237. {
  238. if ($right == Right::CREATEGROUP ||
  239. ($this->disallowLogin && ($right == Right::WEBLOGIN || $right == Right::API))) {
  240. $user = User::getKV('id', $profile->id);
  241. if ($user && !$this->validated($user)) {
  242. $result = false;
  243. return false;
  244. }
  245. }
  246. return true;
  247. }
  248. function onLoginAction($action, &$login)
  249. {
  250. if ($action == 'confirmfirstemail') {
  251. $login = true;
  252. return false;
  253. }
  254. return true;
  255. }
  256. }