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