]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/smssettings.php
correct values for hidden fields
[quix0rs-gnu-social.git] / actions / smssettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/actions/emailsettings.php');
24
25 class SmssettingsAction extends EmailsettingsAction {
26
27         function get_instructions() {
28                 return _('You can receive SMS messages through email from %%site.name%%.');
29         }
30
31         function show_form($msg=NULL, $success=false) {
32                 $user = common_current_user();
33                 $this->form_header(_('SMS Settings'), $msg, $success);
34                 common_element_start('form', array('method' => 'post',
35                                                                                    'id' => 'smssettings',
36                                                                                    'action' =>
37                                                                                    common_local_url('smssettings')));
38
39                 common_element('h2', NULL, _('Address'));
40
41                 if ($user->sms) {
42                         common_element_start('p');
43                         $carrier = $user->getCarrier();
44                         common_element('span', 'address confirmed', $user->sms . ' (' . $carrier->name . ')');
45                         common_element('span', 'input_instructions',
46                                        _('Current confirmed SMS-enabled phone number.'));
47                         common_hidden('sms', $user->sms);
48                         common_hidden('carrier', $user->carrier);
49                         common_element_end('p');
50                         common_submit('remove', _('Remove'));
51                 } else {
52                         $confirm = $this->get_confirmation();
53                         if ($confirm) {
54                                 $carrier = Sms_carrier::staticGet($confirm->address_extra);
55                                 common_element_start('p');
56                                 common_element('span', 'address unconfirmed', $confirm->address . ' (' . $carrier->name . ')');
57                                 common_element('span', 'input_instructions',
58                                                            _('Awaiting confirmation on this phone number.'));
59                                 common_hidden('sms', $confirm->address);
60                                 common_hidden('carrier', $confirm->address_extra);
61                                 common_element_end('p');
62                                 common_submit('cancel', _('Cancel'));
63                                 common_input('code', _('Confirmation code'), NULL,
64                                                          _('Enter the code you received on your phone.'));
65                                 common_submit('confirm', _('Confirm'));
66                         } else {
67                                 common_input('sms', _('SMS Phone number'),
68                                                          ($this->arg('sms')) ? $this->arg('sms') : NULL,
69                                                          _('Phone number, no punctuation or spaces, with area code'));
70                                 $this->carrier_select();
71                                 common_submit('add', _('Add'));
72                         }
73                 }
74
75                 if ($user->sms) {
76                         common_element('h2', NULL, _('Incoming email'));
77                         
78                         if ($user->incomingemail) {
79                                 common_element_start('p');
80                                 common_element('span', 'address', $user->incomingemail);
81                                 common_element('span', 'input_instructions',
82                                                            _('Send email to this address to post new notices.'));
83                                 common_element_end('p');
84                                 common_submit('removeincoming', _('Remove'));
85                         }
86                         
87                         common_element_start('p');
88                         common_element('span', 'input_instructions',
89                                                    _('Make a new email address for posting to; cancels the old one.'));
90                         common_element_end('p');
91                         common_submit('newincoming', _('New'));
92                 }
93                 
94                 common_element('h2', NULL, _('Preferences'));
95                 
96                 common_checkbox('smsnotify',
97                                                 _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
98                                                 $user->smsnotify);
99                         
100                 common_submit('save', _('Save'));
101                 
102                 common_element_end('form');
103                 common_show_footer();
104         }
105
106         function get_confirmation() {
107                 $user = common_current_user();
108                 $confirm = new Confirm_address();
109                 $confirm->user_id = $user->id;
110                 $confirm->address_type = 'sms';
111                 if ($confirm->find(TRUE)) {
112                         return $confirm;
113                 } else {
114                         return NULL;
115                 }
116         }
117
118         function handle_post() {
119
120                 if ($this->arg('save')) {
121                         $this->save_preferences();
122                 } else if ($this->arg('add')) {
123                         $this->add_address();
124                 } else if ($this->arg('cancel')) {
125                         $this->cancel_confirmation();
126                 } else if ($this->arg('remove')) {
127                         $this->remove_address();
128                 } else if ($this->arg('removeincoming')) {
129                         $this->remove_incoming();
130                 } else if ($this->arg('newincoming')) {
131                         $this->new_incoming();
132                 } else if ($this->arg('confirm')) {
133                         $this->confirm_code();
134                 } else {
135                         $this->show_form(_('Unexpected form submission.'));
136                 }
137         }
138
139         function save_preferences() {
140
141                 $smsnotify = $this->boolean('smsnotify');
142                 
143                 $user = common_current_user();
144
145                 assert(!is_null($user)); # should already be checked
146
147                 $user->query('BEGIN');
148
149                 $original = clone($user);
150
151                 $user->smsnotify = $smsnotify;
152
153                 $result = $user->update($original);
154
155                 if ($result === FALSE) {
156                         common_log_db_error($user, 'UPDATE', __FILE__);
157                         common_server_error(_('Couldn\'t update user.'));
158                         return;
159                 }
160
161                 $user->query('COMMIT');
162
163                 $this->show_form(_('Preferences saved.'), true);
164         }
165
166         function add_address() {
167
168                 $user = common_current_user();
169
170                 $sms = $this->trimmed('sms');
171                 $carrier_id = $this->trimmed('carrier');
172                 
173                 # Some validation
174
175                 if (!$sms) {
176                         $this->show_form(_('No phone number.'));
177                         return;
178                 }
179
180                 if (!$carrier_id) {
181                         $this->show_form(_('No carrier selected.'));
182                         return;
183                 }
184                 
185                 $sms = common_canonical_sms($sms);
186                 
187                 if ($user->sms == $sms) {
188                     $this->show_form(_('That is already your phone number.'));
189                     return;
190                 } else if ($this->sms_exists($sms)) {
191                     $this->show_form(_('That phone number already belongs to another user.'));
192                     return;
193                 }
194
195                 $confirm = new Confirm_address();
196                 $confirm->address = $sms;
197                 $confirm->address_extra = $carrier_id;
198                 $confirm->address_type = 'sms';
199                 $confirm->user_id = $user->id;
200                 $confirm->code = common_confirmation_code(40);
201
202                 $result = $confirm->insert();
203
204                 if ($result === FALSE) {
205                         common_log_db_error($confirm, 'INSERT', __FILE__);
206                         common_server_error(_('Couldn\'t insert confirmation code.'));
207                         return;
208                 }
209
210                 $carrier = Sms_carrier::staticGet($carrier_id);
211                 
212                 mail_confirm_sms($confirm->code,
213                                                  $user->nickname,
214                                                  $carrier->toEmailAddress($sms));
215
216                 $msg = _('A confirmation code was sent to the phone number you added. Check your inbox (and spam box!) for the code and instructions on how to use it.');
217
218                 $this->show_form($msg, TRUE);
219         }
220
221         function cancel_confirmation() {
222                 
223                 $sms = $this->trimmed('sms');
224                 $carrier = $this->trimmed('carrier');
225                 
226                 $confirm = $this->get_confirmation();
227                 
228                 if (!$confirm) {
229                         $this->show_form(_('No pending confirmation to cancel.'));
230                         return;
231                 }
232                 if ($confirm->address != $sms) {
233                         $this->show_form(_('That is the wrong confirmation number.'));
234                         return;
235                 }
236
237         $result = $confirm->delete();
238
239         if (!$result) {
240                         common_log_db_error($confirm, 'DELETE', __FILE__);
241             $this->server_error(_('Couldn\'t delete email confirmation.'));
242             return;
243         }
244
245         $this->show_form(_('Confirmation cancelled.'), TRUE);
246         }
247
248         function remove_address() {
249
250                 $user = common_current_user();
251                 $sms = $this->arg('sms');
252                 $carrier = $this->arg('carrier');
253                 
254                 # Maybe an old tab open...?
255
256                 if ($user->sms != $sms) {
257                     $this->show_form(_('That is not your phone number.'));
258                     return;
259                 }
260
261                 $user->query('BEGIN');
262                 $original = clone($user);
263                 $user->sms = NULL;
264                 $user->carrier = NULL;          
265                 $user->smsemail = NULL;         
266                 $result = $user->updateKeys($original);
267                 if (!$result) {
268                         common_log_db_error($user, 'UPDATE', __FILE__);
269                         common_server_error(_('Couldn\'t update user.'));
270                         return;
271                 }
272                 $user->query('COMMIT');
273
274                 $this->show_form(_('The address was removed.'), TRUE);
275         }
276         
277         function sms_exists($sms) {
278                 $user = common_current_user();
279                 $other = User::staticGet('sms', $sms);
280                 if (!$other) {
281                         return false;
282                 } else {
283                         return $other->id != $user->id;
284                 }
285         }
286
287         function carrier_select() {
288                 $carrier = new Sms_carrier();
289                 $cnt = $carrier->find();
290
291                 common_element_start('p');
292                 common_element('label', array('for' => 'carrier'));
293                 common_element_start('select', array('name' => 'carrier',
294                                                                                          'id' => 'carrier'));
295                 common_element('option', array('value' => 0),
296                                            _('Select a carrier'));
297                 while ($carrier->fetch()) {
298                         common_element('option', array('value' => $carrier->id),
299                                                    $carrier->name);
300                 }
301                 common_element_end('select');
302                 common_element_end('p');
303                 common_element('span', 'input_instructions',
304                                            sprintf(_('Mobile carrier for your phone. '.
305                                                                  'If you know a carrier that accepts ' . 
306                                                                  'SMS over email but isn\'t listed here, ' .
307                                                                  'send email to let us know at %s.'),
308                                                            common_config('site', 'email')));
309         }
310
311         function confirm_code() {
312                 
313                 $code = $this->trimmed('code');
314                 
315                 if (!$code) {
316                         $this->show_form(_('No code entered'));
317                         return;
318                 }
319                 
320                 common_redirect(common_local_url('confirmaddress', 
321                                                                                  array('code' => $code)));
322         }
323 }