]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - _darcs/pristine/actions/smssettings.php
move opening brace of class declaration to next line
[quix0rs-gnu-social.git] / _darcs / pristine / 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
28     function get_instructions()
29     {
30         return _('You can receive SMS messages through email from %%site.name%%.');
31     }
32
33     function show_form($msg=null, $success=false)
34     {
35         $user = common_current_user();
36         $this->form_header(_('SMS Settings'), $msg, $success);
37         common_element_start('form', array('method' => 'post',
38                                            'id' => 'smssettings',
39                                            'action' =>
40                                            common_local_url('smssettings')));
41         common_hidden('token', common_session_token());
42         common_element('h2', null, _('Address'));
43
44         if ($user->sms) {
45             common_element_start('p');
46             $carrier = $user->getCarrier();
47             common_element('span', 'address confirmed', $user->sms . ' (' . $carrier->name . ')');
48             common_element('span', 'input_instructions',
49                            _('Current confirmed SMS-enabled phone number.'));
50             common_hidden('sms', $user->sms);
51             common_hidden('carrier', $user->carrier);
52             common_element_end('p');
53             common_submit('remove', _('Remove'));
54         } else {
55             $confirm = $this->get_confirmation();
56             if ($confirm) {
57                 $carrier = Sms_carrier::staticGet($confirm->address_extra);
58                 common_element_start('p');
59                 common_element('span', 'address unconfirmed', $confirm->address . ' (' . $carrier->name . ')');
60                 common_element('span', 'input_instructions',
61                                _('Awaiting confirmation on this phone number.'));
62                 common_hidden('sms', $confirm->address);
63                 common_hidden('carrier', $confirm->address_extra);
64                 common_element_end('p');
65                 common_submit('cancel', _('Cancel'));
66                 common_input('code', _('Confirmation code'), null,
67                              _('Enter the code you received on your phone.'));
68                 common_submit('confirm', _('Confirm'));
69             } else {
70                 common_input('sms', _('SMS Phone number'),
71                              ($this->arg('sms')) ? $this->arg('sms') : null,
72                              _('Phone number, no punctuation or spaces, with area code'));
73                 $this->carrier_select();
74                 common_submit('add', _('Add'));
75             }
76         }
77
78         if ($user->sms) {
79             common_element('h2', null, _('Incoming email'));
80             
81             if ($user->incomingemail) {
82                 common_element_start('p');
83                 common_element('span', 'address', $user->incomingemail);
84                 common_element('span', 'input_instructions',
85                                _('Send email to this address to post new notices.'));
86                 common_element_end('p');
87                 common_submit('removeincoming', _('Remove'));
88             }
89             
90             common_element_start('p');
91             common_element('span', 'input_instructions',
92                            _('Make a new email address for posting to; cancels the old one.'));
93             common_element_end('p');
94             common_submit('newincoming', _('New'));
95         }
96         
97         common_element('h2', null, _('Preferences'));
98         
99         common_checkbox('smsnotify',
100                         _('Send me notices through SMS; I understand I may incur exorbitant charges from my carrier.'),
101                         $user->smsnotify);
102             
103         common_submit('save', _('Save'));
104         
105         common_element_end('form');
106         common_show_footer();
107     }
108
109     function get_confirmation()
110     {
111         $user = common_current_user();
112         $confirm = new Confirm_address();
113         $confirm->user_id = $user->id;
114         $confirm->address_type = 'sms';
115         if ($confirm->find(true)) {
116             return $confirm;
117         } else {
118             return null;
119         }
120     }
121
122     function handle_post()
123     {
124
125         # CSRF protection
126
127         $token = $this->trimmed('token');
128         if (!$token || $token != common_session_token()) {
129             $this->show_form(_('There was a problem with your session token. Try again, please.'));
130             return;
131         }
132
133         if ($this->arg('save')) {
134             $this->save_preferences();
135         } else if ($this->arg('add')) {
136             $this->add_address();
137         } else if ($this->arg('cancel')) {
138             $this->cancel_confirmation();
139         } else if ($this->arg('remove')) {
140             $this->remove_address();
141         } else if ($this->arg('removeincoming')) {
142             $this->remove_incoming();
143         } else if ($this->arg('newincoming')) {
144             $this->new_incoming();
145         } else if ($this->arg('confirm')) {
146             $this->confirm_code();
147         } else {
148             $this->show_form(_('Unexpected form submission.'));
149         }
150     }
151
152     function save_preferences()
153     {
154
155         $smsnotify = $this->boolean('smsnotify');
156         
157         $user = common_current_user();
158
159         assert(!is_null($user)); # should already be checked
160
161         $user->query('BEGIN');
162
163         $original = clone($user);
164
165         $user->smsnotify = $smsnotify;
166
167         $result = $user->update($original);
168
169         if ($result === false) {
170             common_log_db_error($user, 'UPDATE', __FILE__);
171             common_server_error(_('Couldn\'t update user.'));
172             return;
173         }
174
175         $user->query('COMMIT');
176
177         $this->show_form(_('Preferences saved.'), true);
178     }
179
180     function add_address()
181     {
182
183         $user = common_current_user();
184
185         $sms = $this->trimmed('sms');
186         $carrier_id = $this->trimmed('carrier');
187         
188         # Some validation
189
190         if (!$sms) {
191             $this->show_form(_('No phone number.'));
192             return;
193         }
194
195         if (!$carrier_id) {
196             $this->show_form(_('No carrier selected.'));
197             return;
198         }
199         
200         $sms = common_canonical_sms($sms);
201         
202         if ($user->sms == $sms) {
203             $this->show_form(_('That is already your phone number.'));
204             return;
205         } else if ($this->sms_exists($sms)) {
206             $this->show_form(_('That phone number already belongs to another user.'));
207             return;
208         }
209
210           $confirm = new Confirm_address();
211            $confirm->address = $sms;
212            $confirm->address_extra = $carrier_id;
213            $confirm->address_type = 'sms';
214            $confirm->user_id = $user->id;
215            $confirm->code = common_confirmation_code(40);
216
217         $result = $confirm->insert();
218
219         if ($result === false) {
220             common_log_db_error($confirm, 'INSERT', __FILE__);
221             common_server_error(_('Couldn\'t insert confirmation code.'));
222             return;
223         }
224
225         $carrier = Sms_carrier::staticGet($carrier_id);
226         
227         mail_confirm_sms($confirm->code,
228                          $user->nickname,
229                          $carrier->toEmailAddress($sms));
230
231         $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.');
232
233         $this->show_form($msg, true);
234     }
235
236     function cancel_confirmation()
237     {
238         
239         $sms = $this->trimmed('sms');
240         $carrier = $this->trimmed('carrier');
241         
242         $confirm = $this->get_confirmation();
243         
244         if (!$confirm) {
245             $this->show_form(_('No pending confirmation to cancel.'));
246             return;
247         }
248         if ($confirm->address != $sms) {
249             $this->show_form(_('That is the wrong confirmation number.'));
250             return;
251         }
252
253         $result = $confirm->delete();
254
255         if (!$result) {
256             common_log_db_error($confirm, 'DELETE', __FILE__);
257             $this->server_error(_('Couldn\'t delete email confirmation.'));
258             return;
259         }
260
261         $this->show_form(_('Confirmation cancelled.'), true);
262     }
263
264     function remove_address()
265     {
266
267         $user = common_current_user();
268         $sms = $this->arg('sms');
269         $carrier = $this->arg('carrier');
270         
271         # Maybe an old tab open...?
272
273         if ($user->sms != $sms) {
274             $this->show_form(_('That is not your phone number.'));
275             return;
276         }
277
278         $user->query('BEGIN');
279         $original = clone($user);
280         $user->sms = null;
281         $user->carrier = null;        
282         $user->smsemail = null;        
283         $result = $user->updateKeys($original);
284         if (!$result) {
285             common_log_db_error($user, 'UPDATE', __FILE__);
286             common_server_error(_('Couldn\'t update user.'));
287             return;
288         }
289         $user->query('COMMIT');
290
291         $this->show_form(_('The address was removed.'), true);
292     }
293     
294     function sms_exists($sms)
295     {
296         $user = common_current_user();
297         $other = User::staticGet('sms', $sms);
298         if (!$other) {
299             return false;
300         } else {
301             return $other->id != $user->id;
302         }
303     }
304
305     function carrier_select()
306     {
307         $carrier = new Sms_carrier();
308         $cnt = $carrier->find();
309
310         common_element_start('p');
311         common_element('label', array('for' => 'carrier'));
312         common_element_start('select', array('name' => 'carrier',
313                                              'id' => 'carrier'));
314         common_element('option', array('value' => 0),
315                        _('Select a carrier'));
316         while ($carrier->fetch()) {
317             common_element('option', array('value' => $carrier->id),
318                            $carrier->name);
319         }
320         common_element_end('select');
321         common_element_end('p');
322         common_element('span', 'input_instructions',
323                        sprintf(_('Mobile carrier for your phone. '.
324                                  'If you know a carrier that accepts ' . 
325                                  'SMS over email but isn\'t listed here, ' .
326                                  'send email to let us know at %s.'),
327                                common_config('site', 'email')));
328     }
329
330     function confirm_code()
331     {
332         
333         $code = $this->trimmed('code');
334         
335         if (!$code) {
336             $this->show_form(_('No code entered'));
337             return;
338         }
339         
340         common_redirect(common_local_url('confirmaddress', 
341                                          array('code' => $code)));
342     }
343 }