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