@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | //内容写入文件 |
63 | 63 | if($file_append === false){ |
64 | 64 | file_put_contents($file_name,$content); |
65 | - }else{ |
|
65 | + } else{ |
|
66 | 66 | file_put_contents($file_name,$content,FILE_APPEND); |
67 | 67 | } |
68 | 68 | } |
@@ -92,7 +92,7 @@ discard block |
||
92 | 92 | $handle = fopen($file_name, 'w'); |
93 | 93 | fwrite($handle, $content); |
94 | 94 | fclose($handle); |
95 | - }else{ |
|
95 | + } else{ |
|
96 | 96 | $handle = fopen($file_name, 'a'); |
97 | 97 | fwrite($handle, $content); |
98 | 98 | fclose($handle); |
@@ -4,8 +4,8 @@ discard block |
||
4 | 4 | * 文件操作类 |
5 | 5 | * logFilePath: /storage/tinymeng/log/ |
6 | 6 | */ |
7 | -define('logFilePath',dirname(dirname(dirname(dirname(__DIR__)))).DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'tinymeng'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR); |
|
8 | -class File{ |
|
7 | +define('logFilePath', dirname(dirname(dirname(dirname(__DIR__)))).DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.'tinymeng'.DIRECTORY_SEPARATOR.'log'.DIRECTORY_SEPARATOR); |
|
8 | +class File { |
|
9 | 9 | |
10 | 10 | /** |
11 | 11 | * Name: writeLog |
@@ -16,27 +16,27 @@ discard block |
||
16 | 16 | * @param \Exception|null $exception |
17 | 17 | * @return bool |
18 | 18 | */ |
19 | - static public function writeLog($message, $file_name='error',bool $echo = false,\Exception $exception = null){ |
|
20 | - if(!is_string($message)){ |
|
19 | + static public function writeLog($message, $file_name = 'error', bool $echo = false, \Exception $exception = null) { |
|
20 | + if (!is_string($message)) { |
|
21 | 21 | $message = json_encode($message); |
22 | 22 | } |
23 | 23 | list($msec, $sec) = explode(' ', microtime()); |
24 | - $message = date('Y-m-d H:i:s',$sec).'.'.str_replace('0.','',$msec).' : '.$message.PHP_EOL; |
|
25 | - if($exception && $exception instanceof \Exception){ |
|
24 | + $message = date('Y-m-d H:i:s', $sec).'.'.str_replace('0.', '', $msec).' : '.$message.PHP_EOL; |
|
25 | + if ($exception && $exception instanceof \Exception) { |
|
26 | 26 | $message .= ' File: '.$exception->getFile().' ,Line: '.$exception->getLine().' ,Message: '.$exception->getMessage(); |
27 | 27 | } |
28 | - if($echo){ |
|
28 | + if ($echo) { |
|
29 | 29 | echo $message; |
30 | 30 | } |
31 | 31 | $path = logFilePath; |
32 | 32 | if (!is_dir($path)) { |
33 | - if(!mkdir($path, 0755, true)){ |
|
33 | + if (!mkdir($path, 0755, true)) { |
|
34 | 34 | die('创建缓存文件夹"'.$path.'"失败!'); |
35 | 35 | } |
36 | 36 | } |
37 | 37 | |
38 | 38 | $file_name = $path.$file_name; |
39 | - self::filePutContents($file_name."-".date('Ymd',time()).".log",$message,true); |
|
39 | + self::filePutContents($file_name."-".date('Ymd', time()).".log", $message, true); |
|
40 | 40 | return true; |
41 | 41 | } |
42 | 42 | |
@@ -49,10 +49,10 @@ discard block |
||
49 | 49 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
50 | 50 | * http://blog.majiameng.com/article/2724.html |
51 | 51 | */ |
52 | - static public function filePutContents(string $file_name,string $content,bool $file_append = false){ |
|
53 | - if(strrpos($file_name,'/')){ |
|
52 | + static public function filePutContents(string $file_name, string $content, bool $file_append = false) { |
|
53 | + if (strrpos($file_name, '/')) { |
|
54 | 54 | //获取文件夹路径 |
55 | - $dir_name = substr($file_name,0,strrpos($file_name,'/')); |
|
55 | + $dir_name = substr($file_name, 0, strrpos($file_name, '/')); |
|
56 | 56 | //创建文件夹 |
57 | 57 | self::mkdir($dir_name); |
58 | 58 | } |
@@ -61,10 +61,10 @@ discard block |
||
61 | 61 | self::chmod($file_name); |
62 | 62 | |
63 | 63 | //内容写入文件 |
64 | - if($file_append === false){ |
|
65 | - file_put_contents($file_name,$content); |
|
66 | - }else{ |
|
67 | - file_put_contents($file_name,$content,FILE_APPEND); |
|
64 | + if ($file_append === false) { |
|
65 | + file_put_contents($file_name, $content); |
|
66 | + } else { |
|
67 | + file_put_contents($file_name, $content, FILE_APPEND); |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
@@ -77,10 +77,10 @@ discard block |
||
77 | 77 | * file_put_contents和fopen,fwrite,fclose三个组合的区别 |
78 | 78 | * http://blog.majiameng.com/article/2724.html |
79 | 79 | */ |
80 | - static public function fWrite(string $file_name,string $content,bool $file_append = false){ |
|
81 | - if(strrpos($file_name,'/')){ |
|
80 | + static public function fWrite(string $file_name, string $content, bool $file_append = false) { |
|
81 | + if (strrpos($file_name, '/')) { |
|
82 | 82 | //获取文件夹路径 |
83 | - $dir_name = substr($file_name,0,strrpos($file_name,'/')); |
|
83 | + $dir_name = substr($file_name, 0, strrpos($file_name, '/')); |
|
84 | 84 | //创建文件夹 |
85 | 85 | self::mkdir($dir_name); |
86 | 86 | } |
@@ -89,11 +89,11 @@ discard block |
||
89 | 89 | self::chmod($file_name); |
90 | 90 | |
91 | 91 | //内容写入文件 |
92 | - if($file_append === false){ |
|
92 | + if ($file_append === false) { |
|
93 | 93 | $handle = fopen($file_name, 'w'); |
94 | 94 | fwrite($handle, $content); |
95 | 95 | fclose($handle); |
96 | - }else{ |
|
96 | + } else { |
|
97 | 97 | $handle = fopen($file_name, 'a'); |
98 | 98 | fwrite($handle, $content); |
99 | 99 | fclose($handle); |
@@ -107,9 +107,9 @@ discard block |
||
107 | 107 | * @param $dir_name |
108 | 108 | * @return bool |
109 | 109 | */ |
110 | - static public function mkdir(string $dir_name){ |
|
110 | + static public function mkdir(string $dir_name) { |
|
111 | 111 | if (!is_dir($dir_name)) { |
112 | - if(!mkdir($dir_name, 0755, true)){ |
|
112 | + if (!mkdir($dir_name, 0755, true)) { |
|
113 | 113 | die('创建缓存文件夹"'.$dir_name.'"失败!'); |
114 | 114 | } |
115 | 115 | } |
@@ -123,7 +123,7 @@ discard block |
||
123 | 123 | * @return bool |
124 | 124 | */ |
125 | 125 | static public function delDir(string $dir) { |
126 | - if(!file_exists($dir)){//文件不存在 |
|
126 | + if (!file_exists($dir)) {//文件不存在 |
|
127 | 127 | return true; |
128 | 128 | } |
129 | 129 | if (!is_dir($dir)) { |
@@ -134,9 +134,9 @@ discard block |
||
134 | 134 | //先删除目录下的文件 |
135 | 135 | $dh = opendir($dir); |
136 | 136 | while ($file = readdir($dh)) { |
137 | - if($file != "." && $file!="..") { |
|
137 | + if ($file != "." && $file != "..") { |
|
138 | 138 | $full_path = $dir."/".$file; |
139 | - if(!is_dir($full_path)) { |
|
139 | + if (!is_dir($full_path)) { |
|
140 | 140 | unlink($full_path); |
141 | 141 | } else { |
142 | 142 | self::delDir($full_path); |
@@ -146,7 +146,7 @@ discard block |
||
146 | 146 | closedir($dh); |
147 | 147 | |
148 | 148 | //删除当前文件夹: |
149 | - if(rmdir($dir)) { |
|
149 | + if (rmdir($dir)) { |
|
150 | 150 | return true; |
151 | 151 | } else { |
152 | 152 | return false; |
@@ -159,9 +159,9 @@ discard block |
||
159 | 159 | * @param $file_name |
160 | 160 | * @param int $mode |
161 | 161 | */ |
162 | - static public function chmod($file_name,$mode = 0755){ |
|
163 | - if (file_exists($file_name)){ |
|
164 | - @chmod($file_name,$mode); |
|
162 | + static public function chmod($file_name, $mode = 0755) { |
|
163 | + if (file_exists($file_name)) { |
|
164 | + @chmod($file_name, $mode); |
|
165 | 165 | } |
166 | 166 | } |
167 | 167 | |
@@ -175,14 +175,14 @@ discard block |
||
175 | 175 | static public function move(string $file, string $new_file): bool |
176 | 176 | { |
177 | 177 | //文件是否存在 |
178 | - if(!file_exists($file)){ |
|
178 | + if (!file_exists($file)) { |
|
179 | 179 | return false; |
180 | 180 | } |
181 | 181 | |
182 | 182 | //新文件目录 |
183 | - if(strrpos($new_file,'/')){ |
|
183 | + if (strrpos($new_file, '/')) { |
|
184 | 184 | //获取文件夹路径 |
185 | - $dir_name = substr($new_file,0,strrpos($new_file,'/')); |
|
185 | + $dir_name = substr($new_file, 0, strrpos($new_file, '/')); |
|
186 | 186 | //创建文件夹 |
187 | 187 | self::mkdir($dir_name); |
188 | 188 | } |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | //添加文件权限 |
191 | 191 | self::chmod($dir_name); |
192 | 192 | |
193 | - copy($file,$new_file); //拷贝到新目录 |
|
193 | + copy($file, $new_file); //拷贝到新目录 |
|
194 | 194 | unlink($file); //删除旧目录下的文件 |
195 | 195 | |
196 | 196 | return true; |
@@ -110,7 +110,7 @@ |
||
110 | 110 | if ($len <= $split_len) { |
111 | 111 | return array($str); |
112 | 112 | } |
113 | - preg_match_all('/.{' . $split_len . '}|[^\x00]{1,' . $split_len . '}$/us', $str, $ar); |
|
113 | + preg_match_all('/.{'.$split_len.'}|[^\x00]{1,'.$split_len.'}$/us', $str, $ar); |
|
114 | 114 | |
115 | 115 | return $ar[0]; |
116 | 116 | } |
@@ -40,16 +40,16 @@ discard block |
||
40 | 40 | static public function createChannelId($namespace = ''):string { |
41 | 41 | static $guid = ''; |
42 | 42 | $uid = uniqid("", true); |
43 | - $data = $namespace. md5(time() . mt_rand(1,1000000)).uniqid(); |
|
44 | - $hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data))); |
|
45 | - $guid = substr($hash, 0, 8) . |
|
46 | - '-' . |
|
47 | - substr($hash, 8, 4) . |
|
48 | - '-' . |
|
49 | - substr($hash, 12, 4) . |
|
50 | - '-' . |
|
51 | - substr($hash, 16, 4) . |
|
52 | - '-' . |
|
43 | + $data = $namespace.md5(time().mt_rand(1, 1000000)).uniqid(); |
|
44 | + $hash = strtoupper(hash('ripemd128', $uid.$guid.md5($data))); |
|
45 | + $guid = substr($hash, 0, 8). |
|
46 | + '-'. |
|
47 | + substr($hash, 8, 4). |
|
48 | + '-'. |
|
49 | + substr($hash, 12, 4). |
|
50 | + '-'. |
|
51 | + substr($hash, 16, 4). |
|
52 | + '-'. |
|
53 | 53 | substr($hash, 20, 12); |
54 | 54 | return $guid; |
55 | 55 | } |
@@ -62,7 +62,7 @@ discard block |
||
62 | 62 | * @return string |
63 | 63 | */ |
64 | 64 | static public function md5Bit16($str):string { |
65 | - return strtoupper(substr(md5($str),8,16)); |
|
65 | + return strtoupper(substr(md5($str), 8, 16)); |
|
66 | 66 | } |
67 | 67 | |
68 | 68 | /** |
@@ -72,13 +72,13 @@ discard block |
||
72 | 72 | * @return int |
73 | 73 | */ |
74 | 74 | static public function millisecond($time = null) :int{ |
75 | - if(empty($time)){ |
|
75 | + if (empty($time)) { |
|
76 | 76 | list($msec, $sec) = explode(' ', microtime()); |
77 | - $millisecond = (int)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); |
|
78 | - }else{ |
|
77 | + $millisecond = (int) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); |
|
78 | + } else { |
|
79 | 79 | $millisecond = strtotime($time)."000"; |
80 | 80 | } |
81 | - return (int)$millisecond; |
|
81 | + return (int) $millisecond; |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | /** |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | * @param string $string |
88 | 88 | * @return bool |
89 | 89 | */ |
90 | - public static function isContainChinese($string=''):bool { |
|
90 | + public static function isContainChinese($string = ''):bool { |
|
91 | 91 | $result = preg_match('/[\x{4e00}-\x{9fa5}]/u', $string); |
92 | 92 | return $result == 0 ? false : true; |
93 | 93 | } |
@@ -98,7 +98,7 @@ discard block |
||
98 | 98 | * @param string $string |
99 | 99 | * @return bool |
100 | 100 | */ |
101 | - public static function isAllChinese($string=''):bool { |
|
101 | + public static function isAllChinese($string = ''):bool { |
|
102 | 102 | $result = preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $string); |
103 | 103 | return $result == 0 ? false : true; |
104 | 104 | } |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | * @param string $string |
111 | 111 | * @return bool |
112 | 112 | */ |
113 | - public static function isMobile($string=''):bool { |
|
113 | + public static function isMobile($string = ''):bool { |
|
114 | 114 | if (!preg_match("/(^1[3|4|5|7|8][0-9]{9}$)/", $string)) { |
115 | 115 | return false; |
116 | 116 | } |
@@ -125,9 +125,9 @@ discard block |
||
125 | 125 | * @param int $double 小数点保留位数 默认3位 |
126 | 126 | * @return string |
127 | 127 | */ |
128 | - public static function sctonum($num, $double = 3){ |
|
129 | - if(false !== stripos($num, "e")){ |
|
130 | - $a = explode("e",strtolower($num)); |
|
128 | + public static function sctonum($num, $double = 3) { |
|
129 | + if (false !== stripos($num, "e")) { |
|
130 | + $a = explode("e", strtolower($num)); |
|
131 | 131 | return bcmul($a[0], bcpow(10, $a[1], $double), $double); |
132 | 132 | } |
133 | 133 | return $num; |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | * @param string $to |
142 | 142 | * @return array|false|string|string[]|null |
143 | 143 | */ |
144 | - public static function autoCharset($string, $from='gbk', $to='utf-8') { |
|
144 | + public static function autoCharset($string, $from = 'gbk', $to = 'utf-8') { |
|
145 | 145 | $from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from; |
146 | 146 | $to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to; |
147 | 147 | if (strtoupper($from) === strtoupper($to) || empty($string) || (is_scalar($string) && !is_string($string))) { |
@@ -177,7 +177,7 @@ discard block |
||
177 | 177 | * @param $html |
178 | 178 | * @return string |
179 | 179 | */ |
180 | - public static function filterATag($html=''):string { |
|
180 | + public static function filterATag($html = ''):string { |
|
181 | 181 | return preg_replace("#<a[^>]*>(.*?)</a>#is", "$1", $html); |
182 | 182 | } |
183 | 183 | |
@@ -188,7 +188,7 @@ discard block |
||
188 | 188 | * @param $html |
189 | 189 | * @return string |
190 | 190 | */ |
191 | - public static function deleteATag($html=''):string { |
|
191 | + public static function deleteATag($html = ''):string { |
|
192 | 192 | return preg_replace("#<a[^>]*>(.*?)</a>#is", "", $html); |
193 | 193 | } |
194 | 194 | |
@@ -200,36 +200,36 @@ discard block |
||
200 | 200 | * @param bool $is_timestamp 是否是时间戳 |
201 | 201 | * @return string |
202 | 202 | */ |
203 | - public static function getTime($date,$is_timestamp=false):string { |
|
204 | - if($is_timestamp == true){ |
|
203 | + public static function getTime($date, $is_timestamp = false):string { |
|
204 | + if ($is_timestamp == true) { |
|
205 | 205 | $time = $date; |
206 | - }else{ |
|
207 | - $time = strtotime($date);//时间转换为时间戳 |
|
206 | + } else { |
|
207 | + $time = strtotime($date); //时间转换为时间戳 |
|
208 | 208 | } |
209 | 209 | |
210 | - if($time >= time()){ |
|
210 | + if ($time >= time()) { |
|
211 | 211 | return '刚刚'; |
212 | 212 | } |
213 | 213 | $seconds = time() - $time; |
214 | - if($seconds <= 60){ |
|
214 | + if ($seconds <= 60) { |
|
215 | 215 | return '刚刚'; |
216 | 216 | } |
217 | 217 | $minutes = intval($seconds / 60); |
218 | - if($minutes <= 60){ |
|
218 | + if ($minutes <= 60) { |
|
219 | 219 | return $minutes.'分钟前'; |
220 | 220 | } |
221 | 221 | $hours = intval($minutes / 60); |
222 | - if($hours <= 24){ |
|
222 | + if ($hours <= 24) { |
|
223 | 223 | return $hours.'小时前'; |
224 | 224 | } |
225 | 225 | $days = intval($hours / 24); |
226 | - if($days <= 3){ |
|
226 | + if ($days <= 3) { |
|
227 | 227 | return $days.'天前'; |
228 | 228 | } |
229 | - if($days <= 365){ |
|
230 | - return date('m-d',$time); |
|
229 | + if ($days <= 365) { |
|
230 | + return date('m-d', $time); |
|
231 | 231 | } |
232 | - return date('Y-m-d',$time); |
|
232 | + return date('Y-m-d', $time); |
|
233 | 233 | } |
234 | 234 | |
235 | 235 | /** |
@@ -243,11 +243,11 @@ discard block |
||
243 | 243 | $compress = ''; |
244 | 244 | foreach ($chunks as $c) { |
245 | 245 | if (strtolower(substr($c, 0, 19)) == '<!--<nocompress>-->') { |
246 | - $c = substr($c, 19, strlen($c) - 19 - 20); |
|
246 | + $c = substr($c, 19, strlen($c) - 19 - 20); |
|
247 | 247 | $compress .= $c; |
248 | 248 | continue; |
249 | 249 | } elseif (strtolower(substr($c, 0, 12)) == '<nocompress>') { |
250 | - $c = substr($c, 12, strlen($c) - 12 - 13); |
|
250 | + $c = substr($c, 12, strlen($c) - 12 - 13); |
|
251 | 251 | $compress .= $c; |
252 | 252 | continue; |
253 | 253 | } elseif (strtolower(substr($c, 0, 4)) == '<pre' || strtolower(substr($c, 0, 9)) == '<textarea') { |
@@ -295,14 +295,14 @@ discard block |
||
295 | 295 | * @return mixed |
296 | 296 | */ |
297 | 297 | static public function htmlReplaceXcx(string $content):string { |
298 | - $content = str_replace("\r\n","",$content);//出除回车和换行符 |
|
299 | - $content = preg_replace("/style=\".*?\"/si",'',$content);//style样式 |
|
300 | - $content = preg_replace(["/<strong.*?>/si", "/<\/strong>/si"],['<text class="wx-strong">','</text>'],$content);//strong |
|
301 | - $content = preg_replace(["/<p.*?>/si", "/<\/p>/si"],['<view class="wx-p">','</view>'],$content);//p |
|
302 | - $content = preg_replace(["/<a.*?>/si", "/<\/a>/si"],['<text class="wx-a">','</text>'],$content);//a |
|
303 | - $content = preg_replace(["/<span.*?>/si", "/<\/span>/si"],['<text class="wx-span">','</text>'],$content);//span |
|
304 | - $content = preg_replace(["/<h[1-6].*?>/si", "/<\/h[1-6]>/si"],['<view class="wx-h">','</view>'],$content);//h |
|
305 | - $content = preg_replace("/<img.*?/si",'<image class="wx-img"',$content);//img |
|
298 | + $content = str_replace("\r\n", "", $content); //出除回车和换行符 |
|
299 | + $content = preg_replace("/style=\".*?\"/si", '', $content); //style样式 |
|
300 | + $content = preg_replace(["/<strong.*?>/si", "/<\/strong>/si"], ['<text class="wx-strong">', '</text>'], $content); //strong |
|
301 | + $content = preg_replace(["/<p.*?>/si", "/<\/p>/si"], ['<view class="wx-p">', '</view>'], $content); //p |
|
302 | + $content = preg_replace(["/<a.*?>/si", "/<\/a>/si"], ['<text class="wx-a">', '</text>'], $content); //a |
|
303 | + $content = preg_replace(["/<span.*?>/si", "/<\/span>/si"], ['<text class="wx-span">', '</text>'], $content); //span |
|
304 | + $content = preg_replace(["/<h[1-6].*?>/si", "/<\/h[1-6]>/si"], ['<view class="wx-h">', '</view>'], $content); //h |
|
305 | + $content = preg_replace("/<img.*?/si", '<image class="wx-img"', $content); //img |
|
306 | 306 | return $content; |
307 | 307 | } |
308 | 308 | |
@@ -314,8 +314,8 @@ discard block |
||
314 | 314 | * @return string |
315 | 315 | */ |
316 | 316 | static public function pReplaceSpan(string $content):string { |
317 | - $content = str_replace(["\r","\n","\t"],'',$content); |
|
318 | - $content = preg_replace(["/<p/si", "/<\/p>/si"],['<span','</span><br>'],$content);//p |
|
317 | + $content = str_replace(["\r", "\n", "\t"], '', $content); |
|
318 | + $content = preg_replace(["/<p/si", "/<\/p>/si"], ['<span', '</span><br>'], $content); //p |
|
319 | 319 | return $content; |
320 | 320 | } |
321 | 321 | |
@@ -326,10 +326,10 @@ discard block |
||
326 | 326 | * @param string $keyword |
327 | 327 | * @return string |
328 | 328 | */ |
329 | - static public function filterPunctuation($keyword){ |
|
330 | - $keyword = str_replace(["\r\n", "\r", "\n"," "," "], "", trim($keyword));//删除空格 |
|
331 | - $keyword = preg_replace('# #','',$keyword); |
|
332 | - $keyword = preg_replace("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/",'',$keyword); |
|
329 | + static public function filterPunctuation($keyword) { |
|
330 | + $keyword = str_replace(["\r\n", "\r", "\n", " ", " "], "", trim($keyword)); //删除空格 |
|
331 | + $keyword = preg_replace('# #', '', $keyword); |
|
332 | + $keyword = preg_replace("/[ '.,:;*?~`!@#$%^&+=)(<>{}]|\]|\[|\/|\\\|\"|\|/", '', $keyword); |
|
333 | 333 | return $keyword; |
334 | 334 | } |
335 | 335 | |
@@ -341,9 +341,9 @@ discard block |
||
341 | 341 | * @param string $allowable_tags |
342 | 342 | * @return string |
343 | 343 | */ |
344 | - static public function stripTags($content,$allowable_tags = '<font>'){ |
|
345 | - $content = strip_tags($content,$allowable_tags);//替换标签 |
|
346 | - $content = str_replace(["\r\n", "\r", "\n"," "], "", trim($content));//删除空格 |
|
344 | + static public function stripTags($content, $allowable_tags = '<font>') { |
|
345 | + $content = strip_tags($content, $allowable_tags); //替换标签 |
|
346 | + $content = str_replace(["\r\n", "\r", "\n", " "], "", trim($content)); //删除空格 |
|
347 | 347 | return $content; |
348 | 348 | } |
349 | 349 |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | if(empty($time)){ |
76 | 76 | list($msec, $sec) = explode(' ', microtime()); |
77 | 77 | $millisecond = (int)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000); |
78 | - }else{ |
|
78 | + } else{ |
|
79 | 79 | $millisecond = strtotime($time)."000"; |
80 | 80 | } |
81 | 81 | return (int)$millisecond; |
@@ -160,12 +160,12 @@ discard block |
||
160 | 160 | foreach ($string as $key => $val) { |
161 | 161 | $_key = self::autoCharset($key, $from, $to); |
162 | 162 | $string[$_key] = self::autoCharset($val, $from, $to); |
163 | - if ($key != $_key) |
|
164 | - unset($string[$key]); |
|
163 | + if ($key != $_key) { |
|
164 | + unset($string[$key]); |
|
165 | + } |
|
165 | 166 | } |
166 | 167 | return $string; |
167 | - } |
|
168 | - else { |
|
168 | + } else { |
|
169 | 169 | return $string; |
170 | 170 | } |
171 | 171 | } |
@@ -203,7 +203,7 @@ discard block |
||
203 | 203 | public static function getTime($date,$is_timestamp=false):string { |
204 | 204 | if($is_timestamp == true){ |
205 | 205 | $time = $date; |
206 | - }else{ |
|
206 | + } else{ |
|
207 | 207 | $time = strtotime($date);//时间转换为时间戳 |
208 | 208 | } |
209 | 209 |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * Description: Tool.php. |
9 | 9 | */ |
10 | 10 | |
11 | -class Tool{ |
|
11 | +class Tool { |
|
12 | 12 | |
13 | 13 | /** |
14 | 14 | * Description: 对象到数组转换 |
@@ -17,12 +17,12 @@ discard block |
||
17 | 17 | * @param $obj |
18 | 18 | * @return array |
19 | 19 | */ |
20 | - private function objectToArray($obj){ |
|
21 | - if(!is_object($obj) && !is_array($obj)) { |
|
20 | + private function objectToArray($obj) { |
|
21 | + if (!is_object($obj) && !is_array($obj)) { |
|
22 | 22 | return $obj; |
23 | 23 | } |
24 | 24 | $arr = array(); |
25 | - foreach($obj as $k => $v){ |
|
25 | + foreach ($obj as $k => $v) { |
|
26 | 26 | $arr[$k] = $this->objectToArray($v); |
27 | 27 | } |
28 | 28 | return $arr; |
@@ -34,14 +34,14 @@ discard block |
||
34 | 34 | * Updater: |
35 | 35 | * @return string |
36 | 36 | */ |
37 | - public static function getIp(){ |
|
38 | - if(!empty($_SERVER["HTTP_CLIENT_IP"])){ |
|
37 | + public static function getIp() { |
|
38 | + if (!empty($_SERVER["HTTP_CLIENT_IP"])) { |
|
39 | 39 | $cip = $_SERVER["HTTP_CLIENT_IP"]; |
40 | - }else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ |
|
40 | + } else if (!empty($_SERVER["HTTP_X_FORWARDED_FOR"])) { |
|
41 | 41 | $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
42 | - }else if(!empty($_SERVER["REMOTE_ADDR"])){ |
|
42 | + } else if (!empty($_SERVER["REMOTE_ADDR"])) { |
|
43 | 43 | $cip = $_SERVER["REMOTE_ADDR"]; |
44 | - }else{ |
|
44 | + } else { |
|
45 | 45 | $cip = ''; |
46 | 46 | } |
47 | 47 | preg_match("/[\d\.]{7,15}/", $cip, $cips); |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | 'mobile' |
107 | 107 | ); |
108 | 108 | // 从HTTP_USER_AGENT中查找手机浏览器的关键字 |
109 | - if (preg_match("/(" . implode('|', $clientKeyWords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { |
|
109 | + if (preg_match("/(".implode('|', $clientKeyWords).")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) { |
|
110 | 110 | return true; |
111 | 111 | } |
112 | 112 | } |
@@ -144,13 +144,13 @@ discard block |
||
144 | 144 | * @param int $parent_id 父id值 |
145 | 145 | * @return array |
146 | 146 | */ |
147 | - public static function getTreeStructure($list,$filed='id',$parent_filed='pid',$parent_id=0){ |
|
147 | + public static function getTreeStructure($list, $filed = 'id', $parent_filed = 'pid', $parent_id = 0) { |
|
148 | 148 | $result = array(); |
149 | - if(!empty($list)){ |
|
150 | - foreach($list as $key=>$val){ |
|
151 | - if($val[$parent_filed] == $parent_id){ |
|
152 | - $val['child'] = self::getTreeStructure($list,$filed,$parent_filed,$val[$filed]); |
|
153 | - if(empty($val['child'])){ |
|
149 | + if (!empty($list)) { |
|
150 | + foreach ($list as $key=>$val) { |
|
151 | + if ($val[$parent_filed] == $parent_id) { |
|
152 | + $val['child'] = self::getTreeStructure($list, $filed, $parent_filed, $val[$filed]); |
|
153 | + if (empty($val['child'])) { |
|
154 | 154 | unset($val['child']); |
155 | 155 | } |
156 | 156 | $result[] = $val; |
@@ -166,12 +166,12 @@ discard block |
||
166 | 166 | * @Author: TinyMeng <[email protected]> |
167 | 167 | * @param $data |
168 | 168 | */ |
169 | - static public function nullArrayToObject(&$data){ |
|
170 | - foreach ($data as $key=>&$val){ |
|
171 | - if(is_array($val)){ |
|
172 | - if(empty($val)){ |
|
173 | - settype($val,'object'); |
|
174 | - }else{ |
|
169 | + static public function nullArrayToObject(&$data) { |
|
170 | + foreach ($data as $key=>&$val) { |
|
171 | + if (is_array($val)) { |
|
172 | + if (empty($val)) { |
|
173 | + settype($val, 'object'); |
|
174 | + } else { |
|
175 | 175 | self::nullArrayToObject($val); |
176 | 176 | } |
177 | 177 | } |
@@ -37,11 +37,11 @@ discard block |
||
37 | 37 | public static function getIp(){ |
38 | 38 | if(!empty($_SERVER["HTTP_CLIENT_IP"])){ |
39 | 39 | $cip = $_SERVER["HTTP_CLIENT_IP"]; |
40 | - }else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ |
|
40 | + } else if(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){ |
|
41 | 41 | $cip = $_SERVER["HTTP_X_FORWARDED_FOR"]; |
42 | - }else if(!empty($_SERVER["REMOTE_ADDR"])){ |
|
42 | + } else if(!empty($_SERVER["REMOTE_ADDR"])){ |
|
43 | 43 | $cip = $_SERVER["REMOTE_ADDR"]; |
44 | - }else{ |
|
44 | + } else{ |
|
45 | 45 | $cip = ''; |
46 | 46 | } |
47 | 47 | preg_match("/[\d\.]{7,15}/", $cip, $cips); |
@@ -171,7 +171,7 @@ discard block |
||
171 | 171 | if(is_array($val)){ |
172 | 172 | if(empty($val)){ |
173 | 173 | settype($val,'object'); |
174 | - }else{ |
|
174 | + } else{ |
|
175 | 175 | self::nullArrayToObject($val); |
176 | 176 | } |
177 | 177 | } |
@@ -18,10 +18,10 @@ |
||
18 | 18 | |
19 | 19 | public function __construct(int $statusCode, string $message = '', Exception $previous = null, array $headers = [], $code = 0) |
20 | 20 | { |
21 | - $this->headers = $headers; |
|
21 | + $this->headers = $headers; |
|
22 | 22 | |
23 | 23 | /** message */ |
24 | - if(empty($message)) $message = isset(StatusCode::$status_code[$statusCode]) ? StatusCode::$status_code[$statusCode] :StatusCode::$status_code[StatusCode::COMMON_UNKNOWN]; |
|
24 | + if (empty($message)) $message = isset(StatusCode::$status_code[$statusCode]) ? StatusCode::$status_code[$statusCode] : StatusCode::$status_code[StatusCode::COMMON_UNKNOWN]; |
|
25 | 25 | parent::__construct('ERROR_TINYMENG_TOOL: '.$message, $code, $previous); |
26 | 26 | } |
27 | 27 |
@@ -21,7 +21,9 @@ |
||
21 | 21 | $this->headers = $headers; |
22 | 22 | |
23 | 23 | /** message */ |
24 | - if(empty($message)) $message = isset(StatusCode::$status_code[$statusCode]) ? StatusCode::$status_code[$statusCode] :StatusCode::$status_code[StatusCode::COMMON_UNKNOWN]; |
|
24 | + if(empty($message)) { |
|
25 | + $message = isset(StatusCode::$status_code[$statusCode]) ? StatusCode::$status_code[$statusCode] :StatusCode::$status_code[StatusCode::COMMON_UNKNOWN]; |
|
26 | + } |
|
25 | 27 | parent::__construct('ERROR_TINYMENG_TOOL: '.$message, $code, $previous); |
26 | 28 | } |
27 | 29 |
@@ -12,43 +12,43 @@ |
||
12 | 12 | /** |
13 | 13 | * 公共状态码 |
14 | 14 | */ |
15 | - const COMMON_SUCCESS = 200;//成功 |
|
16 | - const COMMON_UNKNOWN = 400;//未知错误 |
|
17 | - const COMMON_REQUEST_METHOD = 401;//错误请求(请求方式错误) |
|
18 | - const COMMON_HEADER_MISS_PARAM = 402;//缺失头信息 |
|
19 | - const COMMON_ACCESS_BARRED = 403;//禁止访问 |
|
20 | - const COMMON_NOT_REQUEST = 404;//请求不存在 |
|
21 | - const COMMON_TINYMENG_REQUEST_METHOD = 405;//站外链接请求错误 |
|
15 | + const COMMON_SUCCESS = 200; //成功 |
|
16 | + const COMMON_UNKNOWN = 400; //未知错误 |
|
17 | + const COMMON_REQUEST_METHOD = 401; //错误请求(请求方式错误) |
|
18 | + const COMMON_HEADER_MISS_PARAM = 402; //缺失头信息 |
|
19 | + const COMMON_ACCESS_BARRED = 403; //禁止访问 |
|
20 | + const COMMON_NOT_REQUEST = 404; //请求不存在 |
|
21 | + const COMMON_TINYMENG_REQUEST_METHOD = 405; //站外链接请求错误 |
|
22 | 22 | |
23 | 23 | /** |
24 | 24 | * 未授权 420 |
25 | 25 | */ |
26 | - const COMMON_TOKEN_INVALID = 421;//token无效 |
|
27 | - const COMMON_SIGN_ERROR = 422;//签名错误 |
|
28 | - const COMMON_NO_ACCESS = 423;//没有权限 |
|
26 | + const COMMON_TOKEN_INVALID = 421; //token无效 |
|
27 | + const COMMON_SIGN_ERROR = 422; //签名错误 |
|
28 | + const COMMON_NO_ACCESS = 423; //没有权限 |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * 数据错误 430 |
32 | 32 | */ |
33 | - const COMMON_PARAM_INVALID = 431;//参数无效 |
|
34 | - const COMMON_PARAM_MISS = 432;//参数缺失 |
|
35 | - const COMMON_PARAMS_VERIFY_ERROR = 433;//字段验证失败 |
|
36 | - const COMMON_CAPTCHA_INVALID = 434;//验证码错误 |
|
37 | - const COMMON_NO_DATA_EXIST = 435;//数据不存在 |
|
38 | - const COMMON_DATA_EXIST = 436;//数据已存在 |
|
39 | - const COMMON_SAVE_FAILURE = 437;//存储失败 |
|
33 | + const COMMON_PARAM_INVALID = 431; //参数无效 |
|
34 | + const COMMON_PARAM_MISS = 432; //参数缺失 |
|
35 | + const COMMON_PARAMS_VERIFY_ERROR = 433; //字段验证失败 |
|
36 | + const COMMON_CAPTCHA_INVALID = 434; //验证码错误 |
|
37 | + const COMMON_NO_DATA_EXIST = 435; //数据不存在 |
|
38 | + const COMMON_DATA_EXIST = 436; //数据已存在 |
|
39 | + const COMMON_SAVE_FAILURE = 437; //存储失败 |
|
40 | 40 | |
41 | 41 | /** |
42 | 42 | * 用户错误码 440 |
43 | 43 | */ |
44 | - const COMMON_NOT_ENTERED_PASSWORD = 440;//未输入密码 |
|
45 | - const COMMON_PASSWORD_INVALID = 441;//密码错误 |
|
44 | + const COMMON_NOT_ENTERED_PASSWORD = 440; //未输入密码 |
|
45 | + const COMMON_PASSWORD_INVALID = 441; //密码错误 |
|
46 | 46 | |
47 | 47 | /** |
48 | 48 | * 系统状态码 300000 |
49 | 49 | */ |
50 | - const USER_STOP_USE = 300105;//用户停止使用 |
|
51 | - const USER_SYSTEM_UPDATE = 300109;//系统升级 |
|
50 | + const USER_STOP_USE = 300105; //用户停止使用 |
|
51 | + const USER_SYSTEM_UPDATE = 300109; //系统升级 |
|
52 | 52 | |
53 | 53 | public static $status_code = [ |
54 | 54 | //公共错误码 |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | class Rsa |
11 | 11 | { |
12 | 12 | |
13 | - private $rsaPath = './';//公钥证书路径 |
|
13 | + private $rsaPath = './'; //公钥证书路径 |
|
14 | 14 | |
15 | 15 | /** |
16 | 16 | * Author: JiaMeng <[email protected]> |
@@ -62,11 +62,11 @@ discard block |
||
62 | 62 | // 生成私钥 |
63 | 63 | $rsa = openssl_pkey_new($config); |
64 | 64 | openssl_pkey_export($rsa, $privKey, $this->privkeypass, $config); |
65 | - file_put_contents($this->rsaPath . DIRECTORY_SEPARATOR . 'priv.key', $privKey); |
|
65 | + file_put_contents($this->rsaPath.DIRECTORY_SEPARATOR.'priv.key', $privKey); |
|
66 | 66 | // 生成公钥 |
67 | 67 | $rsaPri = openssl_pkey_get_details($rsa); |
68 | 68 | $pubKey = $rsaPri['key']; |
69 | - file_put_contents($this->rsaPath . DIRECTORY_SEPARATOR . 'pub.key', $pubKey); |
|
69 | + file_put_contents($this->rsaPath.DIRECTORY_SEPARATOR.'pub.key', $pubKey); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | /** |
@@ -75,7 +75,7 @@ discard block |
||
75 | 75 | */ |
76 | 76 | public function setupPrivKey() |
77 | 77 | { |
78 | - $file = $this->rsaPath . DIRECTORY_SEPARATOR . 'priv.key'; |
|
78 | + $file = $this->rsaPath.DIRECTORY_SEPARATOR.'priv.key'; |
|
79 | 79 | $privKey = file_get_contents($file); |
80 | 80 | $this->_privKey = openssl_pkey_get_private($privKey, $this->privkeypass); |
81 | 81 | return true; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | */ |
88 | 88 | public function setupPubKey() |
89 | 89 | { |
90 | - $file = $this->rsaPath . DIRECTORY_SEPARATOR . 'pub.key'; |
|
90 | + $file = $this->rsaPath.DIRECTORY_SEPARATOR.'pub.key'; |
|
91 | 91 | $pubKey = file_get_contents($file); |
92 | 92 | $this->_pubKey = openssl_pkey_get_public($pubKey); |
93 | 93 | return true; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | namespace tinymeng\tools\async; |
3 | 3 | |
4 | -class AsyncHook{ |
|
4 | +class AsyncHook { |
|
5 | 5 | |
6 | 6 | /** |
7 | 7 | * @var array |
@@ -20,7 +20,7 @@ discard block |
||
20 | 20 | */ |
21 | 21 | public static function hook(callable $callback, $params) { |
22 | 22 | self::$hook_list[] = array('callback' => $callback, 'params' => $params); |
23 | - if(self::$hooked == false) { |
|
23 | + if (self::$hooked == false) { |
|
24 | 24 | self::$hooked = true; |
25 | 25 | //注册一个callback当前在脚本执行完后执行 |
26 | 26 | register_shutdown_function(array(__CLASS__, '__run')); |
@@ -33,10 +33,10 @@ discard block |
||
33 | 33 | */ |
34 | 34 | private static function __run() { |
35 | 35 | fastcgi_finish_request(); |
36 | - if(empty(self::$hook_list)) { |
|
36 | + if (empty(self::$hook_list)) { |
|
37 | 37 | return; |
38 | 38 | } |
39 | - foreach(self::$hook_list as $hook) { |
|
39 | + foreach (self::$hook_list as $hook) { |
|
40 | 40 | $callback = $hook['callback']; |
41 | 41 | $params = $hook['params']; |
42 | 42 | call_user_func_array($callback, $params); |
@@ -8,7 +8,7 @@ discard block |
||
8 | 8 | * Description: Tool.php. |
9 | 9 | */ |
10 | 10 | |
11 | -class Encryption{ |
|
11 | +class Encryption { |
|
12 | 12 | |
13 | 13 | |
14 | 14 | /** |
@@ -22,7 +22,7 @@ discard block |
||
22 | 22 | * @return bool|string |
23 | 23 | */ |
24 | 24 | static public function authcode($string, $operation = 'decode', $key = 'tinymeng', $expiry = 0) { |
25 | - $ckey_length = 4; // 随机密钥长度 取值 0-32; |
|
25 | + $ckey_length = 4; // 随机密钥长度 取值 0-32; |
|
26 | 26 | // 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。 |
27 | 27 | // 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方 |
28 | 28 | // 当此值为 0 时,则不产生随机密钥 |
@@ -30,14 +30,14 @@ discard block |
||
30 | 30 | $key = md5($key); |
31 | 31 | $keya = md5(substr($key, 0, 16)); |
32 | 32 | $keyb = md5(substr($key, 16, 16)); |
33 | - $keyc = $ckey_length ? ($operation == 'decode' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : ''; |
|
33 | + $keyc = $ckey_length ? ($operation == 'decode' ? substr($string, 0, $ckey_length) : substr(md5(microtime()), -$ckey_length)) : ''; |
|
34 | 34 | |
35 | 35 | $cryptkey = $keya.md5($keya.$keyc); |
36 | 36 | $key_length = strlen($cryptkey); |
37 | 37 | |
38 | - if($operation == 'decode'){ |
|
38 | + if ($operation == 'decode') { |
|
39 | 39 | $string = base64_decode(substr($string, $ckey_length)); |
40 | - }else{ |
|
40 | + } else { |
|
41 | 41 | $a = $expiry ? $expiry + time() : 0; |
42 | 42 | $string = sprintf('%010d', $a).substr(md5($string.$keyb), 0, 16).$string; |
43 | 43 | } |
@@ -47,27 +47,27 @@ discard block |
||
47 | 47 | $box = range(0, 255); |
48 | 48 | |
49 | 49 | $rndkey = array(); |
50 | - for($i = 0; $i <= 255; $i++) { |
|
50 | + for ($i = 0; $i <= 255; $i++) { |
|
51 | 51 | $rndkey[$i] = ord($cryptkey[$i % $key_length]); |
52 | 52 | } |
53 | - for($j = $i = 0; $i < 256; $i++) { |
|
53 | + for ($j = $i = 0; $i < 256; $i++) { |
|
54 | 54 | $j = ($j + $box[$i] + $rndkey[$i]) % 256; |
55 | 55 | $tmp = $box[$i]; |
56 | 56 | $box[$i] = $box[$j]; |
57 | 57 | $box[$j] = $tmp; |
58 | 58 | } |
59 | 59 | |
60 | - for($a = $j = $i = 0; $i < $string_length; $i++) { |
|
60 | + for ($a = $j = $i = 0; $i < $string_length; $i++) { |
|
61 | 61 | $a = ($a + 1) % 256; |
62 | 62 | $j = ($j + $box[$a]) % 256; |
63 | 63 | $tmp = $box[$a]; |
64 | 64 | $box[$a] = $box[$j]; |
65 | 65 | $box[$j] = $tmp; |
66 | - $result .= chr( ord($string[$i]) ^ ( $box[($box[$a] + $box[$j]) % 256])); |
|
66 | + $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256])); |
|
67 | 67 | } |
68 | 68 | |
69 | - if($operation == 'decode') { |
|
70 | - if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { |
|
69 | + if ($operation == 'decode') { |
|
70 | + if ((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) { |
|
71 | 71 | return substr($result, 26); |
72 | 72 | } else { |
73 | 73 | return ''; |
@@ -37,7 +37,7 @@ |
||
37 | 37 | |
38 | 38 | if($operation == 'decode'){ |
39 | 39 | $string = base64_decode(substr($string, $ckey_length)); |
40 | - }else{ |
|
40 | + } else{ |
|
41 | 41 | $a = $expiry ? $expiry + time() : 0; |
42 | 42 | $string = sprintf('%010d', $a).substr(md5($string.$keyb), 0, 16).$string; |
43 | 43 | } |