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