]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailRegistration/EmailRegistrationPlugin.php
8759614129b8f1feb8ed1926e4ea4733475bcc38
[quix0rs-gnu-social.git] / plugins / EmailRegistration / EmailRegistrationPlugin.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Email-based registration, as on the StatusNet OnDemand service
7  *
8  * PHP version 5
9  *
10  * 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  Email registration
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2011 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 /**
38  * Email based registration plugin
39  *
40  * @category  Email registration
41  * @package   StatusNet
42  * @author    Brion Vibber <brionv@status.net>
43  * @author    Evan Prodromou <evan@status.net>
44  * @copyright 2011 StatusNet, Inc.
45  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
46  * @link      http://status.net/
47  */
48 class EmailRegistrationPlugin extends Plugin
49 {
50     const CONFIRMTYPE = 'register';
51
52     function onAutoload($cls)
53     {
54         $dir = dirname(__FILE__);
55
56         switch ($cls)
57         {
58         case 'EmailregisterAction':
59             include_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
60             return false;
61         case 'EmailRegistrationForm':
62         case 'ConfirmRegistrationForm':
63             include_once $dir . '/' . strtolower($cls) . '.php';
64             return false;
65         default:
66             return true;
67         }
68     }
69
70     function onArgsInitialize(&$args)
71     {
72         if (array_key_exists('action', $args) && $args['action'] == 'register') {
73             // YOINK!
74             $args['action'] = 'emailregister';
75         }
76         return true;
77     }
78
79     function onLoginAction($action, &$login)
80     {
81         if ($action == 'emailregister') {
82             $login = true;
83             return false;
84         }
85         return true;
86     }
87
88     function onStartLoadDoc(&$title, &$output)
89     {
90         $dir = dirname(__FILE__);
91
92         // @todo FIXME: i18n issue.
93         $docFile = DocFile::forTitle($title, $dir.'/doc-src/');
94
95         if (!empty($docFile)) {
96             $output = $docFile->toHTML();
97             return false;
98         }
99
100         return true;
101     }
102
103     static function registerEmail($email)
104     {
105         $old = User::staticGet('email', $email);
106
107         if (!empty($old)) {
108             // TRANS: Error text when trying to register with an already registered e-mail address.
109             // TRANS: %s is the URL to recover password at.
110             throw new ClientException(sprintf(_m('A user with that email address already exists. You can use the '.
111                                                  '<a href="%s">password recovery</a> tool to recover a missing password.'),
112                                               common_local_url('recoverpassword')));
113         }
114
115         $valid = false;
116
117         if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
118             $valid = Validate::email($email, common_config('email', 'check_domain'));
119             Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
120         }
121
122         if (!$valid) {
123             // TRANS: Error text when trying to register with an invalid e-mail address.
124             throw new ClientException(_m('Not a valid email address.'));
125         }
126
127         $confirm = Confirm_address::getAddress($email, self::CONFIRMTYPE);
128
129         if (empty($confirm)) {
130             $confirm = Confirm_address::saveNew(null, $email, 'register');
131         }
132
133         return $confirm;
134     }
135
136     static function nicknameFromEmail($email)
137     {
138         $parts = explode('@', $email);
139
140         $nickname = $parts[0];
141
142         $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
143
144         $nickname = Nickname::normalize($nickname);
145
146         $original = $nickname;
147
148         $n = 0;
149
150         while (User::staticGet('nickname', $nickname)) {
151             $n++;
152             $nickname = $original . $n;
153         }
154
155         return $nickname;
156     }
157
158     static function sendConfirmEmail($confirm, $title=null)
159     {
160         $sitename = common_config('site', 'name');
161
162         $recipients = array($confirm->address);
163
164         $headers['From'] = mail_notify_from();
165         $headers['To'] = trim($confirm->address);
166          // TRANS: Subject for confirmation e-mail.
167          // TRANS: %s is the StatusNet sitename.
168         $headers['Subject'] = sprintf(_m('Welcome to %s'), $sitename);
169         $headers['Content-Type'] = 'text/html; charset=UTF-8';
170
171         $confirmUrl = common_local_url('register', array('code' => $confirm->code));
172
173         if (empty($title)) {
174             $title = 'confirmemailreg';
175         }
176
177         $confirmTemplate = DocFile::forTitle($title, DocFile::mailPaths());
178
179         $body = $confirmTemplate->toHTML(array('confirmurl' => $confirmUrl));
180
181         mail_send($recipients, $headers, $body);
182     }
183
184     function onEndDocFileForTitle($title, $paths, &$filename)
185     {
186         if ($title == 'confirmemailreg' && empty($filename)) {
187             $filename = dirname(__FILE__).'/mail-src/'.$title;
188             return false;
189         }
190         return true;
191     }
192
193     function onPluginVersion(&$versions)
194     {
195         $versions[] = array('name' => 'EmailRegistration',
196                             'version' => STATUSNET_VERSION,
197                             'author' => 'Evan Prodromou',
198                             'homepage' => 'http://status.net/wiki/Plugin:EmailRegistration',
199                             'rawdescription' =>
200                             // TRANS: Plugin description.
201                             _m('Use email only for registration.'));
202         return true;
203     }
204 }