]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/smssettings.php
5a5a15ade9255a5692f56b2dc12687fbe5e8b52b
[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/emailsettingsaction.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                         } else {
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'));
68                         }
69                 }
70
71                 if ($user->sms) {
72                         common_element('h2', NULL, _('Incoming email'));
73                         
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'));
81                         }
82                         
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'));
88                 }
89                 
90                 common_element('h2', NULL, _('Preferences'));
91                 
92                 common_checkbox('smsnotify',
93                                                 _('Send me notices through SMS; I understand I may incur charges from my carrier.'),
94                                                 $user->smsnotify);
95                         
96                 common_submit('save', _('Save'));
97                 
98                 common_element_end('form');
99                 common_show_footer();
100         }
101
102         function get_confirmation() {
103                 $user = common_current_user();
104                 $confirm = new Confirm_address();
105                 $confirm->user_id = $user->id;
106                 $confirm->address_type = 'email';
107                 if ($confirm->find(TRUE)) {
108                         return $confirm;
109                 } else {
110                         return NULL;
111                 }
112         }
113
114         function handle_post() {
115
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();
128                 } else {
129                         $this->show_form(_('Unexpected form submission.'));
130                 }
131         }
132
133         function save_preferences() {
134
135                 $smsnotify = $this->boolean('emailpost');
136                 
137                 $user = common_current_user();
138
139                 assert(!is_null($user)); # should already be checked
140
141                 $user->query('BEGIN');
142
143                 $original = clone($user);
144
145                 $user->smsnotify = $smsnotify;
146
147                 $result = $user->update($original);
148
149                 if ($result === FALSE) {
150                         common_log_db_error($user, 'UPDATE', __FILE__);
151                         common_server_error(_('Couldn\'t update user.'));
152                         return;
153                 }
154
155                 $user->query('COMMIT');
156
157                 $this->show_form(_('Preferences saved.'), true);
158         }
159
160         function add_address() {
161
162                 $user = common_current_user();
163
164                 $sms = $this->trimmed('sms');
165                 $carrier_id = $this->trimmed('carrier');
166                 
167                 # Some validation
168
169                 if (!$sms) {
170                         $this->show_form(_('No phone number.'));
171                         return;
172                 }
173
174                 if (!$carrier_id) {
175                         $this->show_form(_('No carrier selected.'));
176                         return;
177                 }
178                 
179                 $sms = common_canonical_sms($sms);
180                 
181                 if ($user->sms == $sms) {
182                     $this->show_form(_('That is already your phone number.'));
183                     return;
184                 } else if ($this->sms_exists($sms)) {
185                     $this->show_form(_('That phone number already belongs to another user.'));
186                     return;
187                 }
188
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);
195
196                 $result = $confirm->insert();
197
198                 if ($result === FALSE) {
199                         common_log_db_error($confirm, 'INSERT', __FILE__);
200                         common_server_error(_('Couldn\'t insert confirmation code.'));
201                         return;
202                 }
203
204                 $carrier = Sms_carrier::staticGet($carrier_id);
205                 
206                 mail_confirm_address($confirm->code,
207                                                          $user->nickname,
208                                                          $carrier->toEmailAddress($sms));
209
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.');
211
212                 $this->show_form($msg, TRUE);
213         }
214
215         function cancel_confirmation() {
216                 
217                 $sms = $this->trimmed('sms');
218                 $carrier = $this->trimmed('carrier');
219                 
220                 $confirm = $this->get_confirmation();
221                 
222                 if (!$confirm) {
223                         $this->show_form(_('No pending confirmation to cancel.'));
224                         return;
225                 }
226                 if ($confirm->address != $sms || $confirm->address_extra != $carrier) {
227                         $this->show_form(_('That is the wrong IM address.'));
228                         return;
229                 }
230
231         $result = $confirm->delete();
232
233         if (!$result) {
234                         common_log_db_error($confirm, 'DELETE', __FILE__);
235             $this->server_error(_('Couldn\'t delete email confirmation.'));
236             return;
237         }
238
239         $this->show_form(_('Confirmation cancelled.'), TRUE);
240         }
241
242         function remove_address() {
243
244                 $user = common_current_user();
245                 $sms = $this->arg('sms');
246                 $carrier = $this->arg('carrier');
247                 
248                 # Maybe an old tab open...?
249
250                 if ($user->sms != $sms) {
251                     $this->show_form(_('That is not your phone number.'));
252                     return;
253                 }
254
255                 $user->query('BEGIN');
256                 $original = clone($user);
257                 $user->sms = NULL;
258                 $user->carrier = NULL;          
259                 $result = $user->updateKeys($original);
260                 if (!$result) {
261                         common_log_db_error($user, 'UPDATE', __FILE__);
262                         common_server_error(_('Couldn\'t update user.'));
263                         return;
264                 }
265                 $user->query('COMMIT');
266
267                 $this->show_form(_('The address was removed.'), TRUE);
268         }
269         
270         function sms_exists($sms) {
271                 $user = common_current_user();
272                 $other = User::staticGet('sms', $sms);
273                 if (!$other) {
274                         return false;
275                 } else {
276                         return $other->id != $user->id;
277                 }
278         }
279
280         function carrier_select() {
281                 $carrier = new Sms_carrier();
282                 $cnt = $carrier->find();
283
284                 common_element_start('p');
285                 common_element('label', array('for' => 'carrier'));
286                 common_element_start('select', array('name' => 'carrier',
287                                                                                          'id' => 'carrier'));
288                 common_element('option', array('value' => 0),
289                                            _('Select a carrier'));
290                 while ($carrier->fetch()) {
291                         common_element('option', array('value' => $carrier->id),
292                                                    $carrier->name);
293                 }
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')));
302         }
303 }