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