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/settingsaction.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 SettingsAction
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 $this->elementStart('p');
97 $carrier = $user->getCarrier();
98 $this->element('span', 'address confirmed',
99 $user->sms . ' (' . $carrier->name . ')');
100 $this->element('span', 'input_instructions',
101 _('Current confirmed SMS-enabled phone number.'));
102 $this->hidden('sms', $user->sms);
103 $this->hidden('carrier', $user->carrier);
104 $this->elementEnd('p');
105 $this->submit('remove', _('Remove'));
107 $confirm = $this->getConfirmation();
109 $carrier = Sms_carrier::staticGet($confirm->address_extra);
110 $this->elementStart('p');
111 $this->element('span', 'address unconfirmed',
112 $confirm->address . ' (' . $carrier->name . ')');
113 $this->element('span', 'input_instructions',
114 _('Awaiting confirmation on this phone number.'));
115 $this->hidden('sms', $confirm->address);
116 $this->hidden('carrier', $confirm->address_extra);
117 $this->elementEnd('p');
118 $this->submit('cancel', _('Cancel'));
119 $this->input('code', _('Confirmation code'), null,
120 _('Enter the code you received on your phone.'));
121 $this->submit('confirm', _('Confirm'));
123 $this->input('sms', _('SMS Phone number'),
124 ($this->arg('sms')) ? $this->arg('sms') : null,
125 _('Phone number, no punctuation or spaces, '.
127 $this->carrierSelect();
128 $this->submit('add', _('Add'));
131 $this->elementEnd('fieldset');
134 $this->element('h2', null, _('Incoming email'));
136 if ($user->incomingemail) {
137 $this->elementStart('p');
138 $this->element('span', 'address', $user->incomingemail);
139 $this->element('span', 'input_instructions',
140 _('Send email to this address to post new notices.'));
141 $this->elementEnd('p');
142 $this->submit('removeincoming', _('Remove'));
145 $this->element('p', 'form_guide',
146 _('Make a new email address for posting to; '.
147 'cancels the old one.'));
148 $this->submit('newincoming', _('New'));
151 $this->elementStart('fieldset', array('id' => 'settings_sms_preferences'));
152 $this->element('legend', null, _('Preferences'));
155 $this->elementStart('ul', 'form_datas');
156 $this->elementStart('li');
157 $this->checkbox('smsnotify',
158 _('Send me notices through SMS; '.
159 'I understand I may incur '.
160 'exorbitant charges from my carrier.'),
162 $this->elementEnd('li');
163 $this->elementEnd('ul');
165 $this->submit('save', _('Save'));
167 $this->elementEnd('fieldset');
168 $this->elementEnd('form');
172 * Get a pending confirmation, if any, for this user
176 * @todo very similar to EmailsettingsAction::getConfirmation(); refactor?
179 function getConfirmation()
181 $user = common_current_user();
183 $confirm = new Confirm_address();
185 $confirm->user_id = $user->id;
186 $confirm->address_type = 'sms';
188 if ($confirm->find(true)) {
196 * Handle posts to this form
198 * Based on the button that was pressed, muxes out to other functions
199 * to do the actual task requested.
201 * All sub-functions reload the form with a message -- success or failure.
206 function handlePost()
210 $token = $this->trimmed('token');
211 if (!$token || $token != common_session_token()) {
212 $this->showForm(_('There was a problem with your session token. '.
213 'Try again, please.'));
217 if ($this->arg('save')) {
218 $this->savePreferences();
219 } else if ($this->arg('add')) {
221 } else if ($this->arg('cancel')) {
222 $this->cancelConfirmation();
223 } else if ($this->arg('remove')) {
224 $this->removeAddress();
225 } else if ($this->arg('removeincoming')) {
226 $this->removeIncoming();
227 } else if ($this->arg('newincoming')) {
228 $this->newIncoming();
229 } else if ($this->arg('confirm')) {
230 $this->confirmCode();
232 $this->showForm(_('Unexpected form submission.'));
237 * Handle a request to save preferences
239 * Sets the user's SMS preferences in the DB.
244 function savePreferences()
246 $smsnotify = $this->boolean('smsnotify');
248 $user = common_current_user();
250 assert(!is_null($user)); // should already be checked
252 $user->query('BEGIN');
254 $original = clone($user);
256 $user->smsnotify = $smsnotify;
258 $result = $user->update($original);
260 if ($result === false) {
261 common_log_db_error($user, 'UPDATE', __FILE__);
262 $this->serverError(_('Couldn\'t update user.'));
266 $user->query('COMMIT');
268 $this->showForm(_('Preferences saved.'), true);
272 * Add a new SMS number for confirmation
274 * When the user requests a new SMS number, sends a confirmation
280 function addAddress()
282 $user = common_current_user();
284 $sms = $this->trimmed('sms');
285 $carrier_id = $this->trimmed('carrier');
290 $this->showForm(_('No phone number.'));
295 $this->showForm(_('No carrier selected.'));
299 $sms = common_canonical_sms($sms);
301 if ($user->sms == $sms) {
302 $this->showForm(_('That is already your phone number.'));
304 } else if ($this->smsExists($sms)) {
305 $this->showForm(_('That phone number already belongs to another user.'));
309 $confirm = new Confirm_address();
311 $confirm->address = $sms;
312 $confirm->address_extra = $carrier_id;
313 $confirm->address_type = 'sms';
314 $confirm->user_id = $user->id;
315 $confirm->code = common_confirmation_code(40);
317 $result = $confirm->insert();
319 if ($result === false) {
320 common_log_db_error($confirm, 'INSERT', __FILE__);
321 $this->serverError(_('Couldn\'t insert confirmation code.'));
325 $carrier = Sms_carrier::staticGet($carrier_id);
327 mail_confirm_sms($confirm->code,
329 $carrier->toEmailAddress($sms));
331 $msg = _('A confirmation code was sent to the phone number you added. '.
332 'Check your phone for the code and instructions '.
333 'on how to use it.');
335 $this->showForm($msg, true);
339 * Cancel a pending confirmation
341 * Cancels the confirmation.
346 function cancelConfirmation()
348 $sms = $this->trimmed('sms');
349 $carrier = $this->trimmed('carrier');
351 $confirm = $this->getConfirmation();
354 $this->showForm(_('No pending confirmation to cancel.'));
357 if ($confirm->address != $sms) {
358 $this->showForm(_('That is the wrong confirmation number.'));
362 $result = $confirm->delete();
365 common_log_db_error($confirm, 'DELETE', __FILE__);
366 $this->serverError(_('Couldn\'t delete email confirmation.'));
370 $this->showForm(_('Confirmation cancelled.'), true);
374 * Remove a phone number from the user's account
379 function removeAddress()
381 $user = common_current_user();
383 $sms = $this->arg('sms');
384 $carrier = $this->arg('carrier');
386 // Maybe an old tab open...?
388 if ($user->sms != $sms) {
389 $this->showForm(_('That is not your phone number.'));
393 $user->query('BEGIN');
395 $original = clone($user);
398 $user->carrier = null;
399 $user->smsemail = null;
401 $result = $user->updateKeys($original);
403 common_log_db_error($user, 'UPDATE', __FILE__);
404 $this->serverError(_('Couldn\'t update user.'));
407 $user->query('COMMIT');
409 $this->showForm(_('The address was removed.'), true);
413 * Does this sms number exist in our database?
415 * Also checks if it belongs to someone else
417 * @param string $sms phone number to check
419 * @return boolean does the number exist
422 function smsExists($sms)
424 $user = common_current_user();
426 $other = User::staticGet('sms', $sms);
431 return $other->id != $user->id;
436 * Show a drop-down box with all the SMS carriers in the DB
441 function carrierSelect()
443 $carrier = new Sms_carrier();
445 $cnt = $carrier->find();
447 $this->elementStart('ul');
448 $this->elementStart('li');
449 $this->element('label', array('for' => 'carrier'), _('Mobile carrier'));
450 $this->elementStart('select', array('name' => 'carrier',
452 $this->element('option', array('value' => 0),
453 _('Select a carrier'));
454 while ($carrier->fetch()) {
455 $this->element('option', array('value' => $carrier->id),
458 $this->elementEnd('select');
459 $this->element('p', 'form_guide',
460 sprintf(_('Mobile carrier for your phone. '.
461 'If you know a carrier that accepts ' .
462 'SMS over email but isn\'t listed here, ' .
463 'send email to let us know at %s.'),
464 common_config('site', 'email')));
465 $this->elementEnd('li');
466 $this->elementEnd('ul');
470 * Confirm an SMS confirmation code
472 * Redirects to the confirmaddress page for this code
477 function confirmCode()
479 $code = $this->trimmed('code');
482 $this->showForm(_('No code entered'));
486 common_redirect(common_local_url('confirmaddress',
487 array('code' => $code)));