3 * Laconica, the distributed open-source microblogging tool
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.
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.
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/>.
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/
30 if (!defined('LACONICA')) {
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
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/
48 class SmssettingsAction extends ConnectSettingsAction
53 * @return string Title of the page
58 return _('SMS Settings');
62 * Instructions for use
64 * @return instructions for use
67 function getInstructions()
69 return _('You can receive SMS messages through email from %%site.name%%.');
73 * Content area of the page
75 * Shows a form for adding and removing SMS phone numbers and setting
81 function showContent()
83 $user = common_current_user();
85 $this->elementStart('form', array('method' => 'post',
86 'id' => 'form_settings_sms',
87 'class' => 'form_settings',
89 common_local_url('smssettings')));
91 $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
92 $this->element('legend', null, _('Address'));
93 $this->hidden('token', common_session_token());
96 $carrier = $user->getCarrier();
97 $this->element('p', 'form_confirmed',
98 $user->sms . ' (' . $carrier->name . ')');
99 $this->element('p', 'form_guide',
100 _('Current confirmed SMS-enabled phone number.'));
101 $this->hidden('sms', $user->sms);
102 $this->hidden('carrier', $user->carrier);
103 $this->submit('remove', _('Remove'));
105 $confirm = $this->getConfirmation();
107 $carrier = Sms_carrier::staticGet($confirm->address_extra);
108 $this->element('p', 'form_unconfirmed',
109 $confirm->address . ' (' . $carrier->name . ')');
110 $this->element('p', 'form_guide',
111 _('Awaiting confirmation on this phone number.'));
112 $this->hidden('sms', $confirm->address);
113 $this->hidden('carrier', $confirm->address_extra);
114 $this->submit('cancel', _('Cancel'));
116 $this->elementStart('ul', 'form_data');
117 $this->elementStart('li');
118 $this->input('code', _('Confirmation code'), null,
119 _('Enter the code you received on your phone.'));
120 $this->elementEnd('li');
121 $this->elementEnd('ul');
122 $this->submit('confirm', _('Confirm'));
124 $this->elementStart('ul', 'form_data');
125 $this->elementStart('li');
126 $this->input('sms', _('SMS Phone number'),
127 ($this->arg('sms')) ? $this->arg('sms') : null,
128 _('Phone number, no punctuation or spaces, '.
130 $this->elementEnd('li');
131 $this->elementEnd('ul');
132 $this->carrierSelect();
133 $this->submit('add', _('Add'));
136 $this->elementEnd('fieldset');
139 $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
140 $this->element('legend', null, _('Incoming email'));
142 if ($user->incomingemail) {
143 $this->element('p', 'form_unconfirmed', $user->incomingemail);
144 $this->element('p', 'form_note',
145 _('Send email to this address to post new notices.'));
146 $this->submit('removeincoming', _('Remove'));
149 $this->element('p', 'form_guide',
150 _('Make a new email address for posting to; '.
151 'cancels the old one.'));
152 $this->submit('newincoming', _('New'));
153 $this->elementEnd('fieldset');
156 $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
157 $this->element('legend', null, _('Preferences'));
159 $this->elementStart('ul', 'form_data');
160 $this->elementStart('li');
161 $this->checkbox('smsnotify',
162 _('Send me notices through SMS; '.
163 'I understand I may incur '.
164 'exorbitant charges from my carrier.'),
166 $this->elementEnd('li');
167 $this->elementEnd('ul');
169 $this->submit('save', _('Save'));
171 $this->elementEnd('fieldset');
172 $this->elementEnd('form');
176 * Get a pending confirmation, if any, for this user
180 * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
183 function getConfirmation()
185 $user = common_current_user();
187 $confirm = new Confirm_address();
189 $confirm->user_id = $user->id;
190 $confirm->address_type = 'sms';
192 if ($confirm->find(true)) {
200 * Handle posts to this form
202 * Based on the button that was pressed, muxes out to other functions
203 * to do the actual task requested.
205 * All sub-functions reload the form with a message -- success or failure.
210 function handlePost()
214 $token = $this->trimmed('token');
215 if (!$token || $token != common_session_token()) {
216 $this->showForm(_('There was a problem with your session token. '.
217 'Try again, please.'));
221 if ($this->arg('save')) {
222 $this->savePreferences();
223 } else if ($this->arg('add')) {
225 } else if ($this->arg('cancel')) {
226 $this->cancelConfirmation();
227 } else if ($this->arg('remove')) {
228 $this->removeAddress();
229 } else if ($this->arg('removeincoming')) {
230 $this->removeIncoming();
231 } else if ($this->arg('newincoming')) {
232 $this->newIncoming();
233 } else if ($this->arg('confirm')) {
234 $this->confirmCode();
236 $this->showForm(_('Unexpected form submission.'));
241 * Handle a request to save preferences
243 * Sets the user's SMS preferences in the DB.
248 function savePreferences()
250 $smsnotify = $this->boolean('smsnotify');
252 $user = common_current_user();
254 assert(!is_null($user)); // should already be checked
256 $user->query('BEGIN');
258 $original = clone($user);
260 $user->smsnotify = $smsnotify;
262 $result = $user->update($original);
264 if ($result === false) {
265 common_log_db_error($user, 'UPDATE', __FILE__);
266 $this->serverError(_('Couldn\'t update user.'));
270 $user->query('COMMIT');
272 $this->showForm(_('Preferences saved.'), true);
276 * Add a new SMS number for confirmation
278 * When the user requests a new SMS number, sends a confirmation
284 function addAddress()
286 $user = common_current_user();
288 $sms = $this->trimmed('sms');
289 $carrier_id = $this->trimmed('carrier');
294 $this->showForm(_('No phone number.'));
299 $this->showForm(_('No carrier selected.'));
303 $sms = common_canonical_sms($sms);
305 if ($user->sms == $sms) {
306 $this->showForm(_('That is already your phone number.'));
308 } else if ($this->smsExists($sms)) {
309 $this->showForm(_('That phone number already belongs to another user.'));
313 $confirm = new Confirm_address();
315 $confirm->address = $sms;
316 $confirm->address_extra = $carrier_id;
317 $confirm->address_type = 'sms';
318 $confirm->user_id = $user->id;
319 $confirm->code = common_confirmation_code(40);
321 $result = $confirm->insert();
323 if ($result === false) {
324 common_log_db_error($confirm, 'INSERT', __FILE__);
325 $this->serverError(_('Couldn\'t insert confirmation code.'));
329 $carrier = Sms_carrier::staticGet($carrier_id);
331 mail_confirm_sms($confirm->code,
333 $carrier->toEmailAddress($sms));
335 $msg = _('A confirmation code was sent to the phone number you added. '.
336 'Check your phone for the code and instructions '.
337 'on how to use it.');
339 $this->showForm($msg, true);
343 * Cancel a pending confirmation
345 * Cancels the confirmation.
350 function cancelConfirmation()
352 $sms = $this->trimmed('sms');
353 $carrier = $this->trimmed('carrier');
355 $confirm = $this->getConfirmation();
358 $this->showForm(_('No pending confirmation to cancel.'));
361 if ($confirm->address != $sms) {
362 $this->showForm(_('That is the wrong confirmation number.'));
366 $result = $confirm->delete();
369 common_log_db_error($confirm, 'DELETE', __FILE__);
370 $this->serverError(_('Couldn\'t delete email confirmation.'));
374 $this->showForm(_('Confirmation cancelled.'), true);
378 * Remove a phone number from the user's account
383 function removeAddress()
385 $user = common_current_user();
387 $sms = $this->arg('sms');
388 $carrier = $this->arg('carrier');
390 // Maybe an old tab open...?
392 if ($user->sms != $sms) {
393 $this->showForm(_('That is not your phone number.'));
397 $user->query('BEGIN');
399 $original = clone($user);
402 $user->carrier = null;
403 $user->smsemail = null;
405 $result = $user->updateKeys($original);
407 common_log_db_error($user, 'UPDATE', __FILE__);
408 $this->serverError(_('Couldn\'t update user.'));
411 $user->query('COMMIT');
413 $this->showForm(_('The address was removed.'), true);
417 * Does this sms number exist in our database?
419 * Also checks if it belongs to someone else
421 * @param string $sms phone number to check
423 * @return boolean does the number exist
426 function smsExists($sms)
428 $user = common_current_user();
430 $other = User::staticGet('sms', $sms);
435 return $other->id != $user->id;
440 * Show a drop-down box with all the SMS carriers in the DB
445 function carrierSelect()
447 $carrier = new Sms_carrier();
449 $cnt = $carrier->find();
451 $this->elementStart('ul', 'form_data');
452 $this->elementStart('li');
453 $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
454 $this->elementStart('select', array('name' => 'carrier',
456 $this->element('option', array('value' => 0),
457 _('Select a carrier'));
458 while ($carrier->fetch()) {
459 $this->element('option', array('value' => $carrier->id),
462 $this->elementEnd('select');
463 $this->element('p', 'form_guide',
464 sprintf(_('Mobile carrier for your phone. '.
465 'If you know a carrier that accepts ' .
466 'SMS over email but isn\'t listed here, ' .
467 'send email to let us know at %s.'),
468 common_config('site', 'email')));
469 $this->elementEnd('li');
470 $this->elementEnd('ul');
474 * Confirm an SMS confirmation code
476 * Redirects to the confirmaddress page for this code
481 function confirmCode()
483 $code = $this->trimmed('code');
486 $this->showForm(_('No code entered'));
490 common_redirect(common_local_url('confirmaddress',
491 array('code' => $code)),
496 * Handle a request to remove an incoming email address
501 function removeIncoming()
503 $user = common_current_user();
505 if (!$user->incomingemail) {
506 $this->showForm(_('No incoming email address.'));
510 $orig = clone($user);
512 $user->incomingemail = null;
514 if (!$user->updateKeys($orig)) {
515 common_log_db_error($user, 'UPDATE', __FILE__);
516 $this->serverError(_("Couldn't update user record."));
519 $this->showForm(_('Incoming email address removed.'), true);
523 * Generate a new incoming email address
527 * @see Emailsettings::newIncoming
530 function newIncoming()
532 $user = common_current_user();
534 $orig = clone($user);
536 $user->incomingemail = mail_new_incoming_address();
538 if (!$user->updateKeys($orig)) {
539 common_log_db_error($user, 'UPDATE', __FILE__);
540 $this->serverError(_("Couldn't update user record."));
543 $this->showForm(_('New incoming email address added.'), true);