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