]> git.mxchange.org Git - friendica.git/blob - mod/settings.php
a few minor fixes to settings page
[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         $a->page['htmlhead'] .= "<script> var ispublic = '" . t('everybody') . "';" ;
10
11         $a->page['htmlhead'] .= <<< EOT
12
13         $(document).ready(function() {
14
15                 $('#contact_allow, #contact_deny, #group_allow, #group_deny').change(function() {
16                         var selstr;
17                         $('#contact_allow option:selected, #contact_deny option:selected, #group_allow option:selected, #group_deny option:selected').each( function() {
18                                 selstr = $(this).text();
19                                 $('#jot-perms-icon').removeClass('unlock').addClass('lock');
20                                 $('#jot-public').hide();
21                         });
22                         if(selstr == null) { 
23                                 $('#jot-perms-icon').removeClass('lock').addClass('unlock');
24                                 $('#jot-public').show();
25                         }
26
27                 }).trigger('change');
28
29         });
30
31         </script>
32 EOT;
33
34
35 }
36
37
38 function settings_post(&$a) {
39
40         if(! local_user()) {
41                 notice( t('Permission denied.') . EOL);
42                 return;
43         }
44
45         if(count($a->user) && x($a->user,'uid') && $a->user['uid'] != local_user()) {
46                 notice( t('Permission denied.') . EOL);
47                 return;
48         }
49
50         if(($a->argc > 1) && ($a->argv[1] == 'addon')) {
51                 call_hooks('plugin_settings_post', $_POST);
52                 return;
53         }
54
55         call_hooks('settings_post', $_POST);
56
57         if((x($_POST,'npassword')) || (x($_POST,'confirm'))) {
58
59                 $newpass = $_POST['npassword'];
60                 $confirm = $_POST['confirm'];
61
62                 $err = false;
63                 if($newpass != $confirm ) {
64                         notice( t('Passwords do not match. Password unchanged.') . EOL);
65                         $err = true;
66                 }
67
68                 if((! x($newpass)) || (! x($confirm))) {
69                         notice( t('Empty passwords are not allowed. Password unchanged.') . EOL);
70                         $err = true;
71                 }
72
73                 if(! $err) {
74                         $password = hash('whirlpool',$newpass);
75                         $r = q("UPDATE `user` SET `password` = '%s' WHERE `uid` = %d LIMIT 1",
76                                 dbesc($password),
77                                 intval(local_user())
78                         );
79                         if($r)
80                                 info( t('Password changed.') . EOL);
81                         else
82                                 notice( t('Password update failed. Please try again.') . EOL);
83                 }
84         }
85
86         $theme            = ((x($_POST,'theme'))      ? notags(trim($_POST['theme']))        : '');
87         $username         = ((x($_POST,'username'))   ? notags(trim($_POST['username']))     : '');
88         $email            = ((x($_POST,'email'))      ? notags(trim($_POST['email']))        : '');
89         $timezone         = ((x($_POST,'timezone'))   ? notags(trim($_POST['timezone']))     : '');
90         $defloc           = ((x($_POST,'defloc'))     ? notags(trim($_POST['defloc']))       : '');
91         $openid           = ((x($_POST,'openid_url')) ? notags(trim($_POST['openid_url']))   : '');
92         $maxreq           = ((x($_POST,'maxreq'))     ? intval($_POST['maxreq'])             : 0);
93         $expire           = ((x($_POST,'expire'))     ? intval($_POST['expire'])             : 0);
94
95         $allow_location   = (((x($_POST,'allow_location')) && (intval($_POST['allow_location']) == 1)) ? 1: 0);
96         $publish          = (((x($_POST,'profile_in_directory')) && (intval($_POST['profile_in_directory']) == 1)) ? 1: 0);
97         $net_publish      = (((x($_POST,'profile_in_netdirectory')) && (intval($_POST['profile_in_netdirectory']) == 1)) ? 1: 0);
98         $old_visibility   = (((x($_POST,'visibility')) && (intval($_POST['visibility']) == 1)) ? 1 : 0);
99         $page_flags       = (((x($_POST,'page-flags')) && (intval($_POST['page-flags']))) ? intval($_POST['page-flags']) : 0);
100         $blockwall        = (((x($_POST,'blockwall')) && (intval($_POST['blockwall']) == 1)) ? 0: 1); // this setting is inverted!
101
102         $hide_friends = (($_POST['hide-friends'] == 1) ? 1: 0);
103         $hidewall = (($_POST['hidewall'] == 1) ? 1: 0);
104
105
106
107
108         $mail_server      = ((x($_POST,'mail_server')) ? $_POST['mail_server'] : '');
109         $mail_port        = ((x($_POST,'mail_port')) ? $_POST['mail_port'] : '');
110         $mail_ssl         = ((x($_POST,'mail_ssl')) ? strtolower(trim($_POST['mail_ssl'])) : '');
111         $mail_user        = ((x($_POST,'mail_user')) ? $_POST['mail_user'] : '');
112         $mail_pass        = ((x($_POST,'mail_pass')) ? trim($_POST['mail_pass']) : '');
113         $mail_replyto     = ((x($_POST,'mail_replyto')) ? $_POST['mail_replyto'] : '');
114         $mail_pubmail     = ((x($_POST,'mail_pubmail')) ? $_POST['mail_pubmail'] : '');
115
116
117         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
118
119         if(! $mail_disabled) {
120                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
121                         intval(local_user())
122                 );
123                 if(! count($r)) {
124                         q("INSERT INTO `mailacct` (`uid`) VALUES (%d)",
125                                 intval(local_user())
126                         );
127                 }
128                 if(strlen($mail_pass)) {
129                         $pass = '';
130                         openssl_public_encrypt($mail_pass,$pass,$a->user['pubkey']);
131                         q("UPDATE `mailacct` SET `pass` = '%s' WHERE `uid` = %d LIMIT 1",
132                                         dbesc(bin2hex($pass)),
133                                         intval(local_user())
134                         );
135                 }
136                 $r = q("UPDATE `mailacct` SET `server` = '%s', `port` = %d, `ssltype` = '%s', `user` = '%s',
137                         `mailbox` = 'INBOX', `reply_to` = '%s', `pubmail` = %d WHERE `uid` = %d LIMIT 1",
138                         dbesc($mail_server),
139                         intval($mail_port),
140                         dbesc($mail_ssl),
141                         dbesc($mail_user),
142                         dbesc($mail_replyto),
143                         intval($mail_pubmail),
144                         intval(local_user())
145                 );
146                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
147                         intval(local_user())
148                 );
149                 if(count($r)) {
150                         $eacct = $r[0];
151                         require_once('include/email.php');
152                         $mb = construct_mailbox_name($eacct);
153                         if(strlen($eacct['server'])) {
154                                 $dcrpass = '';
155                                 openssl_private_decrypt(hex2bin($eacct['pass']),$dcrpass,$a->user['prvkey']);
156                                 $mbox = email_connect($mb,$mail_user,$dcrpass);
157                                 unset($dcrpass);
158                                 if(! $mbox)
159                                         notice( t('Failed to connect with email account using the settings provided.') . EOL);
160                         }
161                 }
162         }
163
164         $notify = 0;
165
166         if(x($_POST,'notify1'))
167                 $notify += intval($_POST['notify1']);
168         if(x($_POST,'notify2'))
169                 $notify += intval($_POST['notify2']);
170         if(x($_POST,'notify3'))
171                 $notify += intval($_POST['notify3']);
172         if(x($_POST,'notify4'))
173                 $notify += intval($_POST['notify4']);
174         if(x($_POST,'notify5'))
175                 $notify += intval($_POST['notify5']);
176
177         $email_changed = false;
178
179         $err = '';
180
181         $name_change = false;
182
183         if($username != $a->user['username']) {
184                 $name_change = true;
185                 if(strlen($username) > 40)
186                         $err .= t(' Please use a shorter name.');
187                 if(strlen($username) < 3)
188                         $err .= t(' Name too short.');
189         }
190
191         if($email != $a->user['email']) {
192                 $email_changed = true;
193         if(! valid_email($email))
194                         $err .= t(' Not valid email.');
195                 if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0)) {
196                         $err .= t(' Cannot change to that email.');
197                         $email = $a->user['email'];
198                 }
199         }
200
201         if(strlen($err)) {
202                 notice($err . EOL);
203                 return;
204         }
205
206         if($timezone != $a->user['timezone']) {
207                 if(strlen($timezone))
208                         date_default_timezone_set($timezone);
209         }
210
211         $str_group_allow   = perms2str($_POST['group_allow']);
212         $str_contact_allow = perms2str($_POST['contact_allow']);
213         $str_group_deny    = perms2str($_POST['group_deny']);
214         $str_contact_deny  = perms2str($_POST['contact_deny']);
215
216         $openidserver = $a->user['openidserver'];
217
218         // If openid has changed or if there's an openid but no openidserver, try and discover it.
219
220         if($openid != $a->user['openid'] || (strlen($openid) && (! strlen($openidserver)))) {
221                 $tmp_str = $openid;
222                 if(strlen($tmp_str) && validate_url($tmp_str)) {
223                         logger('updating openidserver');
224                         require_once('library/openid.php');
225                         $open_id_obj = new LightOpenID;
226                         $open_id_obj->identity = $openid;
227                         $openidserver = $open_id_obj->discover($open_id_obj->identity);
228                 }
229                 else
230                         $openidserver = '';
231         }
232
233         $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, `hidewall` = %d  WHERE `uid` = %d LIMIT 1",
234                         dbesc($username),
235                         dbesc($email),
236                         dbesc($openid),
237                         dbesc($timezone),
238                         dbesc($str_contact_allow),
239                         dbesc($str_group_allow),
240                         dbesc($str_contact_deny),
241                         dbesc($str_group_deny),
242                         intval($notify),
243                         intval($page_flags),
244                         dbesc($defloc),
245                         intval($allow_location),
246                         dbesc($theme),
247                         intval($maxreq),
248                         intval($expire),
249                         dbesc($openidserver),
250                         intval($blockwall),
251                         intval($hidewall),
252                         intval(local_user())
253         );
254         if($r)
255                 info( t('Settings updated.') . EOL);
256
257         $r = q("UPDATE `profile` 
258                 SET `publish` = %d, 
259                 `net-publish` = %d,
260                 `hide-friends` = %d
261                 WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
262                 intval($publish),
263                 intval($net_publish),
264                 intval($hide_friends),
265                 intval(local_user())
266         );
267
268
269         if($name_change) {
270                 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `self` = 1 LIMIT 1",
271                         dbesc($username),
272                         dbesc(datetime_convert()),
273                         intval(local_user())
274                 );
275         }               
276
277         if($old_visibility != $net_publish) {
278                 // Update global directory in background
279                 $url = $_SESSION['my_url'];
280                 if($url && strlen(get_config('system','directory_submit_url')))
281                         proc_run('php',"include/directory.php","$url");
282         }
283
284         $_SESSION['theme'] = $theme;
285         if($email_changed && $a->config['register_policy'] == REGISTER_VERIFY) {
286
287                 // FIXME - set to un-verified, blocked and redirect to logout
288
289         }
290
291         goaway($a->get_baseurl() . '/settings' );
292         return; // NOTREACHED
293 }
294                 
295
296 if(! function_exists('settings_content')) {
297 function settings_content(&$a) {
298
299         $o = '';
300         $o .= '<script> $(document).ready(function() { $(\'#nav-settings-link\').addClass(\'nav-selected\'); });</script>';
301
302         if(! local_user()) {
303                 notice( t('Permission denied.') . EOL );
304                 return;
305         }
306         
307         $tabtpl = get_markup_template("settings_tabs.tpl");
308         $tabs = replace_macros($tabtpl, array(
309                 '$account' => array( t('Account settings'), $a->get_baseurl().'/settings'),
310                 '$plugins' => array( t('Plugin settings'), $a->get_baseurl().'/settings/addon')
311         ));
312                 
313         
314
315         if(($a->argc > 1) && ($a->argv[1] === 'addon')) {
316                 $settings_addons = "";
317                 
318                 $r = q("SELECT * FROM `hook` WHERE `hook` = 'plugin_settings' ");
319                 if(! count($r))
320                         $settings_addons = t('No Plugin settings configured');
321
322                 call_hooks('plugin_settings', $settings_addons);
323                 
324                 
325                 $tpl = get_markup_template("settings_addons.tpl");
326                 $o .= replace_macros($tpl, array(
327                         '$title'        => t('Plugin Settings'),
328                         '$tabs'         => $tabs,
329                         '$settings_addons' => $settings_addons
330                 ));
331                 return $o;
332         }
333                 
334         require_once('include/acl_selectors.php');
335
336         $p = q("SELECT * FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
337                 intval(local_user())
338         );
339         if(count($p))
340                 $profile = $p[0];
341
342         $username = $a->user['username'];
343         $email    = $a->user['email'];
344         $nickname = $a->user['nickname'];
345         $timezone = $a->user['timezone'];
346         $notify   = $a->user['notify-flags'];
347         $defloc   = $a->user['default-location'];
348         $openid   = $a->user['openid'];
349         $maxreq   = $a->user['maxreq'];
350         $expire   = ((intval($a->user['expire'])) ? $a->user['expire'] : '');
351         $blockwall = $a->user['blockwall'];
352
353         if(! strlen($a->user['timezone']))
354                 $timezone = date_default_timezone_get();
355
356
357         $mail_disabled = ((function_exists('imap_open') && (! get_config('system','imap_disabled'))) ? 0 : 1);
358
359         if(! $mail_disabled) {
360                 $r = q("SELECT * FROM `mailacct` WHERE `uid` = %d LIMIT 1",
361                         local_user()
362                 );
363         }
364         else {
365                 $r = null;
366         }
367
368         $mail_server  = ((count($r)) ? $r[0]['server'] : '');
369         $mail_port    = ((count($r) && intval($r[0]['port'])) ? intval($r[0]['port']) : '');
370         $mail_ssl     = ((count($r)) ? $r[0]['ssltype'] : '');
371         $mail_user    = ((count($r)) ? $r[0]['user'] : '');
372         $mail_replyto = ((count($r)) ? $r[0]['reply_to'] : '');
373         $mail_pubmail = ((count($r)) ? $r[0]['pubmail'] : 0);
374         $mail_chk     = ((count($r)) ? $r[0]['last_check'] : '0000-00-00 00:00:00');
375
376         $pageset_tpl = get_markup_template('pagetypes.tpl');
377         $pagetype = replace_macros($pageset_tpl,array(
378                 '$page_normal'  => array('page-flags', t('Normal Account'), PAGE_NORMAL, 
379                                                                         t('This account is a normal personal profile'), 
380                                                                         ($a->user['page-flags'] == PAGE_NORMAL)),
381                                                                 
382                 '$page_soapbox'         => array('page-flags', t('Soapbox Account'), PAGE_SOAPBOX, 
383                                                                         t('Automatically approve all connection/friend requests as read-only fans'), 
384                                                                         ($a->user['page-flags'] == PAGE_SOAPBOX)),
385                                                                         
386                 '$page_community'       => array('page-flags', t('Community/Celebrity Account'), PAGE_COMMUNITY, 
387                                                                         t('Automatically approve all connection/friend requests as read-write fans'), 
388                                                                         ($a->user['page-flags'] == PAGE_COMMUNITY)),
389                                                                         
390                 '$page_freelove'        => array('page-flags', t('Automatic Friend Account'), PAGE_FREELOVE, 
391                                                                         t('Automatically approve all connection/friend requests as friends'), 
392                                                                         ($a->user['page-flags'] == PAGE_FREELOVE)),
393         ));
394
395         $noid = get_config('system','no_openid');
396
397         if($noid) {
398                 $openid_field = false;
399         }
400         else {
401                 $openid_field = array('openid_url', t('OpenID:'),$openid, t("\x28Optional\x29 Allow this OpenID to login to this account."));
402         }
403
404
405         $opt_tpl = get_markup_template("field_yesno.tpl");
406         if(get_config('system','publish_all')) {
407                 $profile_in_dir = '<input type="hidden" name="profile_in_directory" value="1" />';
408         }
409         else {
410                 $profile_in_dir = replace_macros($opt_tpl,array(
411                         '$field'        => array('profile_in_directory', t('Publish your default profile in your local site directory?'), $profile['publish'], '', array(t('No'),t('Yes'))),
412                 ));
413         }
414
415         if(strlen(get_config('system','directory_submit_url'))) {
416                 $profile_in_net_dir = replace_macros($opt_tpl,array(
417                         '$field'        => array('profile_in_netdirectory', t('Publish your default profile in the global social directory?'), $profile['net-publish'], '', array(t('No'),t('Yes'))),
418                 ));
419         }
420         else
421                 $profile_in_net_dir = '';
422
423
424         $hide_friends = replace_macros($opt_tpl,array(
425                         '$field'        => array('hide-friends', t('Hide your contact/friend list from viewers of your default profile?'), $profile['hide-friends'], '', array(t('No'),t('Yes'))),
426         ));
427
428         $hide_wall = replace_macros($opt_tpl,array(
429                         '$field'        => array('hidewall',  t('Hide profile details and all your messages from unknown viewers?'), $a->user['hidewall'], '', array(t('No'),t('Yes'))),
430
431         ));
432
433
434
435
436
437
438
439         $invisible = (((! $profile['publish']) && (! $profile['net-publish']))
440                 ? true : false);
441
442         if($invisible)
443                 info( t('Profile is <strong>not published</strong>.') . EOL );
444
445         
446         $default_theme = get_config('system','theme');
447         if(! $default_theme)
448                 $default_theme = 'default';
449         
450         $themes = array();
451         $files = glob('view/theme/*');
452         if($files) {
453                 foreach($files as $file) {
454                         $f = basename($file);
455                         $theme_name = ((file_exists($file . '/experimental')) ?  sprintf("%s - \x28Experimental\x29", $f) : $f);
456                         $themes[$f]=$theme_name;
457                 }
458         }
459         $theme_selected = (!x($_SESSION,'theme')? $default_theme : $_SESSION['theme']);
460
461
462         $subdir = ((strlen($a->get_path())) ? '<br />' . t('or') . ' ' . $a->get_baseurl() . '/profile/' . $nickname : '');
463
464         $tpl_addr = get_markup_template("settings_nick_set.tpl");
465
466         $prof_addr = replace_macros($tpl_addr,array(
467                 '$desc' => t('Your Identity Address is'),
468                 '$nickname' => $nickname,
469                 '$subdir' => $subdir,
470                 '$basepath' => $a->get_hostname()
471         ));
472
473         $stpl = get_markup_template('settings.tpl');
474
475         $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
476
477         
478
479         $o .= replace_macros($stpl,array(
480                 '$tabs'         => $tabs,
481                 '$ptitle'       => t('Account Settings'),
482
483                 '$submit'       => t('Submit'),
484                 '$baseurl' => $a->get_baseurl(),
485                 '$uid' => local_user(),
486                 
487                 '$nickname_block' => $prof_addr,
488                 '$uexport' => t('Export Personal Data'),
489                 
490                 
491                 '$h_pass'       => t('Password Settings'),
492                 '$password1'=> array('npassword', t('New Password:'), '', ''),
493                 '$password2'=> array('confirm', t('Confirm:'), '', t('Leave password fields blank unless changing')),
494                 '$openid'       => $openid_field,
495                 
496                 '$h_basic'      => t('Basic Settings'),
497                 '$username' => array('username',  t('Full Name:'), $username,''),
498                 '$email'        => array('email', t('Email Address:'), $email, ''),
499                 '$timezone' => array('timezone_select' , t('Your Timezone:'), select_timezone($timezone), ''),
500                 '$defloc'       => array('defloc', t('Default Post Location:'), $defloc, ''),
501                 '$allowloc' => array('allow_location', t('Use Browser Location:'), ($a->user['allow_location'] == 1), ''),
502                 '$theme'        => array('theme', t('Display Theme:'), $theme_selected, '', $themes),
503
504
505
506                 '$h_prv'        => t('Security and Privacy Settings'),
507
508                 '$maxreq'       => array('maxreq', t('Maximum Friend Requests/Day:'), $maxreq ,t("\x28to prevent spam abuse\x29")),
509                 '$permissions' => t('Default Post Permissions'),
510                 '$permdesc' => t("\x28click to open/close\x29"),
511                 '$visibility' => $profile['net-publish'],
512                 '$aclselect' => populate_acl($a->user,$celeb),
513
514                 '$blockwall'=> array('blockwall', t('Allow friends to post to your profile page:'), !$blockwall, ''),
515                 '$expire'       => array('expire', t("Automatically expire posts after days:"), $expire, t('If empty, posts will not expire. Expired posts will be deleted')),
516
517                 '$profile_in_dir' => $profile_in_dir,
518                 '$profile_in_net_dir' => $profile_in_net_dir,
519                 '$hide_friends' => $hide_friends,
520                 '$hide_wall' => $hide_wall,
521                 
522                 
523                 
524                 '$h_not'        => t('Notification Settings'),
525                 '$lbl_not'      => t('Send a notification email when:'),
526                 '$notify1'      => array('notify1', t('You receive an introduction'), ($notify & NOTIFY_INTRO), ''),
527                 '$notify2'      => array('notify1', t('Your introductions are confirmed'), ($notify & NOTIFY_CONFIRM), ''),
528                 '$notify3'      => array('notify1', t('Someone writes on your profile wall'), ($notify & NOTIFY_WALL), ''),
529                 '$notify4'      => array('notify1', t('Someone writes a followup comment'), ($notify & NOTIFY_COMMENT), ''),
530                 '$notify5'      => array('notify1', t('You receive a private message'), ($notify & NOTIFY_MAIL), ''),
531                 
532                 
533                 
534                 '$h_imap' => t('Email/Mailbox Setup'),
535                 '$imap_desc' => t("If you wish to communicate with email contacts using this service \x28optional\x29, please specify how to connect to your mailbox."),
536                 '$imap_lastcheck' => array('imap_lastcheck', t('Last successful email check:'), $imap_last_check,''),
537                 '$mail_disabled' => (($mail_disabled) ? t('Email access is disabled on this site.') : ''),
538                 '$mail_server'  => array('mail_server',  t('IMAP server name:'), $mail_server, ''),
539                 '$mail_port'    => array('mail_port',    t('IMAP port:'), $mail_port, ''),
540                 '$mail_ssl'             => array('mail_ssl',     t('Security:'), strtoupper($mail_ssl), '', array( ''=>t('None'), 'TSL'=>'TSL', 'SSL'=>'SSL')),
541                 '$mail_user'    => array('mail_user',    t('Email login name:'), $mail_user, ''),
542                 '$mail_pass'    => array('mail_pass',    t('Email password:'), '', ''),
543                 '$mail_replyto' => array('mail_replyto', t('Reply-to address:'), '', 'Optional'),
544                 '$mail_pubmail' => array('mail_pubmail', t('Send public posts to all email contacts:'), $mail_pubmail, ''),
545                 
546                 
547                 
548                 
549                 '$h_advn' => t('Advanced Page Settings'),
550                 '$pagetype' => $pagetype,
551                 
552
553                 
554                 
555
556                 
557
558
559
560
561                 
562
563         ));
564
565         call_hooks('settings_form',$o);
566
567         $o .= '</form>' . "\r\n";
568
569         return $o;
570
571 }}
572