]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
f34101ba97e75c85b1bc76844cf1a9d5fec5ea0e
[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                 $this->show_avatar_form();
35                 common_show_footer();
36         }
37
38         function handle_post() {
39
40                 # CSRF protection
41
42                 $token = $this->trimmed('token');
43                 if (!$token || $token != common_session_token()) {
44                         $this->show_form(_('There was a problem with your session token. Try again, please.'));
45                         return;
46                 }
47
48                 if ($this->arg('save')) {
49                         $this->save_profile();
50                 } else if ($this->arg('upload')) {
51                         $this->upload_avatar();
52                 }
53         }
54
55         function show_settings_form() {
56
57                 $user = common_current_user();
58                 $profile = $user->getProfile();
59
60                 common_element_start('form', array('method' => 'POST',
61                                                                                    'id' => 'profilesettings',
62                                                                                    'action' =>
63                                                                                    common_local_url('profilesettings')));
64                 common_hidden('token', common_session_token());
65
66
67
68                 # too much common patterns here... abstractable?
69                 common_input('nickname', _('Nickname'),
70                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
71                                          _('1-64 lowercase letters or numbers, no punctuation or spaces'));
72                 common_input('fullname', _('Full name'),
73                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
74                 common_input('homepage', _('Homepage'),
75                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
76                                          _('URL of your homepage, blog, or profile on another site'));
77                 common_textarea('bio', _('Bio'),
78                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
79                                                 _('Describe yourself and your interests in 140 chars'));
80                 common_input('location', _('Location'),
81                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
82                                          _('Where you are, like "City, State (or Region), Country"'));
83
84                 $language = common_language();
85                 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
86                 $timezone = common_timezone();
87                 $timezones = array();
88                 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
89                         $timezones[$v] = $v;
90                 }
91                 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
92
93                 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
94                                                 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
95
96                 common_submit('save', _('Save'));
97
98                 common_element_end('form');
99
100
101         }
102
103         function show_avatar_form() {
104
105                 $user = common_current_user();
106                 $profile = $user->getProfile();
107
108                 $original = $profile->getOriginalAvatar();
109
110
111                 common_element_start('form', array('enctype' => 'multipart/form-data',
112                                                                                    'method' => 'POST',
113                                                                                    'id' => 'profilesettings',
114                                                                                    'action' =>
115                                                                                    common_local_url('profilesettings')));
116                 common_hidden('token', common_session_token());
117
118                 common_element_start('p');
119                 common_element('label', array('for' => 'avatar'), 'Avatar');
120
121                 if ($original) {
122                         common_element('img', array('src' => $original->url,
123                                                                                 'class' => 'avatar original',
124                                                                                 'width' => $original->width,
125                                                                                 'height' => $original->height,
126                                                                                 'alt' => $user->nickname));
127                 }
128
129                 $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
130
131                 if ($avatar) {
132                         common_element('img', array('src' => $avatar->url,
133                                                                                 'class' => 'avatar profile',
134                                                                                 'width' => AVATAR_PROFILE_SIZE,
135                                                                                 'height' => AVATAR_PROFILE_SIZE,
136                                                                                 'alt' => $user->nickname));
137                 }
138
139                 common_element_end('p');
140
141                 common_element('input', array('name' => 'MAX_FILE_SIZE',
142                                                                           'type' => 'hidden',
143                                                                           'id' => 'MAX_FILE_SIZE',
144                                                                           'value' => MAX_AVATAR_SIZE));
145                 common_element('input', array('name' => 'avatarfile',
146                                                                           'type' => 'file',
147                                                                           'id' => 'avatarfile'));
148                 common_submit('upload', _('Upload'));
149                 common_element_end('form');
150
151         }
152
153         function save_profile() {
154                 $nickname = $this->trimmed('nickname');
155                 $fullname = $this->trimmed('fullname');
156                 $homepage = $this->trimmed('homepage');
157                 $bio = $this->trimmed('bio');
158                 $location = $this->trimmed('location');
159                 $autosubscribe = $this->boolean('autosubscribe');
160                 $language = $this->trimmed('language');
161                 $timezone = $this->trimmed('timezone');
162
163                 # Some validation
164
165                 if (!Validate::string($nickname, array('min_length' => 1,
166                                                                                            'max_length' => 64,
167                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
168                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
169                         return;
170                 } else if (!User::allowed_nickname($nickname)) {
171                         $this->show_form(_('Not a valid nickname.'));
172                         return;
173                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
174                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
175                         $this->show_form(_('Homepage is not a valid URL.'));
176                         return;
177                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
178                         $this->show_form(_('Full name is too long (max 255 chars).'));
179                         return;
180                 } else if (!is_null($bio) && strlen($bio) > 140) {
181                         $this->show_form(_('Bio is too long (max 140 chars).'));
182                         return;
183                 } else if (!is_null($location) && strlen($location) > 255) {
184                         $this->show_form(_('Location is too long (max 255 chars).'));
185                         return;
186                 }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
187                         $this->show_form(_('Timezone not selected.'));
188                         return;
189                 } else if ($this->nickname_exists($nickname)) {
190                         $this->show_form(_('Nickname already in use. Try another one.'));
191                         return;
192                 } else if (!is_null($language) && strlen($language) > 50) {
193                         $this->show_form(_('Language is too long (max 50 chars).'));
194                 }
195
196                 $user = common_current_user();
197
198                 $user->query('BEGIN');
199
200                 if ($user->nickname != $nickname ||
201                         $user->language != $language ||
202                         $user->timezone != $timezone) {
203
204                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
205                                                  __FILE__);
206                         common_debug('Updating user language from ' . $user->language . ' to ' . $language,
207                                                  __FILE__);
208                         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
209                                                  __FILE__);
210
211                         $original = clone($user);
212
213                         $user->nickname = $nickname;
214                         $user->language = $language;
215                         $user->timezone = $timezone;
216
217                         $result = $user->updateKeys($original);
218
219                         if ($result === FALSE) {
220                                 common_log_db_error($user, 'UPDATE', __FILE__);
221                                 common_server_error(_('Couldn\'t update user.'));
222                                 return;
223                         } else {
224                                 # Re-initialize language environment if it changed
225                                 common_init_language();
226                         }
227                 }
228
229                 # XXX: XOR
230
231                 if ($user->autosubscribe ^ $autosubscribe) {
232
233                         $original = clone($user);
234
235                         $user->autosubscribe = $autosubscribe;
236
237                         $result = $user->update($original);
238
239                         if ($result === FALSE) {
240                                 common_log_db_error($user, 'UPDATE', __FILE__);
241                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
242                                 return;
243                         }
244                 }
245
246                 $profile = $user->getProfile();
247
248                 $orig_profile = clone($profile);
249
250                 $profile->nickname = $user->nickname;
251                 $profile->fullname = $fullname;
252                 $profile->homepage = $homepage;
253                 $profile->bio = $bio;
254                 $profile->location = $location;
255                 $profile->profileurl = common_profile_url($nickname);
256
257                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
258                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
259
260                 $result = $profile->update($orig_profile);
261
262                 if (!$result) {
263                         common_log_db_error($profile, 'UPDATE', __FILE__);
264                         common_server_error(_('Couldn\'t save profile.'));
265                         return;
266                 }
267
268                 $user->query('COMMIT');
269
270                 common_broadcast_profile($profile);
271
272                 $this->show_form(_('Settings saved.'), TRUE);
273         }
274
275
276         function upload_avatar() {
277                 switch ($_FILES['avatarfile']['error']) {
278                  case UPLOAD_ERR_OK: # success, jump out
279                         break;
280                  case UPLOAD_ERR_INI_SIZE:
281                  case UPLOAD_ERR_FORM_SIZE:
282                         $this->show_form(_('That file is too big.'));
283                         return;
284                  case UPLOAD_ERR_PARTIAL:
285                         @unlink($_FILES['avatarfile']['tmp_name']);
286                         $this->show_form(_('Partial upload.'));
287                         return;
288                  default:
289                         $this->show_form(_('System error uploading file.'));
290                         return;
291                 }
292
293                 $info = @getimagesize($_FILES['avatarfile']['tmp_name']);
294
295                 if (!$info) {
296                         @unlink($_FILES['avatarfile']['tmp_name']);
297                         $this->show_form(_('Not an image or corrupt file.'));
298                         return;
299                 }
300
301                 switch ($info[2]) {
302                  case IMAGETYPE_GIF:
303                  case IMAGETYPE_JPEG:
304                  case IMAGETYPE_PNG:
305                         break;
306                  default:
307                         $this->show_form(_('Unsupported image file format.'));
308                         return;
309                 }
310
311                 $user = common_current_user();
312                 $profile = $user->getProfile();
313
314                 if ($profile->setOriginal($_FILES['avatarfile']['tmp_name'])) {
315                         $this->show_form(_('Avatar updated.'), true);
316                 } else {
317                         $this->show_form(_('Failed updating avatar.'));
318                 }
319
320                 @unlink($_FILES['avatarfile']['tmp_name']);
321         }
322
323         function nickname_exists($nickname) {
324                 $user = common_current_user();
325                 $other = User::staticGet('nickname', $nickname);
326                 if (!$other) {
327                         return false;
328                 } else {
329                         return $other->id != $user->id;
330                 }
331         }
332 }