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