btcloud/app/command/CleanViteJs.php

248 lines
9.8 KiB
PHP
Raw Normal View History

2024-04-30 10:56:41 -04:00
<?php
declare (strict_types = 1);
namespace app\command;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;
use think\facade\Config;
use app\lib\Plugins;
class CleanViteJs extends Command
{
protected function configure()
{
$this->setName('cleanvitejs')
->addArgument('dir', Argument::REQUIRED, '/BTPanel/static/vite/js/路径')
->setDescription('处理宝塔面板vite/js文件');
}
protected function execute(Input $input, Output $output)
{
$dir = trim($input->getArgument('dir'));
if(!file_exists($dir)){
$output->writeln('目录不存在');
return;
}
//$this->handlefile($dir.'/DockerImages.js');
$this->checkdir($dir);
}
private function getExtendCode($content, $part, $n = 1, $startChar = '{', $endChar = '}'){
if(!$part) return false;
$length = strlen($content);
$start = strpos($content, $part);
if($start===false)return false;
$end = $start+strlen($part);
$start--;
$c = 0;
for($i=$start;$i>=0;$i--){
if(substr($content,$i,1) == $startChar) $c++;
if(substr($content,$i,1) == $endChar) $c--;
if($c == $n){
$start = $i;
break;
}
}
$c = 0;
for($i=$end;$i<=$length;$i++){
if(substr($content,$i,1) == $endChar) $c++;
if(substr($content,$i,1) == $startChar) $c--;
if($c == $n){
$end = $i;
break;
}
}
return substr($content, $start, $end - $start + 1);
}
private function getExtendFunction($content, $part, $startChar = '(', $endChar = ')'){
$code = $this->getExtendCode($content, $part, 1, $startChar, $endChar);
if(!$code) return false;
$start = strpos($content, $code) - 1;
$end = $start + strlen($code);
for($i=$start;$i>=0;$i--){
$char = substr($content,$i,1);
2024-12-23 08:46:48 -05:00
if(!ctype_alpha($char)&&$char!='_'){
2024-04-30 10:56:41 -04:00
$start = $i+1;
break;
}
}
if(substr($content,$start-1,1) == ',') $start--;
2024-09-19 09:35:42 -04:00
else if(substr($content,$end+1,1) == ',') $end++;
2024-04-30 10:56:41 -04:00
return substr($content, $start, $end - $start + 1);
}
private function checkdir($basedir){
if($dh=opendir($basedir)){
while (($file=readdir($dh)) !== false){
if($file != '.' && $file != '..'){
if(!is_dir($basedir.'/'.$file) && substr($file,-3)=='.js'){
$this->handlefile($basedir.'/'.$file);
}else if(!is_dir($basedir.'/'.$file) && substr($file,-4)=='.map'){
unlink($basedir.'/'.$file);
}
}
}
closedir($dh);
}
}
2024-09-19 09:35:42 -04:00
private function str_replace_once($needle, $replace, $haystack) {
$pos = strpos($haystack, $needle);
if ($pos === false) {
return $haystack;
}
return substr_replace($haystack, $replace, $pos, strlen($needle));
}
2024-04-30 10:56:41 -04:00
private function handlefile($filepath){
$file = file_get_contents($filepath);
if(!$file)return;
$flag = false;
if(strpos($file, 'window.location.protocol.indexOf("https")>=0')!==false){ //index
$file = str_replace('(window.location.protocol.indexOf("https")>=0)', '1', $file);
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, 'isGetCoupon:', 2);
2024-04-30 10:56:41 -04:00
if($code){
$file = str_replace($code, '{}', $file);
}
2024-12-23 08:46:48 -05:00
$file = preg_replace('!recommendShow:\w+,!', 'recommendShow:!1,', $file, 1);
$code = $this->getExtendCode($file, '"需求反馈"', 1, '[', ']');
if($code){
$file = str_replace($code, '[]', $file);
}
2024-04-30 10:56:41 -04:00
$flag = true;
}
2024-12-23 08:46:48 -05:00
if(strpos($file, '售后QQ群')!==false){ //main
$code = $this->getExtendCode($file, '"微信公众号"', 1);
2024-04-30 10:56:41 -04:00
$code = $this->getExtendFunction($file, $code);
$start = strpos($file, $code) - 1;
for($i=$start;$i>=0;$i--){
if(substr($file,$i,1) == ','){
$start = $i;
break;
}
}
$code = $this->getExtendCode($file, '"/other/customer-service.png"', 2);
2024-12-23 08:46:48 -05:00
$code = $this->getExtendFunction($file, $code);
2024-04-30 10:56:41 -04:00
$end = strpos($file, $code)+strlen($code);
2024-12-23 08:46:48 -05:00
$code = substr($file, $start, $end - $start);
2024-04-30 10:56:41 -04:00
$file = str_replace($code, '', $file);
$flag = true;
}
2024-12-23 08:46:48 -05:00
if(strpos($file, 'useNegotiate')!==false){ //utils
$code = $this->getExtendCode($file, 'createPeerConnection()', 1);
if($code){
$file = str_replace($code, '{}', $file);
}
$file = preg_replace('!\w+\(\(\(\)=>"calc"===\w+\.\w+\.type\)\)!', '!1', $file);
$file = preg_replace('!\w+\(\(\(\)=>"input"===\w+\.\w+\.type\)\)!', '!1', $file);
$file = preg_replace('!\w+\(\(function\(\)\{return"calc"===\w+\.\w+\.type\}\)\)!', '!1', $file);
$file = preg_replace('!\w+\(\(function\(\)\{return"input"===\w+\.\w+\.type\}\)\)!', '!1', $file);
2024-06-01 03:07:31 -04:00
$flag = true;
}
2024-04-30 10:56:41 -04:00
if(strpos($file, '请冷静几秒钟,确认以下要删除的数据')!==false && strpos($file, '"计算结果:"')!==false){ //site
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, '"计算结果:"', 1, '[', ']');
2024-04-30 10:56:41 -04:00
$code = $this->getExtendFunction($file, $code);
$file = str_replace($code, '', $file);
$file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
2024-12-23 08:46:48 -05:00
$file = preg_replace('!value=\!0,(\w+)\.value=5;!', 'value=!1,$1.value=0;', $file);
2024-04-30 10:56:41 -04:00
$flag = true;
}
2024-12-23 08:46:48 -05:00
if(strpos($file, '"left-waf"')!==false && strpos($file, '"iconWaf"')!==false){ //site.table
$code = $this->getExtendCode($file, '"left-waf"');
$code = $this->getExtendCode($file, $code, 1, '[', ']');
$code = $this->getExtendFunction($file, $code);
$file = str_replace($code, '""', $file);
$flag = true;
}
if(strpos($file, 'svgtofont-left-waf')!==false && strpos($file, '"iconWaf"')!==false){ //site.table
2024-08-31 07:54:50 -04:00
$code = $this->getExtendCode($file, 'svgtofont-left-waf');
2024-04-30 10:56:41 -04:00
$code = $this->getExtendCode($file, $code, 1, '[', ']');
$code = $this->getExtendFunction($file, $code);
$file = str_replace($code, '""', $file);
$flag = true;
2024-08-31 07:54:50 -04:00
}
2024-04-30 10:56:41 -04:00
if(strpos($file, '"商用SSL证书"')!==false){ //site-ssl
$code = $this->getExtendFunction($file, '"商用SSL证书"', '{', '}');
$file = str_replace($code, '', $file);
2024-12-23 08:46:48 -05:00
$code = $this->getExtendFunction($file, '"宝塔证书"', '{', '}');
2024-04-30 10:56:41 -04:00
$file = str_replace($code, '', $file);
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, '"购买商业证书"', 2);
if($code){
$code2 = str_replace('"busSslList"', '"letsEncryptList"', $code);
$code2 = str_replace($this->getExtendFunction($code, '"购买商业证书"'), '', $code2);
$file = str_replace($code, $code2, $file);
}
$file = preg_replace('!(\w+)\("sslCertificate"\)!', '$1("EncryptCertificate")', $file);
$flag = true;
}
if(strpos($file, '"busSslList"')!==false && strpos($filepath, '/useStore')){ //site-ssl
$file = str_replace('"busSslList"', '"currentCertInfo"', $file);
2024-04-30 10:56:41 -04:00
$flag = true;
}
if(strpos($file, '如果您希望添加其它Docker应用')!==false){
$code = $this->getExtendCode($file, '如果您希望添加其它Docker应用', 1, '[', ']');
$code = $this->getExtendFunction($file, $code);
$file = str_replace($code, '', $file);
$flag = true;
}
2024-06-01 03:07:31 -04:00
if(strpos($file, '"recom-view"')!==false){ //soft
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, '"recom-view"');
$code = $this->getExtendFunction($file, $code);
$file = str_replace($code, '', $file);
2024-06-01 03:07:31 -04:00
$flag = true;
}
if(strpos($file, '"打开插件文件目录"')!==false){ //soft.table
2024-06-07 02:09:15 -04:00
$code = $this->getExtendFunction($file, '"(续费)"');
2024-06-01 03:07:31 -04:00
$file = str_replace($code, '""', $file);
$flag = true;
}
2024-06-07 02:09:15 -04:00
2024-04-30 10:56:41 -04:00
for($i=0;$i<5;$i++){
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, ',"需求反馈"', 1, '[', ']');
2024-04-30 10:56:41 -04:00
if($code){
2024-12-23 08:46:48 -05:00
$file = str_replace($code, '[]', $file);
2024-04-30 10:56:41 -04:00
$flag = true;
}
}
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, '("需求反馈")', 1, '[', ']');
2024-04-30 10:56:41 -04:00
if($code){
2024-12-23 08:46:48 -05:00
$file = str_replace($code, '[]', $file);
2024-04-30 10:56:41 -04:00
$flag = true;
}
2024-12-23 08:46:48 -05:00
$code = $this->getExtendCode($file, '(" 需求反馈 ")', 1, '[', ']');
if($code && strpos($filepath, 'vue_vue_type_') === false){
$file = str_replace($code, '[]', $file);
2024-04-30 10:56:41 -04:00
$flag = true;
}
2024-12-23 08:46:48 -05:00
2024-04-30 10:56:41 -04:00
if(strpos('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', $file)!==false){
$file = str_replace('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', '暂无搜索结果', $file);
$flag = true;
}
if(!$flag) return;
if(file_put_contents($filepath, $file)){
echo '文件:'.$filepath.' 处理成功'."\n";
}else{
echo '文件:'.$filepath.' 处理失败,可能无写入权限'."\n";
}
}
}