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