3 * StatusNet, the distributed open-source microblogging tool
5 * Plugin that requires the user to have a validated email address before they
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.
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.
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/>.
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/
34 if (!defined('STATUSNET') && !defined('LACONICA')) {
39 * Plugin for requiring a validated email before posting.
41 * Enable this plugin using addPlugin('RequireValidatedEmail');
45 * @author Craig Andrews <candrews@integralblue.com>
46 * @author Brion Vibber <brion@status.net>
47 * @author Evan Prodromou <evan@status.net>
48 * @author Mikael Nordfeldth <mmn@hethane.se>
49 * @copyright 2009-2013 Free Software Foundation, Inc http://www.fsf.org
50 * @copyright 2009-2010 StatusNet, Inc.
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
52 * @link http://status.net/
54 class RequireValidatedEmailPlugin extends Plugin
57 * Users created before this time will be grandfathered in
58 * without the validation requirement.
60 public $grandfatherCutoff = null;
63 * If OpenID plugin is installed, users with a verified OpenID
64 * association whose provider URL matches one of these regexes
65 * will be considered to be sufficiently valid for our needs.
67 * For example, to trust WikiHow and Wikipedia OpenID users:
69 * addPlugin('RequireValidatedEmailPlugin', array(
70 * 'trustedOpenIDs' => array(
71 * '!^http://\w+\.wikihow\.com/!',
72 * '!^http://\w+\.wikipedia\.org/!',
76 public $trustedOpenIDs = array();
79 * Whether or not to disallow login for unvalidated users.
81 public $disallowLogin = false;
83 function onAutoload($cls)
85 $dir = dirname(__FILE__);
89 case 'ConfirmfirstemailAction':
90 include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
97 function onRouterInitialized($m)
99 $m->connect('main/confirmfirst/:code',
100 array('action' => 'confirmfirstemail'));
105 * Event handler for notice saves; rejects the notice
106 * if user's address isn't validated.
108 * @param Notice $notice The notice being saved
110 * @return bool hook result code
112 function onStartNoticeSave($notice)
114 $user = User::getKV('id', $notice->profile_id);
115 if (!empty($user)) { // it's a remote notice
116 if (!$this->validated($user)) {
117 // TRANS: Client exception thrown when trying to post notices before validating an e-mail address.
118 $msg = _m('You must validate your email address before posting.');
119 throw new ClientException($msg);
126 * Event handler for registration attempts; rejects the registration
127 * if email field is missing.
129 * @param Action $action Action being executed
131 * @return bool hook result code
133 function onStartRegisterUser(&$user, &$profile)
135 $email = $user->email;
138 // TRANS: Client exception thrown when trying to register without providing an e-mail address.
139 throw new ClientException(_m('You must provide an email address to register.'));
146 * Check if a user has a validated email address or has been
147 * otherwise grandfathered in.
149 * @param User $user User to valide
153 protected function validated($user)
155 // The email field is only stored after validation...
156 // Until then you'll find them in confirm_address.
157 $knownGood = !empty($user->email) ||
158 $this->grandfathered($user) ||
159 $this->hasTrustedOpenID($user);
161 // Give other plugins a chance to override, if they can validate
162 // that somebody's ok despite a non-validated email.
164 // @todo FIXME: This isn't how to do it! Use Start*/End* instead
165 Event::handle('RequireValidatedEmailPlugin_Override',
166 array($user, &$knownGood));
172 * Check if a user was created before the grandfathering cutoff.
173 * If so, we won't need to check for validation.
175 * @param User $user User to check
177 * @return bool true if user is grandfathered
179 protected function grandfathered($user)
181 if ($this->grandfatherCutoff) {
182 $created = strtotime($user->created . " GMT");
183 $cutoff = strtotime($this->grandfatherCutoff);
184 if ($created < $cutoff) {
192 * Override for RequireValidatedEmail plugin. If we have a user who's
193 * not validated an e-mail, but did come from a trusted provider,
194 * we'll consider them ok.
196 * @param User $user User to check
198 * @return bool true if user has a trusted OpenID.
200 function hasTrustedOpenID($user)
202 if ($this->trustedOpenIDs && class_exists('User_openid')) {
203 foreach ($this->trustedOpenIDs as $regex) {
204 $oid = new User_openid();
206 $oid->user_id = $user->id;
209 while ($oid->fetch()) {
210 if (preg_match($regex, $oid->canonical)) {
220 * Add version information for this plugin.
222 * @param array &$versions Array of associative arrays of version data
224 * @return boolean hook value
226 function onPluginVersion(&$versions)
229 array('name' => 'Require Validated Email',
230 'version' => STATUSNET_VERSION,
231 'author' => 'Craig Andrews, '.
235 'http://status.net/wiki/Plugin:RequireValidatedEmail',
237 // TRANS: Plugin description.
238 _m('Disables posting without a validated email address.'));
244 * Show an error message about validating user email before posting
246 * @param string $tag Current tab tag value
247 * @param Action $action action being shown
248 * @param Form $form object producing the form
250 * @return boolean hook value
252 function onStartMakeEntryForm($tag, $action, &$form)
254 $user = common_current_user();
256 if (!$this->validated($user)) {
257 $action->element('div', array('class'=>'error'), _m('You must validate an email address before posting!'));
264 * Prevent unvalidated folks from creating spam groups.
266 * @param Profile $profile User profile we're checking
267 * @param string $right rights key
268 * @param boolean $result if overriding, set to true/false has right
269 * @return boolean hook result value
271 function onUserRightsCheck(Profile $profile, $right, &$result)
273 if ($right == Right::CREATEGROUP ||
274 ($this->disallowLogin && ($right == Right::WEBLOGIN || $right == Right::API))) {
275 $user = User::getKV('id', $profile->id);
276 if ($user && !$this->validated($user)) {
284 function onLoginAction($action, &$login)
286 if ($action == 'confirmfirstemail') {