]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/RequireValidatedEmail/confirmfirstemail.php
Merge branch 'master' into 0.9.x
[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
48 class ConfirmfirstemailAction extends Action
49 {
50     public $confirm;
51     public $code;
52     public $password;
53     public $user;
54
55     /**
56      * For initializing members of the class.
57      *
58      * @param array $argarray misc. arguments
59      *
60      * @return boolean true
61      */
62
63     function prepare($argarray)
64     {
65         parent::prepare($argarray);
66         $user = common_current_user();
67
68         if (!empty($user)) {
69             throw new ClientException(_('You are already logged in.'));
70         }
71
72         $this->code = $this->trimmed('code');
73
74         $this->confirm = Confirm_address::staticGet('code', $this->code);
75
76         if (empty($this->confirm)) {
77             throw new ClientException(_('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             throw new ServerException(_('No user for that confirmation code.'));
85         }
86
87         $type = $this->confirm->address_type;
88
89         if ($type != 'email') {
90             throw new ServerException(sprintf(_('Unrecognized address type %s.'), $type));
91         }
92
93         if (!empty($this->user->email) && $this->user->email == $confirm->address) {
94             // TRANS: Client error for an already confirmed email/jabber/sms address.
95             throw new ClientException(_('That address has already been confirmed.'));
96         }
97
98         if ($this->isPost()) {
99
100             $this->checkSessionToken();
101
102             $password = $this->trimmed('password');
103             $confirm  = $this->trimmed('confirm');
104
105             if (strlen($password) < 6) {
106                 throw new ClientException(_('Password too short.'));
107                 return;
108             } else if (0 != strcmp($password, $confirm)) {
109                 throw new ClientException(_("Passwords don't match."));
110                 return;
111             }
112
113             $this->password = $password;
114         }
115
116         return true;
117     }
118
119     /**
120      * Handler method
121      *
122      * @param array $argarray is ignored since it's now passed in in prepare()
123      *
124      * @return void
125      */
126
127     function handle($argarray=null)
128     {
129         $homepage = common_local_url('all', 
130                                      array('nickname' => $this->user->nickname));
131
132         if ($this->isPost()) {
133             $this->confirmUser();
134             common_set_user($this->user);
135             common_real_login(true);
136             common_redirect($homepage, 303);
137         } else {
138             $this->showPage();
139         }
140         return;
141     }
142
143     function confirmUser()
144     {
145         $orig = clone($this->user);
146
147         $this->user->email = $this->confirm->address;
148
149         $this->user->updateKeys($orig);
150
151         $this->user->emailChanged();
152
153         $orig = clone($this->user);
154
155         $this->user->password = common_munge_password($this->password, $this->user->id);
156
157         $this->user->update($orig);
158
159         $this->confirm->delete();
160     }
161
162     function showContent()
163     {
164         $this->element('p', 'instructions',
165                        sprintf(_('You have confirmed the email address for your new user account %s. '.
166                                  'Use the form below to set your new password.'),
167                                $this->user->nickname));
168
169         $form = new ConfirmFirstEmailForm($this, $this->code);
170         $form->show();
171     }
172
173     function title()
174     {
175         return _('Set a password');
176     }
177 }
178
179 class ConfirmFirstEmailForm extends Form
180 {
181     public $code;
182
183     function __construct($out, $code)
184     {
185         parent::__construct($out);
186         $this->code = $code;
187     }
188
189     function formLegend()
190     {
191         return _('Confirm email');
192     }
193
194     function action()
195     {
196         return common_local_url('confirmfirstemail',
197                                 array('code' => $this->code));
198     }
199
200     function formClass()
201     {
202         return 'form_settings';
203     }
204
205     function formData()
206     {
207         $this->out->elementStart('ul', 'form_data');
208         $this->out->elementStart('li');
209         $this->out->password('password', _('New password'),
210                              _('6 or more characters.'));
211         $this->out->elementEnd('li');
212         $this->out->elementStart('li');
213         $this->out->password('confirm', _('Confirm'),
214                              _('Same as password above.'));
215         $this->out->elementEnd('li');
216         $this->out->elementEnd('ul');
217     }
218
219     function formActions()
220     {
221         $this->out->submit('save', _('Save'));
222     }
223 }