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