Compat.php 5.1 KB

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