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 {
27 function get_instructions() {
28 return _('You can receive SMS messages through email from %%site.name%%.');
31 function show_form($msg=NULL, $success=false) {
32 $user = common_current_user();
33 $this->form_header(_('SMS Settings'), $msg, $success);
34 common_element_start('form', array('method' => 'post',
35 'id' => 'smssettings',
37 common_local_url('smssettings')));
38 common_hidden('token', common_session_token());
39 common_element('h2', NULL, _('Address'));
42 common_element_start('p');
43 $carrier = $user->getCarrier();
44 common_element('span', 'address confirmed', $user->sms . ' (' . $carrier->name . ')');
45 common_element('span', 'input_instructions',
46 _('Current confirmed SMS-enabled phone number.'));
47 common_hidden('sms', $user->sms);
48 common_hidden('carrier', $user->carrier);
49 common_element_end('p');
50 common_submit('remove', _('Remove'));
52 $confirm = $this->get_confirmation();
54 $carrier = Sms_carrier::staticGet($confirm->address_extra);
55 common_element_start('p');
56 common_element('span', 'address unconfirmed', $confirm->address . ' (' . $carrier->name . ')');
57 common_element('span', 'input_instructions',
58 _('Awaiting confirmation on this phone number.'));
59 common_hidden('sms', $confirm->address);
60 common_hidden('carrier', $confirm->address_extra);
61 common_element_end('p');
62 common_submit('cancel', _('Cancel'));
63 common_input('code', _('Confirmation code'), NULL,
64 _('Enter the code you received on your phone.'));
65 common_submit('confirm', _('Confirm'));
67 common_input('sms', _('SMS Phone number'),
68 ($this->arg('sms')) ? $this->arg('sms') : NULL,
69 _('Phone number, no punctuation or spaces, with area code'));
70 $this->carrier_select();
71 common_submit('add', _('Add'));
76 common_element('h2', NULL, _('Incoming email'));
78 if ($user->incomingemail) {
79 common_element_start('p');
80 common_element('span', 'address', $user->incomingemail);
81 common_element('span', 'input_instructions',
82 _('Send email to this address to post new notices.'));
83 common_element_end('p');
84 common_submit('removeincoming', _('Remove'));
87 common_element_start('p');
88 common_element('span', 'input_instructions',
89 _('Make a new email address for posting to; cancels the old one.'));
90 common_element_end('p');
91 common_submit('newincoming', _('New'));
94 common_element('h2', NULL, _('Preferences'));
96 common_checkbox('smsnotify',
97 _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
100 common_submit('save', _('Save'));
102 common_element_end('form');
103 common_show_footer();
106 function get_confirmation() {
107 $user = common_current_user();
108 $confirm = new Confirm_address();
109 $confirm->user_id = $user->id;
110 $confirm->address_type = 'sms';
111 if ($confirm->find(TRUE)) {
118 function handle_post() {
122 $token = $this->trimmed('token');
123 if (!$token || $token != common_session_token()) {
124 $this->show_form(_('There was a problem with your session token. Try again, please.'));
128 if ($this->arg('save')) {
129 $this->save_preferences();
130 } else if ($this->arg('add')) {
131 $this->add_address();
132 } else if ($this->arg('cancel')) {
133 $this->cancel_confirmation();
134 } else if ($this->arg('remove')) {
135 $this->remove_address();
136 } else if ($this->arg('removeincoming')) {
137 $this->remove_incoming();
138 } else if ($this->arg('newincoming')) {
139 $this->new_incoming();
140 } else if ($this->arg('confirm')) {
141 $this->confirm_code();
143 $this->show_form(_('Unexpected form submission.'));
147 function save_preferences() {
149 $smsnotify = $this->boolean('smsnotify');
151 $user = common_current_user();
153 assert(!is_null($user)); # should already be checked
155 $user->query('BEGIN');
157 $original = clone($user);
159 $user->smsnotify = $smsnotify;
161 $result = $user->update($original);
163 if ($result === FALSE) {
164 common_log_db_error($user, 'UPDATE', __FILE__);
165 common_server_error(_('Couldn\'t update user.'));
169 $user->query('COMMIT');
171 $this->show_form(_('Preferences saved.'), true);
174 function add_address() {
176 $user = common_current_user();
178 $sms = $this->trimmed('sms');
179 $carrier_id = $this->trimmed('carrier');
184 $this->show_form(_('No phone number.'));
189 $this->show_form(_('No carrier selected.'));
193 $sms = common_canonical_sms($sms);
195 if ($user->sms == $sms) {
196 $this->show_form(_('That is already your phone number.'));
198 } else if ($this->sms_exists($sms)) {
199 $this->show_form(_('That phone number already belongs to another user.'));
203 $confirm = new Confirm_address();
204 $confirm->address = $sms;
205 $confirm->address_extra = $carrier_id;
206 $confirm->address_type = 'sms';
207 $confirm->user_id = $user->id;
208 $confirm->code = common_confirmation_code(40);
210 $result = $confirm->insert();
212 if ($result === FALSE) {
213 common_log_db_error($confirm, 'INSERT', __FILE__);
214 common_server_error(_('Couldn\'t insert confirmation code.'));
218 $carrier = Sms_carrier::staticGet($carrier_id);
220 mail_confirm_sms($confirm->code,
222 $carrier->toEmailAddress($sms));
224 $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.');
226 $this->show_form($msg, TRUE);
229 function cancel_confirmation() {
231 $sms = $this->trimmed('sms');
232 $carrier = $this->trimmed('carrier');
234 $confirm = $this->get_confirmation();
237 $this->show_form(_('No pending confirmation to cancel.'));
240 if ($confirm->address != $sms) {
241 $this->show_form(_('That is the wrong confirmation number.'));
245 $result = $confirm->delete();
248 common_log_db_error($confirm, 'DELETE', __FILE__);
249 $this->server_error(_('Couldn\'t delete email confirmation.'));
253 $this->show_form(_('Confirmation cancelled.'), TRUE);
256 function remove_address() {
258 $user = common_current_user();
259 $sms = $this->arg('sms');
260 $carrier = $this->arg('carrier');
262 # Maybe an old tab open...?
264 if ($user->sms != $sms) {
265 $this->show_form(_('That is not your phone number.'));
269 $user->query('BEGIN');
270 $original = clone($user);
272 $user->carrier = NULL;
273 $user->smsemail = NULL;
274 $result = $user->updateKeys($original);
276 common_log_db_error($user, 'UPDATE', __FILE__);
277 common_server_error(_('Couldn\'t update user.'));
280 $user->query('COMMIT');
282 $this->show_form(_('The address was removed.'), TRUE);
285 function sms_exists($sms) {
286 $user = common_current_user();
287 $other = User::staticGet('sms', $sms);
291 return $other->id != $user->id;
295 function carrier_select() {
296 $carrier = new Sms_carrier();
297 $cnt = $carrier->find();
299 common_element_start('p');
300 common_element('label', array('for' => 'carrier'));
301 common_element_start('select', array('name' => 'carrier',
303 common_element('option', array('value' => 0),
304 _('Select a carrier'));
305 while ($carrier->fetch()) {
306 common_element('option', array('value' => $carrier->id),
309 common_element_end('select');
310 common_element_end('p');
311 common_element('span', 'input_instructions',
312 sprintf(_('Mobile carrier for your phone. '.
313 'If you know a carrier that accepts ' .
314 'SMS over email but isn\'t listed here, ' .
315 'send email to let us know at %s.'),
316 common_config('site', 'email')));
319 function confirm_code() {
321 $code = $this->trimmed('code');
324 $this->show_form(_('No code entered'));
328 common_redirect(common_local_url('confirmaddress',
329 array('code' => $code)));