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