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