3 * StatusNet, 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 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://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 if (!common_config('sms', 'enabled')) {
84 $this->element('div', array('class' => 'error'),
85 _('SMS is not available.'));
89 $user = common_current_user();
91 $this->elementStart('form', array('method' => 'post',
92 'id' => 'form_settings_sms',
93 'class' => 'form_settings',
95 common_local_url('smssettings')));
97 $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
98 $this->element('legend', null, _('Address'));
99 $this->hidden('token', common_session_token());
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'));
111 $confirm = $this->getConfirmation();
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'));
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'));
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, '.
136 $this->elementEnd('li');
137 $this->elementEnd('ul');
138 $this->carrierSelect();
139 $this->submit('add', _('Add'));
142 $this->elementEnd('fieldset');
145 $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
146 $this->element('legend', null, _('Incoming email'));
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'));
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');
162 $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
163 $this->element('legend', null, _('Preferences'));
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.'),
172 $this->elementEnd('li');
173 $this->elementEnd('ul');
175 $this->submit('save', _('Save'));
177 $this->elementEnd('fieldset');
178 $this->elementEnd('form');
182 * Get a pending confirmation, if any, for this user
186 * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
189 function getConfirmation()
191 $user = common_current_user();
193 $confirm = new Confirm_address();
195 $confirm->user_id = $user->id;
196 $confirm->address_type = 'sms';
198 if ($confirm->find(true)) {
206 * Handle posts to this form
208 * Based on the button that was pressed, muxes out to other functions
209 * to do the actual task requested.
211 * All sub-functions reload the form with a message -- success or failure.
216 function handlePost()
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.'));
227 if ($this->arg('save')) {
228 $this->savePreferences();
229 } else if ($this->arg('add')) {
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();
242 $this->showForm(_('Unexpected form submission.'));
247 * Handle a request to save preferences
249 * Sets the user's SMS preferences in the DB.
254 function savePreferences()
256 $smsnotify = $this->boolean('smsnotify');
258 $user = common_current_user();
260 assert(!is_null($user)); // should already be checked
262 $user->query('BEGIN');
264 $original = clone($user);
266 $user->smsnotify = $smsnotify;
268 $result = $user->update($original);
270 if ($result === false) {
271 common_log_db_error($user, 'UPDATE', __FILE__);
272 $this->serverError(_('Couldn\'t update user.'));
276 $user->query('COMMIT');
278 $this->showForm(_('Preferences saved.'), true);
282 * Add a new SMS number for confirmation
284 * When the user requests a new SMS number, sends a confirmation
290 function addAddress()
292 $user = common_current_user();
294 $sms = $this->trimmed('sms');
295 $carrier_id = $this->trimmed('carrier');
300 $this->showForm(_('No phone number.'));
305 $this->showForm(_('No carrier selected.'));
309 $sms = common_canonical_sms($sms);
311 if ($user->sms == $sms) {
312 $this->showForm(_('That is already your phone number.'));
314 } else if ($this->smsExists($sms)) {
315 $this->showForm(_('That phone number already belongs to another user.'));
319 $confirm = new Confirm_address();
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);
327 $result = $confirm->insert();
329 if ($result === false) {
330 common_log_db_error($confirm, 'INSERT', __FILE__);
331 $this->serverError(_('Couldn\'t insert confirmation code.'));
335 $carrier = Sms_carrier::staticGet($carrier_id);
337 mail_confirm_sms($confirm->code,
339 $carrier->toEmailAddress($sms));
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.');
345 $this->showForm($msg, true);
349 * Cancel a pending confirmation
351 * Cancels the confirmation.
356 function cancelConfirmation()
358 $sms = $this->trimmed('sms');
359 $carrier = $this->trimmed('carrier');
361 $confirm = $this->getConfirmation();
364 $this->showForm(_('No pending confirmation to cancel.'));
367 if ($confirm->address != $sms) {
368 $this->showForm(_('That is the wrong confirmation number.'));
372 $result = $confirm->delete();
375 common_log_db_error($confirm, 'DELETE', __FILE__);
376 $this->serverError(_('Couldn\'t delete email confirmation.'));
380 $this->showForm(_('Confirmation cancelled.'), true);
384 * Remove a phone number from the user's account
389 function removeAddress()
391 $user = common_current_user();
393 $sms = $this->arg('sms');
394 $carrier = $this->arg('carrier');
396 // Maybe an old tab open...?
398 if ($user->sms != $sms) {
399 $this->showForm(_('That is not your phone number.'));
403 $user->query('BEGIN');
405 $original = clone($user);
408 $user->carrier = null;
409 $user->smsemail = null;
411 $result = $user->updateKeys($original);
413 common_log_db_error($user, 'UPDATE', __FILE__);
414 $this->serverError(_('Couldn\'t update user.'));
417 $user->query('COMMIT');
419 $this->showForm(_('The address was removed.'), true);
423 * Does this sms number exist in our database?
425 * Also checks if it belongs to someone else
427 * @param string $sms phone number to check
429 * @return boolean does the number exist
432 function smsExists($sms)
434 $user = common_current_user();
436 $other = User::staticGet('sms', $sms);
441 return $other->id != $user->id;
446 * Show a drop-down box with all the SMS carriers in the DB
451 function carrierSelect()
453 $carrier = new Sms_carrier();
455 $cnt = $carrier->find();
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',
462 $this->element('option', array('value' => 0),
463 _('Select a carrier'));
464 while ($carrier->fetch()) {
465 $this->element('option', array('value' => $carrier->id),
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');
480 * Confirm an SMS confirmation code
482 * Redirects to the confirmaddress page for this code
487 function confirmCode()
489 $code = $this->trimmed('code');
492 $this->showForm(_('No code entered'));
496 common_redirect(common_local_url('confirmaddress',
497 array('code' => $code)),
502 * Handle a request to remove an incoming email address
507 function removeIncoming()
509 $user = common_current_user();
511 if (!$user->incomingemail) {
512 $this->showForm(_('No incoming email address.'));
516 $orig = clone($user);
518 $user->incomingemail = null;
520 if (!$user->updateKeys($orig)) {
521 common_log_db_error($user, 'UPDATE', __FILE__);
522 $this->serverError(_("Couldn't update user record."));
525 $this->showForm(_('Incoming email address removed.'), true);
529 * Generate a new incoming email address
533 * @see Emailsettings::newIncoming
536 function newIncoming()
538 $user = common_current_user();
540 $orig = clone($user);
542 $user->incomingemail = mail_new_incoming_address();
544 if (!$user->updateKeys($orig)) {
545 common_log_db_error($user, 'UPDATE', __FILE__);
546 $this->serverError(_("Couldn't update user record."));
549 $this->showForm(_('New incoming email address added.'), true);