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.

237 lines
9.9 KiB

7 months ago
5 months ago
7 months ago
5 months ago
7 months ago
5 months ago
7 months ago
5 months ago
7 months ago
5 months ago
7 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 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+,isCheck:\w+,!', ',isCalc:!1,isInput:!1,isCheck:!1,', $file);
  121. $file = preg_replace('!\w+\(\(\(\)=>"calc"===\w+\.type\|\|"checkConfirm"===\w+\.type\)\)!', '!1', $file);
  122. $file = preg_replace('!\w+\(\(\(\)=>"input"===\w+\.type\)\)!', '!1', $file);
  123. $file = preg_replace('!\w+\(\(\(\)=>"check"===\w+\.type\|\|"checkConfirm"===\w+\.type\)\)!', '!1', $file);
  124. $file = preg_replace('!\w+\(\(function\(\)\{return"calc"===\w+\.type\|\|"checkConfirm"===\w+\.type\}\)\)!', '!1', $file);
  125. $file = preg_replace('!\w+\(\(function\(\)\{return"input"===\w+\.type\}\)\)!', '!1', $file);
  126. $file = preg_replace('!\w+\(\(function\(\)\{return"check"===\w+\.type\|\|"checkConfirm"===\w+\.type\}\)\)!', '!1', $file);
  127. $flag = true;
  128. }
  129. if(strpos($file, '请冷静几秒钟,确认以下要删除的数据')!==false && strpos($file, '"计算结果:"')!==false){ //site
  130. $code = $this->getExtendCode($file, '"计算结果:"', 2, '[', ']');
  131. $code = $this->getExtendFunction($file, $code);
  132. $file = str_replace($code, '', $file);
  133. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  134. $file = preg_replace('!\w+\.sum\!==\w+\.addend1\+\w+\.addend2!', '!1', $file);
  135. $file = preg_replace('!,disableDeleteButton:\w+,countdown:\w+,!', ',disableDeleteButton:!1,countdown:!1,', $file);
  136. if(preg_match('/startCountdown:(\w+),/', $file, $matchs)){
  137. $file = str_replace([';'.$matchs[1].'()', $matchs[1].'(),'], '', $file);
  138. }
  139. $flag = true;
  140. }
  141. /*if(strpos($file, '"bt-waf-gray"')!==false){ //site.popup
  142. $code = $this->getExtendCode($file, '"bt-waf-gray"', 2);
  143. $code = $this->getExtendCode($file, $code, 1, '[', ']');
  144. $code = $this->getExtendFunction($file, $code);
  145. $file = str_replace($code, '""', $file);
  146. $flag = true;
  147. }*/
  148. if(strpos($file, '"商用SSL证书"')!==false){ //site-ssl
  149. $code = $this->getExtendFunction($file, '"商用SSL证书"', '{', '}');
  150. $file = str_replace($code, '', $file);
  151. $code = $this->getExtendFunction($file, '"测试证书"', '{', '}');
  152. $file = str_replace($code, '', $file);
  153. $file = str_replace('"currentCertInfo":"busSslList"', '"currentCertInfo":"currentCertInfo"', $file);
  154. $file = preg_replace('!\{(\w+)\.value="busSslList",\w+\(\)\}!', '{$1.value="letsEncryptList"}', $file);
  155. $flag = true;
  156. }
  157. if(strpos($file, '如果您希望添加其它Docker应用')!==false){
  158. $code = $this->getExtendCode($file, '如果您希望添加其它Docker应用', 1, '[', ']');
  159. $code = $this->getExtendFunction($file, $code);
  160. $file = str_replace($code, '', $file);
  161. $flag = true;
  162. }
  163. if(strpos($file, '"recom-view"')!==false){ //soft
  164. $code = $this->getExtendFunction($file, '"recom-view"');
  165. $file = str_replace($code, 'void(0)', $file);
  166. $flag = true;
  167. }
  168. if(strpos($file, '"打开插件文件目录"')!==false){ //soft.table
  169. $code = $this->getExtendFunction($file, '"(续费)"');
  170. $file = str_replace($code, '""', $file);
  171. $code = $this->getExtendFunction($file, '"(续费)"');
  172. $file = str_replace($code, '""', $file);
  173. $flag = true;
  174. }
  175. if(strpos($file, '检测到同名文件')!==false){ //file.
  176. $code = $this->getExtendCode($file, '计算结果:', 3, '[', ']');
  177. $code = $this->getExtendFunction($file, $code);
  178. $file = str_replace($code, '', $file);
  179. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  180. $flag = true;
  181. }
  182. for($i=0;$i<5;$i++){
  183. $code = $this->getExtendCode($file, 'content:"需求反馈"', 2);
  184. if($code){
  185. $code = $this->getExtendFunction($file, $code);
  186. $start = strpos($file, $code);
  187. if(substr($file,$start-1,1) == ':'){
  188. $file = str_replace($code, '{}', $file);
  189. }else{
  190. $file = str_replace($code, '', $file);
  191. }
  192. $flag = true;
  193. }
  194. }
  195. $code = $this->getExtendFunction($file, '("需求反馈")');
  196. if($code){
  197. $file = str_replace($code, '', $file);
  198. $flag = true;
  199. }
  200. $code = $this->getExtendFunction($file, '(" 需求反馈 ")');
  201. if($code){
  202. $file = str_replace($code, '', $file);
  203. $flag = true;
  204. }
  205. if(strpos('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', $file)!==false){
  206. $file = str_replace('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', '暂无搜索结果', $file);
  207. $flag = true;
  208. }
  209. if(!$flag) return;
  210. if(file_put_contents($filepath, $file)){
  211. echo '文件:'.$filepath.' 处理成功'."\n";
  212. }else{
  213. echo '文件:'.$filepath.' 处理失败,可能无写入权限'."\n";
  214. }
  215. }
  216. }