]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
bca86a85f314fe93b4d0ff16bda67689fe5d3959
[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 show_top($arr) {
27                 $msg = $arr[0];
28                 $success = $arr[1];
29                 if ($msg) {
30                         $this->message($msg, $success);
31                 } else {
32                         common_element('div', 'instructions',
33                                                    _t('You can update your personal profile info here '.
34                                                           'so people know more about you.'));
35                 }
36                 $this->settings_menu();
37         }
38         
39         function show_form($msg=NULL, $success=false) {
40                 $user = common_current_user();
41                 $profile = $user->getProfile();
42                 common_show_header(_t('Profile settings'), NULL, array($msg, $success),
43                                                    array($this, 'show_top'));
44
45                 common_element_start('form', array('method' => 'POST',
46                                                                                    'id' => 'profilesettings',
47                                                                                    'action' =>
48                                                                                    common_local_url('profilesettings')));
49                 # too much common patterns here... abstractable?
50                 common_input('nickname', _t('Nickname'),
51                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname,
52                                          _t('1-64 lowercase letters or numbers, no punctuation or spaces'));
53                 common_input('fullname', _t('Full name'),
54                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
55                 common_input('email', _t('Email address'),
56                                          ($this->arg('email')) ? $this->arg('email') : $user->email,
57                                          _t('Used only for updates, announcements, and password recovery'));
58                 common_input('homepage', _t('Homepage'),
59                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage,
60                                          _t('URL of your homepage, blog, or profile on another site'));
61                 common_textarea('bio', _t('Bio'),
62                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio,
63                                                 _t('Describe yourself and your interests in 140 chars'));
64                 common_input('location', _t('Location'),
65                                          ($this->arg('location')) ? $this->arg('location') : $profile->location,
66                                          _t('Where you are, like "City, State (or Region), Country"'));
67                 common_submit('submit', _t('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                 $email = $this->trimmed('email');
77                 $homepage = $this->trimmed('homepage');
78                 $bio = $this->trimmed('bio');
79                 $location = $this->trimmed('location');
80
81                 # Some validation
82                 
83                 if (!Validate::email($email, true)) {
84                         $this->show_form(_t('Not a valid email address.'));
85                         return;
86                 } else if (!Validate::string($nickname, array('min_length' => 1,
87                                                                                                           'max_length' => 64,
88                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
89                         $this->show_form(_t('Nickname must have only letters and numbers and no spaces.'));
90                         return;
91                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
92                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
93                         $this->show_form(_t('Homepage is not a valid URL.'));
94                         return;
95                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
96                         $this->show_form(_t('Fullname is too long (max 255 chars).'));
97                         return;
98                 } else if (!is_null($bio) && strlen($bio) > 140) {
99                         $this->show_form(_t('Bio is too long (max 140 chars).'));
100                         return;
101                 } else if (!is_null($location) && strlen($location) > 255) {
102                         $this->show_form(_t('Location is too long (max 255 chars).'));
103                         return;
104                 } else if ($this->nickname_exists($nickname)) {
105                         $this->show_form(_t('Nickname already exists.'));
106                         return;
107                 } else if ($this->email_exists($email)) {
108                         $this->show_form(_t('Email address already exists.'));
109                         return;
110                 }
111                 
112                 $user = common_current_user();
113                 assert(!is_null($user)); # should already be checked
114
115                 $user->query('BEGIN');
116
117                 if ($user->nickname != $nickname) {
118                         
119                         $original = clone($user);
120                         
121                         $user->nickname = $nickname;
122                         
123                         $result = $user->updateKeys($original);
124                         
125                         if (!$result) {
126                                 common_log_db_error($user, 'UPDATE', __FILE__);
127                                 common_server_error(_t('Couldnt update user.'));
128                                 return;
129                         }
130                 }
131
132                 if ($email != $user->email) {
133                         
134                         $confirm = new Confirm_email();
135                         $confirm->code = common_good_rand(16);
136                         $confirm->user_id = $user->id;
137                         $confirm->email = $email;
138                         
139                         $result = $confirm->insert();
140                         
141                         if (!$result) {
142                                 common_log_db_error($confirm, 'INSERT', __FILE__);
143                                 common_server_error(_t('Couldnt confirm email.'));
144                                 return FALSE;
145                         }
146                         # XXX: try not to do this in the middle of a transaction
147                 
148                         mail_confirm_address($confirm->code,
149                                                                  $profile->nickname,
150                                                                  $email);
151                 }
152                 
153                 $profile = $user->getProfile();
154
155                 $orig_profile = clone($profile);
156
157                 $profile->nickname = $user->nickname;
158                 $profile->fullname = $fullname;
159                 $profile->homepage = $homepage;
160                 $profile->bio = $bio;
161                 $profile->location = $location;
162                 $profile->profileurl = common_profile_url($nickname);
163
164                 $result = $profile->update($orig_profile);
165                 
166                 if (!$result) {
167                         common_log_db_error($profile, 'UPDATE', __FILE__);
168                         common_server_error(_t('Couldnt save profile.'));
169                         return;
170                 }
171
172                 $user->query('COMMIT');
173
174                 common_broadcast_profile($profile);
175
176                 $this->show_form(_t('Settings saved.'), TRUE);
177         }
178         
179         function nickname_exists($nickname) {
180                 $user = common_current_user();
181                 $other = User::staticGet('nickname', $nickname);
182                 if (!$other) {
183                         return false;
184                 } else {
185                         return $other->id != $user->id;
186                 }
187         }
188         
189         function email_exists($email) {
190                 $user = common_current_user();
191                 $other = User::staticGet('email', $email);
192                 if (!$other) {
193                         return false;
194                 } else {
195                         return $other->id != $user->id;
196                 }
197         }
198 }