]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/smssettings.php
SMS settings markup and styles. Minor check with twitter settings form_datas
[quix0rs-gnu-social.git] / actions / smssettings.php
1 <?php
2 /**
3  * Laconica, the distributed open-source microblogging tool
4  *
5  * Settings for SMS
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @copyright 2008-2009 Control Yourself, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://laconi.ca/
28  */
29
30 if (!defined('LACONICA')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
35
36 /**
37  * Settings for SMS
38  *
39  * @category Settings
40  * @package  Laconica
41  * @author   Evan Prodromou <evan@controlyourself.ca>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://laconi.ca/
44  *
45  * @see      SettingsAction
46  */
47
48 class SmssettingsAction extends ConnectSettingsAction
49 {
50     /**
51      * Title of the page
52      *
53      * @return string Title of the page
54      */
55
56     function title()
57     {
58         return _('SMS Settings');
59     }
60
61     /**
62      * Instructions for use
63      *
64      * @return instructions for use
65      */
66
67     function getInstructions()
68     {
69         return _('You can receive SMS messages through email from %%site.name%%.');
70     }
71
72     /**
73      * Content area of the page
74      *
75      * Shows a form for adding and removing SMS phone numbers and setting
76      * SMS preferences.
77      *
78      * @return void
79      */
80
81     function showContent()
82     {
83         $user = common_current_user();
84
85         $this->elementStart('form', array('method' => 'post',
86                                           'id' => 'form_settings_sms',
87                                           'class' => 'form_settings',
88                                           'action' =>
89                                           common_local_url('smssettings')));
90
91         $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
92         $this->element('legend', null, _('Address'));
93         $this->hidden('token', common_session_token());
94
95         if ($user->sms) {
96             $carrier = $user->getCarrier();
97             $this->element('p', 'form_confirmed',
98                            $user->sms . ' (' . $carrier->name . ')');
99             $this->element('p', 'form_guide',
100                            _('Current confirmed SMS-enabled phone number.'));
101             $this->hidden('sms', $user->sms);
102             $this->hidden('carrier', $user->carrier);
103             $this->submit('remove', _('Remove'));
104         } else {
105             $confirm = $this->getConfirmation();
106             if ($confirm) {
107                 $carrier = Sms_carrier::staticGet($confirm->address_extra);
108                 $this->element('p', 'form_unconfirmed',
109                                $confirm->address . ' (' . $carrier->name . ')');
110                 $this->element('p', 'form_guide',
111                                _('Awaiting confirmation on this phone number.'));
112                 $this->hidden('sms', $confirm->address);
113                 $this->hidden('carrier', $confirm->address_extra);
114                 $this->submit('cancel', _('Cancel'));
115
116                 $this->elementStart('ul', 'form_datas');
117                 $this->elementStart('li');
118                 $this->input('code', _('Confirmation code'), null,
119                              _('Enter the code you received on your phone.'));
120                 $this->elementEnd('li');
121                 $this->elementEnd('ul');
122                 $this->submit('confirm', _('Confirm'));
123             } else {
124                 $this->elementStart('ul', 'form_datas');
125                 $this->elementStart('li');
126                 $this->input('sms', _('SMS Phone number'),
127                              ($this->arg('sms')) ? $this->arg('sms') : null,
128                              _('Phone number, no punctuation or spaces, '.
129                                'with area code'));
130                 $this->elementEnd('li');
131                 $this->elementEnd('ul');
132                 $this->carrierSelect();
133                 $this->submit('add', _('Add'));
134             }
135         }
136         $this->elementEnd('fieldset');
137
138         if ($user->sms) {
139         $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
140             $this->element('legend', null, _('Incoming email'));
141
142             if ($user->incomingemail) {
143                 $this->element('p', 'form_unconfirmed', $user->incomingemail);
144                 $this->element('p', 'form_note',
145                                _('Send email to this address to post new notices.'));
146                 $this->submit('removeincoming', _('Remove'));
147             }
148
149             $this->element('p', 'form_guide',
150                            _('Make a new email address for posting to; '.
151                              'cancels the old one.'));
152             $this->submit('newincoming', _('New'));
153             $this->elementEnd('fieldset');
154         }
155
156         $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
157         $this->element('legend', null, _('Preferences'));
158
159         $this->elementStart('ul', 'form_datas');
160         $this->elementStart('li');
161         $this->checkbox('smsnotify',
162                         _('Send me notices through SMS; '.
163                           'I understand I may incur '.
164                           'exorbitant charges from my carrier.'),
165                         $user->smsnotify);
166         $this->elementEnd('li');
167         $this->elementEnd('ul');
168
169         $this->submit('save', _('Save'));
170
171         $this->elementEnd('fieldset');
172         $this->elementEnd('form');
173     }
174
175     /**
176      * Get a pending confirmation, if any, for this user
177      *
178      * @return void
179      *
180      * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
181      */
182
183     function getConfirmation()
184     {
185         $user = common_current_user();
186
187         $confirm = new Confirm_address();
188
189         $confirm->user_id      = $user->id;
190         $confirm->address_type = 'sms';
191
192         if ($confirm->find(true)) {
193             return $confirm;
194         } else {
195             return null;
196         }
197     }
198
199     /**
200      * Handle posts to this form
201      *
202      * Based on the button that was pressed, muxes out to other functions
203      * to do the actual task requested.
204      *
205      * All sub-functions reload the form with a message -- success or failure.
206      *
207      * @return void
208      */
209
210     function handlePost()
211     {
212         // CSRF protection
213
214         $token = $this->trimmed('token');
215         if (!$token || $token != common_session_token()) {
216             $this->showForm(_('There was a problem with your session token. '.
217                               'Try again, please.'));
218             return;
219         }
220
221         if ($this->arg('save')) {
222             $this->savePreferences();
223         } else if ($this->arg('add')) {
224             $this->addAddress();
225         } else if ($this->arg('cancel')) {
226             $this->cancelConfirmation();
227         } else if ($this->arg('remove')) {
228             $this->removeAddress();
229         } else if ($this->arg('removeincoming')) {
230             $this->removeIncoming();
231         } else if ($this->arg('newincoming')) {
232             $this->newIncoming();
233         } else if ($this->arg('confirm')) {
234             $this->confirmCode();
235         } else {
236             $this->showForm(_('Unexpected form submission.'));
237         }
238     }
239
240     /**
241      * Handle a request to save preferences
242      *
243      * Sets the user's SMS preferences in the DB.
244      *
245      * @return void
246      */
247
248     function savePreferences()
249     {
250         $smsnotify = $this->boolean('smsnotify');
251
252         $user = common_current_user();
253
254         assert(!is_null($user)); // should already be checked
255
256         $user->query('BEGIN');
257
258         $original = clone($user);
259
260         $user->smsnotify = $smsnotify;
261
262         $result = $user->update($original);
263
264         if ($result === false) {
265             common_log_db_error($user, 'UPDATE', __FILE__);
266             $this->serverError(_('Couldn\'t update user.'));
267             return;
268         }
269
270         $user->query('COMMIT');
271
272         $this->showForm(_('Preferences saved.'), true);
273     }
274
275     /**
276      * Add a new SMS number for confirmation
277      *
278      * When the user requests a new SMS number, sends a confirmation
279      * message.
280      *
281      * @return void
282      */
283
284     function addAddress()
285     {
286         $user = common_current_user();
287
288         $sms        = $this->trimmed('sms');
289         $carrier_id = $this->trimmed('carrier');
290
291         // Some validation
292
293         if (!$sms) {
294             $this->showForm(_('No phone number.'));
295             return;
296         }
297
298         if (!$carrier_id) {
299             $this->showForm(_('No carrier selected.'));
300             return;
301         }
302
303         $sms = common_canonical_sms($sms);
304
305         if ($user->sms == $sms) {
306             $this->showForm(_('That is already your phone number.'));
307             return;
308         } else if ($this->smsExists($sms)) {
309             $this->showForm(_('That phone number already belongs to another user.'));
310             return;
311         }
312
313         $confirm = new Confirm_address();
314
315         $confirm->address       = $sms;
316         $confirm->address_extra = $carrier_id;
317         $confirm->address_type  = 'sms';
318         $confirm->user_id       = $user->id;
319         $confirm->code          = common_confirmation_code(40);
320
321         $result = $confirm->insert();
322
323         if ($result === false) {
324             common_log_db_error($confirm, 'INSERT', __FILE__);
325             $this->serverError(_('Couldn\'t insert confirmation code.'));
326             return;
327         }
328
329         $carrier = Sms_carrier::staticGet($carrier_id);
330
331         mail_confirm_sms($confirm->code,
332                          $user->nickname,
333                          $carrier->toEmailAddress($sms));
334
335         $msg = _('A confirmation code was sent to the phone number you added. '.
336                  'Check your phone for the code and instructions '.
337                  'on how to use it.');
338
339         $this->showForm($msg, true);
340     }
341
342     /**
343      * Cancel a pending confirmation
344      *
345      * Cancels the confirmation.
346      *
347      * @return void
348      */
349
350     function cancelConfirmation()
351     {
352         $sms     = $this->trimmed('sms');
353         $carrier = $this->trimmed('carrier');
354
355         $confirm = $this->getConfirmation();
356
357         if (!$confirm) {
358             $this->showForm(_('No pending confirmation to cancel.'));
359             return;
360         }
361         if ($confirm->address != $sms) {
362             $this->showForm(_('That is the wrong confirmation number.'));
363             return;
364         }
365
366         $result = $confirm->delete();
367
368         if (!$result) {
369             common_log_db_error($confirm, 'DELETE', __FILE__);
370             $this->serverError(_('Couldn\'t delete email confirmation.'));
371             return;
372         }
373
374         $this->showForm(_('Confirmation cancelled.'), true);
375     }
376
377     /**
378      * Remove a phone number from the user's account
379      *
380      * @return void
381      */
382
383     function removeAddress()
384     {
385         $user = common_current_user();
386
387         $sms     = $this->arg('sms');
388         $carrier = $this->arg('carrier');
389
390         // Maybe an old tab open...?
391
392         if ($user->sms != $sms) {
393             $this->showForm(_('That is not your phone number.'));
394             return;
395         }
396
397         $user->query('BEGIN');
398
399         $original = clone($user);
400
401         $user->sms      = null;
402         $user->carrier  = null;
403         $user->smsemail = null;
404
405         $result = $user->updateKeys($original);
406         if (!$result) {
407             common_log_db_error($user, 'UPDATE', __FILE__);
408             $this->serverError(_('Couldn\'t update user.'));
409             return;
410         }
411         $user->query('COMMIT');
412
413         $this->showForm(_('The address was removed.'), true);
414     }
415
416     /**
417      * Does this sms number exist in our database?
418      *
419      * Also checks if it belongs to someone else
420      *
421      * @param string $sms phone number to check
422      *
423      * @return boolean does the number exist
424      */
425
426     function smsExists($sms)
427     {
428         $user = common_current_user();
429
430         $other = User::staticGet('sms', $sms);
431
432         if (!$other) {
433             return false;
434         } else {
435             return $other->id != $user->id;
436         }
437     }
438
439     /**
440      * Show a drop-down box with all the SMS carriers in the DB
441      *
442      * @return void
443      */
444
445     function carrierSelect()
446     {
447         $carrier = new Sms_carrier();
448
449         $cnt = $carrier->find();
450
451         $this->elementStart('ul', 'form_datas');
452         $this->elementStart('li');
453         $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
454         $this->elementStart('select', array('name' => 'carrier',
455                                             'id' => 'carrier'));
456         $this->element('option', array('value' => 0),
457                        _('Select a carrier'));
458         while ($carrier->fetch()) {
459             $this->element('option', array('value' => $carrier->id),
460                            $carrier->name);
461         }
462         $this->elementEnd('select');
463         $this->element('p', 'form_guide',
464                        sprintf(_('Mobile carrier for your phone. '.
465                                  'If you know a carrier that accepts ' .
466                                  'SMS over email but isn\'t listed here, ' .
467                                  'send email to let us know at %s.'),
468                                common_config('site', 'email')));
469         $this->elementEnd('li');
470         $this->elementEnd('ul');
471     }
472
473     /**
474      * Confirm an SMS confirmation code
475      *
476      * Redirects to the confirmaddress page for this code
477      *
478      * @return void
479      */
480
481     function confirmCode()
482     {
483         $code = $this->trimmed('code');
484
485         if (!$code) {
486             $this->showForm(_('No code entered'));
487             return;
488         }
489
490         common_redirect(common_local_url('confirmaddress',
491                                          array('code' => $code)));
492     }
493 }