RequireValidatedEmailPlugin.php 8.5 KB

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