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')) {
39 * @author Evan Prodromou <evan@status.net>
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
41 * @link http://status.net/
46 class SmssettingsAction extends SettingsAction
51 * @return string Title of the page
56 // TRANS: Title for SMS settings.
57 return _('SMS settings');
61 * Instructions for use
63 * @return instructions for use
66 function getInstructions()
68 // XXX: For consistency of parameters in messages, this should be a
69 // regular parameters, replaced with sprintf().
70 // TRANS: SMS settings page instructions.
71 // TRANS: %%site.name%% is the name of the site.
72 return _('You can receive SMS messages through email from %%site.name%%.');
75 function showScripts()
77 parent::showScripts();
78 $this->autofocus('sms');
82 * Content area of the page
84 * Shows a form for adding and removing SMS phone numbers and setting
90 function showContent()
92 if (!common_config('sms', 'enabled')) {
93 $this->element('div', array('class' => 'error'),
94 // TRANS: Message given in the SMS settings if SMS is not enabled on the site.
95 _('SMS is not available.'));
99 $user = common_current_user();
101 $this->elementStart('form', array('method' => 'post',
102 'id' => 'form_settings_sms',
103 'class' => 'form_settings',
105 common_local_url('smssettings')));
107 $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
108 // TRANS: Form legend for SMS settings form.
109 $this->element('legend', null, _('SMS address'));
110 $this->hidden('token', common_session_token());
113 $carrier = $user->getCarrier();
114 $this->element('p', 'form_confirmed',
115 $user->sms . ' (' . $carrier->name . ')');
116 $this->element('p', 'form_guide',
117 // TRANS: Form guide in SMS settings form.
118 _('Current confirmed SMS-enabled phone number.'));
119 $this->hidden('sms', $user->sms);
120 $this->hidden('carrier', $user->carrier);
121 // TRANS: Button label to remove a confirmed SMS address.
122 $this->submit('remove', _m('BUTTON','Remove'));
124 $confirm = $this->getConfirmation();
126 $carrier = Sms_carrier::staticGet($confirm->address_extra);
127 $this->element('p', 'form_unconfirmed',
128 $confirm->address . ' (' . $carrier->name . ')');
129 $this->element('p', 'form_guide',
130 // TRANS: Form guide in IM settings form.
131 _('Awaiting confirmation on this phone number.'));
132 $this->hidden('sms', $confirm->address);
133 $this->hidden('carrier', $confirm->address_extra);
134 // TRANS: Button label to cancel a SMS address confirmation procedure.
135 $this->submit('cancel', _m('BUTTON','Cancel'));
137 $this->elementStart('ul', 'form_data');
138 $this->elementStart('li');
139 // TRANS: Field label for SMS address input in SMS settings form.
140 $this->input('code', _('Confirmation code'), null,
141 // TRANS: Form field instructions in SMS settings form.
142 _('Enter the code you received on your phone.'));
143 $this->elementEnd('li');
144 $this->elementEnd('ul');
145 // TRANS: Button label to confirm SMS confirmation code in SMS settings.
146 $this->submit('confirm', _m('BUTTON','Confirm'));
148 $this->elementStart('ul', 'form_data');
149 $this->elementStart('li');
150 // TRANS: Field label for SMS phone number input in SMS settings form.
151 $this->input('sms', _('SMS phone number'),
152 ($this->arg('sms')) ? $this->arg('sms') : null,
153 // TRANS: SMS phone number input field instructions in SMS settings form.
154 _('Phone number, no punctuation or spaces, '.
156 $this->elementEnd('li');
157 $this->elementEnd('ul');
158 $this->carrierSelect();
159 // TRANS: Button label for adding a SMS phone number in SMS settings form.
160 $this->submit('add', _m('BUTTON','Add'));
163 $this->elementEnd('fieldset');
166 $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
167 // XXX: Confused! This is about SMS. Should this message be updated?
168 // TRANS: Form legend for incoming SMS settings form.
169 $this->element('legend', null, _('Incoming email'));
171 if ($user->incomingemail) {
172 $this->element('p', 'form_unconfirmed', $user->incomingemail);
173 $this->element('p', 'form_note',
174 // XXX: Confused! This is about SMS. Should this message be updated?
175 // TRANS: Form instructions for incoming SMS e-mail address form in SMS settings.
176 _('Send email to this address to post new notices.'));
177 // TRANS: Button label for removing a set sender SMS e-mail address to post notices from.
178 $this->submit('removeincoming', _m('BUTTON','Remove'));
181 $this->element('p', 'form_guide',
182 // XXX: Confused! This is about SMS. Should this message be updated?
183 // TRANS: Instructions for incoming SMS e-mail address input form.
184 _('Make a new email address for posting to; '.
185 'cancels the old one.'));
186 // TRANS: Button label for adding an SMS e-mail address to send notices from.
187 $this->submit('newincoming', _m('BUTTON','New'));
188 $this->elementEnd('fieldset');
191 $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
192 // TRANS: Form legend for SMS preferences form.
193 $this->element('legend', null, _('SMS preferences'));
195 $this->elementStart('ul', 'form_data');
196 $this->elementStart('li');
197 $this->checkbox('smsnotify',
198 // TRANS: Checkbox label in SMS preferences form.
199 _('Send me notices through SMS; '.
200 'I understand I may incur '.
201 'exorbitant charges from my carrier.'),
203 $this->elementEnd('li');
204 $this->elementEnd('ul');
206 // TRANS: Button label to save SMS preferences.
207 $this->submit('save', _m('BUTTON','Save'));
209 $this->elementEnd('fieldset');
210 $this->elementEnd('form');
214 * Get a pending confirmation, if any, for this user
218 * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
221 function getConfirmation()
223 $user = common_current_user();
225 $confirm = new Confirm_address();
227 $confirm->user_id = $user->id;
228 $confirm->address_type = 'sms';
230 if ($confirm->find(true)) {
238 * Handle posts to this form
240 * Based on the button that was pressed, muxes out to other functions
241 * to do the actual task requested.
243 * All sub-functions reload the form with a message -- success or failure.
248 function handlePost()
252 $token = $this->trimmed('token');
253 if (!$token || $token != common_session_token()) {
254 $this->showForm(_('There was a problem with your session token. '.
255 'Try again, please.'));
259 if ($this->arg('save')) {
260 $this->savePreferences();
261 } else if ($this->arg('add')) {
263 } else if ($this->arg('cancel')) {
264 $this->cancelConfirmation();
265 } else if ($this->arg('remove')) {
266 $this->removeAddress();
267 } else if ($this->arg('removeincoming')) {
268 $this->removeIncoming();
269 } else if ($this->arg('newincoming')) {
270 $this->newIncoming();
271 } else if ($this->arg('confirm')) {
272 $this->confirmCode();
274 // TRANS: Message given submitting a form with an unknown action in SMS settings.
275 $this->showForm(_('Unexpected form submission.'));
280 * Handle a request to save preferences
282 * Sets the user's SMS preferences in the DB.
287 function savePreferences()
289 $smsnotify = $this->boolean('smsnotify');
291 $user = common_current_user();
293 assert(!is_null($user)); // should already be checked
295 $user->query('BEGIN');
297 $original = clone($user);
299 $user->smsnotify = $smsnotify;
301 $result = $user->update($original);
303 if ($result === false) {
304 common_log_db_error($user, 'UPDATE', __FILE__);
305 // TRANS: Server error thrown on database error updating SMS preferences.
306 $this->serverError(_('Couldn\'t update user.'));
310 $user->query('COMMIT');
312 // TRANS: Confirmation message for successful SMS preferences save.
313 $this->showForm(_('SMS preferences saved.'), true);
317 * Add a new SMS number for confirmation
319 * When the user requests a new SMS number, sends a confirmation
325 function addAddress()
327 $user = common_current_user();
329 $sms = $this->trimmed('sms');
330 $carrier_id = $this->trimmed('carrier');
335 // TRANS: Message given saving SMS phone number without having provided one.
336 $this->showForm(_('No phone number.'));
341 // TRANS: Message given saving SMS phone number without having selected a carrier.
342 $this->showForm(_('No carrier selected.'));
346 $sms = common_canonical_sms($sms);
348 if ($user->sms == $sms) {
349 // TRANS: Message given saving SMS phone number that is already set.
350 $this->showForm(_('That is already your phone number.'));
352 } else if ($this->smsExists($sms)) {
353 // TRANS: Message given saving SMS phone number that is already set for another user.
354 $this->showForm(_('That phone number already belongs to another user.'));
358 $confirm = new Confirm_address();
360 $confirm->address = $sms;
361 $confirm->address_extra = $carrier_id;
362 $confirm->address_type = 'sms';
363 $confirm->user_id = $user->id;
364 $confirm->code = common_confirmation_code(40);
366 $result = $confirm->insert();
368 if ($result === false) {
369 common_log_db_error($confirm, 'INSERT', __FILE__);
370 // TRANS: Server error thrown on database error adding SMS confirmation code.
371 $this->serverError(_('Couldn\'t insert confirmation code.'));
375 $carrier = Sms_carrier::staticGet($carrier_id);
377 mail_confirm_sms($confirm->code,
379 $carrier->toEmailAddress($sms));
381 // TRANS: Message given saving valid SMS phone number that is to be confirmed.
382 $msg = _('A confirmation code was sent to the phone number you added. '.
383 'Check your phone for the code and instructions '.
384 'on how to use it.');
386 $this->showForm($msg, true);
390 * Cancel a pending confirmation
392 * Cancels the confirmation.
397 function cancelConfirmation()
399 $sms = $this->trimmed('sms');
400 $carrier = $this->trimmed('carrier');
402 $confirm = $this->getConfirmation();
405 // TRANS: Message given canceling SMS phone number confirmation that is not pending.
406 $this->showForm(_('No pending confirmation to cancel.'));
409 if ($confirm->address != $sms) {
410 // TRANS: Message given canceling SMS phone number confirmation for the wrong phone number.
411 $this->showForm(_('That is the wrong confirmation number.'));
415 $result = $confirm->delete();
418 common_log_db_error($confirm, 'DELETE', __FILE__);
419 // TRANS: Server error thrown on database error canceling SMS phone number confirmation.
420 $this->serverError(_('Couldn\'t delete email confirmation.'));
424 // TRANS: Message given after successfully canceling SMS phone number confirmation.
425 $this->showForm(_('SMS confirmation cancelled.'), true);
429 * Remove a phone number from the user's account
434 function removeAddress()
436 $user = common_current_user();
438 $sms = $this->arg('sms');
439 $carrier = $this->arg('carrier');
441 // Maybe an old tab open...?
443 if ($user->sms != $sms) {
444 // TRANS: Message given trying to remove an SMS phone number that is not
445 // TRANS: registered for the active user.
446 $this->showForm(_('That is not your phone number.'));
450 $user->query('BEGIN');
452 $original = clone($user);
455 $user->carrier = null;
456 $user->smsemail = null;
458 $result = $user->updateKeys($original);
460 common_log_db_error($user, 'UPDATE', __FILE__);
461 // TRANS: Server error thrown on database error removing a registered SMS phone number.
462 $this->serverError(_('Couldn\'t update user.'));
465 $user->query('COMMIT');
467 // TRANS: Message given after successfully removing a registered SMS phone number.
468 $this->showForm(_('The SMS phone number was removed.'), true);
472 * Does this sms number exist in our database?
474 * Also checks if it belongs to someone else
476 * @param string $sms phone number to check
478 * @return boolean does the number exist
481 function smsExists($sms)
483 $user = common_current_user();
485 $other = User::staticGet('sms', $sms);
490 return $other->id != $user->id;
495 * Show a drop-down box with all the SMS carriers in the DB
500 function carrierSelect()
502 $carrier = new Sms_carrier();
504 $cnt = $carrier->find();
506 $this->elementStart('ul', 'form_data');
507 $this->elementStart('li');
508 // TRANS: Label for mobile carrier dropdown menu in SMS settings.
509 $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
510 $this->elementStart('select', array('name' => 'carrier',
512 $this->element('option', array('value' => 0),
513 // TRANS: Default option for mobile carrier dropdown menu in SMS settings.
514 _('Select a carrier'));
515 while ($carrier->fetch()) {
516 $this->element('option', array('value' => $carrier->id),
519 $this->elementEnd('select');
520 $this->element('p', 'form_guide',
521 // TRANS: Form instructions for mobile carrier dropdown menu in SMS settings.
522 // TRANS: %s is an administrative contact's e-mail address.
523 sprintf(_('Mobile carrier for your phone. '.
524 'If you know a carrier that accepts ' .
525 'SMS over email but isn\'t listed here, ' .
526 'send email to let us know at %s.'),
527 common_config('site', 'email')));
528 $this->elementEnd('li');
529 $this->elementEnd('ul');
533 * Confirm an SMS confirmation code
535 * Redirects to the confirmaddress page for this code
540 function confirmCode()
542 $code = $this->trimmed('code');
545 // TRANS: Message given saving SMS phone number confirmation code without having provided one.
546 $this->showForm(_('No code entered'));
550 common_redirect(common_local_url('confirmaddress',
551 array('code' => $code)),
556 * Handle a request to remove an incoming email address
561 function removeIncoming()
563 $user = common_current_user();
565 if (!$user->incomingemail) {
566 $this->showForm(_('No incoming email address.'));
570 $orig = clone($user);
572 $user->incomingemail = null;
574 if (!$user->updateKeys($orig)) {
575 common_log_db_error($user, 'UPDATE', __FILE__);
576 $this->serverError(_("Couldn't update user record."));
579 $this->showForm(_('Incoming email address removed.'), true);
583 * Generate a new incoming email address
587 * @see Emailsettings::newIncoming
590 function newIncoming()
592 $user = common_current_user();
594 $orig = clone($user);
596 $user->incomingemail = mail_new_incoming_address();
598 if (!$user->updateKeys($orig)) {
599 common_log_db_error($user, 'UPDATE', __FILE__);
600 $this->serverError(_("Couldn't update user record."));
603 $this->showForm(_('New incoming email address added.'), true);