PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/app/protected/extensions/sentrylog/lib/Raven/Compat.php

https://bitbucket.org/nn2/zurmo
PHP | 133 lines | 95 code | 17 blank | 21 comment | 17 complexity | f10f3dbe92b9050164d3d3aee2878241 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-2-Clause, AGPL-3.0, BSD-3-Clause, GPL-2.0
  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. * See http://nl2.php.net/manual/en/function.hash-hmac.php
  33. */
  34. public static function _hash_hmac($algo, $data, $key, $raw_output=false)
  35. {
  36. $algo = strtolower($algo);
  37. $pack = 'H'.strlen($algo('test'));
  38. $size = 64;
  39. $opad = str_repeat(chr(0x5C), $size);
  40. $ipad = str_repeat(chr(0x36), $size);
  41. if (strlen($key) > $size) {
  42. $key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));
  43. } else {
  44. $key = str_pad($key, $size, chr(0x00));
  45. }
  46. for ($i = 0; $i < strlen($key) - 1; $i++) {
  47. $opad[$i] = $opad[$i] ^ $key[$i];
  48. $ipad[$i] = $ipad[$i] ^ $key[$i];
  49. }
  50. $output = $algo($opad.pack($pack, $algo($ipad.$data)));
  51. return ($raw_output) ? pack($pack, $output) : $output;
  52. }
  53. /**
  54. * Note that we discard the options given to be compatible
  55. * with PHP < 5.3
  56. */
  57. public static function json_encode($value, $options=0)
  58. {
  59. if (function_exists('json_encode')) {
  60. return json_encode($value);
  61. }
  62. return self::_json_encode($value);
  63. }
  64. /**
  65. * Implementation taken from
  66. * http://www.mike-griffiths.co.uk/php-json_encode-alternative/
  67. */
  68. public static function _json_encode($value)
  69. {
  70. static $jsonReplaces = array(
  71. array('\\', '/', "\n", "\t", "\r", "\b", "\f", '"'),
  72. array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  73. if (is_null($value)) {
  74. return 'null';
  75. }
  76. if ($value === false) {
  77. return 'false';
  78. }
  79. if ($value === true) {
  80. return 'true';
  81. }
  82. if (is_scalar($value)) {
  83. // Always use '.' for floats.
  84. if (is_float($value)) {
  85. return floatval(str_replace(',', '.', strval($value)));
  86. }
  87. if (is_string($value)) {
  88. return sprintf('"%s"',
  89. str_replace($jsonReplaces[0], $jsonReplaces[1], $value));
  90. }
  91. else {
  92. return $value;
  93. }
  94. }
  95. $isList = true;
  96. for ($i = 0, reset($value); true; $i++) {
  97. if (key($value) !== $i) {
  98. $isList = false;
  99. break;
  100. }
  101. }
  102. $result = array();
  103. if ($isList) {
  104. foreach ($value as $v) {
  105. $result[] = self::_json_encode($v);
  106. }
  107. return '[' . join(',', $result) . ']';
  108. }
  109. else {
  110. foreach ($value as $k => $v) {
  111. $result[] = self::_json_encode($k) . ':' . self::_json_encode($v);
  112. }
  113. return '{' . join(',', $result) . '}';
  114. }
  115. }
  116. }