]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
bc4fce50b98c2fca8323e0d015c416a0750d08e9
[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_start('form', array('method' => 'post',
37                                                                                    'id' => 'profilesettings',
38                                                                                    'action' =>
39                                                                                    common_local_url('profilesettings')));
40                 # too much common patterns here... abstractable?
41                 common_input('nickname', _('Nickname'),
42                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
43                                          _('1-64 lowercase letters or numbers, no punctuation or spaces'));
44                 common_input('fullname', _('Full name'),
45                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
46                 common_input('homepage', _('Homepage'),
47                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
48                                          _('URL of your homepage, blog, or profile on another site'));
49                 common_textarea('bio', _('Bio'),
50                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
51                                                 _('Describe yourself and your interests in 140 chars'));
52                 common_input('location', _('Location'),
53                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
54                                          _('Where you are, like "City, State (or Region), Country"'));
55
56                 $language = common_language();
57                 common_dropdown('language', _('Language'), get_nice_language_list(), _('Preferred language'), TRUE, $language);
58                 $timezone = common_timezone();
59                 $timezones = array();
60                 foreach(DateTimeZone::listIdentifiers() as $k => $v) {
61                         $timezones[$v] = $v;
62                 }
63                 common_dropdown('timezone', _('Timezone'), $timezones, _('What timezone are you normally in?'), TRUE, $timezone);
64
65                 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
66                                                 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
67                 common_submit('submit', _('Save'));
68                 common_element_end('form');
69                 common_show_footer();
70         }
71
72         function handle_post() {
73
74                 $nickname = $this->trimmed('nickname');
75                 $fullname = $this->trimmed('fullname');
76                 $homepage = $this->trimmed('homepage');
77                 $bio = $this->trimmed('bio');
78                 $location = $this->trimmed('location');
79                 $autosubscribe = $this->boolean('autosubscribe');
80                 $language = $this->trimmed('language');
81                 $timezone = $this->trimmed('timezone');
82
83                 # Some validation
84
85                 if (!Validate::string($nickname, array('min_length' => 1,
86                                                                                            'max_length' => 64,
87                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
88                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
89                         return;
90                 } else if (!User::allowed_nickname($nickname)) {
91                         $this->show_form(_('Not a valid nickname.'));
92                         return;
93                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
94                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
95                         $this->show_form(_('Homepage is not a valid URL.'));
96                         return;
97                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
98                         $this->show_form(_('Full name is too long (max 255 chars).'));
99                         return;
100                 } else if (!is_null($bio) && strlen($bio) > 140) {
101                         $this->show_form(_('Bio is too long (max 140 chars).'));
102                         return;
103                 } else if (!is_null($location) && strlen($location) > 255) {
104                         $this->show_form(_('Location is too long (max 255 chars).'));
105                         return;
106                 }  else if (is_null($timezone) || !in_array($timezone, DateTimeZone::listIdentifiers())) {
107                         $this->show_form(_('Timezone not selected.'));
108                         return;
109                 } else if ($this->nickname_exists($nickname)) {
110                         $this->show_form(_('Nickname already in use. Try another one.'));
111                         return;
112                 } else if (!is_null($language) && strlen($language) > 50) {
113                         $this->show_form(_('Language is too long (max 50 chars).'));
114                 }
115
116                 $user = common_current_user();
117
118                 $user->query('BEGIN');
119
120                 if ($user->nickname != $nickname ||
121                         $user->language != $language ||
122                         $user->timezone != $timezone) {
123
124                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
125                                                  __FILE__);
126                         common_debug('Updating user language from ' . $user->language . ' to ' . $language,
127                                                  __FILE__);
128                         common_debug('Updating user timezone from ' . $user->timezone . ' to ' . $timezone,
129                                                  __FILE__);
130
131                         $original = clone($user);
132
133                         $user->nickname = $nickname;
134                         $user->language = $language;
135                         $user->timezone = $timezone;
136
137                         $result = $user->updateKeys($original);
138
139                         if ($result === FALSE) {
140                                 common_log_db_error($user, 'UPDATE', __FILE__);
141                                 common_server_error(_('Couldn\'t update user.'));
142                                 return;
143                         } else {
144                                 # Re-initialize language environment if it changed
145                                 common_init_language();
146                         }
147                 }
148
149                 # XXX: XOR
150                 
151                 if ($user->autosubscribe ^ $autosubscribe) {
152                         
153                         $original = clone($user);
154
155                         $user->autosubscribe = $autosubscribe;
156
157                         $result = $user->update($original);
158
159                         if ($result === FALSE) {
160                                 common_log_db_error($user, 'UPDATE', __FILE__);
161                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
162                                 return;
163                         }
164                 }
165                 
166                 $profile = $user->getProfile();
167
168                 $orig_profile = clone($profile);
169
170                 $profile->nickname = $user->nickname;
171                 $profile->fullname = $fullname;
172                 $profile->homepage = $homepage;
173                 $profile->bio = $bio;
174                 $profile->location = $location;
175                 $profile->profileurl = common_profile_url($nickname);
176
177                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
178                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
179
180                 $result = $profile->update($orig_profile);
181
182                 if (!$result) {
183                         common_log_db_error($profile, 'UPDATE', __FILE__);
184                         common_server_error(_('Couldn\'t save profile.'));
185                         return;
186                 }
187
188                 $user->query('COMMIT');
189
190                 common_broadcast_profile($profile);
191
192                 $this->show_form(_('Settings saved.'), TRUE);
193         }
194
195         function nickname_exists($nickname) {
196                 $user = common_current_user();
197                 $other = User::staticGet('nickname', $nickname);
198                 if (!$other) {
199                         return false;
200                 } else {
201                         return $other->id != $user->id;
202                 }
203         }
204 }