PageRenderTime 27ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/ddonthula/zurmouser
PHP | 133 lines | 95 code | 17 blank | 21 comment | 17 complexity | f10f3dbe92b9050164d3d3aee2878241 MD5 | raw file
  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 
 12class Raven_Compat
 13{
 14
 15    public static function gethostname()
 16    {
 17        if (function_exists('gethostname')) {
 18            return gethostname();
 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        return self::_hash_hmac($algo, $data, $key, $raw_output);
 34    }
 35
 36    /**
 37     * Implementation from 'KC Cloyd'.
 38     * See http://nl2.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
 48        if (strlen($key) > $size) {
 49            $key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));
 50        } else {
 51            $key = str_pad($key, $size, chr(0x00));
 52        }
 53
 54        for ($i = 0; $i < strlen($key) - 1; $i++) {
 55            $opad[$i] = $opad[$i] ^ $key[$i];
 56            $ipad[$i] = $ipad[$i] ^ $key[$i];
 57        }
 58
 59        $output = $algo($opad.pack($pack, $algo($ipad.$data)));
 60
 61        return ($raw_output) ? pack($pack, $output) : $output;
 62    }
 63
 64    /**
 65     * Note that we discard the options given to be compatible
 66     * with PHP < 5.3
 67     */
 68    public static function json_encode($value, $options=0)
 69    {
 70        if (function_exists('json_encode')) {
 71            return json_encode($value);
 72        }
 73        return self::_json_encode($value);
 74    }
 75
 76    /** 
 77     * Implementation taken from
 78     * http://www.mike-griffiths.co.uk/php-json_encode-alternative/
 79     */
 80    public static function _json_encode($value)
 81    {
 82        static $jsonReplaces = array(
 83            array('\\', '/', "\n", "\t", "\r", "\b", "\f", '"'), 
 84            array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
 85
 86        if (is_null($value)) {
 87            return 'null';
 88        }
 89        if ($value === false) {
 90            return 'false';
 91        }
 92        if ($value === true) {
 93            return 'true';
 94        }
 95
 96        if (is_scalar($value)) {
 97            
 98            // Always use '.' for floats.
 99            if (is_float($value)) {
100                return floatval(str_replace(',', '.', strval($value)));
101            }
102            if (is_string($value)) {
103                return sprintf('"%s"', 
104                    str_replace($jsonReplaces[0], $jsonReplaces[1], $value));
105            }
106            else {
107                return $value;
108            }
109        }
110
111        $isList = true;
112        for ($i = 0, reset($value); true; $i++) { 
113            if (key($value) !== $i) {
114                $isList = false;
115                break;
116            }
117        }
118        $result = array();
119        if ($isList) {
120            foreach ($value as $v) {
121                $result[] = self::_json_encode($v);
122            }
123            return '[' . join(',', $result) . ']';
124        } 
125        else {
126            foreach ($value as $k => $v) {
127                $result[] = self::_json_encode($k) . ':' . self::_json_encode($v);
128            }
129            return '{' . join(',', $result) . '}';
130        }
131    }
132}
133