PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/utils.php

https://bitbucket.org/kolbyjAFK/phpvirtualbox
PHP | 262 lines | 200 code | 17 blank | 45 comment | 17 complexity | 0d6063ed3880e9d6ce75b18b8baf788c MD5 | raw file
  1. <?php
  2. /**
  3. * Common PHP utilities.
  4. *
  5. * @author Ian Moore (imoore76 at yahoo dot com)
  6. * @copyright Copyright (C) 2010-2012 Ian Moore (imoore76 at yahoo dot com)
  7. * @version $Id$
  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. // No session support? No login...
  22. if(@$settings->noAuth) {
  23. global $_SESSION;
  24. $_SESSION['valid'] = true;
  25. $_SESSION['authCheckHeartbeat'] = time();
  26. $_SESSION['admin'] = true;
  27. return;
  28. }
  29. // Sessions provided by auth module?
  30. if(@$settings->auth->capabilities['sessionStart']) {
  31. call_user_func(array($settings->auth, $settings->auth->capabilities['sessionStart']), $keepopen);
  32. return;
  33. }
  34. // Session 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. session_name((isset($settings->session_name) ? $settings->session_name : md5('phpvbx'.$_SERVER['DOCUMENT_ROOT'].$_SERVER['HTTP_USER_AGENT'])));
  43. session_start();
  44. }
  45. if(!$keepopen) session_write_close();
  46. }
  47. /**
  48. * Strip slashes from string. Needed to pass to array_walk
  49. * @param string $a string to strip slashes from
  50. */
  51. function __vbx_stripslash(&$a) { $a = stripslashes($a); }
  52. /**
  53. * Clean (strip slashes from if applicable) $_GET and $_POST and return
  54. * an array containing a merged array of both.
  55. * @return array
  56. */
  57. function clean_request($r = null) {
  58. if(!$r) $r = array_merge($_GET,$_POST);
  59. if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {array_walk_recursive($r,'__vbx_stripslash');}
  60. return $r;
  61. }
  62. if(!function_exists('hash')) {
  63. // Lower security, but better than nothing
  64. /**
  65. * Return a hash of the passed string. Mimmics PHP's hash() params
  66. * @param unused $type
  67. * @param string $str string to hash
  68. */
  69. function hash($type,$str='') {
  70. return sha1(json_encode($str));
  71. }
  72. }
  73. /*
  74. * Support for PHP compiled with --disable-json
  75. */
  76. if(!function_exists('json_encode')) {
  77. /**
  78. * Mimmics PHP's json_encode
  79. * @link http://au.php.net/manual/en/function.json-encode.php#82904
  80. * @param mixed $a variable to JSON encode
  81. * @param boolean $force_string force $a to be treated like a string.
  82. */
  83. function json_encode($a=false,$force_string=false) {
  84. if (is_null($a)) return 'null';
  85. if ($a === false) return 'false';
  86. if ($a === true) return 'true';
  87. if (is_scalar($a)) {
  88. if (is_float($a)) {
  89. // Always use "." for floats.
  90. return floatval(str_replace(",", ".", strval($a)));
  91. }
  92. if (is_string($a) || $force_string) {
  93. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  94. return '"' . json_encodeUnicodeString(str_replace($jsonReplaces[0], $jsonReplaces[1], $a)) . '"';
  95. } else return $a;
  96. }
  97. $isList = true;
  98. for ($i = 0, reset($a); $i < count($a); $i++, next($a)) {
  99. if (key($a) !== $i) {
  100. $isList = false;
  101. break;
  102. }
  103. }
  104. $result = array();
  105. if ($isList) {
  106. foreach ($a as $v) $result[] = json_encode($v);
  107. return '[' . join(',', $result) . ']';
  108. } else {
  109. foreach ($a as $k => $v) $result[] = json_encode($k,true).':'.json_encode($v);
  110. return '{' . join(',', $result) . '}';
  111. }
  112. }
  113. function json_encodeUnicodeString($value)
  114. {
  115. $strlen_var = strlen($value);
  116. $ascii = "";
  117. /**
  118. * Iterate over every character in the string,
  119. * escaping with a slash or encoding to UTF-8 where necessary
  120. */
  121. for($i = 0; $i < $strlen_var; $i++) {
  122. $ord_var_c = ord($value[$i]);
  123. switch (true) {
  124. case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)):
  125. // characters U-00000000 - U-0000007F (same as ASCII)
  126. $ascii .= $value[$i];
  127. break;
  128. case (($ord_var_c & 0xE0) == 0xC0):
  129. // characters U-00000080 - U-000007FF, mask 110XXXXX
  130. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  131. $char = pack('C*', $ord_var_c, ord($value[$i + 1]));
  132. $i += 1;
  133. $utf16 = json_utf82utf16($char);
  134. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  135. break;
  136. case (($ord_var_c & 0xF0) == 0xE0):
  137. // characters U-00000800 - U-0000FFFF, mask 1110XXXX
  138. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  139. $char = pack('C*', $ord_var_c,
  140. ord($value[$i + 1]),
  141. ord($value[$i + 2]));
  142. $i += 2;
  143. $utf16 = json_utf82utf16($char);
  144. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  145. break;
  146. case (($ord_var_c & 0xF8) == 0xF0):
  147. // characters U-00010000 - U-001FFFFF, mask 11110XXX
  148. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  149. $char = pack('C*', $ord_var_c,
  150. ord($value[$i + 1]),
  151. ord($value[$i + 2]),
  152. ord($value[$i + 3]));
  153. $i += 3;
  154. $utf16 = json_utf82utf16($char);
  155. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  156. break;
  157. case (($ord_var_c & 0xFC) == 0xF8):
  158. // characters U-00200000 - U-03FFFFFF, mask 111110XX
  159. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  160. $char = pack('C*', $ord_var_c,
  161. ord($value[$i + 1]),
  162. ord($value[$i + 2]),
  163. ord($value[$i + 3]),
  164. ord($value[$i + 4]));
  165. $i += 4;
  166. $utf16 = json_utf82utf16($char);
  167. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  168. break;
  169. case (($ord_var_c & 0xFE) == 0xFC):
  170. // characters U-04000000 - U-7FFFFFFF, mask 1111110X
  171. // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  172. $char = pack('C*', $ord_var_c,
  173. ord($value[$i + 1]),
  174. ord($value[$i + 2]),
  175. ord($value[$i + 3]),
  176. ord($value[$i + 4]),
  177. ord($value[$i + 5]));
  178. $i += 5;
  179. $utf16 = json_utf82utf16($char);
  180. $ascii .= sprintf('\u%04s', bin2hex($utf16));
  181. break;
  182. }
  183. }
  184. return $ascii;
  185. }
  186. /**
  187. * Convert a string from one UTF-8 char to one UTF-16 char.
  188. *
  189. * Normally should be handled by mb_convert_encoding, but
  190. * provides a slower PHP-only method for installations
  191. * that lack the multibye string extension.
  192. *
  193. * This method is from the Solar Framework by Paul M. Jones
  194. *
  195. * @link http://solarphp.com
  196. * @param string $utf8 UTF-8 character
  197. * @return string UTF-16 character
  198. */
  199. function json_utf82utf16($utf8)
  200. {
  201. // Check for mb extension otherwise do by hand.
  202. if( function_exists('mb_convert_encoding') ) {
  203. return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
  204. }
  205. switch (strlen($utf8)) {
  206. case 1:
  207. // this case should never be reached, because we are in ASCII range
  208. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  209. return $utf8;
  210. case 2:
  211. // return a UTF-16 character from a 2-byte UTF-8 char
  212. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  213. return chr(0x07 & (ord($utf8{0}) >> 2))
  214. . chr((0xC0 & (ord($utf8{0}) << 6))
  215. | (0x3F & ord($utf8{1})));
  216. case 3:
  217. // return a UTF-16 character from a 3-byte UTF-8 char
  218. // see: http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8
  219. return chr((0xF0 & (ord($utf8{0}) << 4))
  220. | (0x0F & (ord($utf8{1}) >> 2)))
  221. . chr((0xC0 & (ord($utf8{1}) << 6))
  222. | (0x7F & ord($utf8{2})));
  223. }
  224. // ignoring UTF-32 for now, sorry
  225. return '';
  226. }
  227. }