You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

235 lines
9.5 KiB

7 months ago
6 months ago
7 months ago
6 months ago
3 months ago
6 months ago
3 months ago
6 months ago
7 months ago
3 months ago
7 months ago
3 months ago
7 months ago
6 months ago
7 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
7 months ago
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use think\facade\Config;
  11. use app\lib\Plugins;
  12. class CleanViteJs extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('cleanvitejs')
  17. ->addArgument('dir', Argument::REQUIRED, '/BTPanel/static/vite/js/路径')
  18. ->setDescription('处理宝塔面板vite/js文件');
  19. }
  20. protected function execute(Input $input, Output $output)
  21. {
  22. $dir = trim($input->getArgument('dir'));
  23. if(!file_exists($dir)){
  24. $output->writeln('目录不存在');
  25. return;
  26. }
  27. //$this->handlefile($dir.'/DockerImages.js');
  28. $this->checkdir($dir);
  29. }
  30. private function getExtendCode($content, $part, $n = 1, $startChar = '{', $endChar = '}'){
  31. if(!$part) return false;
  32. $length = strlen($content);
  33. $start = strpos($content, $part);
  34. if($start===false)return false;
  35. $end = $start+strlen($part);
  36. $start--;
  37. $c = 0;
  38. for($i=$start;$i>=0;$i--){
  39. if(substr($content,$i,1) == $startChar) $c++;
  40. if(substr($content,$i,1) == $endChar) $c--;
  41. if($c == $n){
  42. $start = $i;
  43. break;
  44. }
  45. }
  46. $c = 0;
  47. for($i=$end;$i<=$length;$i++){
  48. if(substr($content,$i,1) == $endChar) $c++;
  49. if(substr($content,$i,1) == $startChar) $c--;
  50. if($c == $n){
  51. $end = $i;
  52. break;
  53. }
  54. }
  55. return substr($content, $start, $end - $start + 1);
  56. }
  57. private function getExtendFunction($content, $part, $startChar = '(', $endChar = ')'){
  58. $code = $this->getExtendCode($content, $part, 1, $startChar, $endChar);
  59. if(!$code) return false;
  60. $start = strpos($content, $code) - 1;
  61. $end = $start + strlen($code);
  62. for($i=$start;$i>=0;$i--){
  63. $char = substr($content,$i,1);
  64. if(!ctype_alpha($char)){
  65. $start = $i+1;
  66. break;
  67. }
  68. }
  69. if(substr($content,$start-1,1) == ',') $start--;
  70. return substr($content, $start, $end - $start + 1);
  71. }
  72. private function checkdir($basedir){
  73. if($dh=opendir($basedir)){
  74. while (($file=readdir($dh)) !== false){
  75. if($file != '.' && $file != '..'){
  76. if(!is_dir($basedir.'/'.$file) && substr($file,-3)=='.js'){
  77. $this->handlefile($basedir.'/'.$file);
  78. }else if(!is_dir($basedir.'/'.$file) && substr($file,-4)=='.map'){
  79. unlink($basedir.'/'.$file);
  80. }
  81. }
  82. }
  83. closedir($dh);
  84. }
  85. }
  86. private function handlefile($filepath){
  87. $file = file_get_contents($filepath);
  88. if(!$file)return;
  89. $flag = false;
  90. if(strpos($file, 'window.location.protocol.indexOf("https")>=0')!==false){ //index
  91. $file = str_replace('(window.location.protocol.indexOf("https")>=0)', '1', $file);
  92. $file = preg_replace('!setTimeout\(\(\(\)=>\{\w+\(\)\}\),3e3\)!', '', $file);
  93. $file = preg_replace('!setTimeout\(\(function\(\)\{\w+\(\)\}\),3e3\)!', '', $file);
  94. $file = preg_replace('!recommendShow:\w+,!', 'recommendShow:!1,', $file);
  95. $code = $this->getExtendCode($file, '"需求反馈"', 2);
  96. if($code){
  97. $file = str_replace($code, '{}', $file);
  98. }
  99. $flag = true;
  100. }
  101. if(strpos($file, '"WechatOfficial"')!==false){ //main
  102. $code = $this->getExtendCode($file, '"WechatOfficial"', 5);
  103. $code = $this->getExtendFunction($file, $code);
  104. $start = strpos($file, $code) - 1;
  105. for($i=$start;$i>=0;$i--){
  106. if(substr($file,$i,1) == ','){
  107. $start = $i;
  108. break;
  109. }
  110. }
  111. $code = $this->getExtendCode($file, '"/other/customer-service.png"', 2);
  112. $code = $this->getExtendCode($file, $code, 2, '[', ']');
  113. $end = strpos($file, $code)+strlen($code);
  114. $code = substr($file, $start, $end - $start + 1);
  115. $file = str_replace($code, '', $file);
  116. $file = str_replace('startNegotiate(),', '', $file);
  117. $flag = true;
  118. }
  119. if(strpos($file, '"calc"') !== false && strpos($file, '"checkConfirm"') !== false){ //main2
  120. $file = preg_replace('!,isCalc:\w+,isInput:\w+,!', ',isCalc:!1,isInput:!1,', $file);
  121. $file = preg_replace('!"calc"===\w+\.type!', '!1', $file);
  122. $file = preg_replace('!\w+\(\(\(\)=>"input"===\w+\.type\)\)!', '!1', $file);
  123. $file = preg_replace('!"calc"===\w+\.type!', '!1', $file);
  124. $file = preg_replace('!\w+\(\(function\(\)\{return"input"===\w+\.type\}\)\)!', '!1', $file);
  125. $flag = true;
  126. }
  127. if(strpos($file, '请冷静几秒钟,确认以下要删除的数据')!==false && strpos($file, '"计算结果:"')!==false){ //site
  128. $code = $this->getExtendCode($file, '"计算结果:"', 2, '[', ']');
  129. $code = $this->getExtendFunction($file, $code);
  130. $file = str_replace($code, '', $file);
  131. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  132. $file = preg_replace('!\w+\.sum\!==\w+\.addend1\+\w+\.addend2!', '!1', $file);
  133. $file = preg_replace('!,disableDeleteButton:\w+,countdown:\w+,!', ',disableDeleteButton:!1,countdown:!1,', $file);
  134. if(preg_match('/startCountdown:(\w+),/', $file, $matchs)){
  135. $file = str_replace([';'.$matchs[1].'()', $matchs[1].'(),'], '', $file);
  136. }
  137. $flag = true;
  138. }
  139. if(strpos($file, 'svgtofont-left-waf')!==false){ //site.table
  140. $code = $this->getExtendCode($file, 'svgtofont-left-waf');
  141. $code = $this->getExtendCode($file, $code, 1, '[', ']');
  142. $code = $this->getExtendFunction($file, $code);
  143. $file = str_replace($code, '""', $file);
  144. $flag = true;
  145. }
  146. if(strpos($file, '"商用SSL证书"')!==false){ //site-ssl
  147. $code = $this->getExtendFunction($file, '"商用SSL证书"', '{', '}');
  148. $file = str_replace($code, '', $file);
  149. $code = $this->getExtendFunction($file, '"测试证书"', '{', '}');
  150. $file = str_replace($code, '', $file);
  151. $file = str_replace('"currentCertInfo":"busSslList"', '"currentCertInfo":"currentCertInfo"', $file);
  152. $file = preg_replace('!\{(\w+)\.value="busSslList",\w+\(\)\}!', '{$1.value="letsEncryptList"}', $file);
  153. $flag = true;
  154. }
  155. if(strpos($file, '如果您希望添加其它Docker应用')!==false){
  156. $code = $this->getExtendCode($file, '如果您希望添加其它Docker应用', 1, '[', ']');
  157. $code = $this->getExtendFunction($file, $code);
  158. $file = str_replace($code, '', $file);
  159. $flag = true;
  160. }
  161. if(strpos($file, '"recom-view"')!==false){ //soft
  162. $code = $this->getExtendFunction($file, '"recom-view"');
  163. $file = str_replace($code, 'void(0)', $file);
  164. $flag = true;
  165. }
  166. if(strpos($file, '"打开插件文件目录"')!==false){ //soft.table
  167. $code = $this->getExtendFunction($file, '"(续费)"');
  168. $file = str_replace($code, '""', $file);
  169. $code = $this->getExtendFunction($file, '"(续费)"');
  170. $file = str_replace($code, '""', $file);
  171. $flag = true;
  172. }
  173. if(strpos($file, '检测到同名文件')!==false){ //file.
  174. $code = $this->getExtendCode($file, '计算结果:', 3, '[', ']');
  175. $code = $this->getExtendFunction($file, $code);
  176. $file = str_replace($code, '', $file);
  177. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  178. $flag = true;
  179. }
  180. for($i=0;$i<5;$i++){
  181. $code = $this->getExtendCode($file, 'content:"需求反馈"', 2);
  182. if($code){
  183. $code = $this->getExtendFunction($file, $code);
  184. $start = strpos($file, $code);
  185. if(substr($file,$start-1,1) == ':'){
  186. $file = str_replace($code, '{}', $file);
  187. }else{
  188. $file = str_replace($code, '', $file);
  189. }
  190. $flag = true;
  191. }
  192. }
  193. $code = $this->getExtendFunction($file, '("需求反馈")');
  194. if($code){
  195. $file = str_replace($code, '', $file);
  196. $flag = true;
  197. }
  198. $code = $this->getExtendFunction($file, '(" 需求反馈 ")');
  199. if($code){
  200. $file = str_replace($code, '', $file);
  201. $flag = true;
  202. }
  203. if(strpos('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', $file)!==false){
  204. $file = str_replace('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', '暂无搜索结果', $file);
  205. $flag = true;
  206. }
  207. if(!$flag) return;
  208. if(file_put_contents($filepath, $file)){
  209. echo '文件:'.$filepath.' 处理成功'."\n";
  210. }else{
  211. echo '文件:'.$filepath.' 处理失败,可能无写入权限'."\n";
  212. }
  213. }
  214. }