1<?php
2/**
3 * Common PHP utilities.
4 *
5 * @author Ian Moore (imoore76 at yahoo dot com)
6 * @copyright Copyright (C) 2010-2015 Ian Moore (imoore76 at yahoo dot com)
7 * @version $Id: utils.php 592 2015-04-12 19:53:44Z imoore76 $
8 * @see phpVBoxConfigClass
9 * @package phpVirtualBox
10 *
11*/
12
13require_once(dirname(__FILE__).'/config.php');
14
15/**
16 * Initialize session.
17 * @param boolean $keepopen keep session open? The default is
18 * 			to close the session after $_SESSION has been populated.
19 * @uses $_SESSION
20 */
21function session_init($keepopen = false) {
22
23	$settings = new phpVBoxConfigClass();
24
25	// Sessions provided by auth module?
26	if(@$settings->auth->capabilities['sessionStart']) {
27		call_user_func(array($settings->auth, $settings->auth->capabilities['sessionStart']), $keepopen);
28		return;
29	}
30
31	// No session support? No login...
32	if(@$settings->noAuth || !function_exists('session_start')) {
33		global $_SESSION;
34		$_SESSION['valid'] = true;
35		$_SESSION['authCheckHeartbeat'] = time();
36		$_SESSION['admin'] = true;
37		return;
38	}
39
40	// Session not is auto-started by PHP
41	if(!ini_get('session.auto_start')) {
42
43		ini_set('session.use_trans_sid', 0);
44		ini_set('session.use_only_cookies', 1);
45
46		// Session path
47		if(isset($settings->sessionSavePath)) {
48			session_save_path($settings->sessionSavePath);
49		}
50
51		if(isset($settings->session_name)) {
52		    $session_name = $settings->session_name;
53		} else {
54		    $session_name = md5($_SERVER['DOCUMENT_ROOT'].$_SERVER['HTTP_USER_AGENT'].dirname(__FILE__));
55		}
56		session_name($session_name);
57		session_start();
58	}
59
60	if(!$keepopen)
61		session_write_close();
62
63}
64
65
66/**
67 * Clean (strip slashes from if applicable) $_GET and $_POST and return
68 * an array containing a merged array of both.
69 * @return array
70 */
71function clean_request() {
72
73	if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
74	   $json = json_decode(file_get_contents('php://input'), true);
75	   if(!is_array($json))
76	   	   $json = array();
77	} else {
78		$json = array();
79	}
80	$req = array_merge_recursive($_GET, $_POST);
81	return array_merge_recursive($req, $json);
82
83}
84
85if(!function_exists('hash')) {
86// Lower security, but better than nothing
87/**
88 * Return a hash of the passed string. Mimmics PHP's hash() params
89 * @param unused $type
90 * @param string $str string to hash
91 */
92function hash($type,$str='') {
93	return sha1(json_encode($str));
94}
95}