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