]> git.mxchange.org Git - friendica.git/blob - mod/settings.php
prevent admin hijacks
[friendica.git] / mod / settings.php
1 <?php
2
3
4 function settings_init(&$a) {
5         if(local_user()) {
6                 profile_load($a,$a->user['nickname']);
7         }
8 }
9
10
11 function settings_post(&$a) {
12
13         if(! local_user()) {
14                 notice( t('Permission denied.') . EOL);
15                 return;
16         }
17
18         call_hooks('settings_post', $_POST);
19
20         if(($a->argc > 1) && ($a->argv[1] == 'addon'))
21                 return;
22
23         if(count($a->user) && x($a->user,'uid') && $a->user['uid'] != local_user()) {
24                 notice( t('Permission denied.') . EOL);
25                 return;
26         }
27         if((x($_POST,'npassword')) || (x($_POST,'confirm'))) {
28
29                 $newpass = $_POST['npassword'];
30                 $confirm = $_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(local_user())
48                         );
49                         if($r)
50                                 notice( t('Password changed.') . EOL);
51                         else
52                                 notice( t('Password update failed. Please try again.') . EOL);
53                 }
54         }
55
56         $theme            = ((x($_POST,'theme'))      ? notags(trim($_POST['theme']))        : '');
57         $username         = ((x($_POST,'username'))   ? notags(trim($_POST['username']))     : '');
58         $email            = ((x($_POST,'email'))      ? notags(trim($_POST['email']))        : '');
59         $timezone         = ((x($_POST,'timezone'))   ? notags(trim($_POST['timezone']))     : '');
60         $defloc           = ((x($_POST,'defloc'))     ? notags(trim($_POST['defloc']))       : '');
61         $openid           = ((x($_POST,'openid_url')) ? notags(trim($_POST['openid_url']))   : '');
62         $maxreq           = ((x($_POST,'maxreq'))     ? intval($_POST['maxreq'])             : 0);
63
64         $allow_location   = (((x($_POST,'allow_location')) && (intval($_POST['allow_location']) == 1)) ? 1: 0);
65         $publish          = (((x($_POST,'profile_in_directory')) && (intval($_POST['profile_in_directory']) == 1)) ? 1: 0);
66         $net_publish      = (((x($_POST,'profile_in_netdirectory')) && (intval($_POST['profile_in_netdirectory']) == 1)) ? 1: 0);
67         $old_visibility   = (((x($_POST,'visibility')) && (intval($_POST['visibility']) == 1)) ? 1 : 0);
68         $page_flags       = (((x($_POST,'page-flags')) && (intval($_POST['page-flags']))) ? intval($_POST['page-flags']) : 0);
69
70         $notify = 0;
71
72         if(x($_POST,'notify1'))
73                 $notify += intval($_POST['notify1']);
74         if(x($_POST,'notify2'))
75                 $notify += intval($_POST['notify2']);
76         if(x($_POST,'notify3'))
77                 $notify += intval($_POST['notify3']);
78         if(x($_POST,'notify4'))
79                 $notify += intval($_POST['notify4']);
80         if(x($_POST,'notify5'))
81                 $notify += intval($_POST['notify5']);
82
83         $email_changed = false;
84
85         $err = '';
86
87         if($username != $a->user['username']) {
88                 if(strlen($username) > 40)
89                         $err .= t(' Please use a shorter name.');
90                 if(strlen($username) < 3)
91                         $err .= t(' Name too short.');
92         }
93
94         if($email != $a->user['email']) {
95                 $email_changed = true;
96         if(! valid_email($email))
97                         $err .= t(' Not valid email.');
98                 if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0)) {
99                         $err .= t(' Cannot change to that email.');
100                         $email = $a->user['email'];
101                 }
102         }
103
104         if(strlen($err)) {
105                 notice($err . EOL);
106                 return;
107         }
108
109         if($timezone != $a->user['timezone']) {
110                 if(strlen($timezone))
111                         date_default_timezone_set($timezone);
112         }
113
114         $str_group_allow   = perms2str($_POST['group_allow']);
115         $str_contact_allow = perms2str($_POST['contact_allow']);
116         $str_group_deny    = perms2str($_POST['group_deny']);
117         $str_contact_deny  = perms2str($_POST['contact_deny']);
118
119         $openidserver = $a->user['openidserver'];
120
121         // If openid has changed or if there's an openid but no openidserver, try and discover it.
122
123         if($openid != $a->user['openid'] || (strlen($openid) && (! strlen($openidserver)))) {
124                 $tmp_str = $openid;
125                 if(strlen($tmp_str) && validate_url($tmp_str)) {
126                         logger('updating openidserver');
127                         require_once('library/openid.php');
128                         $open_id_obj = new LightOpenID;
129                         $open_id_obj->identity = $openid;
130                         $openidserver = $open_id_obj->discover($open_id_obj->identity);
131                 }
132                 else
133                         $openidserver = '';
134         }
135
136         $r = q("UPDATE `user` SET `username` = '%s', `email` = '%s', `openid` = '%s', `timezone` = '%s',  `allow_cid` = '%s', `allow_gid` = '%s', `deny_cid` = '%s', `deny_gid` = '%s', `notify-flags` = %d, `page-flags` = %d, `default-location` = '%s', `allow_location` = %d, `theme` = '%s', `maxreq` = %d, `openidserver` = '%s'  WHERE `uid` = %d LIMIT 1",
137                         dbesc($username),
138                         dbesc($email),
139                         dbesc($openid),
140                         dbesc($timezone),
141                         dbesc($str_contact_allow),
142                         dbesc($str_group_allow),
143                         dbesc($str_contact_deny),
144                         dbesc($str_group_deny),
145                         intval($notify),
146                         intval($page_flags),
147                         dbesc($defloc),
148                         intval($allow_location),
149                         dbesc($theme),
150                         intval($maxreq),
151                         dbesc($openidserver),
152                         intval(local_user())
153         );
154         if($r)
155                 notice( t('Settings updated.') . EOL);
156
157         $r = q("UPDATE `profile` 
158                 SET `publish` = %d, `net-publish` = %d
159                 WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
160                 intval($publish),
161                 intval($net_publish),
162                 intval(local_user())
163         );
164
165         if($old_visibility != $net_publish) {
166                 // Update global directory in background
167                 $php_path = ((strlen($a->config['php_path'])) ? $a->config['php_path'] : 'php');
168                 $url = $_SESSION['my_url'];
169                 if($url && strlen(get_config('system','directory_submit_url')))
170                         proc_close(proc_open("\"$php_path\" \"include/directory.php\" \"$url\" &",
171                                 array(),$foo));
172         }
173
174         $_SESSION['theme'] = $theme;
175         if($email_changed && $a->config['register_policy'] == REGISTER_VERIFY) {
176
177                 // FIXME - set to un-verified, blocked and redirect to logout
178
179         }
180
181         goaway($a->get_baseurl() . '/settings' );
182         return; // NOTREACHED
183 }
184                 
185
186 if(! function_exists('settings_content')) {
187 function settings_content(&$a) {
188
189         $o = '';
190         $o .= '<script> $(document).ready(function() { $(\'#nav-settings-link\').addClass(\'nav-selected\'); });</script>';
191
192         if(! local_user()) {
193                 notice( t('Permission denied.') . EOL );
194                 return;
195         }
196
197         if(($a->argc > 1) && ($a->argv[1] === 'addon')) {
198                 $o .= '<h1>' . t('Plugin Settings') . '</h1>';
199                 $o .= '<div id="account-settings-link"><a href="settings">' . t('Account Settings') . '</a></div>';
200
201                 $o .= '<form action="settings/addon" method="post" >';
202
203                 $r = q("SELECT * FROM `hook` WHERE `hook` = 'plugin_settings' ");
204                 if(! count($r))
205                         notice( t('No Plugin settings configured') . EOL);
206
207                 call_hooks('plugin_settings', $o);
208                 $o .= '</form>';
209                 return $o;
210         }
211                 
212         require_once('include/acl_selectors.php');
213
214         $p = q("SELECT * FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
215                 intval(local_user())
216         );
217         if(count($p))
218                 $profile = $p[0];
219
220         $username = $a->user['username'];
221         $email    = $a->user['email'];
222         $nickname = $a->user['nickname'];
223         $timezone = $a->user['timezone'];
224         $notify   = $a->user['notify-flags'];
225         $defloc   = $a->user['default-location'];
226         $openid   = $a->user['openid'];
227         $maxreq   = $a->user['maxreq'];
228
229         if(! strlen($a->user['timezone']))
230                 $timezone = date_default_timezone_get();
231
232         $pageset_tpl = load_view_file('view/pagetypes.tpl');
233         $pagetype = replace_macros($pageset_tpl,array(
234                 '$normal'         => (($a->user['page-flags'] == PAGE_NORMAL)      ? " checked=\"checked\" " : ""),
235                 '$soapbox'        => (($a->user['page-flags'] == PAGE_SOAPBOX)     ? " checked=\"checked\" " : ""),
236                 '$community'      => (($a->user['page-flags'] == PAGE_COMMUNITY)   ? " checked=\"checked\" " : ""),
237                 '$freelove'       => (($a->user['page-flags'] == PAGE_FREELOVE)    ? " checked=\"checked\" " : ""),
238                 '$page_normal'    => PAGE_NORMAL,
239                 '$page_soapbox'   => PAGE_SOAPBOX,
240                 '$page_community' => PAGE_COMMUNITY,
241                 '$page_freelove'  => PAGE_FREELOVE
242         ));
243
244         $noid = get_config('system','no_openid');
245
246         if($noid) {
247                 $oidhtml = '';
248         }
249         else {
250                 $oidhtml = '<label id="settings-openid-label" for="settings-openid" >' . t('OpenID: ') . '</label><input type="text" id="settings-openid" class="openid" name="openid_url" value="$openid" />' . t("&nbsp;\x28Optional\x29 Allow this OpenID to login to this account.");
251         }
252
253
254         if(get_config('system','publish_all')) {
255                 $profile_in_dir = '<input type="hidden" name="profile_in_directory" value="1" />';
256         }
257         else {
258                 $opt_tpl = load_view_file("view/profile-in-directory.tpl");
259                 $profile_in_dir = replace_macros($opt_tpl,array(
260                         '$yes_selected' => (($profile['publish'])      ? " checked=\"checked\" " : ""),
261                         '$no_selected'  => (($profile['publish'] == 0) ? " checked=\"checked\" " : "")
262                 ));
263         }
264
265         if(strlen(get_config('system','directory_submit_url'))) {
266                 $opt_tpl = load_view_file("view/profile-in-netdir.tpl");
267
268                 $profile_in_net_dir = replace_macros($opt_tpl,array(
269                         '$yes_selected' => (($profile['net-publish'])      ? " checked=\"checked\" " : ""),
270                         '$no_selected'  => (($profile['net-publish'] == 0) ? " checked=\"checked\" " : "")
271                 ));
272         }
273         else
274                 $profile_in_net_dir = '';
275
276         $loc_checked = (($a->user['allow_location'] == 1)      ? " checked=\"checked\" " : "");
277
278         $invisible = (((! $profile['publish']) && (! $profile['net-publish']))
279                 ? true : false);
280
281         if($invisible)
282                 notice( t('Profile is <strong>not published</strong>.') . EOL );
283
284         $nickname_block = load_view_file("view/settings_nick_set.tpl");
285         
286         $nickname_subdir = '';
287         if(strlen($a->get_path())) {
288                 $subdir_tpl = load_view_file('view/settings_nick_subdir.tpl');
289                 $nickname_subdir = replace_macros($subdir_tpl, array(
290                         '$baseurl' => $a->get_baseurl(),
291                         '$nickname' => $nickname,
292                         '$hostname' => $a->get_hostname()
293                 ));
294         }
295
296         $theme_selector = '<select name="theme" id="theme-select" >';
297         $files = glob('view/theme/*');
298
299         $default_theme = get_config('system','theme');
300         if(! $default_theme)
301                 $default_theme = 'default';
302
303         if($files) {
304                 foreach($files as $file) {
305                         $f = basename($file);
306                         $selected = (($f == $_SESSION['theme']) || ($f === $default_theme && (! x($_SESSION,'theme')))
307                                 ? ' selected="selected" ' : '' );
308                         $theme_selector .= '<option val="' . basename($file) . '"' . $selected . '>' . basename($file) . '</option>';
309                 }
310         }
311         $theme_selector .= '</select>';
312
313
314         $nickname_block = replace_macros($nickname_block,array(
315                 '$nickname' => $nickname,
316                 '$uid' => local_user(),
317                 '$subdir' => $nickname_subdir,
318                 '$basepath' => $a->get_hostname(),
319                 '$baseurl' => $a->get_baseurl()));      
320
321         $stpl = load_view_file('view/settings.tpl');
322
323         $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
324
325         $o .= replace_macros($stpl,array(
326                 '$baseurl' => $a->get_baseurl(),
327                 '$oidhtml' => $oidhtml,
328                 '$uid' => local_user(),
329                 '$username' => $username,
330                 '$openid' => $openid,
331                 '$email' => $email,
332                 '$nickname_block' => $nickname_block,
333                 '$timezone' => $timezone,
334                 '$zoneselect' => select_timezone($timezone),
335                 '$defloc' => $defloc,
336                 '$loc_checked' => $loc_checked,
337                 '$profile_in_dir' => $profile_in_dir,
338                 '$profile_in_net_dir' => $profile_in_net_dir,
339                 '$permissions' => t('Default Post Permissions'),
340                 '$visibility' => $profile['net-publish'],
341                 '$aclselect' => populate_acl($a->user,$celeb),
342                 '$sel_notify1' => (($notify & NOTIFY_INTRO)   ? ' checked="checked" ' : ''),
343                 '$sel_notify2' => (($notify & NOTIFY_CONFIRM) ? ' checked="checked" ' : ''),
344                 '$sel_notify3' => (($notify & NOTIFY_WALL)    ? ' checked="checked" ' : ''),
345                 '$sel_notify4' => (($notify & NOTIFY_COMMENT) ? ' checked="checked" ' : ''),
346                 '$sel_notify5' => (($notify & NOTIFY_MAIL)    ? ' checked="checked" ' : ''),
347                 '$maxreq' => $maxreq,
348                 '$theme' => $theme_selector,
349                 '$pagetype' => $pagetype
350         ));
351
352         call_hooks('settings_form',$o);
353
354         $o .= '</form>' . "\r\n";
355
356         return $o;
357
358 }}
359