3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, Inc.
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 if (!defined('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/actions/emailsettings.php');
25 class SmssettingsAction extends EmailsettingsAction
28 function get_instructions()
30 return _('You can receive SMS messages through email from %%site.name%%.');
33 function show_form($msg=null, $success=false)
35 $user = common_current_user();
36 $this->form_header(_('SMS Settings'), $msg, $success);
37 common_element_start('form', array('method' => 'post',
38 'id' => 'smssettings',
40 common_local_url('smssettings')));
41 common_hidden('token', common_session_token());
42 common_element('h2', null, _('Address'));
45 common_element_start('p');
46 $carrier = $user->getCarrier();
47 common_element('span', 'address confirmed', $user->sms . ' (' . $carrier->name . ')');
48 common_element('span', 'input_instructions',
49 _('Current confirmed SMS-enabled phone number.'));
50 common_hidden('sms', $user->sms);
51 common_hidden('carrier', $user->carrier);
52 common_element_end('p');
53 common_submit('remove', _('Remove'));
55 $confirm = $this->get_confirmation();
57 $carrier = Sms_carrier::staticGet($confirm->address_extra);
58 common_element_start('p');
59 common_element('span', 'address unconfirmed', $confirm->address . ' (' . $carrier->name . ')');
60 common_element('span', 'input_instructions',
61 _('Awaiting confirmation on this phone number.'));
62 common_hidden('sms', $confirm->address);
63 common_hidden('carrier', $confirm->address_extra);
64 common_element_end('p');
65 common_submit('cancel', _('Cancel'));
66 common_input('code', _('Confirmation code'), null,
67 _('Enter the code you received on your phone.'));
68 common_submit('confirm', _('Confirm'));
70 common_input('sms', _('SMS Phone number'),
71 ($this->arg('sms')) ? $this->arg('sms') : null,
72 _('Phone number, no punctuation or spaces, with area code'));
73 $this->carrier_select();
74 common_submit('add', _('Add'));
79 common_element('h2', null, _('Incoming email'));
81 if ($user->incomingemail) {
82 common_element_start('p');
83 common_element('span', 'address', $user->incomingemail);
84 common_element('span', 'input_instructions',
85 _('Send email to this address to post new notices.'));
86 common_element_end('p');
87 common_submit('removeincoming', _('Remove'));
90 common_element_start('p');
91 common_element('span', 'input_instructions',
92 _('Make a new email address for posting to; cancels the old one.'));
93 common_element_end('p');
94 common_submit('newincoming', _('New'));
97 common_element('h2', null, _('Preferences'));
99 common_checkbox('smsnotify',
100 _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
103 common_submit('save', _('Save'));
105 common_element_end('form');
106 common_show_footer();
109 function get_confirmation()
111 $user = common_current_user();
112 $confirm = new Confirm_address();
113 $confirm->user_id = $user->id;
114 $confirm->address_type = 'sms';
115 if ($confirm->find(true)) {
122 function handle_post()
127 $token = $this->trimmed('token');
128 if (!$token || $token != common_session_token()) {
129 $this->show_form(_('There was a problem with your session token. Try again, please.'));
133 if ($this->arg('save')) {
134 $this->save_preferences();
135 } else if ($this->arg('add')) {
136 $this->add_address();
137 } else if ($this->arg('cancel')) {
138 $this->cancel_confirmation();
139 } else if ($this->arg('remove')) {
140 $this->remove_address();
141 } else if ($this->arg('removeincoming')) {
142 $this->remove_incoming();
143 } else if ($this->arg('newincoming')) {
144 $this->new_incoming();
145 } else if ($this->arg('confirm')) {
146 $this->confirm_code();
148 $this->show_form(_('Unexpected form submission.'));
152 function save_preferences()
155 $smsnotify = $this->boolean('smsnotify');
157 $user = common_current_user();
159 assert(!is_null($user)); # should already be checked
161 $user->query('BEGIN');
163 $original = clone($user);
165 $user->smsnotify = $smsnotify;
167 $result = $user->update($original);
169 if ($result === false) {
170 common_log_db_error($user, 'UPDATE', __FILE__);
171 common_server_error(_('Couldn\'t update user.'));
175 $user->query('COMMIT');
177 $this->show_form(_('Preferences saved.'), true);
180 function add_address()
183 $user = common_current_user();
185 $sms = $this->trimmed('sms');
186 $carrier_id = $this->trimmed('carrier');
191 $this->show_form(_('No phone number.'));
196 $this->show_form(_('No carrier selected.'));
200 $sms = common_canonical_sms($sms);
202 if ($user->sms == $sms) {
203 $this->show_form(_('That is already your phone number.'));
205 } else if ($this->sms_exists($sms)) {
206 $this->show_form(_('That phone number already belongs to another user.'));
210 $confirm = new Confirm_address();
211 $confirm->address = $sms;
212 $confirm->address_extra = $carrier_id;
213 $confirm->address_type = 'sms';
214 $confirm->user_id = $user->id;
215 $confirm->code = common_confirmation_code(40);
217 $result = $confirm->insert();
219 if ($result === false) {
220 common_log_db_error($confirm, 'INSERT', __FILE__);
221 common_server_error(_('Couldn\'t insert confirmation code.'));
225 $carrier = Sms_carrier::staticGet($carrier_id);
227 mail_confirm_sms($confirm->code,
229 $carrier->toEmailAddress($sms));
231 $msg = _('A confirmation code was sent to the phone number you added. Check your inbox (and spam box!) for the code and instructions on how to use it.');
233 $this->show_form($msg, true);
236 function cancel_confirmation()
239 $sms = $this->trimmed('sms');
240 $carrier = $this->trimmed('carrier');
242 $confirm = $this->get_confirmation();
245 $this->show_form(_('No pending confirmation to cancel.'));
248 if ($confirm->address != $sms) {
249 $this->show_form(_('That is the wrong confirmation number.'));
253 $result = $confirm->delete();
256 common_log_db_error($confirm, 'DELETE', __FILE__);
257 $this->server_error(_('Couldn\'t delete email confirmation.'));
261 $this->show_form(_('Confirmation cancelled.'), true);
264 function remove_address()
267 $user = common_current_user();
268 $sms = $this->arg('sms');
269 $carrier = $this->arg('carrier');
271 # Maybe an old tab open...?
273 if ($user->sms != $sms) {
274 $this->show_form(_('That is not your phone number.'));
278 $user->query('BEGIN');
279 $original = clone($user);
281 $user->carrier = null;
282 $user->smsemail = null;
283 $result = $user->updateKeys($original);
285 common_log_db_error($user, 'UPDATE', __FILE__);
286 common_server_error(_('Couldn\'t update user.'));
289 $user->query('COMMIT');
291 $this->show_form(_('The address was removed.'), true);
294 function sms_exists($sms)
296 $user = common_current_user();
297 $other = User::staticGet('sms', $sms);
301 return $other->id != $user->id;
305 function carrier_select()
307 $carrier = new Sms_carrier();
308 $cnt = $carrier->find();
310 common_element_start('p');
311 common_element('label', array('for' => 'carrier'));
312 common_element_start('select', array('name' => 'carrier',
314 common_element('option', array('value' => 0),
315 _('Select a carrier'));
316 while ($carrier->fetch()) {
317 common_element('option', array('value' => $carrier->id),
320 common_element_end('select');
321 common_element_end('p');
322 common_element('span', 'input_instructions',
323 sprintf(_('Mobile carrier for your phone. '.
324 'If you know a carrier that accepts ' .
325 'SMS over email but isn\'t listed here, ' .
326 'send email to let us know at %s.'),
327 common_config('site', 'email')));
330 function confirm_code()
333 $code = $this->trimmed('code');
336 $this->show_form(_('No code entered'));
340 common_redirect(common_local_url('confirmaddress',
341 array('code' => $code)));