]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
49e24874871e30c20a2d0254cc8b24eacec7d092
[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 _t('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(_t('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', _t('Nickname'),
42                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
43                                          _t('1-64 lowercase letters or numbers, no punctuation or spaces'));
44                 common_input('fullname', _t('Full name'),
45                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
46                 common_input('email', _t('Email address'),
47                                          ($this->arg('email')) ? $this->arg('email') : $user->email,
48                                          _t('Used only for updates, announcements, and password recovery'));
49                 common_input('homepage', _t('Homepage'),
50                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
51                                          _t('URL of your homepage, blog, or profile on another site'));
52                 common_textarea('bio', _t('Bio'),
53                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
54                                                 _t('Describe yourself and your interests in 140 chars'));
55                 common_input('location', _t('Location'),
56                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
57                                          _t('Where you are, like "City, State (or Region), Country"'));
58                 common_submit('submit', _t('Save'));
59                 common_element_end('form');
60                 common_show_footer();
61         }
62
63         function handle_post() {
64
65                 $nickname = $this->trimmed('nickname');
66                 $fullname = $this->trimmed('fullname');
67                 $email = $this->trimmed('email');
68                 $homepage = $this->trimmed('homepage');
69                 $bio = $this->trimmed('bio');
70                 $location = $this->trimmed('location');
71
72                 # Some validation
73
74                 if ($email && !Validate::email($email, true)) {
75                         $this->show_form(_t('Not a valid email address.'));
76                         return;
77                 } else if (!Validate::string($nickname, array('min_length' => 1,
78                                                                                                           'max_length' => 64,
79                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
80                         $this->show_form(_t('Nickname must have only letters and numbers and no spaces.'));
81                         return;
82                 } else if (!User::allowed_nickname($nickname)) {
83                         $this->show_form(_t('Not a valid nickname.'));
84                         return;
85                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
86                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
87                         $this->show_form(_t('Homepage is not a valid URL.'));
88                         return;
89                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
90                         $this->show_form(_t('Fullname is too long (max 255 chars).'));
91                         return;
92                 } else if (!is_null($bio) && strlen($bio) > 140) {
93                         $this->show_form(_t('Bio is too long (max 140 chars).'));
94                         return;
95                 } else if (!is_null($location) && strlen($location) > 255) {
96                         $this->show_form(_t('Location is too long (max 255 chars).'));
97                         return;
98                 } else if ($this->nickname_exists($nickname)) {
99                         $this->show_form(_t('Nickname already exists.'));
100                         return;
101                 } else if ($this->email_exists($email)) {
102                         $this->show_form(_t('Email address already exists.'));
103                         return;
104                 }
105
106                 $user = common_current_user();
107
108                 $user->query('BEGIN');
109
110                 if ($user->nickname != $nickname) {
111
112                         common_debug('Updating user nickname from ' . $user->nickname . ' to ' . $nickname,
113                                                  __FILE__);
114
115                         $original = clone($user);
116
117                         $user->nickname = $nickname;
118
119                         $result = $user->updateKeys($original);
120
121                         if ($result === FALSE) {
122                                 common_log_db_error($user, 'UPDATE', __FILE__);
123                                 common_server_error(_t('Couldnt update user.'));
124                                 return;
125                         }
126                 }
127
128                 if ($user->email != $email) {
129
130                         common_debug('Updating user email from ' . $user->email . ' to ' . $email,
131                                                  __FILE__);
132
133                         # We don't update email directly; it gets done by confirmemail
134
135                         $confirm = new Confirm_address();
136
137                         $confirm->code = common_confirmation_code(128);
138                         $confirm->user_id = $user->id;
139                         $confirm->address = $email;
140                         $confirm->address_type = 'email';
141
142                         $result = $confirm->insert();
143
144                         if (!$result) {
145                                 common_log_db_error($confirm, 'INSERT', __FILE__);
146                                 common_server_error(_t('Couldnt confirm email.'));
147                                 return FALSE;
148                         }
149
150                         # XXX: try not to do this in the middle of a transaction
151
152                         mail_confirm_address($confirm->code,
153                                                                  $profile->nickname,
154                                                                  $email);
155                 }
156
157                 $profile = $user->getProfile();
158
159                 $orig_profile = clone($profile);
160
161                 $profile->nickname = $user->nickname;
162                 $profile->fullname = $fullname;
163                 $profile->homepage = $homepage;
164                 $profile->bio = $bio;
165                 $profile->location = $location;
166                 $profile->profileurl = common_profile_url($nickname);
167
168                 common_debug('Old profile: ' . common_log_objstring($orig_profile), __FILE__);
169                 common_debug('New profile: ' . common_log_objstring($profile), __FILE__);
170
171                 $result = $profile->update($orig_profile);
172
173                 if (!$result) {
174                         common_log_db_error($profile, 'UPDATE', __FILE__);
175                         common_server_error(_t('Couldnt save profile.'));
176                         return;
177                 }
178
179                 $user->query('COMMIT');
180
181                 common_broadcast_profile($profile);
182
183                 $this->show_form(_t('Settings saved.'), TRUE);
184         }
185
186         function nickname_exists($nickname) {
187                 $user = common_current_user();
188                 $other = User::staticGet('nickname', $nickname);
189                 if (!$other) {
190                         return false;
191                 } else {
192                         return $other->id != $user->id;
193                 }
194         }
195
196         function email_exists($email) {
197                 $user = common_current_user();
198                 $other = User::staticGet('email', $email);
199                 if (!$other) {
200                         return false;
201                 } else {
202                         return $other->id != $user->id;
203                 }
204         }
205 }