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