Passed
Branch master (8dda47)
by Taosikai
28:32 queued 13:47
created

Utils::toUint32val()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 13
nc 18
nop 1
dl 0
loc 20
rs 8.2222
c 0
b 0
f 0
1
<?php
2
/**
3
 * SmartQQ Library
4
 * @author Tao <[email protected]>
5
 */
6
namespace Slince\SmartQQ;
7
8
use Symfony\Component\Filesystem\Filesystem;
9
10
class Utils
11
{
12
    /**
13
     * @var Filesystem
14
     */
15
    protected static $filesystem;
16
17
    /**
18
     * Get filesystem
19
     * @return Filesystem
20
     */
21
    public static function getFilesystem()
22
    {
23
        if (is_null(static::$filesystem)) {
24
            static::$filesystem = new Filesystem();
25
        }
26
        return static::$filesystem;
27
    }
28
29
    /**
30
     * hash
31
     * @param int $uin
32
     * @param string $ptWebQQ
33
     * @return string
34
     */
35
    public static function hash($uin, $ptWebQQ)
36
    {
37
        $x = array(
38
            0, $uin >> 24 & 0xff ^ 0x45,
39
            0, $uin >> 16 & 0xff ^ 0x43,
40
            0, $uin >> 8 & 0xff ^ 0x4f,
41
            0, $uin & 0xff ^ 0x4b,
42
        );
43
        for ($i = 0; $i < 64; ++$i) {
44
            $x[($i & 3) << 1] ^= ord(substr($ptWebQQ, $i, 1));
45
        }
46
        $hex = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');
47
        $hash = '';
48
        for ($i = 0; $i < 8; ++$i) {
49
            $hash .= $hex[$x[$i] >> 4 & 0xf] . $hex[$x[$i] & 0xf];
50
        }
51
        return $hash;
52
    }
53
54
    /**
55
     * 生成ptqrtoken的哈希函数
56
     * @param string $string
57
     * @return int
58
     */
59
    public static function hash33($string)
60
    {
61
        $e = 0;
62
        $n = strlen($string);
63
        for ($i = 0; $n > $i; ++$i) {
64
            $e = static::toUint32val($e);
65
            $e += ($e << 5) + static::charCodeAt($string, $i);
66
        }
67
        return 2147483647 & $e;
68
    }
69
70
    /**
71
     * 入转换为32位无符号整数,若溢出,则只保留低32位
72
     * @link http://outofmemory.cn/code-snippet/18291/out-switch-32-place-sign-integer-spill-maintain-32-place
73
     * @param mixed $var
74
     * @return float|int|string
75
     */
76
    public static function toUint32val($var)
77
    {
78
        if (is_string($var)) {
79
            if (PHP_INT_MAX > 2147483647) {
80
                $var = intval($var);
81
            } else {
82
                $var = floatval($var);
83
            }
84
        }
85
        if (!is_int($var)) {
86
            $var = intval($var);
87
        }
88
        if ((0 > $var) || ($var > 4294967295)) {
89
            $var &= 4294967295;
90
            if (0 > $var) {
91
                $var = sprintf('%u', $var);
92
            }
93
        }
94
        return $var;
95
    }
96
97
    /**
98
     * 计算字符的unicode,类似js中charCodeAt
99
     * [Link](http://www.phpjiayuan.com/90/225.html)
100
     * @param string $str
101
     * @param int $index
102
     * @return null|number
103
     */
104
    public static function charCodeAt($str, $index)
105
    {
106
        $char = mb_substr($str, $index, 1, 'UTF-8');
107
        if (mb_check_encoding($char, 'UTF-8')) {
108
            $ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
109
            return hexdec(bin2hex($ret));
110
        } else {
111
            return null;
112
        }
113
    }
114
115
    /**
116
     * 获取当前时间的毫秒数
117
     * @return float
118
     */
119
    public static function getMillisecond()
120
    {
121
        list($s1, $s2) = explode(' ', microtime());
122
        return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
123
    }
124
125
    /**
126
     * 用于发送消息时生成msg id
127
     * @return int
128
     */
129
    public static function makeMsgId()
130
    {
131
        static $sequence = 0;
132
        static $t = 0;
133
        if (!$t) {
134
            $t = static::getMillisecond();
135
            $t = ($t - $t % 1000) / 1000;
136
            $t = $t % 10000 * 10000;
137
        }
138
        //获取msgId
139
        $sequence++;
140
        return $t + $sequence;
141
    }
142
}
143