]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
base class is_readonly() now returns false by default
[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                 common_checkbox('autosubscribe', _('Automatically subscribe to whoever subscribes to me (best for non-humans)'),
56                                                 ($this->arg('autosubscribe')) ? $this->boolean('autosubscribe') : $user->autosubscribe);
57                 common_submit('submit', _('Save'));
58                 common_element_end('form');
59                 common_show_footer();
60         }
61
62         function handle_post() {
63
64                 $nickname = $this->trimmed('nickname');
65                 $fullname = $this->trimmed('fullname');
66                 $homepage = $this->trimmed('homepage');
67                 $bio = $this->trimmed('bio');
68                 $location = $this->trimmed('location');
69                 $autosubscribe = $this->boolean('autosubscribe');
70                 
71                 # Some validation
72
73                 if (!Validate::string($nickname, array('min_length' => 1,
74                                                                                            'max_length' => 64,
75                                                                                            'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
76                         $this->show_form(_('Nickname must have only lowercase letters and numbers and no spaces.'));
77                         return;
78                 } else if (!User::allowed_nickname($nickname)) {
79                         $this->show_form(_('Not a valid nickname.'));
80                         return;
81                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
82                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
83                         $this->show_form(_('Homepage is not a valid URL.'));
84                         return;
85                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
86                         $this->show_form(_('Full name is too long (max 255 chars).'));
87                         return;
88                 } else if (!is_null($bio) && strlen($bio) > 140) {
89                         $this->show_form(_('Bio is too long (max 140 chars).'));
90                         return;
91                 } else if (!is_null($location) && strlen($location) > 255) {
92                         $this->show_form(_('Location is too long (max 255 chars).'));
93                         return;
94                 } else if ($this->nickname_exists($nickname)) {
95                         $this->show_form(_('Nickname already in use. Try another one.'));
96                         return;
97                 }
98
99                 $user = common_current_user();
100
101                 $user->query('BEGIN');
102
103                 if ($user->nickname != $nickname) {
104
105                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
106                                                  __FILE__);
107
108                         $original = clone($user);
109
110                         $user->nickname = $nickname;
111
112                         $result = $user->updateKeys($original);
113
114                         if ($result === FALSE) {
115                                 common_log_db_error($user, 'UPDATE', __FILE__);
116                                 common_server_error(_('Couldn\'t update user.'));
117                                 return;
118                         }
119                 }
120
121                 # XXX: XOR
122                 
123                 if ($user->autosubscribe ^ $autosubscribe) {
124                         
125                         $original = clone($user);
126
127                         $user->autosubscribe = $autosubscribe;
128
129                         $result = $user->update($original);
130
131                         if ($result === FALSE) {
132                                 common_log_db_error($user, 'UPDATE', __FILE__);
133                                 common_server_error(_('Couldn\'t update user for autosubscribe.'));
134                                 return;
135                         }
136                 }
137                 
138                 $profile = $user->getProfile();
139
140                 $orig_profile = clone($profile);
141
142                 $profile->nickname = $user->nickname;
143                 $profile->fullname = $fullname;
144                 $profile->homepage = $homepage;
145                 $profile->bio = $bio;
146                 $profile->location = $location;
147                 $profile->profileurl = common_profile_url($nickname);
148
149                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
150                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
151
152                 $result = $profile->update($orig_profile);
153
154                 if (!$result) {
155                         common_log_db_error($profile, 'UPDATE', __FILE__);
156                         common_server_error(_('Couldn\'t save profile.'));
157                         return;
158                 }
159
160                 $user->query('COMMIT');
161
162                 common_broadcast_profile($profile);
163
164                 $this->show_form(_('Settings saved.'), TRUE);
165         }
166
167         function nickname_exists($nickname) {
168                 $user = common_current_user();
169                 $other = User::staticGet('nickname', $nickname);
170                 if (!$other) {
171                         return false;
172                 } else {
173                         return $other->id != $user->id;
174                 }
175         }
176 }