257 lines
7.6 KiB
PHP
Executable File
257 lines
7.6 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Http\Request;
|
||
use App\Models\Article;
|
||
use App\Services\TG\Http as TGHttp;
|
||
|
||
class CronController
|
||
{
|
||
|
||
private $sUserName;
|
||
private $sProjectName;
|
||
private $sMailName;
|
||
|
||
private $sProjectPath;
|
||
private $sProjectUrl;
|
||
|
||
private $aArticle;
|
||
|
||
// public function __construct()
|
||
// {
|
||
// $this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName;
|
||
// $this->sProjectUrl = env("github_url_base").":".$this->sUserName."/".$this->sProjectName.".git";
|
||
// }
|
||
|
||
public function index()
|
||
{
|
||
try {
|
||
|
||
$h = date("H");
|
||
$i = date("i");
|
||
$s = date("s");
|
||
|
||
//test
|
||
$aGet = $_GET;
|
||
if (isset($aGet['is_test']) && $aGet['is_test'] == 1) {
|
||
if ($aGet["time_type"] == 'i') {
|
||
$i = (string) $aGet['v'];
|
||
} else if ($aGet["time_type"] == 'h') {
|
||
$h = (string) $aGet['v'];
|
||
}
|
||
}
|
||
|
||
// i_in10规则
|
||
$iIin10 = $i[1];
|
||
$aArticle = Article::where("i_in10", $iIin10)->where("status", 1)->get()->toArray();
|
||
if (!empty($aArticle)) {
|
||
$this->run($aArticle);
|
||
}
|
||
|
||
|
||
$aConditions = Article::select('cron_condition')
|
||
->where('status', 1)
|
||
->distinct()
|
||
->get()
|
||
->toArray();
|
||
|
||
foreach ($aConditions as $row) {
|
||
|
||
$sCondition = $row['cron_condition'];
|
||
|
||
if (!empty($sCondition) && eval("return $sCondition;")) {
|
||
|
||
$aArticle = Article::where("cron_condition", $sCondition)->where("status", 1)->get()->toArray();
|
||
|
||
$this->run($aArticle);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
} catch (\Throwable $e) {
|
||
|
||
$sMessage = "错误: " . $e->getMessage();
|
||
|
||
$oTGHttp = new TGHttp();
|
||
$oTGHttp->sendToTG($sMessage);
|
||
|
||
}
|
||
|
||
}
|
||
|
||
private function run($aArticle)
|
||
{
|
||
foreach ($aArticle as $row) {
|
||
$this->sUserName = $row["user_name"];
|
||
$this->sMailName = $row["mail_name"];
|
||
$this->sProjectName = $row["project_name"];
|
||
$this->sProjectPath = env("BASE_DIR")."github/".$this->sUserName."-".$this->sProjectName;
|
||
$this->sProjectUrl = "git@".$this->sMailName.".com:".$this->sUserName."/".$this->sProjectName.".git";
|
||
$this->aArticle = $row;
|
||
|
||
list($sClass, $sFunc) = explode('#', $row['mod']);
|
||
|
||
$sClass = "App\\Services\\Github\\Mod\\".$sClass;
|
||
|
||
if (method_exists($sClass, $sFunc)) {
|
||
$oInstans = new $sClass();
|
||
$sReadMe = $oInstans->$sFunc($row);
|
||
} else {
|
||
// 处理方法不存在的情况(可选)
|
||
// $this->tgMessage("Method $sClass does not exist.");
|
||
}
|
||
|
||
// 没有文件夹,创建一个
|
||
if (!is_dir($this->sProjectPath)) {
|
||
|
||
mkdir($this->sProjectPath, 0755, true); // 0755 是权限,true 表示递归创建目录
|
||
|
||
}
|
||
|
||
// 没有仓库,创建一个
|
||
if (!is_dir($this->sProjectPath . "/.git")) {
|
||
|
||
$this->gitInit();
|
||
|
||
$this->sshConfigAdd();
|
||
|
||
}
|
||
|
||
// 写入内容
|
||
if ($sReadMe) {
|
||
file_put_contents($this->sProjectPath."/README.md", $this->formatstr($sReadMe));
|
||
}
|
||
|
||
file_put_contents($this->sProjectPath."/version.text", time());
|
||
|
||
// 提交
|
||
$this->gitPush();
|
||
|
||
}
|
||
}
|
||
|
||
private function sshConfigAdd()
|
||
{
|
||
|
||
// 定义要添加的配置
|
||
$host = $this->sMailName;
|
||
$hostname = "github.com";
|
||
$user = "git";
|
||
$identityFile = env("path_ssh")."/id_rsa_".$host;
|
||
|
||
// 构建配置字符串
|
||
$config = "\nHost ".$host.".com\n";
|
||
$config .= "\tHostName $hostname\n";
|
||
$config .= "\tUser $user\n";
|
||
$config .= "\tIdentityFile $identityFile\n";
|
||
|
||
// 指定 SSH 配置文件路径
|
||
$sshConfigFile = env("path_ssh")."/config";
|
||
|
||
// 检查文件是否存在,如果不存在则创建
|
||
if (!file_exists($sshConfigFile)) {
|
||
touch($sshConfigFile);
|
||
}
|
||
|
||
// 追加配置到文件
|
||
file_put_contents($sshConfigFile, $config, FILE_APPEND | LOCK_EX);
|
||
|
||
// 设置文件权限
|
||
chmod($sshConfigFile, 0600);
|
||
|
||
echo "SSH config updated successfully.";
|
||
|
||
}
|
||
|
||
private function formatstr($str)
|
||
{
|
||
$str = substr($str, 1, -1);
|
||
return $str;
|
||
}
|
||
|
||
private function runCommand($command) {
|
||
$output = [];
|
||
$returnVar = 0;
|
||
exec($command, $output, $returnVar);
|
||
|
||
if ($returnVar !== 0) {
|
||
|
||
$aMsg = [
|
||
"命令" => $command,
|
||
"结果" => $output,
|
||
"文章" => $this->aArticle,
|
||
];
|
||
|
||
throw new \Exception(json_encode($aMsg, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
|
||
}
|
||
}
|
||
|
||
|
||
private function gitInit()
|
||
{
|
||
// 进入指定目录
|
||
if (!chdir($this->sProjectPath)) {
|
||
throw new \Exception("无法进入目录 {$this->sProjectPath}\n");
|
||
}
|
||
|
||
// 初始化 Git 仓库
|
||
$this->runCommand('git init');
|
||
|
||
// 添加安全目录
|
||
$this->runCommand("git config --local --add safe.directory \"{$this->sProjectPath}\"");
|
||
|
||
// 设置用户
|
||
$this->runCommand("git config --local user.name \"{$this->sUserName}\"");
|
||
$this->runCommand("git config --local user.email \"{$this->sUserName}@tuta.io\"");
|
||
|
||
// 创建并切换到 main 分支
|
||
$this->runCommand('git branch -M main');
|
||
|
||
// 添加远程仓库
|
||
$this->runCommand("git remote add origin \"{$this->sProjectUrl}\"");
|
||
|
||
echo "操作完成:以初始化仓库[$this->sProjectPath]。\n";
|
||
|
||
}
|
||
|
||
// private function gitPullR()
|
||
// {
|
||
// // 进入指定目录
|
||
// if (!chdir($this->sProjectPath)) {
|
||
// echo "无法进入目录 {$this->sProjectPath}\n";
|
||
// exit(1);
|
||
// }
|
||
//
|
||
// // 获取远程更新
|
||
// $this->runCommand('git fetch origin');
|
||
//
|
||
// // 硬重置到远程主分支
|
||
// $this->runCommand('git reset --hard origin/main');
|
||
//
|
||
// echo "操作完成:已重置到远程主分支。\n";
|
||
// }
|
||
|
||
private function gitPush()
|
||
{
|
||
// 进入指定目录
|
||
if (!chdir($this->sProjectPath)) {
|
||
throw new \Exception("无法进入目录 {$this->sProjectPath}\n");
|
||
}
|
||
|
||
// 添加更改到暂存区
|
||
$this->runCommand('git add .');
|
||
|
||
// 提交更改,使用当前日期作为提交信息
|
||
$commitMessage = date('Y-m-d H:i:s'); // 使用当前日期和时间作为提交信息
|
||
$this->runCommand("git commit -m \"$commitMessage\"");
|
||
|
||
// 推送到远程主分支
|
||
$this->runCommand('git push -u origin main'); // 确保将 'main' 替换为你的默认分支名称
|
||
|
||
echo "操作完成:已提交并推送更改[$this->sProjectPath]。\n";
|
||
}
|
||
|
||
}
|