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.

211 lines
8.9 KiB

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