3 function authenticate_success($user_record, $login_initial = false, $interactive = false) {
7 $_SESSION['uid'] = $user_record['uid'];
8 $_SESSION['theme'] = $user_record['theme'];
9 $_SESSION['authenticated'] = 1;
10 $_SESSION['page_flags'] = $user_record['page-flags'];
11 $_SESSION['my_url'] = $a->get_baseurl() . '/profile/' . $user_record['nickname'];
12 $_SESSION['my_address'] = $user_record['nickname'] . '@' . substr($a->get_baseurl(),strpos($a->get_baseurl(),'://')+3);
13 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
15 $a->user = $user_record;
18 if($a->user['login_date'] === '0000-00-00 00:00:00') {
19 $_SESSION['return_url'] = 'profile_photo/new';
20 $a->module = 'profile_photo';
21 info( t("Welcome ") . $a->user['username'] . EOL);
22 info( t('Please upload a profile photo.') . EOL);
25 info( t("Welcome back ") . $a->user['username'] . EOL);
28 $member_since = strtotime($a->user['register_date']);
29 if(time() < ($member_since + ( 60 * 60 * 24 * 14)))
30 $_SESSION['new_member'] = true;
32 $_SESSION['new_member'] = false;
33 if(strlen($a->user['timezone'])) {
34 date_default_timezone_set($a->user['timezone']);
35 $a->timezone = $a->user['timezone'];
38 $master_record = $a->user;
40 if((x($_SESSION,'submanage')) && intval($_SESSION['submanage'])) {
41 $r = q("select * from user where uid = %d limit 1",
42 intval($_SESSION['submanage'])
45 $master_record = $r[0];
48 $r = q("SELECT `uid`,`username`,`nickname` FROM `user` WHERE `password` = '%s' AND `email` = '%s'",
49 dbesc($master_record['password']),
50 dbesc($master_record['email'])
55 $a->identities = array();
57 $r = q("select `user`.`uid`, `user`.`username`, `user`.`nickname`
58 from manage left join user on manage.mid = user.uid
59 where `manage`.`uid` = %d",
60 intval($master_record['uid'])
63 $a->identities = array_merge($a->identities,$r);
66 logger('auth_identities: ' . print_r($a->identities,true), LOGGER_DEBUG);
68 $r = q("SELECT * FROM `contact` WHERE `uid` = %d AND `self` = 1 LIMIT 1",
69 intval($_SESSION['uid']));
72 $a->cid = $r[0]['id'];
73 $_SESSION['cid'] = $a->cid;
76 header('X-Account-Management-Status: active; name="' . $a->user['username'] . '"; id="' . $a->user['nickname'] .'"');
81 q("UPDATE `user` SET `login_date` = '%s', `language` = '%s' WHERE `uid` = %d LIMIT 1",
82 dbesc(datetime_convert()),
84 intval($_SESSION['uid'])
87 call_hooks('logged_in', $a->user);
89 if(($a->module !== 'home') && isset($_SESSION['return_url']))
90 goaway($a->get_baseurl() . '/' . $_SESSION['return_url']);
97 function can_write_wall(&$a,$owner) {
101 if((! (local_user())) && (! (remote_user())))
106 if(($uid) && ($uid == $owner)) {
112 // use remembered decision and avoid a DB lookup for each and every display item
113 // DO NOT use this function if there are going to be multiple owners
115 // We have a contact-id for an authenticated remote user, this block determines if the contact
116 // belongs to this page owner, and has the necessary permissions to post content
120 elseif($verified === 1)
124 $r = q("SELECT `contact`.*, `user`.`page-flags` FROM `contact` LEFT JOIN `user` on `user`.`uid` = `contact`.`uid`
125 WHERE `contact`.`uid` = %d AND `contact`.`id` = %d AND `contact`.`blocked` = 0 AND `contact`.`pending` = 0
126 AND `user`.`blockwall` = 0 AND `readonly` = 0 AND ( `contact`.`rel` IN ( %d , %d ) OR `user`.`page-flags` = %d ) LIMIT 1",
128 intval(remote_user()),
129 intval(CONTACT_IS_SHARING),
130 intval(CONTACT_IS_FRIEND),
131 intval(PAGE_COMMUNITY)
148 function permissions_sql($owner_id,$remote_verified = false,$groups = null) {
150 $local_user = local_user();
151 $remote_user = remote_user();
154 * Construct permissions
156 * default permissions - anonymous user
159 $sql = " AND allow_cid = ''
166 * Profile owner - everything is visible
169 if(($local_user) && ($local_user == $owner_id)) {
174 * Authenticated visitor. Unless pre-verified,
175 * check that the contact belongs to this $owner_id
176 * and load the groups the visitor belongs to.
177 * If pre-verified, the caller is expected to have already
178 * done this and passed the groups into this function.
181 elseif($remote_user) {
183 if(! $remote_verified) {
184 $r = q("SELECT id FROM contact WHERE id = %d AND uid = %d AND blocked = 0 LIMIT 1",
185 intval($remote_user),
189 $remote_verified = true;
190 $groups = init_groups_visitor($remote_user);
193 if($remote_verified) {
195 $gs = '<<>>'; // should be impossible to match
197 if(is_array($groups) && count($groups)) {
198 foreach($groups as $g)
199 $gs .= '|<' . intval($g) . '>';
203 " AND ( allow_cid = '' OR allow_cid REGEXP '<%d>' )
204 AND ( deny_cid = '' OR NOT deny_cid REGEXP '<%d>' )
205 AND ( allow_gid = '' OR allow_gid REGEXP '%s' )
206 AND ( deny_gid = '' OR NOT deny_gid REGEXP '%s')
208 intval($remote_user),
209 intval($remote_user),
219 function item_permissions_sql($owner_id,$remote_verified = false,$groups = null) {
221 $local_user = local_user();
222 $remote_user = remote_user();
225 * Construct permissions
227 * default permissions - anonymous user
230 $sql = " AND allow_cid = ''
238 * Profile owner - everything is visible
241 if(($local_user) && ($local_user == $owner_id)) {
246 * Authenticated visitor. Unless pre-verified,
247 * check that the contact belongs to this $owner_id
248 * and load the groups the visitor belongs to.
249 * If pre-verified, the caller is expected to have already
250 * done this and passed the groups into this function.
253 elseif($remote_user) {
255 if(! $remote_verified) {
256 $r = q("SELECT id FROM contact WHERE id = %d AND uid = %d AND blocked = 0 LIMIT 1",
257 intval($remote_user),
261 $remote_verified = true;
262 $groups = init_groups_visitor($remote_user);
265 if($remote_verified) {
267 $gs = '<<>>'; // should be impossible to match
269 if(is_array($groups) && count($groups)) {
270 foreach($groups as $g)
271 $gs .= '|<' . intval($g) . '>';
275 " AND ( private = 0 OR ( private = 1 AND wall = 1 AND ( allow_cid = '' OR allow_cid REGEXP '<%d>' )
276 AND ( deny_cid = '' OR NOT deny_cid REGEXP '<%d>' )
277 AND ( allow_gid = '' OR allow_gid REGEXP '%s' )
278 AND ( deny_gid = '' OR NOT deny_gid REGEXP '%s')))
280 intval($remote_user),
281 intval($remote_user),
293 * Functions used to protect against Cross-Site Request Forgery
294 * The security token has to base on at least one value that an attacker can't know - here it's the session ID and the private key.
295 * In this implementation, a security token is reusable (if the user submits a form, goes back and resubmits the form, maybe with small changes;
296 * or if the security token is used for ajax-calls that happen several times), but only valid for a certain amout of time (3hours).
297 * The "typename" seperates the security tokens of different types of forms. This could be relevant in the following case:
298 * A security token is used to protekt a link from CSRF (e.g. the "delete this profile"-link).
299 * If the new page contains by any chance external elements, then the used security token is exposed by the referrer.
300 * Actually, important actions should not be triggered by Links / GET-Requests at all, but somethimes they still are,
301 * so this mechanism brings in some damage control (the attacker would be able to forge a request to a form of this type, but not to forms of other types).
303 function get_form_security_token($typename = '') {
307 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $timestamp . $typename);
309 return $timestamp . '.' . $sec_hash;
312 function check_form_security_token($typename = '', $formname = 'form_security_token') {
313 if (!x($_REQUEST, $formname)) return false;
314 $hash = $_REQUEST[$formname];
316 $max_livetime = 10800; // 3 hours
320 $x = explode('.', $hash);
321 if (time() > (IntVal($x[0]) + $max_livetime)) return false;
323 $sec_hash = hash('whirlpool', $a->user['guid'] . $a->user['prvkey'] . session_id() . $x[0] . $typename);
325 return ($sec_hash == $x[1]);
328 function check_form_security_std_err_msg() {
329 return t('The form security token was not correct. This probably happened because the form has been opened for too long (>3 hours) before submitting it.') . EOL;
331 function check_form_security_token_redirectOnErr($err_redirect, $typename = '', $formname = 'form_security_token') {
332 if (!check_form_security_token($typename, $formname)) {
334 logger('check_form_security_token failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
335 logger('check_form_security_token failed: _REQUEST data: ' . print_r($_REQUEST, true), LOGGER_DATA);
336 notice( check_form_security_std_err_msg() );
337 goaway($a->get_baseurl() . $err_redirect );
340 function check_form_security_token_ForbiddenOnErr($typename = '', $formname = 'form_security_token') {
341 if (!check_form_security_token($typename, $formname)) {
343 logger('check_form_security_token failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
344 logger('check_form_security_token failed: _REQUEST data: ' . print_r($_REQUEST, true), LOGGER_DATA);
345 header('HTTP/1.1 403 Forbidden');