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 // TRANS: Title for SMS settings.
59 return _('SMS settings');
63 * Instructions for use
65 * @return instructions for use
68 function getInstructions()
70 // XXX: For consistency of parameters in messages, this should be a
71 // regular parameters, replaced with sprintf().
72 // TRANS: SMS settings page instructions.
73 // TRANS: %%site.name%% is the name of the site.
74 return _('You can receive SMS messages through email from %%site.name%%.');
77 function showScripts()
79 parent::showScripts();
80 $this->autofocus('sms');
84 * Content area of the page
86 * Shows a form for adding and removing SMS phone numbers and setting
92 function showContent()
94 if (!common_config('sms', 'enabled')) {
95 $this->element('div', array('class' => 'error'),
96 // TRANS: Message given in the SMS settings if SMS is not enabled on the site.
97 _('SMS is not available.'));
101 $user = common_current_user();
103 $this->elementStart('form', array('method' => 'post',
104 'id' => 'form_settings_sms',
105 'class' => 'form_settings',
107 common_local_url('smssettings')));
109 $this->elementStart('fieldset', array('id' => 'settings_sms_address'));
110 // TRANS: Form legend for SMS settings form.
111 $this->element('legend', null, _('SMS address'));
112 $this->hidden('token', common_session_token());
115 $carrier = $user->getCarrier();
116 $this->element('p', 'form_confirmed',
117 $user->sms . ' (' . $carrier->name . ')');
118 $this->element('p', 'form_guide',
119 // TRANS: Form guide in SMS settings form.
120 _('Current confirmed SMS-enabled phone number.'));
121 $this->hidden('sms', $user->sms);
122 $this->hidden('carrier', $user->carrier);
123 // TRANS: Button label to remove a confirmed SMS address.
124 $this->submit('remove', _m('BUTTON','Remove'));
126 $confirm = $this->getConfirmation();
128 $carrier = Sms_carrier::staticGet($confirm->address_extra);
129 $this->element('p', 'form_unconfirmed',
130 $confirm->address . ' (' . $carrier->name . ')');
131 $this->element('p', 'form_guide',
132 // TRANS: Form guide in IM settings form.
133 _('Awaiting confirmation on this phone number.'));
134 $this->hidden('sms', $confirm->address);
135 $this->hidden('carrier', $confirm->address_extra);
136 // TRANS: Button label to cancel a SMS address confirmation procedure.
137 $this->submit('cancel', _m('BUTTON','Cancel'));
139 $this->elementStart('ul', 'form_data');
140 $this->elementStart('li');
141 // TRANS: Field label for SMS address input in SMS settings form.
142 $this->input('code', _('Confirmation code'), null,
143 // TRANS: Form field instructions in SMS settings form.
144 _('Enter the code you received on your phone.'));
145 $this->elementEnd('li');
146 $this->elementEnd('ul');
147 // TRANS: Button label to confirm SMS confirmation code in SMS settings.
148 $this->submit('confirm', _m('BUTTON','Confirm'));
150 $this->elementStart('ul', 'form_data');
151 $this->elementStart('li');
152 // TRANS: Field label for SMS phone number input in SMS settings form.
153 $this->input('sms', _('SMS phone number'),
154 ($this->arg('sms')) ? $this->arg('sms') : null,
155 // TRANS: SMS phone number input field instructions in SMS settings form.
156 _('Phone number, no punctuation or spaces, '.
158 $this->elementEnd('li');
159 $this->elementEnd('ul');
160 $this->carrierSelect();
161 // TRANS: Button label for adding a SMS phone number in SMS settings form.
162 $this->submit('add', _m('BUTTON','Add'));
165 $this->elementEnd('fieldset');
168 $this->elementStart('fieldset', array('id' => 'settings_sms_incoming_email'));
169 // XXX: Confused! This is about SMS. Should this message be updated?
170 // TRANS: Form legend for incoming SMS settings form.
171 $this->element('legend', null, _('Incoming email'));
173 if ($user->incomingemail) {
174 $this->element('p', 'form_unconfirmed', $user->incomingemail);
175 $this->element('p', 'form_note',
176 // XXX: Confused! This is about SMS. Should this message be updated?
177 // TRANS: Form instructions for incoming SMS e-mail address form in SMS settings.
178 _('Send email to this address to post new notices.'));
179 // TRANS: Button label for removing a set sender SMS e-mail address to post notices from.
180 $this->submit('removeincoming', _m('BUTTON','Remove'));
183 $this->element('p', 'form_guide',
184 // XXX: Confused! This is about SMS. Should this message be updated?
185 // TRANS: Instructions for incoming SMS e-mail address input form.
186 _('Make a new email address for posting to; '.
187 'cancels the old one.'));
188 // TRANS: Button label for adding an SMS e-mail address to send notices from.
189 $this->submit('newincoming', _m('BUTTON','New'));
190 $this->elementEnd('fieldset');
193 $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
194 // TRANS: Form legend for SMS preferences form.
195 $this->element('legend', null, _('SMS preferences'));
197 $this->elementStart('ul', 'form_data');
198 $this->elementStart('li');
199 $this->checkbox('smsnotify',
200 // TRANS: Checkbox label in SMS preferences form.
201 _('Send me notices through SMS; '.
202 'I understand I may incur '.
203 'exorbitant charges from my carrier.'),
205 $this->elementEnd('li');
206 $this->elementEnd('ul');
208 // TRANS: Button label to save SMS preferences.
209 $this->submit('save', _m('BUTTON','Save'));
211 $this->elementEnd('fieldset');
212 $this->elementEnd('form');
216 * Get a pending confirmation, if any, for this user
220 * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
223 function getConfirmation()
225 $user = common_current_user();
227 $confirm = new Confirm_address();
229 $confirm->user_id = $user->id;
230 $confirm->address_type = 'sms';
232 if ($confirm->find(true)) {
240 * Handle posts to this form
242 * Based on the button that was pressed, muxes out to other functions
243 * to do the actual task requested.
245 * All sub-functions reload the form with a message -- success or failure.
250 function handlePost()
254 $token = $this->trimmed('token');
255 if (!$token || $token != common_session_token()) {
256 $this->showForm(_('There was a problem with your session token. '.
257 'Try again, please.'));
261 if ($this->arg('save')) {
262 $this->savePreferences();
263 } else if ($this->arg('add')) {
265 } else if ($this->arg('cancel')) {
266 $this->cancelConfirmation();
267 } else if ($this->arg('remove')) {
268 $this->removeAddress();
269 } else if ($this->arg('removeincoming')) {
270 $this->removeIncoming();
271 } else if ($this->arg('newincoming')) {
272 $this->newIncoming();
273 } else if ($this->arg('confirm')) {
274 $this->confirmCode();
276 // TRANS: Message given submitting a form with an unknown action in SMS settings.
277 $this->showForm(_('Unexpected form submission.'));
282 * Handle a request to save preferences
284 * Sets the user's SMS preferences in the DB.
289 function savePreferences()
291 $smsnotify = $this->boolean('smsnotify');
293 $user = common_current_user();
295 assert(!is_null($user)); // should already be checked
297 $user->query('BEGIN');
299 $original = clone($user);
301 $user->smsnotify = $smsnotify;
303 $result = $user->update($original);
305 if ($result === false) {
306 common_log_db_error($user, 'UPDATE', __FILE__);
307 // TRANS: Server error thrown on database error updating SMS preferences.
308 $this->serverError(_('Couldn\'t update user.'));
312 $user->query('COMMIT');
314 // TRANS: Confirmation message for successful SMS preferences save.
315 $this->showForm(_('SMS preferences saved.'), true);
319 * Add a new SMS number for confirmation
321 * When the user requests a new SMS number, sends a confirmation
327 function addAddress()
329 $user = common_current_user();
331 $sms = $this->trimmed('sms');
332 $carrier_id = $this->trimmed('carrier');
337 // TRANS: Message given saving SMS phone number without having provided one.
338 $this->showForm(_('No phone number.'));
343 // TRANS: Message given saving SMS phone number without having selected a carrier.
344 $this->showForm(_('No carrier selected.'));
348 $sms = common_canonical_sms($sms);
350 if ($user->sms == $sms) {
351 // TRANS: Message given saving SMS phone number that is already set.
352 $this->showForm(_('That is already your phone number.'));
354 } else if ($this->smsExists($sms)) {
355 // TRANS: Message given saving SMS phone number that is already set for another user.
356 $this->showForm(_('That phone number already belongs to another user.'));
360 $confirm = new Confirm_address();
362 $confirm->address = $sms;
363 $confirm->address_extra = $carrier_id;
364 $confirm->address_type = 'sms';
365 $confirm->user_id = $user->id;
366 $confirm->code = common_confirmation_code(40);
368 $result = $confirm->insert();
370 if ($result === false) {
371 common_log_db_error($confirm, 'INSERT', __FILE__);
372 // TRANS: Server error thrown on database error adding SMS confirmation code.
373 $this->serverError(_('Couldn\'t insert confirmation code.'));
377 $carrier = Sms_carrier::staticGet($carrier_id);
379 mail_confirm_sms($confirm->code,
381 $carrier->toEmailAddress($sms));
383 // TRANS: Message given saving valid SMS phone number that is to be confirmed.
384 $msg = _('A confirmation code was sent to the phone number you added. '.
385 'Check your phone for the code and instructions '.
386 'on how to use it.');
388 $this->showForm($msg, true);
392 * Cancel a pending confirmation
394 * Cancels the confirmation.
399 function cancelConfirmation()
401 $sms = $this->trimmed('sms');
402 $carrier = $this->trimmed('carrier');
404 $confirm = $this->getConfirmation();
407 // TRANS: Message given canceling SMS phone number confirmation that is not pending.
408 $this->showForm(_('No pending confirmation to cancel.'));
411 if ($confirm->address != $sms) {
412 // TRANS: Message given canceling SMS phone number confirmation for the wrong phone number.
413 $this->showForm(_('That is the wrong confirmation number.'));
417 $result = $confirm->delete();
420 common_log_db_error($confirm, 'DELETE', __FILE__);
421 // TRANS: Server error thrown on database error canceling SMS phone number confirmation.
422 $this->serverError(_('Couldn\'t delete email confirmation.'));
426 // TRANS: Message given after successfully canceling SMS phone number confirmation.
427 $this->showForm(_('SMS confirmation cancelled.'), true);
431 * Remove a phone number from the user's account
436 function removeAddress()
438 $user = common_current_user();
440 $sms = $this->arg('sms');
441 $carrier = $this->arg('carrier');
443 // Maybe an old tab open...?
445 if ($user->sms != $sms) {
446 // TRANS: Message given trying to remove an SMS phone number that is not
447 // TRANS: registered for the active user.
448 $this->showForm(_('That is not your phone number.'));
452 $user->query('BEGIN');
454 $original = clone($user);
457 $user->carrier = null;
458 $user->smsemail = null;
460 $result = $user->updateKeys($original);
462 common_log_db_error($user, 'UPDATE', __FILE__);
463 // TRANS: Server error thrown on database error removing a registered SMS phone number.
464 $this->serverError(_('Couldn\'t update user.'));
467 $user->query('COMMIT');
469 // TRANS: Message given after successfully removing a registered SMS phone number.
470 $this->showForm(_('The SMS phone number was removed.'), true);
474 * Does this sms number exist in our database?
476 * Also checks if it belongs to someone else
478 * @param string $sms phone number to check
480 * @return boolean does the number exist
483 function smsExists($sms)
485 $user = common_current_user();
487 $other = User::staticGet('sms', $sms);
492 return $other->id != $user->id;
497 * Show a drop-down box with all the SMS carriers in the DB
502 function carrierSelect()
504 $carrier = new Sms_carrier();
506 $cnt = $carrier->find();
508 $this->elementStart('ul', 'form_data');
509 $this->elementStart('li');
510 // TRANS: Label for mobile carrier dropdown menu in SMS settings.
511 $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
512 $this->elementStart('select', array('name' => 'carrier',
514 $this->element('option', array('value' => 0),
515 // TRANS: Default option for mobile carrier dropdown menu in SMS settings.
516 _('Select a carrier'));
517 while ($carrier->fetch()) {
518 $this->element('option', array('value' => $carrier->id),
521 $this->elementEnd('select');
522 $this->element('p', 'form_guide',
523 // TRANS: Form instructions for mobile carrier dropdown menu in SMS settings.
524 // TRANS: %s is an administrative contact's e-mail address.
525 sprintf(_('Mobile carrier for your phone. '.
526 'If you know a carrier that accepts ' .
527 'SMS over email but isn\'t listed here, ' .
528 'send email to let us know at %s.'),
529 common_config('site', 'email')));
530 $this->elementEnd('li');
531 $this->elementEnd('ul');
535 * Confirm an SMS confirmation code
537 * Redirects to the confirmaddress page for this code
542 function confirmCode()
544 $code = $this->trimmed('code');
547 // TRANS: Message given saving SMS phone number confirmation code without having provided one.
548 $this->showForm(_('No code entered'));
552 common_redirect(common_local_url('confirmaddress',
553 array('code' => $code)),
558 * Handle a request to remove an incoming email address
563 function removeIncoming()
565 $user = common_current_user();
567 if (!$user->incomingemail) {
568 $this->showForm(_('No incoming email address.'));
572 $orig = clone($user);
574 $user->incomingemail = null;
576 if (!$user->updateKeys($orig)) {
577 common_log_db_error($user, 'UPDATE', __FILE__);
578 $this->serverError(_("Couldn't update user record."));
581 $this->showForm(_('Incoming email address removed.'), true);
585 * Generate a new incoming email address
589 * @see Emailsettings::newIncoming
592 function newIncoming()
594 $user = common_current_user();
596 $orig = clone($user);
598 $user->incomingemail = mail_new_incoming_address();
600 if (!$user->updateKeys($orig)) {
601 common_log_db_error($user, 'UPDATE', __FILE__);
602 $this->serverError(_("Couldn't update user record."));
605 $this->showForm(_('New incoming email address added.'), true);