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