Repository navigation
Expand file tree
/
Copy pathZalyHelper.php
More file actions
224 lines (190 loc) · 6.33 KB
/
Copy pathZalyHelper.php
File metadata and controls
224 lines (190 loc) · 6.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
/**
* Created by PhpStorm.
* User: childeYin<尹少爷>
* Date: 17/07/2018
* Time: 9:57 AM
*/
class ZalyHelper
{
/*
* php 毫秒
*/
public static function getMsectime()
{
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
return $msectime;
}
public function getCurrentTimeSeconds()
{
list($msec, $sec) = explode(' ', microtime());
$msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)));
return $msectime;
}
public static function getMsgId($type, $toId)
{
if($type === 1) {
$timeMillis = self::getMsectime();
$msgId = "U2-" . substr($toId, 0, 8) . "-" . $timeMillis;
} else {
$timeMillis = self::getMsectime();
$msgId = "GP-";
if (!empty($toId)) {
$msgId .= substr($toId, 0, 8);
} else {
$randomStr = self::generateStrKey(8);
$msgId .= $randomStr;
}
$msgId .= "-" . $timeMillis;
}
return $msgId;
}
public static function generateStrId()
{
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
}
public static function generateStrKey($length = 16, $strParams = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
if (!is_int($length) || $length < 0) {
$length = 16;
}
$str = '';
for ($i = $length; $i > 0; $i--) {
$str .= $strParams[mt_rand(0, strlen($strParams) - 1)];
}
return $str;
}
public static function generateNumberKey($length = 16, $strParams = '0123456789')
{
if (!is_int($length) || $length < 0) {
$length = 16;
}
$str = '';
for ($i = $length; $i > 0; $i--) {
$str .= $strParams[mt_rand(0, strlen($strParams) - 1)];
}
return $str;
}
public function judgeOrigin()
{
//获取USER AGENT
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
//分析数据
$isWeb = (strpos($agent, 'windows nt')) ? true : false;
$isIphone = (strpos($agent, 'iphone')) ? true : false;
$isAndroid = (strpos($agent, 'android')) ? true : false;
//输出数据
if ($isWeb) {
return 1;
}
if ($isIphone || $isAndroid) {
return 0;
}
}
public static function quickSortMsg($arr)
{
$len = count($arr);
if ($len <= 1) {
return $arr;
}
$key = $arr[0];
$arrLeft = array();
$arrRight = array();
for ($i = 1; $i < $len; $i++) {
if ($arr[$i]["msgTime"] <= $key["msgTime"]) {
$arrLeft[] = $arr[$i];
} else {
$arrRight[] = $arr[$i];
}
}
$arrLeft = self::quickSortMsg($arrLeft);
$arrRight = self::quickSortMsg($arrRight);
return array_merge($arrRight, array($key), $arrLeft);
}
public static function hideMobile($phone)
{
$isMob = "/^1[0-9]{1}\d{9}$/";
if (preg_match($isMob, $phone)) {
$phone = substr_replace($phone, '****', 3, 4);
}
return $phone;
}
public static function checkOpensslEncryptExists()
{
if (!function_exists("openssl_encrypt")) {
return false;
}
return true;
}
public static function isEmail($email)
{
return preg_match("/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/", $email);
}
public static function isPhoneNumber($phoneNumber)
{
return preg_match("/^1[3456789]{1}\d{9}$/", $phoneNumber);
}
public static function getFullReqUrl($reqUrl)
{
try {
$reqUrlStruct = parse_url($reqUrl);
if (!empty($reqUrlStruct["scheme"])) {
$query = !empty($reqUrlStruct["query"]) ? "?" . $reqUrlStruct["query"] : "";
$reqUrl = $reqUrlStruct["path"] . $query;
}
$defaultScheme = "http";
if ((!empty($_SERVER['REQUEST_SCHEME']) && $_SERVER['REQUEST_SCHEME'] == 'https') ||
(!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ||
(!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443')) {
$defaultScheme = 'https';
}
$defaultHost = $_SERVER['HTTP_HOST'];
$schema = "";
$host = "";
// 必须用scheme,防止用户多输//
if (empty($reqUrlStruct["scheme"]) && empty($reqUrlStruct['host'])) {
$schema = $defaultScheme;
$host = $defaultHost;
} else {
$schema = $reqUrlStruct["scheme"];
$host = $reqUrlStruct["host"];
$port = empty($reqUrlStruct["port"]) ? "" : ":{$reqUrlStruct["port"]}";
$host = $host . $port;
}
if (strpos($reqUrl, "/") == 0) {
$fullUrl = "{$schema}://{$host}{$reqUrl}";
} elseif (strpos($reqUrl, "./") == 0) {
$reqUrl = str_replace("./", "/", $reqUrl);
$fullUrl = "{$schema}://{$host}{$reqUrl}";
} else {
$fullUrl = "{$schema}://{$host}/{$reqUrl}";
}
return $fullUrl;
} catch (Exception $ex) {
return $reqUrl;
}
}
public static function isUicNumber($str)
{
return preg_match("/^[0-9]{6,20}$/", $str);
}
public static function isLoginName($str)
{
return preg_match("/^[A-Za-z0-9_]+$/", $str);
}
}