]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/smssettings.php
632a30ba8ede83957b763defefec6815174ae1d5
[quix0rs-gnu-social.git] / actions / smssettings.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/settingsaction.php');
23 require_once(INSTALLDIR.'/actions/emailsettings.php');
24
25 class SmssettingsAction extends EmailsettingsAction {
26
27         function get_instructions() {
28                 return _('You can receive SMS messages through email from %%site.name%%.');
29         }
30
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',
36                                                                                    'action' =>
37                                                                                    common_local_url('smssettings')));
38
39                 common_element('h2', NULL, _('Address'));
40
41                 if ($user->sms) {
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'));
51                 } else {
52                         $confirm = $this->get_confirmation();
53                         if ($confirm) {
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'));
65                         } else {
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'));
71                         }
72                 }
73
74                 if ($user->sms) {
75                         common_element('h2', NULL, _('Incoming email'));
76                         
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'));
84                         }
85                         
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'));
91                 }
92                 
93                 common_element('h2', NULL, _('Preferences'));
94                 
95                 common_checkbox('smsnotify',
96                                                 _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
97                                                 $user->smsnotify);
98                         
99                 common_submit('save', _('Save'));
100                 
101                 common_element_end('form');
102                 common_show_footer();
103         }
104
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)) {
111                         return $confirm;
112                 } else {
113                         return NULL;
114                 }
115         }
116
117         function handle_post() {
118
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();
133                 } else {
134                         $this->show_form(_('Unexpected form submission.'));
135                 }
136         }
137
138         function save_preferences() {
139
140                 $smsnotify = $this->boolean('emailpost');
141                 
142                 $user = common_current_user();
143
144                 assert(!is_null($user)); # should already be checked
145
146                 $user->query('BEGIN');
147
148                 $original = clone($user);
149
150                 $user->smsnotify = $smsnotify;
151
152                 $result = $user->update($original);
153
154                 if ($result === FALSE) {
155                         common_log_db_error($user, 'UPDATE', __FILE__);
156                         common_server_error(_('Couldn\'t update user.'));
157                         return;
158                 }
159
160                 $user->query('COMMIT');
161
162                 $this->show_form(_('Preferences saved.'), true);
163         }
164
165         function add_address() {
166
167                 $user = common_current_user();
168
169                 $sms = $this->trimmed('sms');
170                 $carrier_id = $this->trimmed('carrier');
171                 
172                 # Some validation
173
174                 if (!$sms) {
175                         $this->show_form(_('No phone number.'));
176                         return;
177                 }
178
179                 if (!$carrier_id) {
180                         $this->show_form(_('No carrier selected.'));
181                         return;
182                 }
183                 
184                 $sms = common_canonical_sms($sms);
185                 
186                 if ($user->sms == $sms) {
187                     $this->show_form(_('That is already your phone number.'));
188                     return;
189                 } else if ($this->sms_exists($sms)) {
190                     $this->show_form(_('That phone number already belongs to another user.'));
191                     return;
192                 }
193
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);
200
201                 $result = $confirm->insert();
202
203                 if ($result === FALSE) {
204                         common_log_db_error($confirm, 'INSERT', __FILE__);
205                         common_server_error(_('Couldn\'t insert confirmation code.'));
206                         return;
207                 }
208
209                 $carrier = Sms_carrier::staticGet($carrier_id);
210                 
211                 mail_confirm_sms($confirm->code,
212                                                  $user->nickname,
213                                                  $carrier->toEmailAddress($sms));
214
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.');
216
217                 $this->show_form($msg, TRUE);
218         }
219
220         function cancel_confirmation() {
221                 
222                 $sms = $this->trimmed('sms');
223                 $carrier = $this->trimmed('carrier');
224                 
225                 $confirm = $this->get_confirmation();
226                 
227                 if (!$confirm) {
228                         $this->show_form(_('No pending confirmation to cancel.'));
229                         return;
230                 }
231                 if ($confirm->address != $sms || $confirm->address_extra != $carrier) {
232                         $this->show_form(_('That is the wrong IM address.'));
233                         return;
234                 }
235
236         $result = $confirm->delete();
237
238         if (!$result) {
239                         common_log_db_error($confirm, 'DELETE', __FILE__);
240             $this->server_error(_('Couldn\'t delete email confirmation.'));
241             return;
242         }
243
244         $this->show_form(_('Confirmation cancelled.'), TRUE);
245         }
246
247         function remove_address() {
248
249                 $user = common_current_user();
250                 $sms = $this->arg('sms');
251                 $carrier = $this->arg('carrier');
252                 
253                 # Maybe an old tab open...?
254
255                 if ($user->sms != $sms) {
256                     $this->show_form(_('That is not your phone number.'));
257                     return;
258                 }
259
260                 $user->query('BEGIN');
261                 $original = clone($user);
262                 $user->sms = NULL;
263                 $user->carrier = NULL;          
264                 $result = $user->updateKeys($original);
265                 if (!$result) {
266                         common_log_db_error($user, 'UPDATE', __FILE__);
267                         common_server_error(_('Couldn\'t update user.'));
268                         return;
269                 }
270                 $user->query('COMMIT');
271
272                 $this->show_form(_('The address was removed.'), TRUE);
273         }
274         
275         function sms_exists($sms) {
276                 $user = common_current_user();
277                 $other = User::staticGet('sms', $sms);
278                 if (!$other) {
279                         return false;
280                 } else {
281                         return $other->id != $user->id;
282                 }
283         }
284
285         function carrier_select() {
286                 $carrier = new Sms_carrier();
287                 $cnt = $carrier->find();
288
289                 common_element_start('p');
290                 common_element('label', array('for' => 'carrier'));
291                 common_element_start('select', array('name' => 'carrier',
292                                                                                          'id' => 'carrier'));
293                 common_element('option', array('value' => 0),
294                                            _('Select a carrier'));
295                 while ($carrier->fetch()) {
296                         common_element('option', array('value' => $carrier->id),
297                                                    $carrier->name);
298                 }
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')));
307         }
308
309         function confirm_code() {
310                 
311                 $code = $this->trimmed('code');
312                 
313                 if (!$code) {
314                         $this->show_form(_('No code entered'));
315                         return;
316                 }
317                 
318                 common_redirect(common_local_url('confirmaddress', 
319                                                                                  array('code' => $this->code)));
320         }
321 }