fix && style

This commit is contained in:
小新
2021-01-19 09:59:09 +08:00
parent 48d5278907
commit 2170b22806
3 changed files with 48 additions and 37 deletions
@@ -1,13 +1,14 @@
<?php <?php
use Workerman\Worker; use Workerman\Worker;
use Workerman\Lib\Timer; use Workerman\Lib\Timer;
// worker // worker
$webDirSource = dirname(dirname(dirname(__FILE__))); $webDirSource = dirname(dirname(dirname(__FILE__)));
$worker = new Worker(); $worker = new Worker();
$worker->name = 'FileMonitor'; $worker->name = 'FileMonitor';
$worker->reloadable = false; $worker->reloadable = false;
$worker->onWorkerStart = function() $worker->onWorkerStart = function () {
{
global $webDirSource; global $webDirSource;
Timer::add(300, 'check_files_remove', $webDirSource); Timer::add(300, 'check_files_remove', $webDirSource);
}; };
@@ -16,14 +17,23 @@ $worker->onWorkerStart = function()
function check_files_remove($webDirSource) function check_files_remove($webDirSource)
{ {
$pdo = new PDO('sqlite:' . $webDirSource . '/sql'); try {
$data = $pdo->query("select * from file_info where addtime < '".(time() - 60*60*2+5)."'")->fetchAll(); $pdo = new PDO('sqlite:' . $webDirSource . '/sql');
foreach($data as $item){ $data = $pdo->query("select * from file_info where addtime < '" . (time() - 60 * 60 * 2 + 5) . "'")->fetchAll();
if(is_file($item['path'])){ foreach ($data as $item) {
unlink($item['path']); if (is_file($item['path'])) {
unlink($item['path']);
}
$pdo->exec("delete from file_info where id=" . $item['id']);
} }
$pdo->exec("delete from file_info where id=".$item['id']); $pdo = null;
} catch (\Throwable $e) {
print_r([
'message' => $e->getMessage(),
'code' => $e->getCode(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
]);
} }
$pdo = null;
return; return;
} }
@@ -58,15 +58,20 @@ class Events
}; };
$task_connection->onMessage = function ($task_connection, $data) use ($client_id) { $task_connection->onMessage = function ($task_connection, $data) use ($client_id) {
if($data == "true") { if ($data == "true") {
$task_connection->close(); $task_connection->close();
}else{ } else {
$data && Gateway::sendToClient($client_id, $data . "\r\n"); $data && Gateway::sendToClient($client_id, $data . "\r\n");
} }
}; };
$task_connection->connect(); $task_connection->connect();
} catch (\Throwable $e) { } catch (\Throwable $e) {
print_r([
'message' => $e->getMessage(),
'code' => $e->getCode(),
'line' => $e->getLine(),
'trace' => $e->getTraceAsString(),
]);
} }
} }
@@ -7,45 +7,42 @@ $task_worker = new Worker('text://127.0.0.1:16345');
$task_worker->count = 10; $task_worker->count = 10;
$task_worker->name = 'TaskWorker_github'; $task_worker->name = 'TaskWorker_github';
$datatime = date("Ymd"); $datatime = date("Ymd");
$webDir = dirname(dirname(dirname(dirname(__FILE__)))) . "/staticweb/source/".$datatime; $webDir = dirname(dirname(dirname(dirname(__FILE__)))) . "/staticweb/source/" . $datatime;
$task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datatime) { $task_worker->onMessage = function ($connection, $task_data) use ($webDir, $datatime) {
$dataArr = json_decode($task_data, true); $dataArr = json_decode($task_data, true);
if(!$dataArr){ if (!$dataArr) {
return; return;
} }
if(isset($dataArr['type'])){ if (isset($dataArr['type'])) {
switch($dataArr['type']){ switch ($dataArr['type']) {
case "1": case "1":
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql'); $pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$pdo->exec("UPDATE statistics SET down_num=down_num+1 where id=1"); $pdo->exec("UPDATE statistics SET down_num=down_num+1 where id=1");
$connection->send("true"); $connection->send("true");
$pdo = null; $pdo = null;
return true; return true;
break;
case "2": case "2":
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql'); $pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$data = $pdo->query("select down_num from statistics where id=1")->fetch(); $data = $pdo->query("select down_num from statistics where id=1")->fetch();
$down_num = $data[0]; $down_num = $data[0];
$connection->send(json_encode([ $connection->send(json_encode([
'status' => 0, 'status' => 0,
'msg' =>"完成", 'msg' => "完成",
'data' =>[ 'data' => [
'sta' => 4, 'sta' => 4,
'down_num' => $down_num 'down_num' => $down_num
] ]
],JSON_UNESCAPED_UNICODE) . "\r\n"); ], JSON_UNESCAPED_UNICODE) . "\r\n");
$pdo = null; $pdo = null;
$connection->send("true"); $connection->send("true");
return true; return true;
break;
} }
} }
if (preg_match("/%|\"|\.\.|\\\|\?|\||`|&|reboot|rm |mv |shutdown|<|>|sh |;|'/",$dataArr['url']) if (preg_match("/%|\"|\.\.|\\\|\?|\||`|&|reboot|rm |mv |shutdown|<|>|sh |;|'/", $dataArr['url'])
|| ||
( (
!preg_match("/^((https|http):\/\/)?([\w\.\s\S\/\-\_^]+?)\.(tar\.gz|gz|tgz|tar\.bz2|bz2|msi|tar|zip|tar\.xz|tar\.z|rpm|deb|exe|apk|dmg|7z|rar)$/iu",$dataArr['url'],$ext) !preg_match("/^((https|http):\/\/)?([\w\.\s\S\/\-\_^]+?)\.(tar\.gz|gz|tgz|tar\.bz2|bz2|msi|tar|zip|tar\.xz|tar\.z|rpm|deb|exe|apk|dmg|7z|rar)$/iu", $dataArr['url'], $ext)
&& !preg_match("/^https:\/\/github.com\/([\w\/\-\.\_]+)$/iu", $dataArr['url'] && !preg_match("/^https:\/\/github.com\/([\w\/\-\.\_]+)$/iu", $dataArr['url']
) )
) )
@@ -57,7 +54,7 @@ $task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datat
if (!is_dir($webDir)) { if (!is_dir($webDir)) {
mkdir($webDir, 0777); mkdir($webDir, 0777);
} }
if(stristr($dataArr['url'],'github') && (!isset($ext) || !$ext)) { if (stristr($dataArr['url'], 'github') && (!isset($ext) || !$ext)) {
//github的检出、打包 //github的检出、打包
$connection->send(json_encode([ $connection->send(json_encode([
'status' => 0, 'status' => 0,
@@ -68,7 +65,7 @@ $task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datat
], JSON_UNESCAPED_UNICODE) . "\r\n"); ], JSON_UNESCAPED_UNICODE) . "\r\n");
$git_dir_path = md5($dataArr['url']); $git_dir_path = md5($dataArr['url']);
if (!is_dir($webDir . "/" . $git_dir_path) && !is_file($webDir . "/{$git_dir_path}.tar")) { if (!is_dir($webDir . "/" . $git_dir_path) && !is_file($webDir . "/{$git_dir_path}.tar")) {
system('cd ' . $webDir . ';git clone --depth=50 "' . $dataArr['url'] . '" ' . $git_dir_path); system('cd ' . $webDir . ';git clone --depth=50 "' . $dataArr['url'] . '" ' . $git_dir_path);
} }
$connection->send(json_encode([ $connection->send(json_encode([
'status' => 0, 'status' => 0,
@@ -78,7 +75,7 @@ $task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datat
] ]
], JSON_UNESCAPED_UNICODE) . "\r\n"); ], JSON_UNESCAPED_UNICODE) . "\r\n");
if (!is_file($webDir . "/{$git_dir_path}.tar")) { if (!is_file($webDir . "/{$git_dir_path}.tar")) {
if(!is_dir($webDir."/".$git_dir_path)){ if (!is_dir($webDir . "/" . $git_dir_path)) {
$connection->send(json_encode([ $connection->send(json_encode([
'status' => 1, 'status' => 1,
'msg' => "您输入的链接可能是不支持的后缀,如果是git仓储,请确认链接正确", 'msg' => "您输入的链接可能是不支持的后缀,如果是git仓储,请确认链接正确",
@@ -102,7 +99,7 @@ $task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datat
] ]
], JSON_UNESCAPED_UNICODE) . "\r\n"); ], JSON_UNESCAPED_UNICODE) . "\r\n");
$connection->send("true"); $connection->send("true");
}else{ } else {
//普通的压缩包下载处理 //普通的压缩包下载处理
$connection->send(json_encode([ $connection->send(json_encode([
'status' => 0, 'status' => 0,
@@ -113,7 +110,7 @@ $task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datat
], JSON_UNESCAPED_UNICODE) . "\r\n"); ], JSON_UNESCAPED_UNICODE) . "\r\n");
$fileExt = end($ext); $fileExt = end($ext);
$fileName = md5($dataArr['url']) . ".{$fileExt}"; $fileName = md5($dataArr['url']) . ".{$fileExt}";
system('cd ' . $webDir . '; wget "'.$dataArr['url'] . '" -O '.$fileName); system('cd ' . $webDir . '; wget "' . $dataArr['url'] . '" -O ' . $fileName);
$pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql'); $pdo = new PDO('sqlite:' . dirname(dirname(dirname(__FILE__))) . '/sql');
$pdo->exec("insert into file_info ('path', 'addtime') VALUES ('" . $webDir . "/{$fileName}" . "','" . time() . "')"); $pdo->exec("insert into file_info ('path', 'addtime') VALUES ('" . $webDir . "/{$fileName}" . "','" . time() . "')");
$pdo = null; $pdo = null;
@@ -130,7 +127,6 @@ $task_worker->onMessage = function ($connection, $task_data) use ($webDir,$datat
} }
}; };
if (!defined('GLOBAL_START')) { if (!defined('GLOBAL_START')) {
Worker::runAll(); Worker::runAll();