]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RequireValidatedEmail/RequireValidatedEmailPlugin.php
disallow login for users without validated email
[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  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
49  * @copyright 2009-2010 StatusNet, Inc.
50  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
51  * @link      http://status.net/
52  */
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
61     public $grandfatherCutoff = null;
62
63     /**
64      * If OpenID plugin is installed, users with a verified OpenID
65      * association whose provider URL matches one of these regexes
66      * will be considered to be sufficiently valid for our needs.
67      *
68      * For example, to trust WikiHow and Wikipedia OpenID users:
69      *
70      * addPlugin('RequireValidatedEmailPlugin', array(
71      *    'trustedOpenIDs' => array(
72      *        '!^http://\w+\.wikihow\.com/!',
73      *        '!^http://\w+\.wikipedia\.org/!',
74      *    ),
75      * ));
76      */
77
78     public $trustedOpenIDs = array();
79
80     /**
81      * Whether or not to disallow login for unvalidated users.
82      */
83
84     public $disallowLogin = false;
85
86     /**
87      * Event handler for notice saves; rejects the notice
88      * if user's address isn't validated.
89      *
90      * @param Notice $notice The notice being saved
91      *
92      * @return bool hook result code
93      */
94
95     function onStartNoticeSave($notice)
96     {
97         $user = User::staticGet('id', $notice->profile_id);
98         if (!empty($user)) { // it's a remote notice
99             if (!$this->validated($user)) {
100                 $msg = _m("You must validate your email address before posting.");
101                 throw new ClientException($msg);
102             }
103         }
104         return true;
105     }
106
107     /**
108      * Event handler for registration attempts; rejects the registration
109      * if email field is missing.
110      *
111      * @param Action $action Action being executed
112      *
113      * @return bool hook result code
114      */
115     function onStartRegistrationTry($action)
116     {
117         $email = $action->trimmed('email');
118
119         if (empty($email)) {
120             $action->showForm(_m('You must provide an email address to register.'));
121             return false;
122         }
123
124         // Default form will run address format validation and reject if bad.
125
126         return true;
127     }
128
129     /**
130      * Check if a user has a validated email address or has been
131      * otherwise grandfathered in.
132      *
133      * @param User $user User to valide
134      *
135      * @return bool
136      */
137     protected function validated($user)
138     {
139         // The email field is only stored after validation...
140         // Until then you'll find them in confirm_address.
141         $knownGood = !empty($user->email) ||
142           $this->grandfathered($user) ||
143           $this->hasTrustedOpenID($user);
144
145         // Give other plugins a chance to override, if they can validate
146         // that somebody's ok despite a non-validated email.
147
148         // FIXME: This isn't how to do it! Use Start*/End* instead
149
150         Event::handle('RequireValidatedEmailPlugin_Override',
151                       array($user, &$knownGood));
152
153         return $knownGood;
154     }
155
156     /**
157      * Check if a user was created before the grandfathering cutoff.
158      * If so, we won't need to check for validation.
159      *
160      * @param User $user User to check
161      *
162      * @return bool true if user is grandfathered
163      */
164     protected function grandfathered($user)
165     {
166         if ($this->grandfatherCutoff) {
167             $created = strtotime($user->created . " GMT");
168             $cutoff  = strtotime($this->grandfatherCutoff);
169             if ($created < $cutoff) {
170                 return true;
171             }
172         }
173         return false;
174     }
175
176     /**
177      * Override for RequireValidatedEmail plugin. If we have a user who's
178      * not validated an e-mail, but did come from a trusted provider,
179      * we'll consider them ok.
180      *
181      * @param User $user User to check
182      *
183      * @return bool true if user has a trusted OpenID.
184      */
185
186     function hasTrustedOpenID($user)
187     {
188         if ($this->trustedOpenIDs && class_exists('User_openid')) {
189             foreach ($this->trustedOpenIDs as $regex) {
190                 $oid = new User_openid();
191
192                 $oid->user_id = $user->id;
193
194                 $oid->find();
195                 while ($oid->fetch()) {
196                     if (preg_match($regex, $oid->canonical)) {
197                         return true;
198                     }
199                 }
200             }
201         }
202         return false;
203     }
204
205     /**
206      * Add version information for this plugin.
207      *
208      * @param array &$versions Array of associative arrays of version data
209      *
210      * @return boolean hook value
211      */
212
213     function onPluginVersion(&$versions)
214     {
215         $versions[] =
216           array('name' => 'Require Validated Email',
217                 'version' => STATUSNET_VERSION,
218                 'author' => 'Craig Andrews, '.
219                 'Evan Prodromou, '.
220                 'Brion Vibber',
221                 'homepage' =>
222                 'http://status.net/wiki/Plugin:RequireValidatedEmail',
223                 'rawdescription' =>
224                 _m('Disables posting without a validated email address.'));
225         return true;
226     }
227
228     /**
229      * Hide the notice form if the user isn't able to post.
230      *
231      * @param Action $action action being shown
232      *
233      * @return boolean hook value
234      */
235
236     function onStartShowNoticeForm($action)
237     {
238         $user = common_current_user();
239         if (!empty($user)) { // it's a remote notice
240             if (!$this->validated($user)) {
241                 return false;
242             }
243         }
244         return true;
245     }
246
247     /**
248      * Prevent unvalidated folks from creating spam groups.
249      *
250      * @param Profile $profile User profile we're checking
251      * @param string $right rights key
252      * @param boolean $result if overriding, set to true/false has right
253      * @return boolean hook result value
254      */
255     function onUserRightsCheck(Profile $profile, $right, &$result)
256     {
257         if ($right == Right::CREATEGROUP ||
258             ($this->disallowLogin && ($right == Right::WEBLOGIN || $right == Right::API))) {
259             $user = User::staticGet('id', $profile->id);
260             if ($user && !$this->validated($user)) {
261                 $result = false;
262                 return false;
263             }
264         }
265         return true;
266     }
267 }