You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.php 2.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. require_once(dirname(__FILE__).'/config.php');
  13. /**
  14. * Initialize session.
  15. * @param boolean $keepopen keep session open? The default is
  16. * to close the session after $_SESSION has been populated.
  17. * @uses $_SESSION
  18. */
  19. function session_init($keepopen = false) {
  20. $settings = new phpVBoxConfigClass();
  21. // Sessions provided by auth module?
  22. if(@$settings->auth->capabilities['sessionStart']) {
  23. call_user_func(array($settings->auth, $settings->auth->capabilities['sessionStart']), $keepopen);
  24. return;
  25. }
  26. // No session support? No login...
  27. if(@$settings->noAuth || !function_exists('session_start')) {
  28. global $_SESSION;
  29. $_SESSION['valid'] = true;
  30. $_SESSION['authCheckHeartbeat'] = time();
  31. $_SESSION['admin'] = true;
  32. return;
  33. }
  34. // Session not is auto-started by PHP
  35. if(!ini_get('session.auto_start')) {
  36. ini_set('session.use_trans_sid', 0);
  37. ini_set('session.use_only_cookies', 1);
  38. // Session path
  39. if(isset($settings->sessionSavePath)) {
  40. session_save_path($settings->sessionSavePath);
  41. }
  42. if(isset($settings->session_name)) {
  43. $session_name = $settings->session_name;
  44. } else {
  45. $session_name = md5($_SERVER['DOCUMENT_ROOT'].$_SERVER['HTTP_USER_AGENT'].dirname(__FILE__));
  46. }
  47. session_name($session_name);
  48. session_start();
  49. }
  50. if(!$keepopen)
  51. session_write_close();
  52. }
  53. /**
  54. * Clean (strip slashes from if applicable) $_GET and $_POST and return
  55. * an array containing a merged array of both.
  56. * @return array
  57. */
  58. function clean_request() {
  59. if($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
  60. $json = json_decode(file_get_contents('php://input'), true);
  61. if(!is_array($json))
  62. $json = array();
  63. } else {
  64. $json = array();
  65. }
  66. $req = array_merge_recursive($_GET, $_POST);
  67. return array_merge_recursive($req, $json);
  68. }
  69. if(!function_exists('hash')) {
  70. // Lower security, but better than nothing
  71. /**
  72. * Return a hash of the passed string. Mimmics PHP's hash() params
  73. * @param unused $type
  74. * @param string $str string to hash
  75. */
  76. function hash($type,$str='') {
  77. return sha1(json_encode($str));
  78. }
  79. }