作者 lyh

Merge branch 'master' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

... ... @@ -38,14 +38,13 @@ class HtmlCollect extends Command
public function handle()
{
// while (true) {
$this->start_update();
$this->start_collect();
// }
}
protected function start_update()
protected function start_collect()
{
// $task_id = $this->get_task();
$task_id = '298_1';
$task_id = $this->get_task();
if ($task_id === false) {
//所有项目采集完成
sleep(60);
... ... @@ -70,7 +69,7 @@ class HtmlCollect extends Command
return true;
}
echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', task_type: ' . $collect_id . ', collect start' . PHP_EOL;
echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', collect start' . PHP_EOL;
$collect_info->status = CollectTask::STATUS_ING;
$collect_info->save();
... ... @@ -84,7 +83,7 @@ class HtmlCollect extends Command
$html = $this->upload_source($html, $source_list, $project_id);
}
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', task_type: ' . $collect_id . ', error: ' . $e->getMessage() . PHP_EOL;
echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', error: ' . $e->getMessage() . PHP_EOL;
return true;
}
... ... @@ -92,7 +91,7 @@ class HtmlCollect extends Command
$collect_info->status = CollectTask::STATUS_COM;
$collect_info->save();
echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', task_type: ' . $collect_id . ', collect end' . PHP_EOL;
echo 'date:' . date('Y-m-d H:i:s') . ', project_id: ' . $project_id . ', collect_id: ' . $collect_id . ', collect end' . PHP_EOL;
}
//关闭数据库
DB::disconnect('custom_mysql');
... ... @@ -150,7 +149,7 @@ class HtmlCollect extends Command
return $source;
}
//图片
//image
preg_match_all('/<img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i', $html, $result_img);
$img = $result_img[2] ?? [];
foreach ($img as $vi) {
... ... @@ -182,6 +181,15 @@ class HtmlCollect extends Command
$check_vc && $source[] = $check_vc;
}
//css background
preg_match_all("/url\(['\"]?(\s*[^>]+?)['\"]?\)/i", $html, $result_css_b);
$css_b = $result_css_b[1] ?? [];
foreach ($css_b as $vc_b) {
$check_vc_b = $this->url_check($vc_b, $project_id, $domain);
$check_vc_b && $source[] = $check_vc_b;
}
return $source;
}
... ... @@ -194,9 +202,11 @@ class HtmlCollect extends Command
$host = $arr['host'] ?? '';
$path = $arr['path'] ?? '';
if ((strpos($host, '.globalso.') === false)
&& (strpos($host, '.goodao.') === false)
&& $path && (strpos($path, '.') !== false)) {
if (
(strpos($host, '.globalso.') === false) &&
(strpos($host, '.goodao.') === false) &&
$path && (strpos($path, '.') !== false)
) {
$source = CollectSource::where('project_id', $project_id)->where('origin', $url)->first();
if (!$source) {
... ... @@ -236,6 +246,50 @@ class HtmlCollect extends Command
'updated_at' => date('Y-m-d H:i:s'),
]);
$html = str_replace($vs['url'], getImageUrl($new_source), $html);
if (substr($new_source, -3, 3) == 'css') {
// 下载css文件中的资源
$css_html = file_get_contents($vs['url_complete']);
preg_match_all("/url\(['\"](\s*[^>]+?)['\"]\)/i", $css_html, $result_css_source);
$css_source = $result_css_source[1] ?? [];
$url_arr = explode('/', $vs['url_complete']);
$target_arr = explode('/', $new_source);
foreach ($css_source as $vcs) {
$vcs_arr = parse_url($vcs);
if (isset($vcs_arr['domain'])) {
//不是相对路径,不下载
continue;
}
$vcs = $vcs_arr['path'] ?? '';
if (!$vcs) {
continue;
}
$source_info = CollectSource::where('project_id', $project_id)->where('origin', $vcs)->first();
if ($source_info) {
//已存在,不下载
continue;
}
$url_arr[count($url_arr) - 1] = $vcs;
$url_css_complete = implode('/', $url_arr);
$target_arr[count($target_arr) - 1] = $vcs;
$path = implode('/', $target_arr);
$new_source_css = CosService::uploadRemote($project_id, 'source', $url_css_complete, $path);
if ($new_source_css) {
CollectSource::insert([
'project_id' => $project_id,
'origin' => $vcs,
'target' => $new_source_css,
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s'),
]);
}
}
}
}
} else {
$html = str_replace($vs['url'], getImageUrl($vs['url_complete']), $html);
... ...
... ... @@ -421,17 +421,19 @@ if (!function_exists('getImageUrl')) {
* @time :2023/7/20 16:46
*/
function getImageUrl($path,$location = 1){
if(empty($path)){
return '';
}
if((strpos($path,'https://')!== false) || (strpos($path,'http://') !== false)){
return $path;
}
if(is_array($path)){
$url =[];
foreach ($path as $v){
$url[] = getImageUrl($v);
}
$url = array_filter($url);
}else{
if(empty($path)){
return '';
}
if((strpos($path,'https://')!== false) || (strpos($path,'http://') !== false)){
return $path;
}
if($location == 1){
$cos = config('filesystems.disks.cos');
$cosCdn = $cos['cdn'];
... ...
... ... @@ -92,9 +92,9 @@ class CollectLogic extends Logic
'title' => $item['title'],
'url' => $this->domain . $item['route'],
'keywords' => $keyword,
'description' => strip_tags($item['intro']?:''),
'desc' => strip_tags($item['intro']?:''),
'content' => strip_tags($item['content'] ?: ''),
'img' => array_column($item['gallery'] ?: [], 'url')
'images' => getImageUrl(array_column($item['gallery'] ?: [], 'url'))
];
}
$list['list'] = $data;
... ... @@ -114,9 +114,9 @@ class CollectLogic extends Logic
'title' => $item['name'],
'url' => $this->domain . $item['url'],
'keywords' => $item['seo_keywords'],
'description' => strip_tags($item['remark']?:''),
'desc' => strip_tags($item['remark']?:''),
'content' => strip_tags($item['text'] ?: ''),
'img' => $item['image'] ?:''
'images' => getImageUrl([$item['image']] ?:[])
];
}
$list['list'] = $data;
... ... @@ -136,9 +136,9 @@ class CollectLogic extends Logic
'title' => $item['name'],
'url' => $this->domain . $item['url'],
'keywords' => $item['seo_keywords'],
'description' => strip_tags($item['remark']?:''),
'desc' => strip_tags($item['remark']?:''),
'content' => strip_tags($item['text'] ?: ''),
'img' => $item['image'] ?:''
'images' => getImageUrl([$item['image']] ?:[])
];
}
$list['list'] = $data;
... ...
... ... @@ -12,6 +12,7 @@ use App\Utils\HttpUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
use Symfony\Component\Process\Process;
class DomainInfoLogic extends BaseLogic
{
... ... @@ -226,4 +227,44 @@ class DomainInfoLogic extends BaseLogic
return $this->success();
}
/**
* 验证是否cname或者A记录解析到目标服务器
* @param $domain
* @param $server_info
* @return mixed
* @author zbj
* @date 2023/11/13
*/
public function check_cname($domain, $server_info){
$checkA = false;
$checkCname = false;
$process = new Process(['nslookup', '-qt=a', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line){
if($line){
$checkA = strpos($line, $server_info['host']) !== false;
}
}
if($checkA){
return $domain;
}
//是否cname
$process = new Process(['nslookup', '-qt=cname', $domain]);
$process->run();
$output = explode(PHP_EOL, $process->getOutput());
foreach ($output as $line){
if($line){
$checkCname = (strpos($line, $server_info['init_domain']) !== false);
}
}
if($checkCname){
return $domain;
}
return false;
}
}
... ...
... ... @@ -2,6 +2,8 @@
namespace App\Http\Logic\Aside\Project;
use App\Enums\Common\Code;
use App\Exceptions\AsideGlobalException;
use App\Models\Com\NoticeLog;
use App\Models\Devops\ServerConfig;
use App\Models\Project\ProjectRenew;
... ... @@ -36,6 +38,7 @@ use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
use App\Http\Logic\Aside\Domain\DomainInfoLogic;
/**
* Class ProjectLogic
... ... @@ -130,6 +133,9 @@ class ProjectLogic extends BaseLogic
$this->createSite($this->param);
}
DB::commit();
}catch (AsideGlobalException $e){
DB::rollBack();
$this->fail($e->getMessage());
}catch (\Exception $e){
DB::rollBack();
$this->fail('请填写完整后再提交');
... ... @@ -505,18 +511,24 @@ class ProjectLogic extends BaseLogic
/**
* 创建站点
* @param $param
* @throws AsideGlobalException
* @author Akun
* @date 2023/10/17 10:04
*/
public function createSite($param){
if(isset($param['serve_id']) && $param['serve_id'] && isset($param['deploy_optimize']['domain']) && $param['deploy_optimize']['domain']){
$server_model = new ServerConfig();
$server_info = $server_model->read(['id'=>$param['serve_id']],'init_domain');
$server_info = $server_model->read(['id'=>$param['serve_id']],['init_domain','host']);
$domain_model = new DomainInfo();
$domain_info = $domain_model->read(['id'=>$param['deploy_optimize']['domain']],'domain');
if($server_info && $domain_info){
//验证解析
if (!DomainInfoLogic::instance()->check_cname($domain_info['domain'], $server_info)) {
throw new AsideGlobalException(Code::SYSTEM_ERROR,'域名' . $domain_info['domain'] . '未解析至目标服务器');
}
$api_url = 'http://'.$server_info['init_domain'].'/api/createSite';
$api_param = ['domain'=>$domain_info['domain']];
try {
... ...
... ... @@ -68,18 +68,23 @@ class CosService
* @param $project_id
* @param $image_type
* @param $file_url
* @param $key
* @return string
* @author Akun
* @date 2023/09/21 9:39
*/
public static function uploadRemote($project_id,$image_type,$file_url)
public static function uploadRemote($project_id,$image_type,$file_url,$key='')
{
$ext = explode('.',$file_url);
if(!$key){
$ext = explode('.',$file_url);
$filename = uniqid().rand(10000,99999).'.'.end($ext);
$filename = uniqid().rand(10000,99999).'.'.end($ext);
$uploads = config('upload.default_file');
$path = $uploads['path_b'].'/'.$project_id.'/'.$image_type.'/'.date('Y-m');
$key = $path.'/'.$filename;
}
$uploads = config('upload.default_file');
$path = $uploads['path_b'].'/'.$project_id.'/'.$image_type.'/'.date('Y-m');
$cos = config('filesystems.disks.cos');
$cosClient = new Client([
'region' => $cos['region'],
... ... @@ -88,7 +93,6 @@ class CosService
'secretKey' => $cos['credentials']['secretKey'],
],
]);
$key = $path.'/'.$filename;
try {
$cosClient->putObject([
'Bucket' => $cos['bucket'],
... ...