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