]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/smssettings.php
6af1872a0ec1d88bcf117ff20ba4ef2cadc57a6b
[quix0rs-gnu-social.git] / actions / smssettings.php
1 <?php
2 /**
3  * StatusNet, 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   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @copyright 2008-2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET') && !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  StatusNet
41  * @author   Evan Prodromou <evan@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
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         // TRANS: Title for SMS settings.
59         return _('SMS settings');
60     }
61
62     /**
63      * Instructions for use
64      *
65      * @return instructions for use
66      */
67
68     function getInstructions()
69     {
70         // XXX: For consistency of parameters in messages, this should be a
71         //      regular parameters, replaced with sprintf().
72         // TRANS: SMS settings page instructions.
73         // TRANS: %%site.name%% is the name of the site.
74         return _('You can receive SMS messages through email from %%site.name%%.');
75     }
76
77     function showScripts()
78     {
79         parent::showScripts();
80         $this->autofocus('sms');
81     }
82
83     /**
84      * Content area of the page
85      *
86      * Shows a form for adding and removing SMS phone numbers and setting
87      * SMS preferences.
88      *
89      * @return void
90      */
91
92     function showContent()
93     {
94         if (!common_config('sms', 'enabled')) {
95             $this->element('div', array('class' => 'error'),
96                            // TRANS: Message given in the SMS settings if SMS is not enabled on the site.
97                            _('SMS is not available.'));
98             return;
99         }
100
101         $user = common_current_user();
102
103         $this->elementStart('form', array('method' => 'post',
104                                           'id' => 'form_settings_sms',
105                                           'class' => 'form_settings',
106                                           'action' =>
107                                           common_local_url('smssettings')));
108
109         $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
110         // TRANS: Form legend for SMS settings form.
111         $this->element('legend', null, _('SMS address'));
112         $this->hidden('token', common_session_token());
113
114         if ($user->sms) {
115             $carrier = $user->getCarrier();
116             $this->element('p', 'form_confirmed',
117                            $user->sms . ' (' . $carrier->name . ')');
118             $this->element('p', 'form_guide',
119                            // TRANS: Form guide in SMS settings form.
120                            _('Current confirmed SMS-enabled phone number.'));
121             $this->hidden('sms', $user->sms);
122             $this->hidden('carrier', $user->carrier);
123             // TRANS: Button label to remove a confirmed SMS address.
124             $this->submit('remove', _m('BUTTON','Remove'));
125         } else {
126             $confirm = $this->getConfirmation();
127             if ($confirm) {
128                 $carrier = Sms_carrier::staticGet($confirm->address_extra);
129                 $this->element('p', 'form_unconfirmed',
130                                $confirm->address . ' (' . $carrier->name . ')');
131                 $this->element('p', 'form_guide',
132                                // TRANS: Form guide in IM settings form.
133                                _('Awaiting confirmation on this phone number.'));
134                 $this->hidden('sms', $confirm->address);
135                 $this->hidden('carrier', $confirm->address_extra);
136                 // TRANS: Button label to cancel a SMS address confirmation procedure.
137                 $this->submit('cancel', _m('BUTTON','Cancel'));
138
139                 $this->elementStart('ul', 'form_data');
140                 $this->elementStart('li');
141                 // TRANS: Field label for SMS address input in SMS settings form.
142                 $this->input('code', _('Confirmation code'), null,
143                              // TRANS: Form field instructions in SMS settings form.
144                              _('Enter the code you received on your phone.'));
145                 $this->elementEnd('li');
146                 $this->elementEnd('ul');
147                 // TRANS: Button label to confirm SMS confirmation code in SMS settings.
148                 $this->submit('confirm', _m('BUTTON','Confirm'));
149             } else {
150                 $this->elementStart('ul', 'form_data');
151                 $this->elementStart('li');
152                 // TRANS: Field label for SMS phone number input in SMS settings form.
153                 $this->input('sms', _('SMS phone number'),
154                              ($this->arg('sms')) ? $this->arg('sms') : null,
155                              // TRANS: SMS phone number input field instructions in SMS settings form.
156                              _('Phone number, no punctuation or spaces, '.
157                                'with area code'));
158                 $this->elementEnd('li');
159                 $this->elementEnd('ul');
160                 $this->carrierSelect();
161                 // TRANS: Button label for adding a SMS phone number in SMS settings form.
162                 $this->submit('add', _m('BUTTON','Add'));
163             }
164         }
165         $this->elementEnd('fieldset');
166
167         if ($user->sms) {
168         $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
169             // XXX: Confused! This is about SMS. Should this message be updated?
170             // TRANS: Form legend for incoming SMS settings form.
171             $this->element('legend', null, _('Incoming email'));
172
173             if ($user->incomingemail) {
174                 $this->element('p', 'form_unconfirmed', $user->incomingemail);
175                 $this->element('p', 'form_note',
176                                // XXX: Confused! This is about SMS. Should this message be updated?
177                                // TRANS: Form instructions for incoming SMS e-mail address form in SMS settings.
178                                _('Send email to this address to post new notices.'));
179                 // TRANS: Button label for removing a set sender SMS e-mail address to post notices from.
180                 $this->submit('removeincoming', _m('BUTTON','Remove'));
181             }
182
183             $this->element('p', 'form_guide',
184                            // XXX: Confused! This is about SMS. Should this message be updated?
185                            // TRANS: Instructions for incoming SMS e-mail address input form.
186                            _('Make a new email address for posting to; '.
187                              'cancels the old one.'));
188             // TRANS: Button label for adding an SMS e-mail address to send notices from.
189             $this->submit('newincoming', _m('BUTTON','New'));
190             $this->elementEnd('fieldset');
191         }
192
193         $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
194         // TRANS: Form legend for SMS preferences form.
195         $this->element('legend', null, _('SMS preferences'));
196
197         $this->elementStart('ul', 'form_data');
198         $this->elementStart('li');
199         $this->checkbox('smsnotify',
200                         // TRANS: Checkbox label in SMS preferences form.
201                         _('Send me notices through SMS; '.
202                           'I understand I may incur '.
203                           'exorbitant charges from my carrier.'),
204                         $user->smsnotify);
205         $this->elementEnd('li');
206         $this->elementEnd('ul');
207
208         // TRANS: Button label to save SMS preferences.
209         $this->submit('save', _m('BUTTON','Save'));
210
211         $this->elementEnd('fieldset');
212         $this->elementEnd('form');
213     }
214
215     /**
216      * Get a pending confirmation, if any, for this user
217      *
218      * @return void
219      *
220      * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
221      */
222
223     function getConfirmation()
224     {
225         $user = common_current_user();
226
227         $confirm = new Confirm_address();
228
229         $confirm->user_id      = $user->id;
230         $confirm->address_type = 'sms';
231
232         if ($confirm->find(true)) {
233             return $confirm;
234         } else {
235             return null;
236         }
237     }
238
239     /**
240      * Handle posts to this form
241      *
242      * Based on the button that was pressed, muxes out to other functions
243      * to do the actual task requested.
244      *
245      * All sub-functions reload the form with a message -- success or failure.
246      *
247      * @return void
248      */
249
250     function handlePost()
251     {
252         // CSRF protection
253
254         $token = $this->trimmed('token');
255         if (!$token || $token != common_session_token()) {
256             $this->showForm(_('There was a problem with your session token. '.
257                               'Try again, please.'));
258             return;
259         }
260
261         if ($this->arg('save')) {
262             $this->savePreferences();
263         } else if ($this->arg('add')) {
264             $this->addAddress();
265         } else if ($this->arg('cancel')) {
266             $this->cancelConfirmation();
267         } else if ($this->arg('remove')) {
268             $this->removeAddress();
269         } else if ($this->arg('removeincoming')) {
270             $this->removeIncoming();
271         } else if ($this->arg('newincoming')) {
272             $this->newIncoming();
273         } else if ($this->arg('confirm')) {
274             $this->confirmCode();
275         } else {
276             // TRANS: Message given submitting a form with an unknown action in SMS settings.
277             $this->showForm(_('Unexpected form submission.'));
278         }
279     }
280
281     /**
282      * Handle a request to save preferences
283      *
284      * Sets the user's SMS preferences in the DB.
285      *
286      * @return void
287      */
288
289     function savePreferences()
290     {
291         $smsnotify = $this->boolean('smsnotify');
292
293         $user = common_current_user();
294
295         assert(!is_null($user)); // should already be checked
296
297         $user->query('BEGIN');
298
299         $original = clone($user);
300
301         $user->smsnotify = $smsnotify;
302
303         $result = $user->update($original);
304
305         if ($result === false) {
306             common_log_db_error($user, 'UPDATE', __FILE__);
307             // TRANS: Server error thrown on database error updating SMS preferences.
308             $this->serverError(_('Couldn\'t update user.'));
309             return;
310         }
311
312         $user->query('COMMIT');
313
314         // TRANS: Confirmation message for successful SMS preferences save.
315         $this->showForm(_('SMS preferences saved.'), true);
316     }
317
318     /**
319      * Add a new SMS number for confirmation
320      *
321      * When the user requests a new SMS number, sends a confirmation
322      * message.
323      *
324      * @return void
325      */
326
327     function addAddress()
328     {
329         $user = common_current_user();
330
331         $sms        = $this->trimmed('sms');
332         $carrier_id = $this->trimmed('carrier');
333
334         // Some validation
335
336         if (!$sms) {
337             // TRANS: Message given saving SMS phone number without having provided one.
338             $this->showForm(_('No phone number.'));
339             return;
340         }
341
342         if (!$carrier_id) {
343             // TRANS: Message given saving SMS phone number without having selected a carrier.
344             $this->showForm(_('No carrier selected.'));
345             return;
346         }
347
348         $sms = common_canonical_sms($sms);
349
350         if ($user->sms == $sms) {
351             // TRANS: Message given saving SMS phone number that is already set.
352             $this->showForm(_('That is already your phone number.'));
353             return;
354         } else if ($this->smsExists($sms)) {
355             // TRANS: Message given saving SMS phone number that is already set for another user.
356             $this->showForm(_('That phone number already belongs to another user.'));
357             return;
358         }
359
360         $confirm = new Confirm_address();
361
362         $confirm->address       = $sms;
363         $confirm->address_extra = $carrier_id;
364         $confirm->address_type  = 'sms';
365         $confirm->user_id       = $user->id;
366         $confirm->code          = common_confirmation_code(40);
367
368         $result = $confirm->insert();
369
370         if ($result === false) {
371             common_log_db_error($confirm, 'INSERT', __FILE__);
372             // TRANS: Server error thrown on database error adding SMS confirmation code.
373             $this->serverError(_('Couldn\'t insert confirmation code.'));
374             return;
375         }
376
377         $carrier = Sms_carrier::staticGet($carrier_id);
378
379         mail_confirm_sms($confirm->code,
380                          $user->nickname,
381                          $carrier->toEmailAddress($sms));
382
383         // TRANS: Message given saving valid SMS phone number that is to be confirmed.
384         $msg = _('A confirmation code was sent to the phone number you added. '.
385                  'Check your phone for the code and instructions '.
386                  'on how to use it.');
387
388         $this->showForm($msg, true);
389     }
390
391     /**
392      * Cancel a pending confirmation
393      *
394      * Cancels the confirmation.
395      *
396      * @return void
397      */
398
399     function cancelConfirmation()
400     {
401         $sms     = $this->trimmed('sms');
402         $carrier = $this->trimmed('carrier');
403
404         $confirm = $this->getConfirmation();
405
406         if (!$confirm) {
407             // TRANS: Message given canceling SMS phone number confirmation that is not pending.
408             $this->showForm(_('No pending confirmation to cancel.'));
409             return;
410         }
411         if ($confirm->address != $sms) {
412             // TRANS: Message given canceling SMS phone number confirmation for the wrong phone number.
413             $this->showForm(_('That is the wrong confirmation number.'));
414             return;
415         }
416
417         $result = $confirm->delete();
418
419         if (!$result) {
420             common_log_db_error($confirm, 'DELETE', __FILE__);
421             // TRANS: Server error thrown on database error canceling SMS phone number confirmation.
422             $this->serverError(_('Couldn\'t delete email confirmation.'));
423             return;
424         }
425
426         // TRANS: Message given after successfully canceling SMS phone number confirmation.
427         $this->showForm(_('SMS confirmation cancelled.'), true);
428     }
429
430     /**
431      * Remove a phone number from the user's account
432      *
433      * @return void
434      */
435
436     function removeAddress()
437     {
438         $user = common_current_user();
439
440         $sms     = $this->arg('sms');
441         $carrier = $this->arg('carrier');
442
443         // Maybe an old tab open...?
444
445         if ($user->sms != $sms) {
446             // TRANS: Message given trying to remove an SMS phone number that is not
447             // TRANS: registered for the active user.
448             $this->showForm(_('That is not your phone number.'));
449             return;
450         }
451
452         $user->query('BEGIN');
453
454         $original = clone($user);
455
456         $user->sms      = null;
457         $user->carrier  = null;
458         $user->smsemail = null;
459
460         $result = $user->updateKeys($original);
461         if (!$result) {
462             common_log_db_error($user, 'UPDATE', __FILE__);
463             // TRANS: Server error thrown on database error removing a registered SMS phone number.
464             $this->serverError(_('Couldn\'t update user.'));
465             return;
466         }
467         $user->query('COMMIT');
468
469         // TRANS: Message given after successfully removing a registered SMS phone number.
470         $this->showForm(_('The SMS phone number was removed.'), true);
471     }
472
473     /**
474      * Does this sms number exist in our database?
475      *
476      * Also checks if it belongs to someone else
477      *
478      * @param string $sms phone number to check
479      *
480      * @return boolean does the number exist
481      */
482
483     function smsExists($sms)
484     {
485         $user = common_current_user();
486
487         $other = User::staticGet('sms', $sms);
488
489         if (!$other) {
490             return false;
491         } else {
492             return $other->id != $user->id;
493         }
494     }
495
496     /**
497      * Show a drop-down box with all the SMS carriers in the DB
498      *
499      * @return void
500      */
501
502     function carrierSelect()
503     {
504         $carrier = new Sms_carrier();
505
506         $cnt = $carrier->find();
507
508         $this->elementStart('ul', 'form_data');
509         $this->elementStart('li');
510         // TRANS: Label for mobile carrier dropdown menu in SMS settings.
511         $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
512         $this->elementStart('select', array('name' => 'carrier',
513                                             'id' => 'carrier'));
514         $this->element('option', array('value' => 0),
515                        // TRANS: Default option for mobile carrier dropdown menu in SMS settings.
516                        _('Select a carrier'));
517         while ($carrier->fetch()) {
518             $this->element('option', array('value' => $carrier->id),
519                            $carrier->name);
520         }
521         $this->elementEnd('select');
522         $this->element('p', 'form_guide',
523                        // TRANS: Form instructions for mobile carrier dropdown menu in SMS settings.
524                        // TRANS: %s is an administrative contact's e-mail address.
525                        sprintf(_('Mobile carrier for your phone. '.
526                                  'If you know a carrier that accepts ' .
527                                  'SMS over email but isn\'t listed here, ' .
528                                  'send email to let us know at %s.'),
529                                common_config('site', 'email')));
530         $this->elementEnd('li');
531         $this->elementEnd('ul');
532     }
533
534     /**
535      * Confirm an SMS confirmation code
536      *
537      * Redirects to the confirmaddress page for this code
538      *
539      * @return void
540      */
541
542     function confirmCode()
543     {
544         $code = $this->trimmed('code');
545
546         if (!$code) {
547             // TRANS: Message given saving SMS phone number confirmation code without having provided one.
548             $this->showForm(_('No code entered'));
549             return;
550         }
551
552         common_redirect(common_local_url('confirmaddress',
553                                          array('code' => $code)),
554                         303);
555     }
556
557     /**
558      * Handle a request to remove an incoming email address
559      *
560      * @return void
561      */
562
563     function removeIncoming()
564     {
565         $user = common_current_user();
566
567         if (!$user->incomingemail) {
568             $this->showForm(_('No incoming email address.'));
569             return;
570         }
571
572         $orig = clone($user);
573
574         $user->incomingemail = null;
575
576         if (!$user->updateKeys($orig)) {
577             common_log_db_error($user, 'UPDATE', __FILE__);
578             $this->serverError(_("Couldn't update user record."));
579         }
580
581         $this->showForm(_('Incoming email address removed.'), true);
582     }
583
584     /**
585      * Generate a new incoming email address
586      *
587      * @return void
588      *
589      * @see Emailsettings::newIncoming
590      */
591
592     function newIncoming()
593     {
594         $user = common_current_user();
595
596         $orig = clone($user);
597
598         $user->incomingemail = mail_new_incoming_address();
599
600         if (!$user->updateKeys($orig)) {
601             common_log_db_error($user, 'UPDATE', __FILE__);
602             $this->serverError(_("Couldn't update user record."));
603         }
604
605         $this->showForm(_('New incoming email address added.'), true);
606     }
607 }