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')));
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', $user->sms);
60 common_hidden('carrier', $user->carrier);
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() {
120 if ($this->arg('save')) {
121 $this->save_preferences();
122 } else if ($this->arg('add')) {
123 $this->add_address();
124 } else if ($this->arg('cancel')) {
125 $this->cancel_confirmation();
126 } else if ($this->arg('remove')) {
127 $this->remove_address();
128 } else if ($this->arg('removeincoming')) {
129 $this->remove_incoming();
130 } else if ($this->arg('newincoming')) {
131 $this->new_incoming();
132 } else if ($this->arg('confirm')) {
133 $this->confirm_code();
135 $this->show_form(_('Unexpected form submission.'));
139 function save_preferences() {
141 $smsnotify = $this->boolean('smsnotify');
143 $user = common_current_user();
145 assert(!is_null($user)); # should already be checked
147 $user->query('BEGIN');
149 $original = clone($user);
151 $user->smsnotify = $smsnotify;
153 $result = $user->update($original);
155 if ($result === FALSE) {
156 common_log_db_error($user, 'UPDATE', __FILE__);
157 common_server_error(_('Couldn\'t update user.'));
161 $user->query('COMMIT');
163 $this->show_form(_('Preferences saved.'), true);
166 function add_address() {
168 $user = common_current_user();
170 $sms = $this->trimmed('sms');
171 $carrier_id = $this->trimmed('carrier');
176 $this->show_form(_('No phone number.'));
181 $this->show_form(_('No carrier selected.'));
185 $sms = common_canonical_sms($sms);
187 if ($user->sms == $sms) {
188 $this->show_form(_('That is already your phone number.'));
190 } else if ($this->sms_exists($sms)) {
191 $this->show_form(_('That phone number already belongs to another user.'));
195 $confirm = new Confirm_address();
196 $confirm->address = $sms;
197 $confirm->address_extra = $carrier_id;
198 $confirm->address_type = 'sms';
199 $confirm->user_id = $user->id;
200 $confirm->code = common_confirmation_code(40);
202 $result = $confirm->insert();
204 if ($result === FALSE) {
205 common_log_db_error($confirm, 'INSERT', __FILE__);
206 common_server_error(_('Couldn\'t insert confirmation code.'));
210 $carrier = Sms_carrier::staticGet($carrier_id);
212 mail_confirm_sms($confirm->code,
214 $carrier->toEmailAddress($sms));
216 $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.');
218 $this->show_form($msg, TRUE);
221 function cancel_confirmation() {
223 $sms = $this->trimmed('sms');
224 $carrier = $this->trimmed('carrier');
226 $confirm = $this->get_confirmation();
229 $this->show_form(_('No pending confirmation to cancel.'));
232 if ($confirm->address != $sms || $confirm->address_extra != $carrier) {
233 $this->show_form(_('That is the wrong IM address.'));
237 $result = $confirm->delete();
240 common_log_db_error($confirm, 'DELETE', __FILE__);
241 $this->server_error(_('Couldn\'t delete email confirmation.'));
245 $this->show_form(_('Confirmation cancelled.'), TRUE);
248 function remove_address() {
250 $user = common_current_user();
251 $sms = $this->arg('sms');
252 $carrier = $this->arg('carrier');
254 # Maybe an old tab open...?
256 if ($user->sms != $sms) {
257 $this->show_form(_('That is not your phone number.'));
261 $user->query('BEGIN');
262 $original = clone($user);
264 $user->carrier = NULL;
265 $user->smsemail = NULL;
266 $result = $user->updateKeys($original);
268 common_log_db_error($user, 'UPDATE', __FILE__);
269 common_server_error(_('Couldn\'t update user.'));
272 $user->query('COMMIT');
274 $this->show_form(_('The address was removed.'), TRUE);
277 function sms_exists($sms) {
278 $user = common_current_user();
279 $other = User::staticGet('sms', $sms);
283 return $other->id != $user->id;
287 function carrier_select() {
288 $carrier = new Sms_carrier();
289 $cnt = $carrier->find();
291 common_element_start('p');
292 common_element('label', array('for' => 'carrier'));
293 common_element_start('select', array('name' => 'carrier',
295 common_element('option', array('value' => 0),
296 _('Select a carrier'));
297 while ($carrier->fetch()) {
298 common_element('option', array('value' => $carrier->id),
301 common_element_end('select');
302 common_element_end('p');
303 common_element('span', 'input_instructions',
304 sprintf(_('Mobile carrier for your phone. '.
305 'If you know a carrier that accepts ' .
306 'SMS over email but isn\'t listed here, ' .
307 'send email to let us know at %s.'),
308 common_config('site', 'email')));
311 function confirm_code() {
313 $code = $this->trimmed('code');
316 $this->show_form(_('No code entered'));
320 common_redirect(common_local_url('confirmaddress',
321 array('code' => $code)));