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