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