aboutsummaryrefslogtreecommitdiff
path: root/lib/session.php
blob: ccd1e8f4cf40ea1f6c21e46735dafce9503a9438 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<?php

//require_once(dirname(dirname(__FILE__)).'/config.php'); 
//require_once "render.php";

require_once "Auth/OpenID/Server.php";

// require_once('elgg/includes.php');

/**
 * Set up the session
 */
// get information from Elgg if logged in
// KJ - this should not be necessary as it can always be generated from the user name
function init()
{
	global $CFG;

	if (elgg_is_logged_in()) {
		setLoggedInUser(normaliseUsername($_SESSION['user']->username));
	} else {
		setLoggedInUser(null);
	}
}


/**
 * Get the URL of the current script
 */
function getServerURL()
{
    global $CONFIG;
    
    return $CONFIG->wwwroot.'mod/openid_server/server.php';
}

/**
 * Build a URL to a server action
 */
function buildURL($action=null, $escaped=true)
{
    $url = getServerURL();
    if ($action) {
        $url .= '/' . $action;
    }
    return $escaped ? htmlspecialchars($url, ENT_QUOTES) : $url;
}

/**
 * Extract the current action from the request
 * KJ - this should be replaced by Elgg 1 action system
 */
function getAction()
{
    $path_info = @$_SERVER['PATH_INFO'];
    $action = ($path_info) ? substr($path_info, 1) : '';
    $function_name = 'action_' . $action;
    return $function_name;
}

/**
 * Write the response to the request
 */
function writeResponse($resp)
{
    list ($headers, $body) = $resp;
    array_walk($headers, 'header');
    header(header_connection_close);
    print $body;
}

/**
 * Instantiate a new OpenID server object
 */
function getServer()
{
    global $CONFIG;
    static $server;
    $op_endpoint = getServerURL();
    if (!isset($server)) {
        $server =& new Auth_OpenID_Server(getOpenIDServerStore(),$op_endpoint);
    }
    return $server;
}

/**
 * Return whether the trust root is currently trusted
 *
 */
function isTrusted($identity_url, $trust_root, $return_to)
{
	global $store;
	
    if ($identity_url != getLoggedInUser()) {
        return false;
    }
    
    $sites = $store->getTrustedSites($identity_url);
    
    if (empty($sites)) {
	    return false;
    } else {
		return in_array($trust_root, $sites) && fnmatch($trust_root.'*',$return_to);
	}
}


/**
 * Get the openid_url out of the cookie
 *
 * @return mixed $openid_url The URL that was stored in the cookie or
 * false if there is none present or if the cookie is bad.
 */
function getLoggedInUser()
{
    global $CONFIG;
    if (elgg_is_logged_in()) {
        return $CONFIG->wwwroot.'profile/'.$_SESSION['user']->username;
    } else {
        return '';
    }    
}

function getRequestInfo()
{
    return isset($_SESSION['openid_server_request'])
        ? unserialize($_SESSION['openid_server_request'])
        : false;
}

function setRequestInfo($info=null)
{
    if (!isset($info)) {
        unset($_SESSION['openid_server_request']);
    } else {
        $_SESSION['openid_server_request'] = serialize($info);
    }
}

?>