]> git.mxchange.org Git - friendica.git/blob - mod/settings.php
0cac6e0de706160921e1e2e549117b856f82cf4f
[friendica.git] / mod / settings.php
1 <?php
2
3
4 function settings_init(&$a) {
5
6         if(! local_user()) {
7                 notice("Permission denied." . EOL);
8                 $a->error = 404;
9                 return;
10         }
11         require_once("mod/profile.php");
12         profile_load($a,$a->user['nickname']);
13 }
14
15
16 function settings_post(&$a) {
17
18
19         if(! local_user()) {
20                 notice( t('Permission denied.') . EOL);
21                 return;
22         }
23         if(count($a->user) && x($a->user,'uid') && $a->user['uid'] != $_SESSION['uid']) {
24                 notice( t('Permission denied.') . EOL);
25                 return;
26         }
27         if((x($_POST,'password')) || (x($_POST,'confirm'))) {
28
29                 $newpass = trim($_POST['password']);
30                 $confirm = trim($_POST['confirm']);
31
32                 $err = false;
33                 if($newpass != $confirm ) {
34                         notice( t('Passwords do not match. Password unchanged.') . EOL);
35                         $err = true;
36                 }
37
38                 if((! x($newpass)) || (! x($confirm))) {
39                         notice( t('Empty passwords are not allowed. Password unchanged.') . EOL);
40                         $err = true;
41                 }
42
43                 if(! $err) {
44                         $password = hash('whirlpool',$newpass);
45                         $r = q("UPDATE `user` SET `password` = '%s' WHERE `uid` = %d LIMIT 1",
46                                 dbesc($password),
47                                 intval($_SESSION['uid']));
48                         if($r)
49                                 notice( t('Password changed.') . EOL);
50                         else
51                                 notice( t('Password update failed. Please try again.') . EOL);
52                 }
53         }
54
55         $theme = notags(trim($_POST['theme']));
56         $username = notags(trim($_POST['username']));
57         $email = notags(trim($_POST['email']));
58         $timezone = notags(trim($_POST['timezone']));
59
60         $publish = (($_POST['profile_in_directory'] == 1) ? 1: 0);
61         $net_publish = (($_POST['profile_in_netdirectory'] == 1) ? 1: 0);
62         $old_visibility = ((intval($_POST['visibility']) == 1) ? 1 : 0);
63
64         $notify = 0;
65
66         if($_POST['notify1'])
67                 $notify += intval($_POST['notify1']);
68         if($_POST['notify2'])
69                 $notify += intval($_POST['notify2']);
70         if($_POST['notify3'])
71                 $notify += intval($_POST['notify3']);
72         if($_POST['notify4'])
73                 $notify += intval($_POST['notify4']);
74         if($_POST['notify5'])
75                 $notify += intval($_POST['notify5']);
76
77         $username_changed = false;
78         $email_changed = false;
79         $zone_changed = false;
80         $err = '';
81
82         if($username != $a->user['username']) {
83                 $username_changed = true;
84                 if(strlen($username) > 40)
85                         $err .= t(' Please use a shorter name.');
86                 if(strlen($username) < 3)
87                         $err .= t(' Name too short.');
88         }
89         if($email != $a->user['email']) {
90                 $email_changed = true;
91                 if(!eregi('[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\.[A-Za-z]{2,6}',$email))
92                         $err .= t(' Not valid email.');
93                 $r = q("SELECT `uid` FROM `user`
94                         WHERE `email` = '%s' LIMIT 1",
95                         dbesc($email)
96                         );
97                 if($r !== NULL && count($r))
98                         $err .= t(' This email address is already registered.');
99         }
100
101         if(strlen($err)) {
102                 notice($err . EOL);
103                 return;
104         }
105         if($timezone != $a->user['timezone']) {
106                 $zone_changed = true;
107                 if(strlen($timezone))
108                         date_default_timezone_set($timezone);
109         }
110
111         $str_group_allow = '';
112         $group_allow = $_POST['group_allow'];
113         if(is_array($group_allow)) {
114                 array_walk($group_allow,'sanitise_acl');
115                 $str_group_allow = implode('',$group_allow);
116         }
117
118         $str_contact_allow = '';
119         $contact_allow = $_POST['contact_allow'];
120         if(is_array($contact_allow)) {
121                 array_walk($contact_allow,'sanitise_acl');
122                 $str_contact_allow = implode('',$contact_allow);
123         }
124
125         $str_group_deny = '';
126         $group_deny = $_POST['group_deny'];
127         if(is_array($group_deny)) {
128                 array_walk($group_deny,'sanitise_acl');
129                 $str_group_deny = implode('',$group_deny);
130         }
131
132         $str_contact_deny = '';
133         $contact_deny = $_POST['contact_deny'];
134         if(is_array($contact_deny)) {
135                 array_walk($contact_deny,'sanitise_acl');
136                 $str_contact_deny = implode('',$contact_deny);
137         }
138
139         $r = q("UPDATE `user` SET `username` = '%s', `email` = '%s', `timezone` = '%s',  `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `notify-flags` = %d, `theme` = '%s'  WHERE `uid` = %d LIMIT 1",
140                         dbesc($username),
141                         dbesc($email),
142                         dbesc($timezone),
143                         dbesc($str_contact_allow),
144                         dbesc($str_group_allow),
145                         dbesc($str_contact_deny),
146                         dbesc($str_group_deny),
147                         intval($notify),
148                         dbesc($theme),
149                         intval($_SESSION['uid'])
150         );
151         if($r)
152                 notice( t('Settings updated.') . EOL);
153
154         $r = q("UPDATE `profile` 
155                 SET `publish` = %d, `net-publish` = %d
156                 WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
157                 intval($publish),
158                 intval($net_publish),
159                 intval($_SESSION['uid'])
160         );
161
162         if($old_visibility != $net_publish) {
163                 // Update global directory in background
164                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
165                 $url = $_SESSION['my_url'];
166                 if($url && strlen(get_config('system','directory_submit_url')))
167                         proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",
168                                 array(),$foo));
169         }
170
171         $_SESSION['theme'] = $theme;
172         if($email_changed && $a->config['register_policy'] == REGISTER_VERIFY) {
173
174                 // FIXME - set to un-verified, blocked and redirect to logout
175
176         }
177
178         goaway($a->get_baseurl() . '/settings' );
179 }
180                 
181
182 if(! function_exists('settings_content')) {
183 function settings_content(&$a) {
184
185         if(! local_user()) {
186                 notice( t('Permission denied.') . EOL );
187                 return;
188         }
189
190         require_once('view/acl_selectors.php');
191
192         $p = q("SELECT * FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
193                 intval($_SESSION['uid'])
194         );
195         if(count($p))
196                 $profile = $p[0];
197
198         $username = $a->user['username'];
199         $email    = $a->user['email'];
200         $nickname = $a->user['nickname'];
201         $timezone = $a->user['timezone'];
202         $notify   = $a->user['notify-flags'];
203
204         if(! strlen($a->user['timezone']))
205                 $timezone = date_default_timezone_get();
206
207
208         $opt_tpl = file_get_contents("view/profile-in-directory.tpl");
209         $profile_in_dir = replace_macros($opt_tpl,array(
210                 '$yes_selected' => (($profile['publish']) ? " checked=\"checked\" " : ""),
211                 '$no_selected' => (($profile['publish'] == 0) ? " checked=\"checked\" " : "")
212         ));
213
214         if(strlen(get_config('system','directory_submit_url'))) {
215                 $opt_tpl = file_get_contents("view/profile-in-netdir.tpl");
216
217                 $profile_in_net_dir = replace_macros($opt_tpl,array(
218                         '$yes_selected' => (($profile['net-publish']) ? " checked=\"checked\" " : ""),
219                         '$no_selected' => (($profile['net-publish'] == 0) ? " checked=\"checked\" " : "")
220                 ));
221         }
222         else
223                 $profile_in_net_dir = '';
224
225
226
227
228
229         $nickname_block = file_get_contents("view/settings_nick_set.tpl");
230         
231
232         $nickname_subdir = '';
233         if(strlen($a->get_path())) {
234                 $subdir_tpl = file_get_contents('view/settings_nick_subdir.tpl');
235                 $nickname_subdir = replace_macros($subdir_tpl, array(
236                         '$baseurl' => $a->get_baseurl(),
237                         '$nickname' => $nickname,
238                         '$hostname' => $a->get_hostname()
239                 ));
240         }
241
242         $theme_selector = '<select name="theme" id="theme-select" >';
243         $files = glob('view/theme/*');
244         if($files) {
245                 foreach($files as $file) {
246                         $f = basename($file);
247                         $selected = (($f == $_SESSION['theme']) || ($f == 'default' && (! x($_SESSION,'theme')))
248                                 ? ' selected="selected" ' : '' );
249                         $theme_selector .= '<option val="' . basename($file) . '"' . $selected . '>' . basename($file) . '</option>';
250                 }
251         }
252         $theme_selector .= '</select>';
253
254
255         $nickname_block = replace_macros($nickname_block,array(
256                 '$nickname' => $nickname,
257                 '$uid' => $_SESSION['uid'],
258                 '$subdir' => $nickname_subdir,
259                 '$basepath' => $a->get_hostname(),
260                 '$baseurl' => $a->get_baseurl()));      
261
262         $o = file_get_contents('view/settings.tpl');
263
264         $o = replace_macros($o,array(
265                 '$baseurl' => $a->get_baseurl(),
266                 '$uid' => $_SESSION['uid'],
267                 '$username' => $username,
268                 '$email' => $email,
269                 '$nickname_block' => $nickname_block,
270                 '$timezone' => $timezone,
271                 '$zoneselect' => select_timezone($timezone),
272                 '$profile_in_dir' => $profile_in_dir,
273                 '$profile_in_net_dir' => $profile_in_net_dir,
274                 '$permissions' => t('Default Post Permissions'),
275                 '$visibility' => $profile['net-publish'],
276                 '$aclselect' => populate_acl($a->user),
277                 '$sel_notify1' => (($notify & NOTIFY_INTRO)   ? ' checked="checked" ' : ''),
278                 '$sel_notify2' => (($notify & NOTIFY_CONFIRM) ? ' checked="checked" ' : ''),
279                 '$sel_notify3' => (($notify & NOTIFY_WALL)    ? ' checked="checked" ' : ''),
280                 '$sel_notify4' => (($notify & NOTIFY_COMMENT) ? ' checked="checked" ' : ''),
281                 '$sel_notify5' => (($notify & NOTIFY_MAIL)    ? ' checked="checked" ' : ''),
282                 '$theme' => $theme_selector
283         ));
284
285         return $o;
286
287 }}