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'));
63 common_input('sms', _('SMS Phone number'),
64 ($this->arg('sms')) ? $this->arg('sms') : NULL,
65 _('Phone number, no punctuation or spaces, with area code'));
66 $this->carrier_select();
67 common_submit('add', _('Add'));
72 common_element('h2', NULL, _('Incoming email'));
74 if ($user->incomingemail) {
75 common_element_start('p');
76 common_element('span', 'address', $user->incomingemail);
77 common_element('span', 'input_instructions',
78 _('Send email to this address to post new notices.'));
79 common_element_end('p');
80 common_submit('removeincoming', _('Remove'));
83 common_element_start('p');
84 common_element('span', 'input_instructions',
85 _('Make a new email address for posting to; cancels the old one.'));
86 common_element_end('p');
87 common_submit('newincoming', _('New'));
90 common_element('h2', NULL, _('Preferences'));
92 common_checkbox('smsnotify',
93 _('Send me notices through SMS; I understand I may incur charges from my carrier.'),
96 common_submit('save', _('Save'));
98 common_element_end('form');
102 function get_confirmation() {
103 $user = common_current_user();
104 $confirm = new Confirm_address();
105 $confirm->user_id = $user->id;
106 $confirm->address_type = 'sms';
107 if ($confirm->find(TRUE)) {
114 function handle_post() {
116 if ($this->arg('save')) {
117 $this->save_preferences();
118 } else if ($this->arg('add')) {
119 $this->add_address();
120 } else if ($this->arg('cancel')) {
121 $this->cancel_confirmation();
122 } else if ($this->arg('remove')) {
123 $this->remove_address();
124 } else if ($this->arg('removeincoming')) {
125 $this->remove_incoming();
126 } else if ($this->arg('newincoming')) {
127 $this->new_incoming();
129 $this->show_form(_('Unexpected form submission.'));
133 function save_preferences() {
135 $smsnotify = $this->boolean('emailpost');
137 $user = common_current_user();
139 assert(!is_null($user)); # should already be checked
141 $user->query('BEGIN');
143 $original = clone($user);
145 $user->smsnotify = $smsnotify;
147 $result = $user->update($original);
149 if ($result === FALSE) {
150 common_log_db_error($user, 'UPDATE', __FILE__);
151 common_server_error(_('Couldn\'t update user.'));
155 $user->query('COMMIT');
157 $this->show_form(_('Preferences saved.'), true);
160 function add_address() {
162 $user = common_current_user();
164 $sms = $this->trimmed('sms');
165 $carrier_id = $this->trimmed('carrier');
170 $this->show_form(_('No phone number.'));
175 $this->show_form(_('No carrier selected.'));
179 $sms = common_canonical_sms($sms);
181 if ($user->sms == $sms) {
182 $this->show_form(_('That is already your phone number.'));
184 } else if ($this->sms_exists($sms)) {
185 $this->show_form(_('That phone number already belongs to another user.'));
189 $confirm = new Confirm_address();
190 $confirm->address = $sms;
191 $confirm->address_extra = $carrier_id;
192 $confirm->address_type = 'sms';
193 $confirm->user_id = $user->id;
194 $confirm->code = common_confirmation_code(64);
196 $result = $confirm->insert();
198 if ($result === FALSE) {
199 common_log_db_error($confirm, 'INSERT', __FILE__);
200 common_server_error(_('Couldn\'t insert confirmation code.'));
204 $carrier = Sms_carrier::staticGet($carrier_id);
206 mail_confirm_address($confirm->code,
208 $carrier->toEmailAddress($sms));
210 $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.');
212 $this->show_form($msg, TRUE);
215 function cancel_confirmation() {
217 $sms = $this->trimmed('sms');
218 $carrier = $this->trimmed('carrier');
220 $confirm = $this->get_confirmation();
223 $this->show_form(_('No pending confirmation to cancel.'));
226 if ($confirm->address != $sms || $confirm->address_extra != $carrier) {
227 $this->show_form(_('That is the wrong IM address.'));
231 $result = $confirm->delete();
234 common_log_db_error($confirm, 'DELETE', __FILE__);
235 $this->server_error(_('Couldn\'t delete email confirmation.'));
239 $this->show_form(_('Confirmation cancelled.'), TRUE);
242 function remove_address() {
244 $user = common_current_user();
245 $sms = $this->arg('sms');
246 $carrier = $this->arg('carrier');
248 # Maybe an old tab open...?
250 if ($user->sms != $sms) {
251 $this->show_form(_('That is not your phone number.'));
255 $user->query('BEGIN');
256 $original = clone($user);
258 $user->carrier = NULL;
259 $result = $user->updateKeys($original);
261 common_log_db_error($user, 'UPDATE', __FILE__);
262 common_server_error(_('Couldn\'t update user.'));
265 $user->query('COMMIT');
267 $this->show_form(_('The address was removed.'), TRUE);
270 function sms_exists($sms) {
271 $user = common_current_user();
272 $other = User::staticGet('sms', $sms);
276 return $other->id != $user->id;
280 function carrier_select() {
281 $carrier = new Sms_carrier();
282 $cnt = $carrier->find();
284 common_element_start('p');
285 common_element('label', array('for' => 'carrier'));
286 common_element_start('select', array('name' => 'carrier',
288 common_element('option', array('value' => 0),
289 _('Select a carrier'));
290 while ($carrier->fetch()) {
291 common_element('option', array('value' => $carrier->id),
294 common_element_end('select');
295 common_element_end('p');
296 common_element('span', 'input_instructions',
297 sprintf(_('Mobile carrier for your phone. '.
298 'If you know a carrier that accepts ' .
299 'SMS over email but isn\'t listed here, ' .
300 'send email to let us know at %s.'),
301 common_config('site', 'email')));