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