]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
e5698011ac0b7769ab3bea9fe122153e30871271
[quix0rs-gnu-social.git] / actions / profilesettings.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 ProfilesettingsAction extends SettingsAction {
25
26         function get_instructions() {
27                 return _('You can update your personal profile info here '.
28                                   'so people know more about you.');
29         }
30
31         function show_form($msg=NULL, $success=false) {
32                 $this->form_header(_('Profile settings'), $msg, $success);
33                 $this->show_settings_form();
34                 common_element('h2', NULL, _('Avatar'));
35                 $this->show_avatar_form();
36                 common_element('h2', NULL, _('Change password'));
37                 $this->show_password_form();
38                 common_show_footer();
39         }
40
41         function handle_post() {
42
43                 # CSRF protection
44
45                 $token = $this->trimmed('token');
46                 if (!$token || $token != common_session_token()) {
47                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
48                         return;
49                 }
50
51                 if ($this->arg('save')) {
52                         $this->save_profile();
53                 } else if ($this->arg('upload')) {
54                         $this->upload_avatar();
55                 } else if ($this->arg('changepass')) {
56                         $this->change_password();
57                 }
58         }
59
60         function show_settings_form() {
61
62                 $user = common_current_user();
63                 $profile = $user->getProfile();
64
65                 common_element_start('form', array('method' => 'POST',
66                                                                                    'id' => 'profilesettings',
67                                                                                    'action' =>
68                                                                                    common_local_url('profilesettings')));
69                 common_hidden('token', common_session_token());
70
71
72
73                 # too much common patterns here... abstractable?
74                 common_input('nickname', _('Nickname'),
75                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
76                                          _('1-64 lowercase letters or numbers, no punctuation or spaces'));
77                 common_input('fullname', _('Full name'),
78                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
79                 common_input('homepage', _('Homepage'),
80                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
81                                          _('URL of your homepage, blog, or profile on another site'));
82                 common_textarea('bio', _('Bio'),
83                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
84                                                 _('Describe yourself and your interests in 140 chars'));
85                 common_input('location', _('Location'),
86                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
87                                          _('Where you are, like "City, State (or Region), Country"'));
88
89                 $language = common_language();
90                 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
91                 $timezone = common_timezone();
92                 $timezones = array();
93                 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
94                         $timezones[$v] = $v;
95                 }
96                 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
97
98                 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
99                                                 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
100
101                 common_submit('save', _('Save'));
102
103                 common_element_end('form');
104
105
106         }
107
108         function show_avatar_form() {
109
110                 $user = common_current_user();
111                 $profile = $user->getProfile();
112
113                 if (!$profile) {
114                         common_log_db_error($user, 'SELECT', __FILE__);
115                         $this->server_error(_('User without matching profile'));
116                         return;
117                 }
118                 
119                 $original = $profile->getOriginalAvatar();
120
121
122                 common_element_start('form', array('enctype' => 'multipart/form-data',
123                                                                                    'method' => 'POST',
124                                                                                    'id' => 'avatar',
125                                                                                    'action' =>
126                                                                                    common_local_url('profilesettings')));
127                 common_hidden('token', common_session_token());
128
129                 if ($original) {
130                         common_element('img', array('src' => $original->url,
131                                                                                 'class' => 'avatar original',
132                                                                                 'width' => $original->width,
133                                                                                 'height' => $original->height,
134                                                                                 'alt' => $user->nickname));
135                 }
136
137                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
138
139                 if ($avatar) {
140                         common_element('img', array('src' => $avatar->url,
141                                                                                 'class' => 'avatar profile',
142                                                                                 'width' => AVATAR_PROFILE_SIZE,
143                                                                                 'height' => AVATAR_PROFILE_SIZE,
144                                                                                 'alt' => $user->nickname));
145                 }
146
147
148                 common_element('input', array('name' => 'MAX_FILE_SIZE',
149                                                                           'type' => 'hidden',
150                                                                           'id' => 'MAX_FILE_SIZE',
151                                                                           'value' => MAX_AVATAR_SIZE));
152
153                 common_element_start('p');
154
155
156                 common_element('input', array('name' => 'avatarfile',
157                                                                           'type' => 'file',
158                                                                           'id' => 'avatarfile'));
159                 common_element_end('p');
160
161                 common_submit('upload', _('Upload'));
162                 common_element_end('form');
163
164         }
165
166         function show_password_form() {
167
168                 $user = common_current_user();
169                 common_element_start('form', array('method' => 'POST',
170                                                                                    'id' => 'password',
171                                                                                    'action' =>
172                                                                                    common_local_url('profilesettings')));
173
174                 common_hidden('token', common_session_token());
175
176                 # Users who logged in with OpenID won't have a pwd
177                 if ($user->password) {
178                         common_password('oldpassword', _('Old password'));
179                 }
180                 common_password('newpassword', _('New password'),
181                                                 _('6 or more characters'));
182                 common_password('confirm', _('Confirm'),
183                                                 _('same as password above'));
184                 common_submit('changepass', _('Change'));
185                 common_element_end('form');
186         }
187
188         function save_profile() {
189                 $nickname = $this->trimmed('nickname');
190                 $fullname = $this->trimmed('fullname');
191                 $homepage = $this->trimmed('homepage');
192                 $bio = $this->trimmed('bio');
193                 $location = $this->trimmed('location');
194                 $autosubscribe = $this->boolean('autosubscribe');
195                 $language = $this->trimmed('language');
196                 $timezone = $this->trimmed('timezone');
197
198                 # Some validation
199
200                 if (!Validate::string($nickname, array('min_length' => 1,
201                                                                                            'max_length' => 64,
202                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
203                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
204                         return;
205                 } else if (!User::allowed_nickname($nickname)) {
206                         $this->show_form(_('Not a valid nickname.'));
207                         return;
208                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
209                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
210                         $this->show_form(_('Homepage is not a valid URL.'));
211                         return;
212                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
213                         $this->show_form(_('Full name is too long (max 255 chars).'));
214                         return;
215                 } else if (!is_null($bio) && strlen($bio) > 140) {
216                         $this->show_form(_('Bio is too long (max 140 chars).'));
217                         return;
218                 } else if (!is_null($location) && strlen($location) > 255) {
219                         $this->show_form(_('Location is too long (max 255 chars).'));
220                         return;
221                 }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
222                         $this->show_form(_('Timezone not selected.'));
223                         return;
224                 } else if ($this->nickname_exists($nickname)) {
225                         $this->show_form(_('Nickname already in use. Try another one.'));
226                         return;
227                 } else if (!is_null($language) && strlen($language) > 50) {
228                         $this->show_form(_('Language is too long (max 50 chars).'));
229                 }
230
231                 $user = common_current_user();
232
233                 $user->query('BEGIN');
234
235                 if ($user->nickname != $nickname ||
236                         $user->language != $language ||
237                         $user->timezone != $timezone) {
238
239                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
240                                                  __FILE__);
241                         common_debug('Updating user language from ' . $user->language . ' to ' . $language,
242                                                  __FILE__);
243                         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
244                                                  __FILE__);
245
246                         $original = clone($user);
247
248                         $user->nickname = $nickname;
249                         $user->language = $language;
250                         $user->timezone = $timezone;
251
252                         $result = $user->updateKeys($original);
253
254                         if ($result === FALSE) {
255                                 common_log_db_error($user, 'UPDATE', __FILE__);
256                                 common_server_error(_('Couldn\'t update user.'));
257                                 return;
258                         } else {
259                                 # Re-initialize language environment if it changed
260                                 common_init_language();
261                         }
262                 }
263
264                 # XXX: XOR
265
266                 if ($user->autosubscribe ^ $autosubscribe) {
267
268                         $original = clone($user);
269
270                         $user->autosubscribe = $autosubscribe;
271
272                         $result = $user->update($original);
273
274                         if ($result === FALSE) {
275                                 common_log_db_error($user, 'UPDATE', __FILE__);
276                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
277                                 return;
278                         }
279                 }
280
281                 $profile = $user->getProfile();
282
283                 $orig_profile = clone($profile);
284
285                 $profile->nickname = $user->nickname;
286                 $profile->fullname = $fullname;
287                 $profile->homepage = $homepage;
288                 $profile->bio = $bio;
289                 $profile->location = $location;
290                 $profile->profileurl = common_profile_url($nickname);
291
292                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
293                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
294
295                 $result = $profile->update($orig_profile);
296
297                 if (!$result) {
298                         common_log_db_error($profile, 'UPDATE', __FILE__);
299                         common_server_error(_('Couldn\'t save profile.'));
300                         return;
301                 }
302
303                 $user->query('COMMIT');
304
305                 common_broadcast_profile($profile);
306
307                 $this->show_form(_('Settings saved.'), TRUE);
308         }
309
310
311         function upload_avatar() {
312                 switch ($_FILES['avatarfile']['error']) {
313                  case UPLOAD_ERR_OK: # success, jump out
314                         break;
315                  case UPLOAD_ERR_INI_SIZE:
316                  case UPLOAD_ERR_FORM_SIZE:
317                         $this->show_form(_('That file is too big.'));
318                         return;
319                  case UPLOAD_ERR_PARTIAL:
320                         @unlink($_FILES['avatarfile']['tmp_name']);
321                         $this->show_form(_('Partial upload.'));
322                         return;
323                  default:
324                         $this->show_form(_('System error uploading file.'));
325                         return;
326                 }
327
328                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
329
330                 if (!$info) {
331                         @unlink($_FILES['avatarfile']['tmp_name']);
332                         $this->show_form(_('Not an image or corrupt file.'));
333                         return;
334                 }
335
336                 switch ($info[2]) {
337                  case IMAGETYPE_GIF:
338                  case IMAGETYPE_JPEG:
339                  case IMAGETYPE_PNG:
340                         break;
341                  default:
342                         $this->show_form(_('Unsupported image file format.'));
343                         return;
344                 }
345
346                 $user = common_current_user();
347                 $profile = $user->getProfile();
348
349                 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
350                         $this->show_form(_('Avatar updated.'), true);
351                 } else {
352                         $this->show_form(_('Failed updating avatar.'));
353                 }
354
355                 @unlink($_FILES['avatarfile']['tmp_name']);
356         }
357
358         function nickname_exists($nickname) {
359                 $user = common_current_user();
360                 $other = User::staticGet('nickname', $nickname);
361                 if (!$other) {
362                         return false;
363                 } else {
364                         return $other->id != $user->id;
365                 }
366         }
367
368         function change_password() {
369
370                 $user = common_current_user();
371                 assert(!is_null($user)); # should already be checked
372
373                 # FIXME: scrub input
374
375                 $newpassword = $this->arg('newpassword');
376                 $confirm = $this->arg('confirm');
377                 $token = $this->arg('token');
378
379                 if (0 != strcmp($newpassword, $confirm)) {
380                         $this->show_form(_('Passwords don\'t match.'));
381                         return;
382                 }
383
384                 if ($user->password) {
385                         $oldpassword = $this->arg('oldpassword');
386
387                         if (!common_check_user($user->nickname, $oldpassword)) {
388                                 $this->show_form(_('Incorrect old password'));
389                                 return;
390                         }
391                 }
392
393                 $original = clone($user);
394
395                 $user->password = common_munge_password($newpassword, $user->id);
396
397                 $val = $user->validate();
398                 if ($val !== TRUE) {
399                         $this->show_form(_('Error saving user; invalid.'));
400                         return;
401                 }
402
403                 if (!$user->update($original)) {
404                         common_server_error(_('Can\'t save new password.'));
405                         return;
406                 }
407
408                 $this->show_form(_('Password saved.'), true);
409         }
410
411 }