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