]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
Link rtsp, mms & tel URI schemes, correct pseudo-protocol ones.
[quix0rs-gnu-social.git] / actions / profilesettings.php
1 <?php
2 /**
3  * Laconica, 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   Laconica
24  * @author    Evan Prodromou <evan@controlyourself.ca>
25  * @author    Zach Copley <zach@controlyourself.ca>
26  * @copyright 2008-2009 Control Yourself, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28  * @link      http://laconi.ca/
29  */
30
31 if (!defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/accountsettingsaction.php';
36
37 /**
38  * Change profile settings
39  *
40  * @category Settings
41  * @package  Laconica
42  * @author   Evan Prodromou <evan@controlyourself.ca>
43  * @author   Zach Copley <zach@controlyourself.ca>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://laconi.ca/
46  */
47
48 class ProfilesettingsAction extends AccountSettingsAction
49 {
50     /**
51      * Title of the page
52      *
53      * @return string Title of the page
54      */
55
56     function title()
57     {
58         return _('Profile settings');
59     }
60
61     /**
62      * Instructions for use
63      *
64      * @return instructions for use
65      */
66
67     function getInstructions()
68     {
69         return _('You can update your personal profile info here '.
70                   'so people know more about you.');
71     }
72
73     /**
74      * Content area of the page
75      *
76      * Shows a form for uploading an avatar.
77      *
78      * @return void
79      */
80
81     function showContent()
82     {
83         $user = common_current_user();
84         $profile = $user->getProfile();
85
86         $this->elementStart('form', array('method' => 'post',
87                                            'id' => 'form_settings_profile',
88                                            'class' => 'form_settings',
89                                            'action' => common_local_url('profilesettings')));
90         $this->elementStart('fieldset');
91         $this->element('legend', null, _('Profile information'));
92         $this->hidden('token', common_session_token());
93
94         # too much common patterns here... abstractable?
95
96         $this->elementStart('ul', 'form_data');
97         $this->elementStart('li');
98         $this->input('nickname', _('Nickname'),
99                      ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
100                      _('1-64 lowercase letters or numbers, no punctuation or spaces'));
101         $this->elementEnd('li');
102         $this->elementStart('li');
103         $this->input('fullname', _('Full name'),
104                      ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
105         $this->elementEnd('li');
106         $this->elementStart('li');
107         $this->input('homepage', _('Homepage'),
108                      ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
109                      _('URL of your homepage, blog, or profile on another site'));
110         $this->elementEnd('li');
111         $this->elementStart('li');
112         $this->textarea('bio', _('Bio'),
113                         ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
114                         _('Describe yourself and your interests in 140 chars'));
115         $this->elementEnd('li');
116         $this->elementStart('li');
117         $this->input('location', _('Location'),
118                      ($this->arg('location')) ? $this->arg('location') : $profile->location,
119                      _('Where you are, like "City, State (or Region), Country"'));
120         $this->elementEnd('li');
121         $this->elementStart('li');
122         $this->input('tags', _('Tags'),
123                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
124                      _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
125         $this->elementEnd('li');
126         $this->elementStart('li');
127         $language = common_language();
128         $this->dropdown('language', _('Language'),
129                         get_nice_language_list(), _('Preferred language'),
130                         true, $language);
131         $this->elementEnd('li');
132         $timezone = common_timezone();
133         $timezones = array();
134         foreach(DateTimeZone::listIdentifiers() as $k => $v) {
135             $timezones[$v] = $v;
136         }
137         $this->elementStart('li');
138         $this->dropdown('timezone', _('Timezone'),
139                         $timezones, _('What timezone are you normally in?'),
140                         true, $timezone);
141         $this->elementEnd('li');
142         $this->elementStart('li');
143         $this->checkbox('autosubscribe',
144                         _('Automatically subscribe to whoever '.
145                           'subscribes to me (best for non-humans)'),
146                         ($this->arg('autosubscribe')) ?
147                         $this->boolean('autosubscribe') : $user->autosubscribe);
148         $this->elementEnd('li');
149         $this->elementEnd('ul');
150         $this->submit('save', _('Save'));
151
152         $this->elementEnd('fieldset');
153         $this->elementEnd('form');
154
155     }
156
157     /**
158      * Handle a post
159      *
160      * Validate input and save changes. Reload the form with a success
161      * or error message.
162      *
163      * @return void
164      */
165
166     function handlePost()
167     {
168         # CSRF protection
169
170         $token = $this->trimmed('token');
171         if (!$token || $token != common_session_token()) {
172             $this->showForm(_('There was a problem with your session token. '.
173                                'Try again, please.'));
174             return;
175         }
176
177         $nickname = $this->trimmed('nickname');
178         $fullname = $this->trimmed('fullname');
179         $homepage = $this->trimmed('homepage');
180         $bio = $this->trimmed('bio');
181         $location = $this->trimmed('location');
182         $autosubscribe = $this->boolean('autosubscribe');
183         $language = $this->trimmed('language');
184         $timezone = $this->trimmed('timezone');
185         $tagstring = $this->trimmed('tags');
186
187         # Some validation
188
189         if (!Validate::string($nickname, array('min_length' => 1,
190                                                'max_length' => 64,
191                                                'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
192             $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
193             return;
194         } else if (!User::allowed_nickname($nickname)) {
195             $this->showForm(_('Not a valid nickname.'));
196             return;
197         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
198                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
199             $this->showForm(_('Homepage is not a valid URL.'));
200             return;
201         } else if (!is_null($fullname) && strlen($fullname) > 255) {
202             $this->showForm(_('Full name is too long (max 255 chars).'));
203             return;
204         } else if (!is_null($bio) && strlen($bio) > 140) {
205             $this->showForm(_('Bio is too long (max 140 chars).'));
206             return;
207         } else if (!is_null($location) && strlen($location) > 255) {
208             $this->showForm(_('Location is too long (max 255 chars).'));
209             return;
210         }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
211             $this->showForm(_('Timezone not selected.'));
212             return;
213         } else if ($this->nicknameExists($nickname)) {
214             $this->showForm(_('Nickname already in use. Try another one.'));
215             return;
216         } else if (!is_null($language) && strlen($language) > 50) {
217             $this->showForm(_('Language is too long (max 50 chars).'));
218             return;
219         }
220
221         if ($tagstring) {
222             $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
223         } else {
224             $tags = array();
225         }
226
227         foreach ($tags as $tag) {
228             if (!common_valid_profile_tag($tag)) {
229                 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
230                 return;
231             }
232         }
233
234         $user = common_current_user();
235
236         $user->query('BEGIN');
237
238         if ($user->nickname != $nickname ||
239             $user->language != $language ||
240             $user->timezone != $timezone) {
241
242             common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
243                          __FILE__);
244             common_debug('Updating user language from ' . $user->language . ' to ' . $language,
245                          __FILE__);
246             common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
247                          __FILE__);
248
249             $original = clone($user);
250
251             $user->nickname = $nickname;
252             $user->language = $language;
253             $user->timezone = $timezone;
254
255             $result = $user->updateKeys($original);
256
257             if ($result === false) {
258                 common_log_db_error($user, 'UPDATE', __FILE__);
259                 $this->serverError(_('Couldn\'t update user.'));
260                 return;
261             } else {
262                 # Re-initialize language environment if it changed
263                 common_init_language();
264             }
265         }
266
267         # XXX: XOR
268
269         if ($user->autosubscribe ^ $autosubscribe) {
270
271             $original = clone($user);
272
273             $user->autosubscribe = $autosubscribe;
274
275             $result = $user->update($original);
276
277             if ($result === false) {
278                 common_log_db_error($user, 'UPDATE', __FILE__);
279                 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
280                 return;
281             }
282         }
283
284         $profile = $user->getProfile();
285
286         $orig_profile = clone($profile);
287
288         $profile->nickname = $user->nickname;
289         $profile->fullname = $fullname;
290         $profile->homepage = $homepage;
291         $profile->bio = $bio;
292         $profile->location = $location;
293         $profile->profileurl = common_profile_url($nickname);
294
295         common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
296         common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
297
298         $result = $profile->update($orig_profile);
299
300         if (!$result) {
301             common_log_db_error($profile, 'UPDATE', __FILE__);
302             $this->serverError(_('Couldn\'t save profile.'));
303             return;
304         }
305
306         # Set the user tags
307
308         $result = $user->setSelfTags($tags);
309
310         if (!$result) {
311             $this->serverError(_('Couldn\'t save tags.'));
312             return;
313         }
314
315         $user->query('COMMIT');
316
317         common_broadcast_profile($profile);
318
319         $this->showForm(_('Settings saved.'), true);
320     }
321
322     function nicknameExists($nickname)
323     {
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 }