github/app/Http/Controllers/CronController.php
2025-04-14 14:47:46 +08:00

119 lines
3.5 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Article;
//use App\Services\TomTool\Http;
//use voku\helper\AntiXSS;
class CronController
{
private $sUserName;
private $sProjectName;
public function index()
{
$h = date("H");
$i = date("i");
$s = date("s");
//test
$i = 7;
$aConditions = Article::select('cron_condition')
->where('status', 1)
->distinct()
->get()
->toArray();
foreach ($aConditions as $row) {
$sCondition = $row['cron_condition'];
if (eval("return $sCondition;")) {
$aArticle = Article::where("cron_condition", $sCondition)->where("status", 1)->get()->toArray();
$this->run($aArticle);
}
}
}
private function run($aArticle)
{
foreach ($aArticle as $row) {
$this->sUserName = $row["user_name"];
$this->sProjectName = $row["project_name"];
list($sClass, $sFunc) = explode('#', $row['mod']);
$sClass = "App\\Services\\Github\\File\\".$sClass;
if (method_exists($sClass, $sFunc)) {
$oInstans = new $sClass();
$sReadMe = $oInstans->$sFunc($row);
// var_dump($sReadMe);
} else {
// 处理方法不存在的情况(可选)
// $this->tgMessage("Method $sClass does not exist.");
}
// 目录
$sBaseDir = env("BASE_DIR");
$sUserProjectDir = $sBaseDir."github/".$this->sUserName."-".$this->sProjectName;
$sGitShDir = $sBaseDir."github";
// 没有文件夹,创建一个
if (!is_dir($sUserProjectDir)) {
mkdir($sUserProjectDir, 0755, true); // 0755 是权限true 表示递归创建目录
}
// 没有仓库,创建一个
if (!is_dir($sUserProjectDir . "/.git")) {
$sGithubProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git";
$output = shell_exec("sh " . $sGitShDir . "/init.sh " . escapeshellarg($sUserProjectDir) . " " . escapeshellarg($sGithubProjectUrl) . " " . escapeshellarg($this->sUserName));
echo "Output from init.sh:\n$output\n";
$output = shell_exec("git config --global --add safe.directory $sUserProjectDir 2>&1");
//
// echo "<br>Output from --add safa.directory:\n$output\n";
}
// 写入内容
file_put_contents($sUserProjectDir."/README.md", $this->formatstr($sReadMe));
file_put_contents($sUserProjectDir."/version.text", time());
// 提交
$output = shell_exec("sh ".$sGitShDir."/push.sh " . escapeshellarg($sUserProjectDir));
echo "Output from push.sh:\n$output\n";
}
}
private function tgMessage($sMsg)
{
if (env("is_loc") == true) {
echo $sMsg;
} else {
echo 11;
}
}
private function formatstr($str)
{
$str = substr($str, 1, -1);
return $str;
}
}