]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RequireValidatedEmail/confirmfirstemail.php
0b6843fa470b913595af3f4bcaa56e7a761620a1
[quix0rs-gnu-social.git] / plugins / RequireValidatedEmail / confirmfirstemail.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * Action for confirming first email registration
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  Confirmation
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  * Class comment
39  *
40  * @category  Action
41  * @package   StatusNet
42  * @author    Evan Prodromou <evan@status.net>
43  * @copyright 2011 StatusNet, Inc.
44  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
45  * @link      http://status.net/
46  */
47 class ConfirmfirstemailAction extends Action
48 {
49     public $confirm;
50     public $code;
51     public $password;
52     public $user;
53
54     /**
55      * For initializing members of the class.
56      *
57      * @param array $argarray misc. arguments
58      *
59      * @return boolean true
60      */
61     function prepare($argarray)
62     {
63         parent::prepare($argarray);
64         $user = common_current_user();
65
66         if (!empty($user)) {
67             // TRANS: Client exception thrown when trying to register while already logged in.
68             throw new ClientException(_m('You are already logged in.'));
69         }
70
71         $this->code = $this->trimmed('code');
72
73         $this->confirm = Confirm_address::staticGet('code', $this->code);
74
75         if (empty($this->confirm)) {
76             // TRANS: Client exception thrown when trying to register with a non-existing confirmation code.
77             throw new ClientException(_m('Confirmation code not found.'));
78             return;
79         }
80
81         $this->user = User::staticGet('id', $this->confirm->user_id);
82
83         if (empty($this->user)) {
84             // TRANS: Client exception thrown when trying to register with a confirmation code that is not connected with a user.
85             throw new ServerException(_m('No user for that confirmation code.'));
86         }
87
88         $type = $this->confirm->address_type;
89
90         if ($type != 'email') {
91             // TRANS: Client exception thrown when trying to register with a invalid e-mail address.
92             // TRANS: %s is the invalid e-mail address.
93             throw new ServerException(sprintf(_m('Unrecognized address type %s.'), $type));
94         }
95
96         if (!empty($this->user->email) && $this->user->email == $confirm->address) {
97             // TRANS: Client error for an already confirmed email/jabber/sms address.
98             throw new ClientException(_m('That address has already been confirmed.'));
99         }
100
101         if ($this->isPost()) {
102
103             $this->checkSessionToken();
104
105             $password = $this->trimmed('password');
106             $confirm  = $this->trimmed('confirm');
107
108             if (strlen($password) < 6) {
109                 // TRANS: Client exception thrown when trying to register with too short a password.
110                 throw new ClientException(_m('Password too short.'));
111                 return;
112             } else if (0 != strcmp($password, $confirm)) {
113                 // TRANS: Client exception thrown when trying to register without providing the same password twice.
114                 throw new ClientException(_m('Passwords do not match.'));
115                 return;
116             }
117
118             $this->password = $password;
119         }
120
121         return true;
122     }
123
124     /**
125      * Handler method
126      *
127      * @param array $argarray is ignored since it's now passed in in prepare()
128      *
129      * @return void
130      */
131     function handle($argarray=null)
132     {
133         $homepage = common_local_url('all',
134                                      array('nickname' => $this->user->nickname));
135
136         if ($this->isPost()) {
137             $this->confirmUser();
138             common_set_user($this->user);
139             common_real_login(true);
140             common_redirect($homepage, 303);
141         } else {
142             $this->showPage();
143         }
144         return;
145     }
146
147     function confirmUser()
148     {
149         $orig = clone($this->user);
150
151         $this->user->email = $this->confirm->address;
152
153         $this->user->updateKeys($orig);
154
155         $this->user->emailChanged();
156
157         $orig = clone($this->user);
158
159         $this->user->password = common_munge_password($this->password, $this->user->id);
160
161         $this->user->update($orig);
162
163         $this->confirm->delete();
164     }
165
166     function showContent()
167     {
168         $this->element('p', 'instructions',
169                        // TRANS: Form instructions. %s is the nickname of the to be registered user.
170                        sprintf(_m('You have confirmed the email address for your new user account %s. '.
171                                  'Use the form below to set your new password.'),
172                                $this->user->nickname));
173
174         $form = new ConfirmFirstEmailForm($this, $this->code);
175         $form->show();
176     }
177
178     function title()
179     {
180         // TRANS: Page title.
181         return _m('Set a password');
182     }
183 }
184
185 class ConfirmFirstEmailForm extends Form
186 {
187     public $code;
188
189     function __construct($out, $code)
190     {
191         parent::__construct($out);
192         $this->code = $code;
193     }
194
195     function formLegend()
196     {
197         // TRANS: Form legend.
198         return _m('Confirm email address');
199     }
200
201     function action()
202     {
203         return common_local_url('confirmfirstemail',
204                                 array('code' => $this->code));
205     }
206
207     function formClass()
208     {
209         return 'form_settings';
210     }
211
212     function formData()
213     {
214         $this->out->elementStart('ul', 'form_data');
215         $this->out->elementStart('li');
216         // TRANS: Field label.
217         $this->out->password('password', _m('New password'),
218                              // TRANS: Field title for password field.
219                              _m('6 or more characters.'));
220         $this->out->elementEnd('li');
221         $this->out->elementStart('li');
222         // TRANS: Field label for repeat password field.
223         $this->out->password('confirm', _m('LABEL','Confirm'),
224                              // TRANS: Field title for repeat password field.
225                              _m('Same as password above.'));
226         $this->out->elementEnd('li');
227         $this->out->elementEnd('ul');
228     }
229
230     function formActions()
231     {
232         // TRANS: Button text for completing registration by e-mail.
233         $this->out->submit('save', _m('BUTTON','Save'));
234     }
235 }