| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of Raven. |
| 5 |
* |
| 6 |
* (c) Sentry Team |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
class Raven_Compat |
| 13 |
{ |
| 14 |
public static function gethostname() |
| 15 |
{ |
| 16 |
if (function_exists('gethostname')) { |
| 17 |
return gethostname(); |
| 18 |
} |
| 19 |
|
| 20 |
return self::_gethostname(); |
| 21 |
} |
| 22 |
|
| 23 |
public static function _gethostname() |
| 24 |
{ |
| 25 |
return php_uname('n'); |
| 26 |
} |
| 27 |
|
| 28 |
public static function hash_hmac($algo, $data, $key, $raw_output = false) |
| 29 |
{ |
| 30 |
if (function_exists('hash_hmac')) { |
| 31 |
return hash_hmac($algo, $data, $key, $raw_output); |
| 32 |
} |
| 33 |
|
| 34 |
return self::_hash_hmac($algo, $data, $key, $raw_output); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Implementation from 'KC Cloyd'. |
| 39 |
* |
| 40 |
* @param string $algo Name of selected hashing algorithm |
| 41 |
* @param string $data Message to be hashed |
| 42 |
* @param string $key Shared secret key used for generating the HMAC variant of the message digest |
| 43 |
* @param bool $raw_output Must be binary |
| 44 |
* @return string |
| 45 |
* @doc http://php.net/manual/en/function.hash-hmac.php |
| 46 |
*/ |
| 47 |
public static function _hash_hmac($algo, $data, $key, $raw_output = false) |
| 48 |
{ |
| 49 |
$algo = strtolower($algo); |
| 50 |
$pack = 'H'.strlen($algo('test')); |
| 51 |
$size = 64; |
| 52 |
$opad = str_repeat(chr(0x5C), $size); |
| 53 |
$ipad = str_repeat(chr(0x36), $size); |
| 54 |
|
| 55 |
if (strlen($key) > $size) { |
| 56 |
$key = str_pad(pack($pack, $algo($key)), $size, chr(0x00)); |
| 57 |
} else { |
| 58 |
$key = str_pad($key, $size, chr(0x00)); |
| 59 |
} |
| 60 |
|
| 61 |
$keyLastPos = strlen($key) - 1; |
| 62 |
for ($i = 0; $i < $keyLastPos; $i++) { |
| 63 |
$opad[$i] = $opad[$i] ^ $key[$i]; |
| 64 |
$ipad[$i] = $ipad[$i] ^ $key[$i]; |
| 65 |
} |
| 66 |
|
| 67 |
$output = $algo($opad.pack($pack, $algo($ipad.$data))); |
| 68 |
|
| 69 |
return ($raw_output) ? pack($pack, $output) : $output; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Note that we discard the options given to be compatible |
| 74 |
* with PHP < 5.3 |
| 75 |
* |
| 76 |
* @param mixed $value |
| 77 |
* @param int $options |
| 78 |
* @param int $depth Set the maximum depth |
| 79 |
* @return string |
| 80 |
*/ |
| 81 |
public static function json_encode($value, $options = 0, $depth = 512) |
| 82 |
{ |
| 83 |
if (function_exists('json_encode')) { |
| 84 |
if (PHP_VERSION_ID < 50300) { |
| 85 |
return json_encode($value); |
| 86 |
} elseif (PHP_VERSION_ID < 50500) { |
| 87 |
return json_encode($value, $options); |
| 88 |
} else { |
| 89 |
return json_encode($value, $options, $depth); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// @codeCoverageIgnoreStart |
| 94 |
return self::_json_encode($value, $depth); |
| 95 |
// @codeCoverageIgnoreEnd |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* @param mixed $value |
| 100 |
* @param int $depth Set the maximum depth |
| 101 |
* @return string|false |
| 102 |
*/ |
| 103 |
public static function _json_encode($value, $depth = 513) |
| 104 |
{ |
| 105 |
if (ini_get('xdebug.extended_info') !== false) { |
| 106 |
ini_set('xdebug.max_nesting_level', 2048); |
| 107 |
} |
| 108 |
return self::_json_encode_lowlevel($value, $depth); |
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Implementation taken from |
| 113 |
* http://www.mike-griffiths.co.uk/php-json_encode-alternative/ |
| 114 |
* |
| 115 |
* @param mixed $value |
| 116 |
* @param int $depth Set the maximum depth |
| 117 |
* @return string|false |
| 118 |
*/ |
| 119 |
private static function _json_encode_lowlevel($value, $depth) |
| 120 |
{ |
| 121 |
static $jsonReplaces = array( |
| 122 |
array('\\', '/', "\n", "\t", "\r", "\f", '"'), |
| 123 |
array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\f', '\"')); |
| 124 |
|
| 125 |
if (is_null($value)) { |
| 126 |
return 'null'; |
| 127 |
} |
| 128 |
if ($value === false) { |
| 129 |
return 'false'; |
| 130 |
} |
| 131 |
if ($value === true) { |
| 132 |
return 'true'; |
| 133 |
} |
| 134 |
|
| 135 |
if (is_scalar($value)) { |
| 136 |
// Always use '.' for floats. |
| 137 |
if (is_float($value)) { |
| 138 |
return floatval(str_replace(',', '.', strval($value))); |
| 139 |
} |
| 140 |
if (is_string($value)) { |
| 141 |
return sprintf('"%s"', |
| 142 |
str_replace($jsonReplaces[0], $jsonReplaces[1], $value)); |
| 143 |
} else { |
| 144 |
return $value; |
| 145 |
} |
| 146 |
} elseif ($depth <= 1) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$isList = true; |
| 151 |
for ($i = 0, reset($value); $i<count($value); $i++, next($value)) { |
| 152 |
if (key($value) !== $i) { |
| 153 |
$isList = false; |
| 154 |
break; |
| 155 |
} |
| 156 |
} |
| 157 |
$result = array(); |
| 158 |
if ($isList) { |
| 159 |
foreach ($value as $v) { |
| 160 |
$this_value = self::_json_encode($v, $depth - 1); |
| 161 |
if ($this_value === false) { |
| 162 |
return false; |
| 163 |
} |
| 164 |
$result[] = $this_value; |
| 165 |
} |
| 166 |
|
| 167 |
return '[' . join(',', $result) . ']'; |
| 168 |
} else { |
| 169 |
foreach ($value as $k => $v) { |
| 170 |
$this_value = self::_json_encode($v, $depth - 1); |
| 171 |
if ($this_value === false) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
$result[] = self::_json_encode($k, $depth - 1).':'.$this_value; |
| 175 |
} |
| 176 |
|
| 177 |
return '{' . join(',', $result) . '}'; |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|