]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailRegistration/EmailRegistrationPlugin.php
[TRANSLATION] Update license and copyright notice in translation files
[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 PLUGIN_VERSION = '2.0.0';
51
52     const CONFIRMTYPE = 'register';
53
54     function onArgsInitialize(&$args)
55     {
56         if (array_key_exists('action', $args) && $args['action'] == 'register') {
57             // YOINK!
58             $args['action'] = 'emailregister';
59         }
60         return true;
61     }
62
63     function onLoginAction($action, &$login)
64     {
65         if ($action == 'emailregister') {
66             $login = true;
67             return false;
68         }
69         return true;
70     }
71
72     function onStartLoadDoc(&$title, &$output)
73     {
74         $dir = dirname(__FILE__);
75
76         // @todo FIXME: i18n issue.
77         $docFile = DocFile::forTitle($title, $dir.'/doc-src/');
78
79         if (!empty($docFile)) {
80             $output = $docFile->toHTML();
81             return false;
82         }
83
84         return true;
85     }
86
87     static function registerEmail($email)
88     {
89         $old = User::getKV('email', $email);
90
91         if (!empty($old)) {
92             // TRANS: Error text when trying to register with an already registered e-mail address.
93             // TRANS: %s is the URL to recover password at.
94             throw new ClientException(sprintf(_m('A user with that email address already exists. You can use the '.
95                                                  '<a href="%s">password recovery</a> tool to recover a missing password.'),
96                                               common_local_url('recoverpassword')));
97         }
98
99         $valid = false;
100
101         if (Event::handle('StartValidateUserEmail', array(null, $email, &$valid))) {
102             $valid = Validate::email($email, common_config('email', 'check_domain'));
103             Event::handle('EndValidateUserEmail', array(null, $email, &$valid));
104         }
105
106         if (!$valid) {
107             // TRANS: Error text when trying to register with an invalid e-mail address.
108             throw new ClientException(_m('Not a valid email address.'));
109         }
110
111         try {
112             $confirm = Confirm_address::getByAddress($email, self::CONFIRMTYPE);
113         } catch (NoResultException $e) {
114             $confirm = Confirm_address::saveNew(null, $email, 'register');
115         }
116
117         return $confirm;
118     }
119
120     static function nicknameFromEmail($email)
121     {
122         $parts = explode('@', $email);
123
124         $nickname = $parts[0];
125
126         $nickname = preg_replace('/[^A-Za-z0-9]/', '', $nickname);
127
128         $nickname = Nickname::normalize($nickname);
129
130         $original = $nickname;
131
132         $n = 0;
133
134         while (User::getKV('nickname', $nickname)) {
135             $n++;
136             $nickname = $original . $n;
137         }
138
139         return $nickname;
140     }
141
142     static function sendConfirmEmail($confirm, $title=null)
143     {
144         $sitename = common_config('site', 'name');
145
146         $recipients = array($confirm->address);
147
148         $headers['From'] = mail_notify_from();
149         $headers['To'] = trim($confirm->address);
150          // TRANS: Subject for confirmation e-mail.
151          // TRANS: %s is the StatusNet sitename.
152         $headers['Subject'] = sprintf(_m('Welcome to %s'), $sitename);
153         $headers['Content-Type'] = 'text/html; charset=UTF-8';
154
155         $confirmUrl = common_local_url('register', array('code' => $confirm->code));
156
157         if (empty($title)) {
158             $title = 'confirmemailreg';
159         }
160
161         $confirmTemplate = DocFile::forTitle($title, DocFile::mailPaths());
162
163         $body = $confirmTemplate->toHTML(array('confirmurl' => $confirmUrl));
164
165         mail_send($recipients, $headers, $body);
166     }
167
168     function onEndDocFileForTitle($title, $paths, &$filename)
169     {
170         if ($title == 'confirmemailreg' && empty($filename)) {
171             $filename = dirname(__FILE__).'/mail-src/'.$title;
172             return false;
173         }
174         return true;
175     }
176
177     function onPluginVersion(array &$versions)
178     {
179         $versions[] = array('name' => 'EmailRegistration',
180                             'version' => self::PLUGIN_VERSION,
181                             'author' => 'Evan Prodromou',
182                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/EmailRegistration',
183                             'rawdescription' =>
184                             // TRANS: Plugin description.
185                             _m('Use email only for registration.'));
186         return true;
187     }
188 }