]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
remove namespace setting from location; it's unused
[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         return _('Profile settings');
61     }
62
63     /**
64      * Instructions for use
65      *
66      * @return instructions for use
67      */
68
69     function getInstructions()
70     {
71         return _('You can update your personal profile info here '.
72                   'so people know more about you.');
73     }
74
75     function showScripts()
76     {
77         parent::showScripts();
78         $this->autofocus('nickname');
79     }
80
81     /**
82      * Content area of the page
83      *
84      * Shows a form for uploading an avatar.
85      *
86      * @return void
87      */
88
89     function showContent()
90     {
91         $user = common_current_user();
92         $profile = $user->getProfile();
93
94         $this->elementStart('form', array('method' => 'post',
95                                            'id' => 'form_settings_profile',
96                                            'class' => 'form_settings',
97                                            'action' => common_local_url('profilesettings')));
98         $this->elementStart('fieldset');
99         $this->element('legend', null, _('Profile information'));
100         $this->hidden('token', common_session_token());
101
102         // too much common patterns here... abstractable?
103         $this->elementStart('ul', 'form_data');
104         if (Event::handle('StartProfileFormData', array($this))) {
105             $this->elementStart('li');
106             $this->input('nickname', _('Nickname'),
107                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
108                          _('1-64 lowercase letters or numbers, no punctuation or spaces'));
109             $this->elementEnd('li');
110             $this->elementStart('li');
111             $this->input('fullname', _('Full name'),
112                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
113             $this->elementEnd('li');
114             $this->elementStart('li');
115             $this->input('homepage', _('Homepage'),
116                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
117                          _('URL of your homepage, blog, or profile on another site'));
118             $this->elementEnd('li');
119             $this->elementStart('li');
120             $maxBio = Profile::maxBio();
121             if ($maxBio > 0) {
122                 $bioInstr = sprintf(_('Describe yourself and your interests in %d chars'),
123                                     $maxBio);
124             } else {
125                 $bioInstr = _('Describe yourself and your interests');
126             }
127             $this->textarea('bio', _('Bio'),
128                             ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
129                             $bioInstr);
130             $this->elementEnd('li');
131             $this->elementStart('li');
132             $this->input('location', _('Location'),
133                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
134                          _('Where you are, like "City, State (or Region), Country"'));
135             $this->elementEnd('li');
136             $this->elementStart('li');
137             $this->checkbox('sharelocation', _('Share my current location when posting notices'),
138                             ($this->arg('sharelocation')) ?
139                             $this->arg('sharelocation') : $user->shareLocation());
140             $this->elementEnd('li');
141             Event::handle('EndProfileFormData', array($this));
142             $this->elementStart('li');
143             $this->input('tags', _('Tags'),
144                          ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
145                          _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
146             $this->elementEnd('li');
147             $this->elementStart('li');
148             $language = common_language();
149             $this->dropdown('language', _('Language'),
150                             get_nice_language_list(), _('Preferred language'),
151                             false, $language);
152             $this->elementEnd('li');
153             $timezone = common_timezone();
154             $timezones = array();
155             foreach(DateTimeZone::listIdentifiers() as $k => $v) {
156                 $timezones[$v] = $v;
157             }
158             $this->elementStart('li');
159             $this->dropdown('timezone', _('Timezone'),
160                             $timezones, _('What timezone are you normally in?'),
161                             true, $timezone);
162             $this->elementEnd('li');
163             $this->elementStart('li');
164             $this->checkbox('autosubscribe',
165                             _('Automatically subscribe to whoever '.
166                               'subscribes to me (best for non-humans)'),
167                             ($this->arg('autosubscribe')) ?
168                             $this->boolean('autosubscribe') : $user->autosubscribe);
169             $this->elementEnd('li');
170         }
171         $this->elementEnd('ul');
172         $this->submit('save', _('Save'));
173
174         $this->elementEnd('fieldset');
175         $this->elementEnd('form');
176     }
177
178     /**
179      * Handle a post
180      *
181      * Validate input and save changes. Reload the form with a success
182      * or error message.
183      *
184      * @return void
185      */
186
187     function handlePost()
188     {
189         // CSRF protection
190         $token = $this->trimmed('token');
191         if (!$token || $token != common_session_token()) {
192             $this->showForm(_('There was a problem with your session token. '.
193                         'Try again, please.'));
194             return;
195         }
196
197         if (Event::handle('StartProfileSaveForm', array($this))) {
198
199             $nickname = $this->trimmed('nickname');
200             $fullname = $this->trimmed('fullname');
201             $homepage = $this->trimmed('homepage');
202             $bio = $this->trimmed('bio');
203             $location = $this->trimmed('location');
204             $autosubscribe = $this->boolean('autosubscribe');
205             $language = $this->trimmed('language');
206             $timezone = $this->trimmed('timezone');
207             $tagstring = $this->trimmed('tags');
208
209             // Some validation
210             if (!Validate::string($nickname, array('min_length' => 1,
211                             'max_length' => 64,
212                             'format' => NICKNAME_FMT))) {
213                 $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
214                 return;
215             } else if (!User::allowed_nickname($nickname)) {
216                 $this->showForm(_('Not a valid nickname.'));
217                 return;
218             } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
219                     !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
220                 $this->showForm(_('Homepage is not a valid URL.'));
221                 return;
222             } else if (!is_null($fullname) && mb_strlen($fullname) > 255) {
223                 $this->showForm(_('Full name is too long (max 255 chars).'));
224                 return;
225             } else if (Profile::bioTooLong($bio)) {
226                 $this->showForm(sprintf(_('Bio is too long (max %d chars).'),
227                                         Profile::maxBio()));
228                 return;
229             } else if (!is_null($location) && mb_strlen($location) > 255) {
230                 $this->showForm(_('Location is too long (max 255 chars).'));
231                 return;
232             }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
233                 $this->showForm(_('Timezone not selected.'));
234                 return;
235             } else if ($this->nicknameExists($nickname)) {
236                 $this->showForm(_('Nickname already in use. Try another one.'));
237                 return;
238             } else if (!is_null($language) && strlen($language) > 50) {
239                 $this->showForm(_('Language is too long (max 50 chars).'));
240                 return;
241             }
242
243             if ($tagstring) {
244                 $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
245             } else {
246                 $tags = array();
247             }
248
249             foreach ($tags as $tag) {
250                 if (!common_valid_profile_tag($tag)) {
251                     $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
252                     return;
253                 }
254             }
255
256             $user = common_current_user();
257
258             $user->query('BEGIN');
259
260             if ($user->nickname != $nickname ||
261                     $user->language != $language ||
262                     $user->timezone != $timezone) {
263
264                 common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
265                         __FILE__);
266                 common_debug('Updating user language from ' . $user->language . ' to ' . $language,
267                         __FILE__);
268                 common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
269                         __FILE__);
270
271                 $original = clone($user);
272
273                 $user->nickname = $nickname;
274                 $user->language = $language;
275                 $user->timezone = $timezone;
276
277                 $result = $user->updateKeys($original);
278
279                 if ($result === false) {
280                     common_log_db_error($user, 'UPDATE', __FILE__);
281                     $this->serverError(_('Couldn\'t update user.'));
282                     return;
283                 } else {
284                     // Re-initialize language environment if it changed
285                     common_init_language();
286                 }
287             }
288
289 // XXX: XOR
290             if ($user->autosubscribe ^ $autosubscribe) {
291
292                 $original = clone($user);
293
294                 $user->autosubscribe = $autosubscribe;
295
296                 $result = $user->update($original);
297
298                 if ($result === false) {
299                     common_log_db_error($user, 'UPDATE', __FILE__);
300                     $this->serverError(_('Couldn\'t update user for autosubscribe.'));
301                     return;
302                 }
303             }
304
305             $profile = $user->getProfile();
306
307             $orig_profile = clone($profile);
308
309             $profile->nickname = $user->nickname;
310             $profile->fullname = $fullname;
311             $profile->homepage = $homepage;
312             $profile->bio = $bio;
313             $profile->location = $location;
314
315             $loc = Location::fromName($location);
316
317             if (!empty($loc)) {
318                 $profile->lat         = $loc->lat;
319                 $profile->lon         = $loc->lon;
320                 $profile->location_id = $loc->location_id;
321                 $profile->location_ns = $loc->location_ns;
322             }
323
324             $profile->profileurl = common_profile_url($nickname);
325
326             common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
327             common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
328
329             $result = $profile->update($orig_profile);
330
331             if ($result === false) {
332                 common_log_db_error($profile, 'UPDATE', __FILE__);
333                 $this->serverError(_('Couldn\'t save profile.'));
334                 return;
335             }
336
337             // Set the user tags
338             $result = $user->setSelfTags($tags);
339
340             if (!$result) {
341                 $this->serverError(_('Couldn\'t save tags.'));
342                 return;
343             }
344
345             $user->query('COMMIT');
346             Event::handle('EndProfileSaveForm', array($this));
347             common_broadcast_profile($profile);
348
349             $this->showForm(_('Settings saved.'), true);
350
351         }
352     }
353
354     function nicknameExists($nickname)
355     {
356         $user = common_current_user();
357         $other = User::staticGet('nickname', $nickname);
358         if (!$other) {
359            return false;
360         } else {
361             return $other->id != $user->id;
362         }
363     }
364 }