]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RequireValidatedEmail/actions/confirmfirstemail.php
Managed_DataObject->updateWithKeys throws its own exception
[quix0rs-gnu-social.git] / plugins / RequireValidatedEmail / actions / 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::getKV('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::getKV('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         // Throws exception on failure.
154         $this->user->updateWithKeys($orig);
155
156         $this->user->emailChanged();
157
158         $orig = clone($this->user);
159
160         $this->user->password = common_munge_password($this->password, $this->user->id);
161
162         $this->user->update($orig);
163
164         $this->confirm->delete();
165     }
166
167     function showContent()
168     {
169         $this->element('p', 'instructions',
170                        // TRANS: Form instructions. %s is the nickname of the to be registered user.
171                        sprintf(_m('You have confirmed the email address for your new user account %s. '.
172                                  'Use the form below to set your new password.'),
173                                $this->user->nickname));
174
175         $form = new ConfirmFirstEmailForm($this, $this->code);
176         $form->show();
177     }
178
179     function title()
180     {
181         // TRANS: Page title.
182         return _m('Set a password');
183     }
184 }
185
186 class ConfirmFirstEmailForm extends Form
187 {
188     public $code;
189
190     function __construct($out, $code)
191     {
192         parent::__construct($out);
193         $this->code = $code;
194     }
195
196     function formLegend()
197     {
198         // TRANS: Form legend.
199         return _m('Confirm email address');
200     }
201
202     function action()
203     {
204         return common_local_url('confirmfirstemail',
205                                 array('code' => $this->code));
206     }
207
208     function formClass()
209     {
210         return 'form_settings';
211     }
212
213     function formData()
214     {
215         $this->out->elementStart('ul', 'form_data');
216         $this->out->elementStart('li');
217         // TRANS: Field label.
218         $this->out->password('password', _m('New password'),
219                              // TRANS: Field title for password field.
220                              _m('6 or more characters.'));
221         $this->out->elementEnd('li');
222         $this->out->elementStart('li');
223         // TRANS: Field label for repeat password field.
224         $this->out->password('confirm', _m('LABEL','Confirm'),
225                              // TRANS: Field title for repeat password field.
226                              _m('Same as password above.'));
227         $this->out->elementEnd('li');
228         $this->out->elementEnd('ul');
229     }
230
231     function formActions()
232     {
233         // TRANS: Button text for completing registration by e-mail.
234         $this->out->submit('save', _m('BUTTON','Save'));
235     }
236 }