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('span', 'address unconfirmed', $confirm->address . ' (' . $carrier->name . ')');
56 common_element('span', 'input_instructions',
57 _('Awaiting confirmation on this phone number.'));
58 common_hidden('sms', $user->sms);
59 common_hidden('carrier', $user->carrier);
60 common_element_end('p');
61 common_submit('cancel', _('Cancel'));
62 common_input('code', _('Confirmation code'), NULL,
63 _('Enter the code you received on your phone.'));
64 common_submit('confirm', _('Confirm'));
66 common_input('sms', _('SMS Phone number'),
67 ($this->arg('sms')) ? $this->arg('sms') : NULL,
68 _('Phone number, no punctuation or spaces, with area code'));
69 $this->carrier_select();
70 common_submit('add', _('Add'));
75 common_element('h2', NULL, _('Incoming email'));
77 if ($user->incomingemail) {
78 common_element_start('p');
79 common_element('span', 'address', $user->incomingemail);
80 common_element('span', 'input_instructions',
81 _('Send email to this address to post new notices.'));
82 common_element_end('p');
83 common_submit('removeincoming', _('Remove'));
86 common_element_start('p');
87 common_element('span', 'input_instructions',
88 _('Make a new email address for posting to; cancels the old one.'));
89 common_element_end('p');
90 common_submit('newincoming', _('New'));
93 common_element('h2', NULL, _('Preferences'));
95 common_checkbox('smsnotify',
96 _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
99 common_submit('save', _('Save'));
101 common_element_end('form');
102 common_show_footer();
105 function get_confirmation() {
106 $user = common_current_user();
107 $confirm = new Confirm_address();
108 $confirm->user_id = $user->id;
109 $confirm->address_type = 'sms';
110 if ($confirm->find(TRUE)) {
117 function handle_post() {
119 if ($this->arg('save')) {
120 $this->save_preferences();
121 } else if ($this->arg('add')) {
122 $this->add_address();
123 } else if ($this->arg('cancel')) {
124 $this->cancel_confirmation();
125 } else if ($this->arg('remove')) {
126 $this->remove_address();
127 } else if ($this->arg('removeincoming')) {
128 $this->remove_incoming();
129 } else if ($this->arg('newincoming')) {
130 $this->new_incoming();
131 } else if ($this->arg('confirm')) {
132 $this->confirm_code();
134 $this->show_form(_('Unexpected form submission.'));
138 function save_preferences() {
140 $smsnotify = $this->boolean('emailpost');
142 $user = common_current_user();
144 assert(!is_null($user)); # should already be checked
146 $user->query('BEGIN');
148 $original = clone($user);
150 $user->smsnotify = $smsnotify;
152 $result = $user->update($original);
154 if ($result === FALSE) {
155 common_log_db_error($user, 'UPDATE', __FILE__);
156 common_server_error(_('Couldn\'t update user.'));
160 $user->query('COMMIT');
162 $this->show_form(_('Preferences saved.'), true);
165 function add_address() {
167 $user = common_current_user();
169 $sms = $this->trimmed('sms');
170 $carrier_id = $this->trimmed('carrier');
175 $this->show_form(_('No phone number.'));
180 $this->show_form(_('No carrier selected.'));
184 $sms = common_canonical_sms($sms);
186 if ($user->sms == $sms) {
187 $this->show_form(_('That is already your phone number.'));
189 } else if ($this->sms_exists($sms)) {
190 $this->show_form(_('That phone number already belongs to another user.'));
194 $confirm = new Confirm_address();
195 $confirm->address = $sms;
196 $confirm->address_extra = $carrier_id;
197 $confirm->address_type = 'sms';
198 $confirm->user_id = $user->id;
199 $confirm->code = common_confirmation_code(40);
201 $result = $confirm->insert();
203 if ($result === FALSE) {
204 common_log_db_error($confirm, 'INSERT', __FILE__);
205 common_server_error(_('Couldn\'t insert confirmation code.'));
209 $carrier = Sms_carrier::staticGet($carrier_id);
211 mail_confirm_sms($confirm->code,
213 $carrier->toEmailAddress($sms));
215 $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.');
217 $this->show_form($msg, TRUE);
220 function cancel_confirmation() {
222 $sms = $this->trimmed('sms');
223 $carrier = $this->trimmed('carrier');
225 $confirm = $this->get_confirmation();
228 $this->show_form(_('No pending confirmation to cancel.'));
231 if ($confirm->address != $sms || $confirm->address_extra != $carrier) {
232 $this->show_form(_('That is the wrong IM address.'));
236 $result = $confirm->delete();
239 common_log_db_error($confirm, 'DELETE', __FILE__);
240 $this->server_error(_('Couldn\'t delete email confirmation.'));
244 $this->show_form(_('Confirmation cancelled.'), TRUE);
247 function remove_address() {
249 $user = common_current_user();
250 $sms = $this->arg('sms');
251 $carrier = $this->arg('carrier');
253 # Maybe an old tab open...?
255 if ($user->sms != $sms) {
256 $this->show_form(_('That is not your phone number.'));
260 $user->query('BEGIN');
261 $original = clone($user);
263 $user->carrier = NULL;
264 $result = $user->updateKeys($original);
266 common_log_db_error($user, 'UPDATE', __FILE__);
267 common_server_error(_('Couldn\'t update user.'));
270 $user->query('COMMIT');
272 $this->show_form(_('The address was removed.'), TRUE);
275 function sms_exists($sms) {
276 $user = common_current_user();
277 $other = User::staticGet('sms', $sms);
281 return $other->id != $user->id;
285 function carrier_select() {
286 $carrier = new Sms_carrier();
287 $cnt = $carrier->find();
289 common_element_start('p');
290 common_element('label', array('for' => 'carrier'));
291 common_element_start('select', array('name' => 'carrier',
293 common_element('option', array('value' => 0),
294 _('Select a carrier'));
295 while ($carrier->fetch()) {
296 common_element('option', array('value' => $carrier->id),
299 common_element_end('select');
300 common_element_end('p');
301 common_element('span', 'input_instructions',
302 sprintf(_('Mobile carrier for your phone. '.
303 'If you know a carrier that accepts ' .
304 'SMS over email but isn\'t listed here, ' .
305 'send email to let us know at %s.'),
306 common_config('site', 'email')));
309 function confirm_code() {
311 $code = $this->trimmed('code');
314 $this->show_form(_('No code entered'));
318 common_redirect(common_local_url('confirmaddress',
319 array('code' => $this->code)));