]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
Break up settings into two tabset
[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' => 'profilesettings',
88                                            'action' => common_local_url('profilesettings')));
89         $this->hidden('token', common_session_token());
90
91         # too much common patterns here... abstractable?
92
93         $this->input('nickname', _('Nickname'),
94                      ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
95                      _('1-64 lowercase letters or numbers, no punctuation or spaces'));
96         $this->input('fullname', _('Full name'),
97                      ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
98         $this->input('homepage', _('Homepage'),
99                      ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
100                      _('URL of your homepage, blog, or profile on another site'));
101         $this->textarea('bio', _('Bio'),
102                         ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
103                         _('Describe yourself and your interests in 140 chars'));
104         $this->input('location', _('Location'),
105                      ($this->arg('location')) ? $this->arg('location') : $profile->location,
106                      _('Where you are, like "City, State (or Region), Country"'));
107         $this->input('tags', _('Tags'),
108                      ($this->arg('tags')) ? $this->arg('tags') : implode(' ', $user->getSelfTags()),
109                      _('Tags for yourself (letters, numbers, -, ., and _), comma- or space- separated'));
110
111         $language = common_language();
112         $this->dropdown('language', _('Language'),
113                         get_nice_language_list(), _('Preferred language'),
114                         true, $language);
115
116         $timezone = common_timezone();
117         $timezones = array();
118         foreach(DateTimeZone::listIdentifiers() as $k => $v) {
119             $timezones[$v] = $v;
120         }
121         $this->dropdown('timezone', _('Timezone'),
122                         $timezones, _('What timezone are you normally in?'),
123                         true, $timezone);
124
125         $this->checkbox('autosubscribe',
126                         _('Automatically subscribe to whoever '.
127                           'subscribes to me (best for non-humans)'),
128                         ($this->arg('autosubscribe')) ?
129                         $this->boolean('autosubscribe') : $user->autosubscribe);
130
131         $this->submit('save', _('Save'));
132
133         $this->elementEnd('form');
134
135     }
136
137     /**
138      * Handle a post
139      *
140      * Validate input and save changes. Reload the form with a success
141      * or error message.
142      *
143      * @return void
144      */
145
146     function handlePost()
147     {
148         # CSRF protection
149
150         $token = $this->trimmed('token');
151         if (!$token || $token != common_session_token()) {
152             $this->showForm(_('There was a problem with your session token. '.
153                                'Try again, please.'));
154             return;
155         }
156
157         $nickname = $this->trimmed('nickname');
158         $fullname = $this->trimmed('fullname');
159         $homepage = $this->trimmed('homepage');
160         $bio = $this->trimmed('bio');
161         $location = $this->trimmed('location');
162         $autosubscribe = $this->boolean('autosubscribe');
163         $language = $this->trimmed('language');
164         $timezone = $this->trimmed('timezone');
165         $tagstring = $this->trimmed('tags');
166
167         # Some validation
168
169         if (!Validate::string($nickname, array('min_length' => 1,
170                                                'max_length' => 64,
171                                                'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
172             $this->showForm(_('Nickname must have only lowercase letters and numbers and no spaces.'));
173             return;
174         } else if (!User::allowed_nickname($nickname)) {
175             $this->showForm(_('Not a valid nickname.'));
176             return;
177         } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
178                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
179             $this->showForm(_('Homepage is not a valid URL.'));
180             return;
181         } else if (!is_null($fullname) && strlen($fullname) > 255) {
182             $this->showForm(_('Full name is too long (max 255 chars).'));
183             return;
184         } else if (!is_null($bio) && strlen($bio) > 140) {
185             $this->showForm(_('Bio is too long (max 140 chars).'));
186             return;
187         } else if (!is_null($location) && strlen($location) > 255) {
188             $this->showForm(_('Location is too long (max 255 chars).'));
189             return;
190         }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
191             $this->showForm(_('Timezone not selected.'));
192             return;
193         } else if ($this->nicknameExists($nickname)) {
194             $this->showForm(_('Nickname already in use. Try another one.'));
195             return;
196         } else if (!is_null($language) && strlen($language) > 50) {
197             $this->showForm(_('Language is too long (max 50 chars).'));
198             return;
199         }
200
201         if ($tagstring) {
202             $tags = array_map('common_canonical_tag', preg_split('/[\s,]+/', $tagstring));
203         } else {
204             $tags = array();
205         }
206
207         foreach ($tags as $tag) {
208             if (!common_valid_profile_tag($tag)) {
209                 $this->showForm(sprintf(_('Invalid tag: "%s"'), $tag));
210                 return;
211             }
212         }
213
214         $user = common_current_user();
215
216         $user->query('BEGIN');
217
218         if ($user->nickname != $nickname ||
219             $user->language != $language ||
220             $user->timezone != $timezone) {
221
222             common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
223                          __FILE__);
224             common_debug('Updating user language from ' . $user->language . ' to ' . $language,
225                          __FILE__);
226             common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
227                          __FILE__);
228
229             $original = clone($user);
230
231             $user->nickname = $nickname;
232             $user->language = $language;
233             $user->timezone = $timezone;
234
235             $result = $user->updateKeys($original);
236
237             if ($result === false) {
238                 common_log_db_error($user, 'UPDATE', __FILE__);
239                 $this->serverError(_('Couldn\'t update user.'));
240                 return;
241             } else {
242                 # Re-initialize language environment if it changed
243                 common_init_language();
244             }
245         }
246
247         # XXX: XOR
248
249         if ($user->autosubscribe ^ $autosubscribe) {
250
251             $original = clone($user);
252
253             $user->autosubscribe = $autosubscribe;
254
255             $result = $user->update($original);
256
257             if ($result === false) {
258                 common_log_db_error($user, 'UPDATE', __FILE__);
259                 $this->serverError(_('Couldn\'t update user for autosubscribe.'));
260                 return;
261             }
262         }
263
264         $profile = $user->getProfile();
265
266         $orig_profile = clone($profile);
267
268         $profile->nickname = $user->nickname;
269         $profile->fullname = $fullname;
270         $profile->homepage = $homepage;
271         $profile->bio = $bio;
272         $profile->location = $location;
273         $profile->profileurl = common_profile_url($nickname);
274
275         common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
276         common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
277
278         $result = $profile->update($orig_profile);
279
280         if (!$result) {
281             common_log_db_error($profile, 'UPDATE', __FILE__);
282             $this->serverError(_('Couldn\'t save profile.'));
283             return;
284         }
285
286         # Set the user tags
287
288         $result = $user->setSelfTags($tags);
289
290         if (!$result) {
291             $this->serverError(_('Couldn\'t save tags.'));
292             return;
293         }
294
295         $user->query('COMMIT');
296
297         common_broadcast_profile($profile);
298
299         $this->showForm(_('Settings saved.'), true);
300     }
301
302     function nicknameExists($nickname)
303     {
304         $user = common_current_user();
305         $other = User::staticGet('nickname', $nickname);
306         if (!$other) {
307            return false;
308         } else {
309             return $other->id != $user->id;
310         }
311     }
312 }