]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/smssettings.php
Merge branch 'master' of http://goukihq.org/misc/git/laconica-locales
[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                 common_hidden('token', common_session_token());
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_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'));
66                         } else {
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'));
72                         }
73                 }
74
75                 if ($user->sms) {
76                         common_element('h2', NULL, _('Incoming email'));
77                         
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'));
85                         }
86                         
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'));
92                 }
93                 
94                 common_element('h2', NULL, _('Preferences'));
95                 
96                 common_checkbox('smsnotify',
97                                                 _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
98                                                 $user->smsnotify);
99                         
100                 common_submit('save', _('Save'));
101                 
102                 common_element_end('form');
103                 common_show_footer();
104         }
105
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)) {
112                         return $confirm;
113                 } else {
114                         return NULL;
115                 }
116         }
117
118         function handle_post() {
119
120                 # CSRF protection
121
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.'));
125                         return;
126                 }
127
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();
142                 } else {
143                         $this->show_form(_('Unexpected form submission.'));
144                 }
145         }
146
147         function save_preferences() {
148
149                 $smsnotify = $this->boolean('smsnotify');
150                 
151                 $user = common_current_user();
152
153                 assert(!is_null($user)); # should already be checked
154
155                 $user->query('BEGIN');
156
157                 $original = clone($user);
158
159                 $user->smsnotify = $smsnotify;
160
161                 $result = $user->update($original);
162
163                 if ($result === FALSE) {
164                         common_log_db_error($user, 'UPDATE', __FILE__);
165                         common_server_error(_('Couldn\'t update user.'));
166                         return;
167                 }
168
169                 $user->query('COMMIT');
170
171                 $this->show_form(_('Preferences saved.'), true);
172         }
173
174         function add_address() {
175
176                 $user = common_current_user();
177
178                 $sms = $this->trimmed('sms');
179                 $carrier_id = $this->trimmed('carrier');
180                 
181                 # Some validation
182
183                 if (!$sms) {
184                         $this->show_form(_('No phone number.'));
185                         return;
186                 }
187
188                 if (!$carrier_id) {
189                         $this->show_form(_('No carrier selected.'));
190                         return;
191                 }
192                 
193                 $sms = common_canonical_sms($sms);
194                 
195                 if ($user->sms == $sms) {
196                     $this->show_form(_('That is already your phone number.'));
197                     return;
198                 } else if ($this->sms_exists($sms)) {
199                     $this->show_form(_('That phone number already belongs to another user.'));
200                     return;
201                 }
202
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);
209
210                 $result = $confirm->insert();
211
212                 if ($result === FALSE) {
213                         common_log_db_error($confirm, 'INSERT', __FILE__);
214                         common_server_error(_('Couldn\'t insert confirmation code.'));
215                         return;
216                 }
217
218                 $carrier = Sms_carrier::staticGet($carrier_id);
219                 
220                 mail_confirm_sms($confirm->code,
221                                                  $user->nickname,
222                                                  $carrier->toEmailAddress($sms));
223
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.');
225
226                 $this->show_form($msg, TRUE);
227         }
228
229         function cancel_confirmation() {
230                 
231                 $sms = $this->trimmed('sms');
232                 $carrier = $this->trimmed('carrier');
233                 
234                 $confirm = $this->get_confirmation();
235                 
236                 if (!$confirm) {
237                         $this->show_form(_('No pending confirmation to cancel.'));
238                         return;
239                 }
240                 if ($confirm->address != $sms) {
241                         $this->show_form(_('That is the wrong confirmation number.'));
242                         return;
243                 }
244
245         $result = $confirm->delete();
246
247         if (!$result) {
248                         common_log_db_error($confirm, 'DELETE', __FILE__);
249             $this->server_error(_('Couldn\'t delete email confirmation.'));
250             return;
251         }
252
253         $this->show_form(_('Confirmation cancelled.'), TRUE);
254         }
255
256         function remove_address() {
257
258                 $user = common_current_user();
259                 $sms = $this->arg('sms');
260                 $carrier = $this->arg('carrier');
261                 
262                 # Maybe an old tab open...?
263
264                 if ($user->sms != $sms) {
265                     $this->show_form(_('That is not your phone number.'));
266                     return;
267                 }
268
269                 $user->query('BEGIN');
270                 $original = clone($user);
271                 $user->sms = NULL;
272                 $user->carrier = NULL;          
273                 $user->smsemail = NULL;         
274                 $result = $user->updateKeys($original);
275                 if (!$result) {
276                         common_log_db_error($user, 'UPDATE', __FILE__);
277                         common_server_error(_('Couldn\'t update user.'));
278                         return;
279                 }
280                 $user->query('COMMIT');
281
282                 $this->show_form(_('The address was removed.'), TRUE);
283         }
284         
285         function sms_exists($sms) {
286                 $user = common_current_user();
287                 $other = User::staticGet('sms', $sms);
288                 if (!$other) {
289                         return false;
290                 } else {
291                         return $other->id != $user->id;
292                 }
293         }
294
295         function carrier_select() {
296                 $carrier = new Sms_carrier();
297                 $cnt = $carrier->find();
298
299                 common_element_start('p');
300                 common_element('label', array('for' => 'carrier'));
301                 common_element_start('select', array('name' => 'carrier',
302                                                                                          'id' => 'carrier'));
303                 common_element('option', array('value' => 0),
304                                            _('Select a carrier'));
305                 while ($carrier->fetch()) {
306                         common_element('option', array('value' => $carrier->id),
307                                                    $carrier->name);
308                 }
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')));
317         }
318
319         function confirm_code() {
320                 
321                 $code = $this->trimmed('code');
322                 
323                 if (!$code) {
324                         $this->show_form(_('No code entered'));
325                         return;
326                 }
327                 
328                 common_redirect(common_local_url('confirmaddress', 
329                                                                                  array('code' => $code)));
330         }
331 }