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@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/
30 if (!defined('STATUSNET') && !defined('LACONICA')) {
34 require_once INSTALLDIR.'/lib/connectsettingsaction.php';
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/
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%%.');
72 function showScripts()
74 parent::showScripts();
75 $this->autofocus('sms');
79 * Content area of the page
81 * Shows a form for adding and removing SMS phone numbers and setting
87 function showContent()
89 if (!common_config('sms', 'enabled')) {
90 $this->element('div', array('class' => 'error'),
91 _('SMS is not available.'));
95 $user = common_current_user();
97 $this->elementStart('form', array('method' => 'post',
98 'id' => 'form_settings_sms',
99 'class' => 'form_settings',
101 common_local_url('smssettings')));
103 $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
104 $this->element('legend', null, _('Address'));
105 $this->hidden('token', common_session_token());
108 $carrier = $user->getCarrier();
109 $this->element('p', 'form_confirmed',
110 $user->sms . ' (' . $carrier->name . ')');
111 $this->element('p', 'form_guide',
112 _('Current confirmed SMS-enabled phone number.'));
113 $this->hidden('sms', $user->sms);
114 $this->hidden('carrier', $user->carrier);
115 $this->submit('remove', _('Remove'));
117 $confirm = $this->getConfirmation();
119 $carrier = Sms_carrier::staticGet($confirm->address_extra);
120 $this->element('p', 'form_unconfirmed',
121 $confirm->address . ' (' . $carrier->name . ')');
122 $this->element('p', 'form_guide',
123 _('Awaiting confirmation on this phone number.'));
124 $this->hidden('sms', $confirm->address);
125 $this->hidden('carrier', $confirm->address_extra);
126 $this->submit('cancel', _('Cancel'));
128 $this->elementStart('ul', 'form_data');
129 $this->elementStart('li');
130 $this->input('code', _('Confirmation code'), null,
131 _('Enter the code you received on your phone.'));
132 $this->elementEnd('li');
133 $this->elementEnd('ul');
134 $this->submit('confirm', _('Confirm'));
136 $this->elementStart('ul', 'form_data');
137 $this->elementStart('li');
138 $this->input('sms', _('SMS phone number'),
139 ($this->arg('sms')) ? $this->arg('sms') : null,
140 _('Phone number, no punctuation or spaces, '.
142 $this->elementEnd('li');
143 $this->elementEnd('ul');
144 $this->carrierSelect();
145 $this->submit('add', _('Add'));
148 $this->elementEnd('fieldset');
151 $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
152 $this->element('legend', null, _('Incoming email'));
154 if ($user->incomingemail) {
155 $this->element('p', 'form_unconfirmed', $user->incomingemail);
156 $this->element('p', 'form_note',
157 _('Send email to this address to post new notices.'));
158 $this->submit('removeincoming', _('Remove'));
161 $this->element('p', 'form_guide',
162 _('Make a new email address for posting to; '.
163 'cancels the old one.'));
164 $this->submit('newincoming', _('New'));
165 $this->elementEnd('fieldset');
168 $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
169 $this->element('legend', null, _('Preferences'));
171 $this->elementStart('ul', 'form_data');
172 $this->elementStart('li');
173 $this->checkbox('smsnotify',
174 _('Send me notices through SMS; '.
175 'I understand I may incur '.
176 'exorbitant charges from my carrier.'),
178 $this->elementEnd('li');
179 $this->elementEnd('ul');
181 $this->submit('save', _('Save'));
183 $this->elementEnd('fieldset');
184 $this->elementEnd('form');
188 * Get a pending confirmation, if any, for this user
192 * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
195 function getConfirmation()
197 $user = common_current_user();
199 $confirm = new Confirm_address();
201 $confirm->user_id = $user->id;
202 $confirm->address_type = 'sms';
204 if ($confirm->find(true)) {
212 * Handle posts to this form
214 * Based on the button that was pressed, muxes out to other functions
215 * to do the actual task requested.
217 * All sub-functions reload the form with a message -- success or failure.
222 function handlePost()
226 $token = $this->trimmed('token');
227 if (!$token || $token != common_session_token()) {
228 $this->showForm(_('There was a problem with your session token. '.
229 'Try again, please.'));
233 if ($this->arg('save')) {
234 $this->savePreferences();
235 } else if ($this->arg('add')) {
237 } else if ($this->arg('cancel')) {
238 $this->cancelConfirmation();
239 } else if ($this->arg('remove')) {
240 $this->removeAddress();
241 } else if ($this->arg('removeincoming')) {
242 $this->removeIncoming();
243 } else if ($this->arg('newincoming')) {
244 $this->newIncoming();
245 } else if ($this->arg('confirm')) {
246 $this->confirmCode();
248 $this->showForm(_('Unexpected form submission.'));
253 * Handle a request to save preferences
255 * Sets the user's SMS preferences in the DB.
260 function savePreferences()
262 $smsnotify = $this->boolean('smsnotify');
264 $user = common_current_user();
266 assert(!is_null($user)); // should already be checked
268 $user->query('BEGIN');
270 $original = clone($user);
272 $user->smsnotify = $smsnotify;
274 $result = $user->update($original);
276 if ($result === false) {
277 common_log_db_error($user, 'UPDATE', __FILE__);
278 $this->serverError(_('Couldn\'t update user.'));
282 $user->query('COMMIT');
284 $this->showForm(_('Preferences saved.'), true);
288 * Add a new SMS number for confirmation
290 * When the user requests a new SMS number, sends a confirmation
296 function addAddress()
298 $user = common_current_user();
300 $sms = $this->trimmed('sms');
301 $carrier_id = $this->trimmed('carrier');
306 $this->showForm(_('No phone number.'));
311 $this->showForm(_('No carrier selected.'));
315 $sms = common_canonical_sms($sms);
317 if ($user->sms == $sms) {
318 $this->showForm(_('That is already your phone number.'));
320 } else if ($this->smsExists($sms)) {
321 $this->showForm(_('That phone number already belongs to another user.'));
325 $confirm = new Confirm_address();
327 $confirm->address = $sms;
328 $confirm->address_extra = $carrier_id;
329 $confirm->address_type = 'sms';
330 $confirm->user_id = $user->id;
331 $confirm->code = common_confirmation_code(40);
333 $result = $confirm->insert();
335 if ($result === false) {
336 common_log_db_error($confirm, 'INSERT', __FILE__);
337 $this->serverError(_('Couldn\'t insert confirmation code.'));
341 $carrier = Sms_carrier::staticGet($carrier_id);
343 mail_confirm_sms($confirm->code,
345 $carrier->toEmailAddress($sms));
347 $msg = _('A confirmation code was sent to the phone number you added. '.
348 'Check your phone for the code and instructions '.
349 'on how to use it.');
351 $this->showForm($msg, true);
355 * Cancel a pending confirmation
357 * Cancels the confirmation.
362 function cancelConfirmation()
364 $sms = $this->trimmed('sms');
365 $carrier = $this->trimmed('carrier');
367 $confirm = $this->getConfirmation();
370 $this->showForm(_('No pending confirmation to cancel.'));
373 if ($confirm->address != $sms) {
374 $this->showForm(_('That is the wrong confirmation number.'));
378 $result = $confirm->delete();
381 common_log_db_error($confirm, 'DELETE', __FILE__);
382 $this->serverError(_('Couldn\'t delete email confirmation.'));
386 $this->showForm(_('Confirmation cancelled.'), true);
390 * Remove a phone number from the user's account
395 function removeAddress()
397 $user = common_current_user();
399 $sms = $this->arg('sms');
400 $carrier = $this->arg('carrier');
402 // Maybe an old tab open...?
404 if ($user->sms != $sms) {
405 $this->showForm(_('That is not your phone number.'));
409 $user->query('BEGIN');
411 $original = clone($user);
414 $user->carrier = null;
415 $user->smsemail = null;
417 $result = $user->updateKeys($original);
419 common_log_db_error($user, 'UPDATE', __FILE__);
420 $this->serverError(_('Couldn\'t update user.'));
423 $user->query('COMMIT');
425 $this->showForm(_('The address was removed.'), true);
429 * Does this sms number exist in our database?
431 * Also checks if it belongs to someone else
433 * @param string $sms phone number to check
435 * @return boolean does the number exist
438 function smsExists($sms)
440 $user = common_current_user();
442 $other = User::staticGet('sms', $sms);
447 return $other->id != $user->id;
452 * Show a drop-down box with all the SMS carriers in the DB
457 function carrierSelect()
459 $carrier = new Sms_carrier();
461 $cnt = $carrier->find();
463 $this->elementStart('ul', 'form_data');
464 $this->elementStart('li');
465 $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
466 $this->elementStart('select', array('name' => 'carrier',
468 $this->element('option', array('value' => 0),
469 _('Select a carrier'));
470 while ($carrier->fetch()) {
471 $this->element('option', array('value' => $carrier->id),
474 $this->elementEnd('select');
475 $this->element('p', 'form_guide',
476 sprintf(_('Mobile carrier for your phone. '.
477 'If you know a carrier that accepts ' .
478 'SMS over email but isn\'t listed here, ' .
479 'send email to let us know at %s.'),
480 common_config('site', 'email')));
481 $this->elementEnd('li');
482 $this->elementEnd('ul');
486 * Confirm an SMS confirmation code
488 * Redirects to the confirmaddress page for this code
493 function confirmCode()
495 $code = $this->trimmed('code');
498 $this->showForm(_('No code entered'));
502 common_redirect(common_local_url('confirmaddress',
503 array('code' => $code)),
508 * Handle a request to remove an incoming email address
513 function removeIncoming()
515 $user = common_current_user();
517 if (!$user->incomingemail) {
518 $this->showForm(_('No incoming email address.'));
522 $orig = clone($user);
524 $user->incomingemail = null;
526 if (!$user->updateKeys($orig)) {
527 common_log_db_error($user, 'UPDATE', __FILE__);
528 $this->serverError(_("Couldn't update user record."));
531 $this->showForm(_('Incoming email address removed.'), true);
535 * Generate a new incoming email address
539 * @see Emailsettings::newIncoming
542 function newIncoming()
544 $user = common_current_user();
546 $orig = clone($user);
548 $user->incomingemail = mail_new_incoming_address();
550 if (!$user->updateKeys($orig)) {
551 common_log_db_error($user, 'UPDATE', __FILE__);
552 $this->serverError(_("Couldn't update user record."));
555 $this->showForm(_('New incoming email address added.'), true);