]> git.mxchange.org Git - friendica.git/blob - mod/settings.php
more templates converted to string files
[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                         '$yes_selected' => (($profile['publish'])      ? " checked=\"checked\" " : ""),
289                         '$no_selected'  => (($profile['publish'] == 0) ? " checked=\"checked\" " : "")
290                 ));
291         }
292
293         if(strlen(get_config('system','directory_submit_url'))) {
294                 $opt_tpl = load_view_file("view/profile-in-netdir.tpl");
295
296                 $profile_in_net_dir = replace_macros($opt_tpl,array(
297                         '$yes_selected' => (($profile['net-publish'])      ? " checked=\"checked\" " : ""),
298                         '$no_selected'  => (($profile['net-publish'] == 0) ? " checked=\"checked\" " : "")
299                 ));
300         }
301         else
302                 $profile_in_net_dir = '';
303
304         $loc_checked = (($a->user['allow_location'] == 1)      ? " checked=\"checked\" " : "");
305
306         $invisible = (((! $profile['publish']) && (! $profile['net-publish']))
307                 ? true : false);
308
309         if($invisible)
310                 notice( t('Profile is <strong>not published</strong>.') . EOL );
311
312         $nickname_block = load_view_file("view/settings_nick_set.tpl");
313         
314         $nickname_subdir = '';
315         if(strlen($a->get_path())) {
316                 $subdir_tpl = load_view_file('view/settings_nick_subdir.tpl');
317                 $nickname_subdir = replace_macros($subdir_tpl, array(
318                         '$baseurl' => $a->get_baseurl(),
319                         '$nickname' => $nickname,
320                         '$hostname' => $a->get_hostname()
321                 ));
322         }
323
324         $theme_selector = '<select name="theme" id="theme-select" >';
325         $files = glob('view/theme/*');
326
327         $default_theme = get_config('system','theme');
328         if(! $default_theme)
329                 $default_theme = 'default';
330
331         if($files) {
332                 foreach($files as $file) {
333                         $f = basename($file);
334                         $selected = (($f == $_SESSION['theme']) || ($f === $default_theme && (! x($_SESSION,'theme')))
335                                 ? ' selected="selected" ' : '' );
336                         $theme_selector .= '<option val="' . basename($file) . '"' . $selected . '>' . basename($file) . '</option>';
337                 }
338         }
339         $theme_selector .= '</select>';
340
341
342         $nickname_block = replace_macros($nickname_block,array(
343                 '$nickname' => $nickname,
344                 '$uid' => local_user(),
345                 '$subdir' => $nickname_subdir,
346                 '$basepath' => $a->get_hostname(),
347                 '$baseurl' => $a->get_baseurl()));      
348
349         $stpl = load_view_file('view/settings.tpl');
350
351         $celeb = ((($a->user['page-flags'] == PAGE_SOAPBOX) || ($a->user['page-flags'] == PAGE_COMMUNITY)) ? true : false);
352
353         $uexport = '<div id="uexport-link"><a href="uexport" >' . t('Export Personal Data') . '</a></div>';
354
355
356         $o .= replace_macros($stpl,array(
357                 '$baseurl' => $a->get_baseurl(),
358                 '$oidhtml' => $oidhtml,
359                 '$uexport' => $uexport,
360                 '$uid' => local_user(),
361                 '$username' => $username,
362                 '$openid' => $openid,
363                 '$email' => $email,
364                 '$nickname_block' => $nickname_block,
365                 '$timezone' => $timezone,
366                 '$zoneselect' => select_timezone($timezone),
367                 '$defloc' => $defloc,
368                 '$loc_checked' => $loc_checked,
369                 '$profile_in_dir' => $profile_in_dir,
370                 '$profile_in_net_dir' => $profile_in_net_dir,
371                 '$permissions' => t('Default Post Permissions'),
372                 '$visibility' => $profile['net-publish'],
373                 '$aclselect' => populate_acl($a->user,$celeb),
374                 '$sel_notify1' => (($notify & NOTIFY_INTRO)   ? ' checked="checked" ' : ''),
375                 '$sel_notify2' => (($notify & NOTIFY_CONFIRM) ? ' checked="checked" ' : ''),
376                 '$sel_notify3' => (($notify & NOTIFY_WALL)    ? ' checked="checked" ' : ''),
377                 '$sel_notify4' => (($notify & NOTIFY_COMMENT) ? ' checked="checked" ' : ''),
378                 '$sel_notify5' => (($notify & NOTIFY_MAIL)    ? ' checked="checked" ' : ''),
379                 '$maxreq' => $maxreq,
380                 '$expire' => $expire,
381                 '$blockw_checked' => (($blockwall) ? '' : ' checked="checked" ' ),
382                 '$theme' => $theme_selector,
383                 '$pagetype' => $pagetype
384         ));
385
386         call_hooks('settings_form',$o);
387
388         $o .= '</form>' . "\r\n";
389
390         return $o;
391
392 }}
393