267 lines
8.1 KiB
PHP
Executable File
267 lines
8.1 KiB
PHP
Executable File
<?php
|
||
|
||
namespace App\Http\Controllers;
|
||
|
||
use Illuminate\Http\Request;
|
||
use App\Models\Article;
|
||
use App\Models\Option;
|
||
use App\Services\TG\Http as TGHttp;
|
||
use App\Services\Github\Github as ServiceGithub;
|
||
|
||
class CronController
|
||
{
|
||
|
||
private $sUserName;
|
||
private $sProjectName;
|
||
private $sMailName;
|
||
private $sMailTail;
|
||
|
||
private $sProjectPath;
|
||
private $sProjectUrl;
|
||
|
||
private $aArticle;
|
||
|
||
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) {
|
||
$isTest = 1;
|
||
if ($aGet["time_type"] == 'i') {
|
||
$i = (string) $aGet['v'];
|
||
} else if ($aGet["time_type"] == 'h') {
|
||
$h = (string) $aGet['v'];
|
||
}
|
||
} else {
|
||
$isTest = 0;
|
||
}
|
||
|
||
// 暂停
|
||
$option = Option::where("type", "pause")->first();
|
||
$isPause = $option ? $option->value : null;
|
||
if ($isPause == 1 && $isTest !== 1) {
|
||
exit("暂停");
|
||
}
|
||
|
||
// i_in10规则
|
||
$iIin10 = $i[1];
|
||
$aArticle = Article::where("i_in10", $iIin10)->where("status", 1)->get()->toArray();
|
||
if (!empty($aArticle)) {
|
||
$this->run($aArticle);
|
||
}
|
||
|
||
// inday_count规则
|
||
$iRand = rand(1, 1440);
|
||
// $iRand = 1;
|
||
$oIndayArticle = Article::where("inday_count", ">=", $iRand)->where("status", 1)->get()->toArray(); // inday24 >= rand10
|
||
if (!empty($oIndayArticle)) {
|
||
$this->run($oIndayArticle);
|
||
}
|
||
|
||
// i%7==0
|
||
$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->sMailTail = $row["mail_tail"];
|
||
$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;
|
||
|
||
$sReadMe = ServiceGithub::GetReadMe($row);
|
||
|
||
// 没有文件夹,创建一个
|
||
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.\n";
|
||
|
||
}
|
||
|
||
private function formatstr($str)
|
||
{
|
||
$str = substr($str, 1, -1);
|
||
return $str;
|
||
}
|
||
|
||
private function runCommand($command, $show_error = 1) {
|
||
$output = [];
|
||
$returnVar = 0;
|
||
exec($command, $output, $returnVar);
|
||
|
||
if ($returnVar !== 0 && $this->aArticle['show_error'] == 1 && $show_error == 1) {
|
||
|
||
$aMsg = [
|
||
"命令" => $command,
|
||
"结果" => json_encode($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->sMailName}@{$this->sMailTail}\""); //这里得改,直接tuta不行
|
||
|
||
// 创建并切换到 main 分支
|
||
$this->runCommand('git branch -M main');
|
||
|
||
// 添加远程仓库
|
||
$this->runCommand("git remote add origin \"{$this->sProjectUrl}\"");
|
||
|
||
// 立即拉取覆盖一次
|
||
$this->runCommand("git fetch origin", 0);
|
||
$this->runCommand("git reset --hard origin/main", 0);
|
||
|
||
echo "操作完成:以初始化仓库[".$this->aArticle['id']."]。\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->aArticle['id']."]。\n";
|
||
}
|
||
|
||
}
|