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.

244 lines
9.9 KiB

5 months ago
23 hours ago
5 months ago
23 hours ago
5 months ago
4 months ago
5 months ago
4 months ago
3 weeks ago
4 months ago
5 months ago
3 weeks ago
5 months ago
3 weeks ago
5 months ago
4 months ago
23 hours ago
5 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
5 months ago
23 hours ago
5 months ago
23 hours ago
5 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. else if(substr($content,$end+1,1) == ',') $end++;
  71. return substr($content, $start, $end - $start + 1);
  72. }
  73. private function checkdir($basedir){
  74. if($dh=opendir($basedir)){
  75. while (($file=readdir($dh)) !== false){
  76. if($file != '.' && $file != '..'){
  77. if(!is_dir($basedir.'/'.$file) && substr($file,-3)=='.js'){
  78. $this->handlefile($basedir.'/'.$file);
  79. }else if(!is_dir($basedir.'/'.$file) && substr($file,-4)=='.map'){
  80. unlink($basedir.'/'.$file);
  81. }
  82. }
  83. }
  84. closedir($dh);
  85. }
  86. }
  87. private function str_replace_once($needle, $replace, $haystack) {
  88. $pos = strpos($haystack, $needle);
  89. if ($pos === false) {
  90. return $haystack;
  91. }
  92. return substr_replace($haystack, $replace, $pos, strlen($needle));
  93. }
  94. private function handlefile($filepath){
  95. $file = file_get_contents($filepath);
  96. if(!$file)return;
  97. $flag = false;
  98. if(strpos($file, 'window.location.protocol.indexOf("https")>=0')!==false){ //index
  99. $file = str_replace('(window.location.protocol.indexOf("https")>=0)', '1', $file);
  100. $file = preg_replace('!setTimeout\(\(\(\)=>\{\w+\(\)\}\),3e3\)!', '', $file);
  101. $file = preg_replace('!setTimeout\(\(function\(\)\{\w+\(\)\}\),3e3\)!', '', $file);
  102. $file = preg_replace('!recommendShow:\w+,!', 'recommendShow:!1,', $file);
  103. $code = $this->getExtendCode($file, '"需求反馈"', 2);
  104. if($code){
  105. $file = str_replace($code, '{}', $file);
  106. }
  107. $flag = true;
  108. }
  109. if(strpos($file, '"WechatOfficial"')!==false){ //main
  110. $code = $this->getExtendCode($file, '"WechatOfficial"', 5);
  111. $code = $this->getExtendFunction($file, $code);
  112. $start = strpos($file, $code) - 1;
  113. for($i=$start;$i>=0;$i--){
  114. if(substr($file,$i,1) == ','){
  115. $start = $i;
  116. break;
  117. }
  118. }
  119. $code = $this->getExtendCode($file, '"/other/customer-service.png"', 2);
  120. $code = $this->getExtendCode($file, $code, 2, '[', ']');
  121. $end = strpos($file, $code)+strlen($code);
  122. $code = substr($file, $start, $end - $start + 1);
  123. $file = str_replace($code, '', $file);
  124. $file = str_replace('startNegotiate(),', '', $file);
  125. $flag = true;
  126. }
  127. if(strpos($file, '"calc"') !== false && strpos($file, '"checkConfirm"') !== false){ //main2
  128. $file = preg_replace('!,isCalc:\w+,isInput:\w+,!', ',isCalc:!1,isInput:!1,', $file);
  129. $file = preg_replace('!"calc"===\w+\.type!', '!1', $file);
  130. $file = preg_replace('!\w+\(\(\(\)=>"input"===\w+\.type\)\)!', '!1', $file);
  131. $file = preg_replace('!\w+\(\(function\(\)\{return"input"===\w+\.type\}\)\)!', '!1', $file);
  132. $flag = true;
  133. }
  134. if(strpos($file, '请冷静几秒钟,确认以下要删除的数据')!==false && strpos($file, '"计算结果:"')!==false){ //site
  135. $code = $this->getExtendCode($file, '"计算结果:"', 2, '[', ']');
  136. $code = $this->getExtendFunction($file, $code);
  137. $file = str_replace($code, '', $file);
  138. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  139. $file = preg_replace('!\w+\.sum\!==\w+\.addend1\+\w+\.addend2!', '!1', $file);
  140. $file = preg_replace('!,disableDeleteButton:\w+,countdown:\w+,!', ',disableDeleteButton:!1,countdown:!1,', $file);
  141. if(preg_match('/startCountdown:(\w+),/', $file, $matchs)){
  142. $file = str_replace([';'.$matchs[1].'()', $matchs[1].'(),'], '', $file);
  143. }
  144. $flag = true;
  145. }
  146. if(strpos($file, 'svgtofont-left-waf')!==false){ //site.table
  147. $code = $this->getExtendCode($file, 'svgtofont-left-waf');
  148. $code = $this->getExtendCode($file, $code, 1, '[', ']');
  149. $code = $this->getExtendFunction($file, $code);
  150. $file = str_replace($code, '""', $file);
  151. $flag = true;
  152. }
  153. if(strpos($file, '"商用SSL证书"')!==false){ //site-ssl
  154. $code = $this->getExtendFunction($file, '"商用SSL证书"', '{', '}');
  155. $file = str_replace($code, '', $file);
  156. $code = $this->getExtendFunction($file, '"测试证书"', '{', '}');
  157. $file = str_replace($code, '', $file);
  158. $file = str_replace('"currentCertInfo":"busSslList"', '"currentCertInfo":"currentCertInfo"', $file);
  159. $file = preg_replace('!\{(\w+)\.value="busSslList",\w+\(\)\}!', '{$1.value="letsEncryptList"}', $file);
  160. $file = preg_replace('!defaultActive:(\w+)\("sslCertificate"\)!', 'defaultActive:$1("EncryptCertificate")', $file);
  161. $flag = true;
  162. }
  163. if(strpos($file, '如果您希望添加其它Docker应用')!==false){
  164. $code = $this->getExtendCode($file, '如果您希望添加其它Docker应用', 1, '[', ']');
  165. $code = $this->getExtendFunction($file, $code);
  166. $file = str_replace($code, '', $file);
  167. $flag = true;
  168. }
  169. if(strpos($file, '"recom-view"')!==false){ //soft
  170. $code = $this->getExtendFunction($file, '"recom-view"');
  171. $file = str_replace($code, 'void(0)', $file);
  172. $flag = true;
  173. }
  174. if(strpos($file, '"打开插件文件目录"')!==false){ //soft.table
  175. $code = $this->getExtendFunction($file, '"(续费)"');
  176. $file = str_replace($code, '""', $file);
  177. $code = $this->getExtendFunction($file, '"(续费)"');
  178. $file = str_replace($code, '""', $file);
  179. $flag = true;
  180. }
  181. if(strpos($file, '检测到同名文件')!==false){ //file.
  182. $code = $this->getExtendCode($file, '计算结果:', 3, '[', ']');
  183. $code = $this->getExtendFunction($file, $code);
  184. $file = str_replace($code, '', $file);
  185. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  186. $flag = true;
  187. }
  188. for($i=0;$i<5;$i++){
  189. $code = $this->getExtendCode($file, 'content:"需求反馈"', 2);
  190. if($code){
  191. $code = $this->getExtendFunction($file, $code);
  192. $start = strpos($file, $code);
  193. if(substr($file,$start-1,1) == ':'){
  194. $file = $this->str_replace_once($code, '{}', $file);
  195. }else{
  196. $file = $this->str_replace_once($code, '', $file);
  197. }
  198. $flag = true;
  199. }
  200. }
  201. $code = $this->getExtendFunction($file, '("需求反馈")');
  202. if($code){
  203. $file = str_replace($code, '', $file);
  204. $flag = true;
  205. }
  206. $code = $this->getExtendFunction($file, '(" 需求反馈 ")');
  207. if($code){
  208. $file = str_replace($code, '', $file);
  209. $flag = true;
  210. }
  211. if(strpos('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', $file)!==false){
  212. $file = str_replace('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', '暂无搜索结果', $file);
  213. $flag = true;
  214. }
  215. if(!$flag) return;
  216. if(file_put_contents($filepath, $file)){
  217. echo '文件:'.$filepath.' 处理成功'."\n";
  218. }else{
  219. echo '文件:'.$filepath.' 处理失败,可能无写入权限'."\n";
  220. }
  221. }
  222. }