]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/profilesettings.php
fix passing request around
[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_form($msg=NULL, $success=false) {
27                 $user = common_current_user();
28                 $profile = $user->getProfile();
29                 common_show_header(_t('Profile settings'));
30                 $this->settings_menu();
31                 $this->message($msg, $success);
32                 common_element_start('form', array('method' => 'POST',
33                                                                                    'id' => 'profilesettings',
34                                                                                    'action' =>
35                                                                                    common_local_url('profilesettings')));
36                 # too much common patterns here... abstractable?
37                 common_input('nickname', _t('Nickname'),
38                                          ($this->arg('nickname')) ? $this->arg('nickname') : $profile->nickname);
39                 common_input('fullname', _t('Full name'),
40                                          ($this->arg('fullname')) ? $this->arg('fullname') : $profile->fullname);
41                 common_input('email', _t('Email address'),
42                                          ($this->arg('email')) ? $this->arg('email') : $user->email);
43                 common_input('homepage', _t('Homepage'),
44                                          ($this->arg('homepage')) ? $this->arg('homepage') : $profile->homepage);
45                 common_textarea('bio', _t('Bio'),
46                                                 ($this->arg('bio')) ? $this->arg('bio') : $profile->bio);
47                 common_input('location', _t('Location'),
48                                          ($this->arg('location')) ? $this->arg('location') : $profile->location);
49                 common_submit('submit', _t('Save'));
50                 common_element_end('form');
51                 common_show_footer();
52         }
53
54         function handle_post() {
55                 
56                 $nickname = $this->trimmed('nickname');
57                 $fullname = $this->trimmed('fullname');
58                 $email = $this->trimmed('email');
59                 $homepage = $this->trimmed('homepage');
60                 $bio = $this->trimmed('bio');
61                 $location = $this->trimmed('location');
62
63                 # Some validation
64                 
65                 if (!Validate::email($email, true)) {
66                         $this->show_form(_t('Not a valid email address.'));
67                         return;
68                 } else if (!Validate::string($nickname, array('min_length' => 1,
69                                                                                                           'max_length' => 64,
70                                                                                                           'format' => VALIDATE_NUM . VALIDATE_ALPHA_LOWER))) {
71                         $this->show_form(_t('Nickname must have only letters and numbers and no spaces.'));
72                         return;
73                 } else if (!is_null($homepage) && (strlen($homepage) > 0) &&
74                                    !Validate::uri($homepage, array('allowed_schemes' => array('http', 'https')))) {
75                         $this->show_form(_t('Homepage is not a valid URL.'));
76                         return;
77                 } else if (!is_null($fullname) && strlen($fullname) > 255) {
78                         $this->show_form(_t('Fullname is too long (max 255 chars).'));
79                         return;
80                 } else if (!is_null($bio) && strlen($bio) > 140) {
81                         $this->show_form(_t('Bio is too long (max 140 chars).'));
82                         return;
83                 } else if (!is_null($location) && strlen($location) > 255) {
84                         $this->show_form(_t('Location is too long (max 255 chars).'));
85                         return;
86                 } else if ($this->nickname_exists($nickname)) {
87                         $this->show_form(_t('Nickname already exists.'));
88                         return;
89                 } else if ($this->email_exists($email)) {
90                         $this->show_form(_t('Email address already exists.'));
91                         return;
92                 }
93                 
94                 $user = common_current_user();
95                 assert(!is_null($user)); # should already be checked
96
97                 # FIXME: transaction!
98
99                 $original = clone($user);
100
101                 $user->nickname = $nickname;
102                 $user->email = $email;
103
104                 if (!$user->update($original)) {
105                         common_server_error(_t('Couldnt update user.'));
106                         return;
107                 }
108
109                 $profile = $user->getProfile();
110
111                 $orig_profile = clone($profile);
112
113                 $profile->nickname = $user->nickname;
114                 $profile->fullname = $fullname;
115                 $profile->homepage = $homepage;
116                 $profile->bio = $bio;
117                 $profile->location = $location;
118                 $profile->profileurl = common_profile_url($nickname);
119
120                 if (FALSE === $profile->update($orig_profile)) {
121                         common_server_error(_t('Couldnt save profile.'));
122                         return;
123                 }
124
125                 $this->show_form(_t('Settings saved.'), TRUE);
126         }
127         
128         function nickname_exists($nickname) {
129                 $user = common_current_user();
130                 $other = User::staticGet('nickname', $nickname);
131                 if (!$other) {
132                         return false;
133                 } else {
134                         return $other->id != $user->id;
135                 }
136         }
137         
138         function email_exists($email) {
139                 $user = common_current_user();
140                 $other = User::staticGet('email', $email);
141                 if (!$other) {
142                         return false;
143                 } else {
144                         return $other->id != $user->id;
145                 }
146         }
147 }