summaryrefslogtreecommitdiffstats
path: root/endpoints/lib/utils.php
blob: 356971a06ab3552afd03e16b2d06331e05997889 (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
<?php
/**
 * Common PHP utilities.
 *
 * @author Ian Moore (imoore76 at yahoo dot com)
 * @copyright Copyright (C) 2010-2015 Ian Moore (imoore76 at yahoo dot com)
 * @version $Id: utils.php 592 2015-04-12 19:53:44Z imoore76 $
 * @see phpVBoxConfigClass
 * @package phpVirtualBox
 *
*/

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

/**
 * Initialize session.
 * @param boolean $keepopen keep session open? The default is
 * 			to close the session after $_SESSION has been populated.
 * @uses $_SESSION
 */
function session_init($keepopen = false) {

	$settings = new phpVBoxConfigClass();

	// Sessions provided by auth module?
	if(@$settings->auth->capabilities['sessionStart']) {
		call_user_func(array($settings->auth, $settings->auth->capabilities['sessionStart']), $keepopen);
		return;
	}

	// No session support? No login...
	if(@$settings->noAuth || !function_exists('session_start')) {
		global $_SESSION;
		$_SESSION['valid'] = true;
		$_SESSION['authCheckHeartbeat'] = time();
		$_SESSION['admin'] = true;
		return;
	}

	// Session not is auto-started by PHP
	if(!ini_get('session.auto_start')) {

		ini_set('session.use_trans_sid', 0);
		ini_set('session.use_only_cookies', 1);

		// Session path
		if(isset($settings->sessionSavePath)) {
			session_save_path($settings->sessionSavePath);
		}

		if(isset($settings->session_name)) {
		    $session_name = $settings->session_name;
		} else {
		    $session_name = md5($_SERVER['DOCUMENT_ROOT'].$_SERVER['HTTP_USER_AGENT'].dirname(__FILE__));
		}
		session_name($session_name);
		session_start();
	}

	if(!$keepopen)
		session_write_close();

}


/**
 * Clean (strip slashes from if applicable) $_GET and $_POST and return
 * an array containing a merged array of both.
 * @return array
 */
function clean_request() {

	if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
	   $json = json_decode(file_get_contents('php://input'), true);
	   if(!is_array($json))
	   	   $json = array();
	} else {
		$json = array();
	}
	$req = array_merge_recursive($_GET, $_POST);
	return array_merge_recursive($req, $json);

}

if(!function_exists('hash')) {
// Lower security, but better than nothing
/**
 * Return a hash of the passed string. Mimmics PHP's hash() params
 * @param unused $type
 * @param string $str string to hash
 */
function hash($type,$str='') {
	return sha1(json_encode($str));
}
}