]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
fix notices in dumpschema
[quix0rs-gnu-social.git] / actions / profilesettings.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Change profile settings
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Settings
23  * @package   StatusNet
24  * @author    Evan Prodromou <evan@status.net>
25  * @author    Zach Copley <zach@status.net>
26  * @author    Sarven Capadisli <csarven@status.net>
27  * @copyright 2008-2009 StatusNet, Inc.
28  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
29  * @link      http://status.net/
30  */
31
32 if (!defined('STATUSNET') && !defined('LACONICA')) {
33     exit(1);
34 }
35
36 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
37
38 /**
39  * Change profile settings
40  *
41  * @category Settings
42  * @package  StatusNet
43  * @author   Evan Prodromou <evan@status.net>
44  * @author   Zach Copley <zach@status.net>
45  * @author   Sarven Capadisli <csarven@status.net>
46  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
47  * @link     http://status.net/
48  */
49
50 class ProfilesettingsAction extends AccountSettingsAction
51 {
52     /**
53      * Title of the page
54      *
55      * @return string Title of the page
56      */
57
58     function title()
59     {
60         // TRANS: Page title for profile settings.
61         return _('Profile settings');
62     }
63
64     /**
65      * Instructions for use
66      *
67      * @return instructions for use
68      */
69
70     function getInstructions()
71     {
72         // TRANS: Usage instructions for profile settings.
73         return _('You can update your personal profile info here '.
74                  'so people know more about you.');
75     }
76
77     function showScripts()
78     {
79         parent::showScripts();
80         $this->autofocus('nickname');
81     }
82
83     /**
84      * Content area of the page
85      *
86      * Shows a form for uploading an avatar.
87      *
88      * @return void
89      */
90
91     function showContent()
92     {
93         $user = common_current_user();
94         $profile = $user->getProfile();
95
96         $this->elementStart('form', array('method' => 'post',
97                                           'id' => 'form_settings_profile',
98                                           'class' => 'form_settings',
99                                           'action' => common_local_url('profilesettings')));
100         $this->elementStart('fieldset');
101         // TRANS: Profile settings form legend.
102         $this->element('legend', null, _('Profile information'));
103         $this->hidden('token', common_session_token());
104
105         // too much common patterns here... abstractable?
106         $this->elementStart('ul', 'form_data');
107         if (Event::handle('StartProfileFormData', array($this))) {
108             $this->elementStart('li');
109             // TRANS: Field label in form for profile settings.
110             $this->input('nickname', _('Nickname'),
111                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
112                          // TRANS: Tooltip for field label in form for profile settings.
113                          _('1-64 lowercase letters or numbers, no punctuation or spaces.'));
114             $this->elementEnd('li');
115             $this->elementStart('li');
116             // TRANS: Field label in form for profile settings.
117             $this->input('fullname', _('Full name'),
118                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
119             $this->elementEnd('li');
120             $this->elementStart('li');
121             // TRANS: Field label in form for profile settings.
122             $this->input('homepage', _('Homepage'),
123                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
124                          // TRANS: Tooltip for field label in form for profile settings.
125                          _('URL of your homepage, blog, or profile on another site.'));
126             $this->elementEnd('li');
127             $this->elementStart('li');
128             $maxBio = Profile::maxBio();
129             if ($maxBio > 0) {
130                 // TRANS: Tooltip for field label in form for profile settings. Plural
131                 // TRANS: is decided by the number of characters available for the
132                 // TRANS: biography (%d).
133                 $bioInstr = sprintf(_m('Describe yourself and your interests in %d character',
134                                        'Describe yourself and your interests in %d characters',
135                                        $maxBio),
136                                     $maxBio);
137             } else {
138                 // TRANS: Tooltip for field label in form for profile settings.
139                 $bioInstr = _('Describe yourself and your interests');
140             }
141             // TRANS: Text area label in form for profile settings where users can provide.
142             // TRANS: their biography.
143             $this->textarea('bio', _('Bio'),
144                             ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
145                             $bioInstr);
146             $this->elementEnd('li');
147             $this->elementStart('li');
148             // TRANS: Field label in form for profile settings.
149             $this->input('location', _('Location'),
150                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
151                          // TRANS: Tooltip for field label in form for profile settings.
152                          _('Where you are, like "City, State (or Region), Country"'));
153             $this->elementEnd('li');
154             if (common_config('location', 'share') == 'user') {
155                 $this->elementStart('li');
156                 // TRANS: Checkbox label in form for profile settings.
157                 $this->checkbox('sharelocation', _('Share my current location when posting notices'),
158                                 ($this->arg('sharelocation')) ?
159                                 $this->arg('sharelocation') : $user->shareLocation());
160                 $this->elementEnd('li');
161             }
162             Event::handle('EndProfileFormData', array($this));
163             $this->elementStart('li');
164             // TRANS: Field label in form for profile settings.
165             $this->input('tags', _('Tags'),
166                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
167                          // TRANS: Tooltip for field label in form for profile settings.
168                          _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
169             $this->elementEnd('li');
170             $this->elementStart('li');
171             $language = common_language();
172             // TRANS: Dropdownlist label in form for profile settings.
173             $this->dropdown('language', _('Language'),
174                          // TRANS: Tooltip for dropdown list label in form for profile settings.
175                             get_nice_language_list(), _('Preferred language'),
176                             false, $language);
177             $this->elementEnd('li');
178             $timezone = common_timezone();
179             $timezones = array();
180             foreach(DateTimeZone::listIdentifiers() as $k => $v) {
181                 $timezones[$v] = $v;
182             }
183             $this->elementStart('li');
184             // TRANS: Dropdownlist label in form for profile settings.
185             $this->dropdown('timezone', _('Timezone'),
186                          // TRANS: Tooltip for dropdown list label in form for profile settings.
187                             $timezones, _('What timezone are you normally in?'),
188                             true, $timezone);
189             $this->elementEnd('li');
190             $this->elementStart('li');
191             $this->checkbox('autosubscribe',
192                             // TRANS: Checkbox label in form for profile settings.
193                             _('Automatically subscribe to whoever '.
194                               'subscribes to me (best for non-humans)'),
195                             ($this->arg('autosubscribe')) ?
196                             $this->boolean('autosubscribe') : $user->autosubscribe);
197             $this->elementEnd('li');
198         }
199         $this->elementEnd('ul');
200         // TRANS: Button to save input in profile settings.
201         $this->submit('save', _m('BUTTON','Save'));
202
203         $this->elementEnd('fieldset');
204         $this->elementEnd('form');
205     }
206
207     /**
208      * Handle a post
209      *
210      * Validate input and save changes. Reload the form with a success
211      * or error message.
212      *
213      * @return void
214      */
215
216     function handlePost()
217     {
218         // CSRF protection
219         $token = $this->trimmed('token');
220         if (!$token || $token != common_session_token()) {
221             $this->showForm(_('There was a problem with your session token. '.
222                               'Try again, please.'));
223             return;
224         }
225
226         if (Event::handle('StartProfileSaveForm', array($this))) {
227
228             $nickname = $this->trimmed('nickname');
229             $fullname = $this->trimmed('fullname');
230             $homepage = $this->trimmed('homepage');
231             $bio = $this->trimmed('bio');
232             $location = $this->trimmed('location');
233             $autosubscribe = $this->boolean('autosubscribe');
234             $language = $this->trimmed('language');
235             $timezone = $this->trimmed('timezone');
236             $tagstring = $this->trimmed('tags');
237
238             // Some validation
239             if (!Validate::string($nickname, array('min_length' => 1,
240                                                    'max_length' => 64,
241                                                    'format' => NICKNAME_FMT))) {
242                 // TRANS: Validation error in form for profile settings.
243                 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
244                 return;
245             } else if (!User::allowed_nickname($nickname)) {
246                 // TRANS: Validation error in form for profile settings.
247                 $this->showForm(_('Not a valid nickname.'));
248                 return;
249             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
250                        !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
251                 // TRANS: Validation error in form for profile settings.
252                 $this->showForm(_('Homepage is not a valid URL.'));
253                 return;
254             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
255                 // TRANS: Validation error in form for profile settings.
256                 $this->showForm(_('Full name is too long (maximum 255 characters).'));
257                 return;
258             } else if (Profile::bioTooLong($bio)) {
259                 // TRANS: Validation error in form for profile settings.
260                 // TRANS: Plural form is used based on the maximum number of allowed
261                 // TRANS: characters for the biography (%d).
262                 $this->showForm(sprintf(_m('Bio is too long (maximum %d character).',
263                                            'Bio is too long (maximum %d characters).',
264                                            Profile::maxBio()),
265                                         Profile::maxBio()));
266                 return;
267             } else if (!is_null($location) && mb_strlen($location) > 255) {
268                 // TRANS: Validation error in form for profile settings.
269                 $this->showForm(_('Location is too long (maximum 255 characters).'));
270                 return;
271             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
272                 // TRANS: Validation error in form for profile settings.
273                 $this->showForm(_('Timezone not selected.'));
274                 return;
275             } else if ($this->nicknameExists($nickname)) {
276                 // TRANS: Validation error in form for profile settings.
277                 $this->showForm(_('Nickname already in use. Try another one.'));
278                 return;
279             } else if (!is_null($language) && strlen($language) > 50) {
280                 // TRANS: Validation error in form for profile settings.
281                 $this->showForm(_('Language is too long (maximum 50 characters).'));
282                 return;
283             }
284
285             if ($tagstring) {
286                 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
287             } else {
288                 $tags = array();
289             }
290
291             foreach ($tags as $tag) {
292                 if (!common_valid_profile_tag($tag)) {
293                     // TRANS: Validation error in form for profile settings.
294                     // TRANS: %s is an invalid tag.
295                     $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
296                     return;
297                 }
298             }
299
300             $user = common_current_user();
301
302             $user->query('BEGIN');
303
304             if ($user->nickname != $nickname ||
305                 $user->language != $language ||
306                 $user->timezone != $timezone) {
307
308                 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
309                              __FILE__);
310                 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
311                              __FILE__);
312                 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
313                              __FILE__);
314
315                 $original = clone($user);
316
317                 $user->nickname = $nickname;
318                 $user->language = $language;
319                 $user->timezone = $timezone;
320
321                 $result = $user->updateKeys($original);
322
323                 if ($result === false) {
324                     common_log_db_error($user, 'UPDATE', __FILE__);
325                     // TRANS: Server error thrown when user profile settings could not be updated.
326                     $this->serverError(_('Couldn\'t update user.'));
327                     return;
328                 } else {
329                     // Re-initialize language environment if it changed
330                     common_init_language();
331                     // Clear the site owner, in case nickname changed
332                     if ($user->hasRole(Profile_role::OWNER)) {
333                         User::blow('user:site_owner');
334                     }
335                 }
336             }
337
338             // XXX: XOR
339             if ($user->autosubscribe ^ $autosubscribe) {
340
341                 $original = clone($user);
342
343                 $user->autosubscribe = $autosubscribe;
344
345                 $result = $user->update($original);
346
347                 if ($result === false) {
348                     common_log_db_error($user, 'UPDATE', __FILE__);
349                     // TRANS: Server error thrown when user profile settings could not be updated to
350                     // TRANS: automatically subscribe to any subscriber.
351                     $this->serverError(_('Couldn\'t update user for autosubscribe.'));
352                     return;
353                 }
354             }
355
356             $profile = $user->getProfile();
357
358             $orig_profile = clone($profile);
359
360             $profile->nickname = $user->nickname;
361             $profile->fullname = $fullname;
362             $profile->homepage = $homepage;
363             $profile->bio = $bio;
364             $profile->location = $location;
365
366             $loc = Location::fromName($location);
367
368             if (empty($loc)) {
369                 $profile->lat         = null;
370                 $profile->lon         = null;
371                 $profile->location_id = null;
372                 $profile->location_ns = null;
373             } else {
374                 $profile->lat         = $loc->lat;
375                 $profile->lon         = $loc->lon;
376                 $profile->location_id = $loc->location_id;
377                 $profile->location_ns = $loc->location_ns;
378             }
379
380             $profile->profileurl = common_profile_url($nickname);
381
382             if (common_config('location', 'share') == 'user') {
383
384                 $exists = false;
385
386                 $prefs = User_location_prefs::staticGet('user_id', $user->id);
387
388                 if (empty($prefs)) {
389                     $prefs = new User_location_prefs();
390
391                     $prefs->user_id = $user->id;
392                     $prefs->created = common_sql_now();
393                 } else {
394                     $exists = true;
395                     $orig = clone($prefs);
396                 }
397
398                 $prefs->share_location = $this->boolean('sharelocation');
399
400                 if ($exists) {
401                     $result = $prefs->update($orig);
402                 } else {
403                     $result = $prefs->insert();
404                 }
405
406                 if ($result === false) {
407                     common_log_db_error($prefs, ($exists) ? 'UPDATE' : 'INSERT', __FILE__);
408                     // TRANS: Server error thrown when user profile location preference settings could not be updated.
409                     $this->serverError(_('Couldn\'t save location prefs.'));
410                     return;
411                 }
412             }
413
414             common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
415             common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
416
417             $result = $profile->update($orig_profile);
418
419             if ($result === false) {
420                 common_log_db_error($profile, 'UPDATE', __FILE__);
421                 // TRANS: Server error thrown when user profile settings could not be saved.
422                 $this->serverError(_('Couldn\'t save profile.'));
423                 return;
424             }
425
426             // Set the user tags
427             $result = $user->setSelfTags($tags);
428
429             if (!$result) {
430                 // TRANS: Server error thrown when user profile settings tags could not be saved.
431                 $this->serverError(_('Couldn\'t save tags.'));
432                 return;
433             }
434
435             $user->query('COMMIT');
436             Event::handle('EndProfileSaveForm', array($this));
437             common_broadcast_profile($profile);
438
439             // TRANS: Confirmation shown when user profile settings are saved.
440             $this->showForm(_('Settings saved.'), true);
441
442         }
443     }
444
445     function nicknameExists($nickname)
446     {
447         $user = common_current_user();
448         $other = User::staticGet('nickname', $nickname);
449         if (!$other) {
450             return false;
451         } else {
452             return $other->id != $user->id;
453         }
454     }
455 }