]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/emailsettings.php
correct name for common_canonical_email
[quix0rs-gnu-social.git] / actions / emailsettings.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
24 class EmailsettingsAction extends SettingsAction {
25
26         function get_instructions() {
27                 return _('Manage how you get email from %%site.name%%.');
28         }
29
30         function show_form($msg=NULL, $success=false) {
31                 $user = common_current_user();
32                 $this->form_header(_('Email Settings'), $msg, $success);
33                 common_element_start('form', array('method' => 'post',
34                                                                                    'id' => 'emailsettings',
35                                                                                    'action' =>
36                                                                                    common_local_url('emailsettings')));
37
38                 common_element('h2', NULL, _('Address'));
39
40                 if ($user->email) {
41                         common_element_start('p');
42                         common_element('span', 'address confirmed', $user->email);
43                         common_element('span', 'input_instructions',
44                                        _('Current confirmed email address.'));
45                         common_hidden('email', $user->email);
46                         common_element_end('p');
47                         common_submit('remove', _('Remove'));
48                 } else {
49                         $confirm = $this->get_confirmation();
50                         if ($confirm) {
51                                 common_element_start('p');
52                                 common_element('span', 'address unconfirmed', $confirm->address);
53                                 common_element('span', 'input_instructions',
54                                                            _('Awaiting confirmation on this address. Check your inbox (and spam box!) for a message with further instructions.'));
55                                 common_hidden('email', $confirm->address);
56                                 common_element_end('p');
57                                 common_submit('cancel', _('Cancel'));
58                         } else {
59                                 common_input('email', _('Email Address'),
60                                                          ($this->arg('email')) ? $this->arg('email') : NULL,
61                                                          _('Email address, like "UserName@example.org"'));
62                                 common_submit('add', _('Add'));
63                         }
64                 }
65
66                 common_element('h2', NULL, _('Preferences'));
67
68                 common_checkbox('emailnotifysub',
69                                 _('Send me notices of new subscriptions through email.'),
70                                 $user->emailnotifysub);
71                 
72                 common_submit('save', _('Save'));
73
74                 common_element_end('form');
75                 common_show_footer();
76         }
77
78         function get_confirmation() {
79                 $user = common_current_user();
80                 $confirm = new Confirm_address();
81                 $confirm->user_id = $user->id;
82                 $confirm->address_type = 'email';
83                 if ($confirm->find(TRUE)) {
84                         return $confirm;
85                 } else {
86                         return NULL;
87                 }
88         }
89
90         function handle_post() {
91
92                 if ($this->arg('save')) {
93                         $this->save_preferences();
94                 } else if ($this->arg('add')) {
95                         $this->add_address();
96                 } else if ($this->arg('cancel')) {
97                         $this->cancel_confirmation();
98                 } else if ($this->arg('remove')) {
99                         $this->remove_address();
100                 } else {
101                         $this->show_form(_('Unexpected form submission.'));
102                 }
103         }
104
105         function save_preferences() {
106
107                 $emailnotifysub = $this->boolean('emailnotifysub');
108
109                 $user = common_current_user();
110
111                 assert(!is_null($user)); # should already be checked
112
113                 $user->query('BEGIN');
114
115                 $original = clone($user);
116
117                 $user->emailnotifysub = $emailnotifysub;
118
119                 $result = $user->update($original);
120
121                 if ($result === FALSE) {
122                         common_log_db_error($user, 'UPDATE', __FILE__);
123                         common_server_error(_('Couldn\'t update user.'));
124                         return;
125                 }
126
127                 $user->query('COMMIT');
128
129                 $this->show_form(_('Preferences saved.'), true);
130         }
131
132         function add_address() {
133
134                 $user = common_current_user();
135
136                 $email = $this->trimmed('email');
137
138                 # Some validation
139
140                 if (!$email) {
141                         $this->show_form(_('No email address.'));
142                         return;
143                 }
144
145                 $email = common_canonical_email($email);
146
147                 if (!$email) {
148                     $this->show_form(_('Cannot normalize that email address'));
149                     return;
150                 }
151                 if (!Validate::email($email, true)) {
152                     $this->show_form(_('Not a valid email address'));
153                     return;
154                 } else if ($user->email == $email) {
155                     $this->show_form(_('That is already your email address.'));
156                     return;
157                 } else if ($this->email_exists($email)) {
158                     $this->show_form(_('That email address already belongs to another user.'));
159                     return;
160                 }
161
162                 $confirm = new Confirm_address();
163                 $confirm->address = $email;
164                 $confirm->address_type = 'email';
165                 $confirm->user_id = $user->id;
166                 $confirm->code = common_confirmation_code(64);
167
168                 $result = $confirm->insert();
169
170                 if ($result === FALSE) {
171                         common_log_db_error($confirm, 'INSERT', __FILE__);
172                         common_server_error(_('Couldn\'t insert confirmation code.'));
173                         return;
174                 }
175
176                 $msg = _('A confirmation code was sent to the email address you added. Check your inbox (and spam box!) for the code and instructions on how to use it.');
177
178                 $this->show_form($msg, TRUE);
179         }
180
181         function cancel_confirmation() {
182                 $email = $this->arg('email');
183                 $confirm = $this->get_confirmation();
184                 if (!$confirm) {
185                         $this->show_form(_('No pending confirmation to cancel.'));
186                         return;
187                 }
188                 if ($confirm->address != $email) {
189                         $this->show_form(_('That is the wrong IM address.'));
190                         return;
191                 }
192
193         $result = $confirm->delete();
194
195         if (!$result) {
196                         common_log_db_error($confirm, 'DELETE', __FILE__);
197             $this->server_error(_('Couldn\'t delete email confirmation.'));
198             return;
199         }
200
201         $this->show_form(_('Confirmation cancelled.'), TRUE);
202         }
203
204         function remove_address() {
205
206                 $user = common_current_user();
207                 $email = $this->arg('email');
208
209                 # Maybe an old tab open...?
210
211                 if ($user->email != $email) {
212                     $this->show_form(_('That is not your email address.'));
213                     return;
214                 }
215
216                 $user->query('BEGIN');
217                 $original = clone($user);
218                 $user->email = NULL;
219                 $result = $user->updateKeys($original);
220                 if (!$result) {
221                         common_log_db_error($user, 'UPDATE', __FILE__);
222                         common_server_error(_('Couldn\'t update user.'));
223                         return;
224                 }
225                 $user->query('COMMIT');
226
227                 # XXX: unsubscribe to the old address
228
229                 $this->show_form(_('The address was removed.'), TRUE);
230         }
231
232         function email_exists($email) {
233                 $user = common_current_user();
234                 $other = User::staticGet('email', $email);
235                 if (!$other) {
236                         return false;
237                 } else {
238                         return $other->id != $user->id;
239                 }
240         }
241 }