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