]> git.mxchange.org Git - friendica.git/blob - mod/settings.php
Merge branch 'master' of https://github.com/erikl/friendika into erikl-master
[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 function settings_post(&$a) {
12
13         if(! local_user()) {
14                 notice( t('Permission denied.') . EOL);
15                 return;
16         }
17
18         if(count($a->user) && x($a->user,'uid') && $a->user['uid'] != local_user()) {
19                 notice( t('Permission denied.') . EOL);
20                 return;
21         }
22
23         if(($a->argc > 1) && ($a->argv[1] == 'addon')) {
24                 call_hooks('plugin_settings_post', $_POST);
25                 return;
26         }
27
28         call_hooks('settings_post', $_POST);
29
30         if((x($_POST,'npassword')) || (x($_POST,'confirm'))) {
31
32                 $newpass = $_POST['npassword'];
33                 $confirm = $_POST['confirm'];
34
35                 $err = false;
36                 if($newpass != $confirm ) {
37                         notice( t('Passwords do not match. Password unchanged.') . EOL);
38                         $err = true;
39                 }
40
41                 if((! x($newpass)) || (! x($confirm))) {
42                         notice( t('Empty passwords are not allowed. Password unchanged.') . EOL);
43                         $err = true;
44                 }
45
46                 if(! $err) {
47                         $password = hash('whirlpool',$newpass);
48                         $r = q("UPDATE `user` SET `password` = '%s' WHERE `uid` = %d LIMIT 1",
49                                 dbesc($password),
50                                 intval(local_user())
51                         );
52                         if($r)
53                                 notice( t('Password changed.') . EOL);
54                         else
55                                 notice( t('Password update failed. Please try again.') . EOL);
56                 }
57         }
58
59         $theme            = ((x($_POST,'theme'))      ? notags(trim($_POST['theme']))        : '');
60         $username         = ((x($_POST,'username'))   ? notags(trim($_POST['username']))     : '');
61         $email            = ((x($_POST,'email'))      ? notags(trim($_POST['email']))        : '');
62         $timezone         = ((x($_POST,'timezone'))   ? notags(trim($_POST['timezone']))     : '');
63         $defloc           = ((x($_POST,'defloc'))     ? notags(trim($_POST['defloc']))       : '');
64         $openid           = ((x($_POST,'openid_url')) ? notags(trim($_POST['openid_url']))   : '');
65         $maxreq           = ((x($_POST,'maxreq'))     ? intval($_POST['maxreq'])             : 0);
66         $expire           = ((x($_POST,'expire'))     ? intval($_POST['expire'])             : 0);
67
68         $allow_location   = (((x($_POST,'allow_location')) && (intval($_POST['allow_location']) == 1)) ? 1: 0);
69         $publish          = (((x($_POST,'profile_in_directory')) && (intval($_POST['profile_in_directory']) == 1)) ? 1: 0);
70         $net_publish      = (((x($_POST,'profile_in_netdirectory')) && (intval($_POST['profile_in_netdirectory']) == 1)) ? 1: 0);
71         $old_visibility   = (((x($_POST,'visibility')) && (intval($_POST['visibility']) == 1)) ? 1 : 0);
72         $page_flags       = (((x($_POST,'page-flags')) && (intval($_POST['page-flags']))) ? intval($_POST['page-flags']) : 0);
73
74         $notify = 0;
75
76         if(x($_POST,'notify1'))
77                 $notify += intval($_POST['notify1']);
78         if(x($_POST,'notify2'))
79                 $notify += intval($_POST['notify2']);
80         if(x($_POST,'notify3'))
81                 $notify += intval($_POST['notify3']);
82         if(x($_POST,'notify4'))
83                 $notify += intval($_POST['notify4']);
84         if(x($_POST,'notify5'))
85                 $notify += intval($_POST['notify5']);
86
87         $email_changed = false;
88
89         $err = '';
90
91         $name_change = false;
92
93         if($username != $a->user['username']) {
94                 $name_change = true;
95                 if(strlen($username) > 40)
96                         $err .= t(' Please use a shorter name.');
97                 if(strlen($username) < 3)
98                         $err .= t(' Name too short.');
99         }
100
101         if($email != $a->user['email']) {
102                 $email_changed = true;
103         if(! valid_email($email))
104                         $err .= t(' Not valid email.');
105                 if((x($a->config,'admin_email')) && (strcasecmp($email,$a->config['admin_email']) == 0)) {
106                         $err .= t(' Cannot change to that email.');
107                         $email = $a->user['email'];
108                 }
109         }
110
111         if(strlen($err)) {
112                 notice($err . EOL);
113                 return;
114         }
115
116         if($timezone != $a->user['timezone']) {
117                 if(strlen($timezone))
118                         date_default_timezone_set($timezone);
119         }
120
121         $str_group_allow   = perms2str($_POST['group_allow']);
122         $str_contact_allow = perms2str($_POST['contact_allow']);
123         $str_group_deny    = perms2str($_POST['group_deny']);
124         $str_contact_deny  = perms2str($_POST['contact_deny']);
125
126         $openidserver = $a->user['openidserver'];
127
128         // If openid has changed or if there's an openid but no openidserver, try and discover it.
129
130         if($openid != $a->user['openid'] || (strlen($openid) && (! strlen($openidserver)))) {
131                 $tmp_str = $openid;
132                 if(strlen($tmp_str) && validate_url($tmp_str)) {
133                         logger('updating openidserver');
134                         require_once('library/openid.php');
135                         $open_id_obj = new LightOpenID;
136                         $open_id_obj->identity = $openid;
137                         $openidserver = $open_id_obj->discover($open_id_obj->identity);
138                 }
139                 else
140                         $openidserver = '';
141         }
142
143         $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'  WHERE `uid` = %d LIMIT 1",
144                         dbesc($username),
145                         dbesc($email),
146                         dbesc($openid),
147                         dbesc($timezone),
148                         dbesc($str_contact_allow),
149                         dbesc($str_group_allow),
150                         dbesc($str_contact_deny),
151                         dbesc($str_group_deny),
152                         intval($notify),
153                         intval($page_flags),
154                         dbesc($defloc),
155                         intval($allow_location),
156                         dbesc($theme),
157                         intval($maxreq),
158                         intval($expire),
159                         dbesc($openidserver),
160                         intval(local_user())
161         );
162         if($r)
163                 notice( t('Settings updated.') . EOL);
164
165         $r = q("UPDATE `profile` 
166                 SET `publish` = %d, `net-publish` = %d
167                 WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
168                 intval($publish),
169                 intval($net_publish),
170                 intval(local_user())
171         );
172
173
174         if($name_change) {
175                 q("UPDATE `contact` SET `name` = '%s', `name-date` = '%s' WHERE `uid` = %d AND `self` = 1 LIMIT 1",
176                         dbesc($username),
177                         dbesc(datetime_convert()),
178                         intval(local_user())
179                 );
180         }               
181
182         if($old_visibility != $net_publish) {
183                 // Update global directory in background
184                 $url = $_SESSION['my_url'];
185                 if($url && strlen(get_config('system','directory_submit_url')))
186                         proc_run('php',"include/directory.php","$url");
187         }
188
189         $_SESSION['theme'] = $theme;
190         if($email_changed && $a->config['register_policy'] == REGISTER_VERIFY) {
191
192                 // FIXME - set to un-verified, blocked and redirect to logout
193
194         }
195
196         goaway($a->get_baseurl() . '/settings' );
197         return; // NOTREACHED
198 }
199                 
200
201 if(! function_exists('settings_content')) {
202 function settings_content(&$a) {
203
204         $o = '';
205         $o .= '<script> $(document).ready(function() { $(\'#nav-settings-link\').addClass(\'nav-selected\'); });</script>';
206
207         if(! local_user()) {
208                 notice( t('Permission denied.') . EOL );
209                 return;
210         }
211
212         if(($a->argc > 1) && ($a->argv[1] === 'addon')) {
213                 $o .= '<h1>' . t('Plugin Settings') . '</h1>';
214                 $o .= '<div id="account-settings-link"><a href="settings">' . t('Account Settings') . '</a></div>';
215
216                 $o .= '<form action="settings/addon" method="post" >';
217
218                 $r = q("SELECT * FROM `hook` WHERE `hook` = 'plugin_settings' ");
219                 if(! count($r))
220                         notice( t('No Plugin settings configured') . EOL);
221
222                 call_hooks('plugin_settings', $o);
223                 $o .= '</form>';
224                 return $o;
225         }
226                 
227         require_once('include/acl_selectors.php');
228
229         $p = q("SELECT * FROM `profile` WHERE `is-default` = 1 AND `uid` = %d LIMIT 1",
230                 intval(local_user())
231         );
232         if(count($p))
233                 $profile = $p[0];
234
235         $username = $a->user['username'];
236         $email    = $a->user['email'];
237         $nickname = $a->user['nickname'];
238         $timezone = $a->user['timezone'];
239         $notify   = $a->user['notify-flags'];
240         $defloc   = $a->user['default-location'];
241         $openid   = $a->user['openid'];
242         $maxreq   = $a->user['maxreq'];
243         $expire   = ((intval($a->user['expire'])) ? $a->user['expire'] : '');
244
245         if(! strlen($a->user['timezone']))
246                 $timezone = date_default_timezone_get();
247
248         $pageset_tpl = load_view_file('view/pagetypes.tpl');
249         $pagetype = replace_macros($pageset_tpl,array(
250                 '$normal'         => (($a->user['page-flags'] == PAGE_NORMAL)      ? " checked=\"checked\" " : ""),
251                 '$soapbox'        => (($a->user['page-flags'] == PAGE_SOAPBOX)     ? " checked=\"checked\" " : ""),
252                 '$community'      => (($a->user['page-flags'] == PAGE_COMMUNITY)   ? " checked=\"checked\" " : ""),
253                 '$freelove'       => (($a->user['page-flags'] == PAGE_FREELOVE)    ? " checked=\"checked\" " : ""),
254                 '$page_normal'    => PAGE_NORMAL,
255                 '$page_soapbox'   => PAGE_SOAPBOX,
256                 '$page_community' => PAGE_COMMUNITY,
257                 '$page_freelove'  => PAGE_FREELOVE
258         ));
259
260         $noid = get_config('system','no_openid');
261
262         if($noid) {
263                 $oidhtml = '';
264         }
265         else {
266                 $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.");
267         }
268
269
270         if(get_config('system','publish_all')) {
271                 $profile_in_dir = '<input type="hidden" name="profile_in_directory" value="1" />';
272         }
273         else {
274                 $opt_tpl = load_view_file("view/profile-in-directory.tpl");
275                 $profile_in_dir = replace_macros($opt_tpl,array(
276                         '$yes_selected' => (($profile['publish'])      ? " checked=\"checked\" " : ""),
277                         '$no_selected'  => (($profile['publish'] == 0) ? " checked=\"checked\" " : "")
278                 ));
279         }
280
281         if(strlen(get_config('system','directory_submit_url'))) {
282                 $opt_tpl = load_view_file("view/profile-in-netdir.tpl");
283
284                 $profile_in_net_dir = replace_macros($opt_tpl,array(
285                         '$yes_selected' => (($profile['net-publish'])      ? " checked=\"checked\" " : ""),
286                         '$no_selected'  => (($profile['net-publish'] == 0) ? " checked=\"checked\" " : "")
287                 ));
288         }
289         else
290                 $profile_in_net_dir = '';
291
292         $loc_checked = (($a->user['allow_location'] == 1)      ? " checked=\"checked\" " : "");
293
294         $invisible = (((! $profile['publish']) && (! $profile['net-publish']))
295                 ? true : false);
296
297         if($invisible)
298                 notice( t('Profile is <strong>not published</strong>.') . EOL );
299
300         $nickname_block = load_view_file("view/settings_nick_set.tpl");
301         
302         $nickname_subdir = '';
303         if(strlen($a->get_path())) {
304                 $subdir_tpl = load_view_file('view/settings_nick_subdir.tpl');
305                 $nickname_subdir = replace_macros($subdir_tpl, array(
306                         '$baseurl' => $a->get_baseurl(),
307                         '$nickname' => $nickname,
308                         '$hostname' => $a->get_hostname()
309                 ));
310         }
311
312         $theme_selector = '<select name="theme" id="theme-select" >';
313         $files = glob('view/theme/*');
314
315         $default_theme = get_config('system','theme');
316         if(! $default_theme)
317                 $default_theme = 'default';
318
319         if($files) {
320                 foreach($files as $file) {
321                         $f = basename($file);
322                         $selected = (($f == $_SESSION['theme']) || ($f === $default_theme && (! x($_SESSION,'theme')))
323                                 ? ' selected="selected" ' : '' );
324                         $theme_selector .= '<option val="' . basename($file) . '"' . $selected . '>' . basename($file) . '</option>';
325                 }
326         }
327         $theme_selector .= '</select>';
328
329
330         $nickname_block = replace_macros($nickname_block,array(
331                 '$nickname' => $nickname,
332                 '$uid' => local_user(),
333                 '$subdir' => $nickname_subdir,
334                 '$basepath' => $a->get_hostname(),
335                 '$baseurl' => $a->get_baseurl()));      
336
337         $stpl = load_view_file('view/settings.tpl');
338
339         $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
340
341         $o .= replace_macros($stpl,array(
342                 '$baseurl' => $a->get_baseurl(),
343                 '$oidhtml' => $oidhtml,
344                 '$uid' => local_user(),
345                 '$username' => $username,
346                 '$openid' => $openid,
347                 '$email' => $email,
348                 '$nickname_block' => $nickname_block,
349                 '$timezone' => $timezone,
350                 '$zoneselect' => select_timezone($timezone),
351                 '$defloc' => $defloc,
352                 '$loc_checked' => $loc_checked,
353                 '$profile_in_dir' => $profile_in_dir,
354                 '$profile_in_net_dir' => $profile_in_net_dir,
355                 '$permissions' => t('Default Post Permissions'),
356                 '$visibility' => $profile['net-publish'],
357                 '$aclselect' => populate_acl($a->user,$celeb),
358                 '$sel_notify1' => (($notify & NOTIFY_INTRO)   ? ' checked="checked" ' : ''),
359                 '$sel_notify2' => (($notify & NOTIFY_CONFIRM) ? ' checked="checked" ' : ''),
360                 '$sel_notify3' => (($notify & NOTIFY_WALL)    ? ' checked="checked" ' : ''),
361                 '$sel_notify4' => (($notify & NOTIFY_COMMENT) ? ' checked="checked" ' : ''),
362                 '$sel_notify5' => (($notify & NOTIFY_MAIL)    ? ' checked="checked" ' : ''),
363                 '$maxreq' => $maxreq,
364                 '$expire' => $expire,
365                 '$theme' => $theme_selector,
366                 '$pagetype' => $pagetype
367         ));
368
369         call_hooks('settings_form',$o);
370
371         $o .= '</form>' . "\r\n";
372
373         return $o;
374
375 }}
376