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.

332 lines
14 KiB

10 months ago
2 months ago
10 months ago
5 months ago
10 months ago
5 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
2 months ago
1 month ago
9 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
2 months ago
6 months ago
10 months ago
6 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
1 month ago
10 months ago
9 months ago
2 months ago
9 months ago
9 months ago
9 months ago
9 months ago
10 months ago
2 months ago
10 months ago
1 month ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
2 months ago
10 months ago
1 month ago
2 months ago
10 months ago
2 months ago
10 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)&&$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. $code = $this->getExtendCode($file, 'isGetCoupon:', 2);
  101. if($code){
  102. $file = str_replace($code, '{}', $file);
  103. }
  104. $file = preg_replace('!recommendShow:\w+,!', 'recommendShow:!1,', $file, 1);
  105. $code = $this->getExtendCode($file, '"需求反馈"', 1, '[', ']');
  106. if($code){
  107. $file = str_replace($code, '[]', $file);
  108. }
  109. $flag = true;
  110. }
  111. if(strpos($file, '售后QQ群:')!==false){ //main
  112. $code = $this->getExtendCode($file, '"微信公众号"', 1);
  113. $code = $this->getExtendFunction($file, $code);
  114. $start = strpos($file, $code) - 1;
  115. for($i=$start;$i>=0;$i--){
  116. if(substr($file,$i,1) == ','){
  117. $start = $i;
  118. break;
  119. }
  120. }
  121. $code = $this->getExtendCode($file, '"/other/customer-service.png"', 2);
  122. $code = $this->getExtendFunction($file, $code);
  123. $end = strpos($file, $code)+strlen($code);
  124. $code = substr($file, $start, $end - $start);
  125. $file = str_replace($code, '', $file);
  126. $flag = true;
  127. }
  128. if(strpos($file, 'useNegotiate')!==false){ //utils
  129. $code = $this->getExtendCode($file, 'createPeerConnection()', 1);
  130. if($code){
  131. $file = str_replace($code, '{}', $file);
  132. }
  133. $file = preg_replace('!\w+\(\(\(\)=>"calc"===\w+\.\w+\.type\)\)!', '!1', $file);
  134. $file = preg_replace('!\w+\(\(\(\)=>"input"===\w+\.\w+\.type\)\)!', '!1', $file);
  135. $file = preg_replace('!\w+\(\(function\(\)\{return"calc"===\w+\.\w+\.type\}\)\)!', '!1', $file);
  136. $file = preg_replace('!\w+\(\(function\(\)\{return"input"===\w+\.\w+\.type\}\)\)!', '!1', $file);
  137. $code = $this->getExtendCode($file, '"自动部署"', 2);
  138. if($code){
  139. $file = str_replace($code, '', $file);
  140. }
  141. $flag = true;
  142. }
  143. if(strpos($file, '请冷静几秒钟,确认以下要删除的数据')!==false && strpos($file, '"计算结果:"')!==false){ //site
  144. $code = $this->getExtendCode($file, '"计算结果:"', 1, '[', ']');
  145. $code = $this->getExtendFunction($file, $code);
  146. $file = str_replace($code, '', $file);
  147. $file = preg_replace('!\w+\.sum===\w+\.addend1\+\w+\.addend2!', '!0', $file);
  148. $file = preg_replace('!value=\!0,(\w+)\.value=5;!', 'value=!1,$1.value=0;', $file);
  149. $flag = true;
  150. }
  151. if(strpos($file, '"left-waf"')!==false && strpos($file, '"iconWaf"')!==false){ //site.table
  152. $code = $this->getExtendCode($file, '"left-waf"');
  153. $code = $this->getExtendCode($file, $code, 1, '[', ']');
  154. $code = $this->getExtendFunction($file, $code);
  155. $file = str_replace($code, '""', $file);
  156. $flag = true;
  157. }
  158. if(strpos($file, 'svgtofont-left-waf')!==false && strpos($file, '"iconWaf"')!==false){ //site.table
  159. $code = $this->getExtendCode($file, 'svgtofont-left-waf');
  160. $code = $this->getExtendCode($file, $code, 1, '[', ']');
  161. $code = $this->getExtendFunction($file, $code);
  162. $file = str_replace($code, '""', $file);
  163. $flag = true;
  164. }
  165. if(strpos($file, '"商用SSL证书"')!==false){ //site-ssl
  166. $code = $this->getExtendFunction($file, '"商用SSL证书"', '{', '}');
  167. $file = str_replace($code, '', $file);
  168. $code = $this->getExtendFunction($file, '"宝塔证书"', '{', '}');
  169. $file = str_replace($code, '', $file);
  170. $code = $this->getExtendCode($file, '"购买商业证书"', 2);
  171. if($code){
  172. $code2 = str_replace('"busSslList"', '"letsEncryptList"', $code);
  173. $code2 = str_replace($this->getExtendFunction($code, '"购买商业证书"'), '', $code2);
  174. $file = str_replace($code, $code2, $file);
  175. }
  176. $file = preg_replace('!(\w+)\("sslCertificate"\)!', '$1("EncryptCertificate")', $file);
  177. $flag = true;
  178. }
  179. if(strpos($file, '"busSslList"')!==false && strpos($filepath, '/useStore')){ //site-ssl
  180. $file = str_replace('"busSslList"', '"currentCertInfo"', $file);
  181. $flag = true;
  182. }
  183. if(strpos($file, '"商用SSL"')!==false){ //ssl
  184. $code = $this->getExtendFunction($file, '"商用SSL"', '{', '}');
  185. $file = str_replace($code, '', $file);
  186. $code = $this->getExtendFunction($file, '"宝塔证书"', '{', '}');
  187. $file = str_replace($code, '', $file);
  188. $code = $this->getExtendCode($file, ',"联系客服"', 2, '[', ']');
  189. if($code){
  190. $file = str_replace($code, '[]', $file);
  191. }
  192. $code = $this->getExtendCode($file, ',"联系客服"', 2, '[', ']');
  193. if($code){
  194. $file = str_replace($code, '[]', $file);
  195. }
  196. }
  197. if(strpos($file, '"SSL-CERTIFICATE-STORE"')!==false){ //ssl
  198. $file = str_replace('("ssl")', '("encrypt")', $file);
  199. $flag = true;
  200. }
  201. if(strpos($file, '如果您希望添加其它Docker应用')!==false){
  202. $code = $this->getExtendCode($file, '如果您希望添加其它Docker应用', 1, '[', ']');
  203. $code = $this->getExtendFunction($file, $code);
  204. $file = str_replace($code, '', $file);
  205. $flag = true;
  206. }
  207. if(strpos($file, '"recom-view"')!==false){ //soft
  208. $code = $this->getExtendCode($file, '"recom-view"');
  209. $code = $this->getExtendFunction($file, $code);
  210. $file = str_replace($code, '', $file);
  211. $flag = true;
  212. }
  213. if(strpos($file, '"打开插件文件目录"')!==false){ //soft.table
  214. $code = $this->getExtendFunction($file, '"(续费)"');
  215. $file = str_replace($code, '""', $file);
  216. $flag = true;
  217. }
  218. for($i=0;$i<5;$i++){
  219. $code = $this->getExtendCode($file, ',"需求反馈"', 1, '[', ']');
  220. if($code){
  221. if(strpos($code, 'svgtofont-desired')){
  222. $file = str_replace($code, '[]', $file);
  223. }else{
  224. $code = $this->getExtendFunction($code, ',"需求反馈"');
  225. $file = str_replace($code, '', $file);
  226. }
  227. $flag = true;
  228. }
  229. }
  230. $code = $this->getExtendCode($file, '("需求反馈")', 1, '[', ']');
  231. if($code){
  232. $file = str_replace($code, '[]', $file);
  233. $flag = true;
  234. }
  235. $code = $this->getExtendCode($file, '(" 需求反馈 ")', 1, '[', ']');
  236. if($code && strpos($filepath, 'vue_vue_type_') === false){
  237. $file = str_replace($code, '[]', $file);
  238. $flag = true;
  239. }
  240. $code = $this->getExtendFunction($file, '," 需求反馈 "');
  241. if($code){
  242. $file = str_replace($code, '', $file);
  243. $flag = true;
  244. }
  245. if(strpos('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', $file)!==false){
  246. $file = str_replace('暂无搜索结果,<span class="text-primary cursor-pointer NpsDialog">提交需求反馈</span>', '暂无搜索结果', $file);
  247. $flag = true;
  248. }
  249. if(strpos($file, 'getReceiveCoupon()')!==false){ //aapanel-优惠券
  250. $code = $this->getExtendCode($file, 'getReceiveCoupon()');
  251. $file = str_replace($code, '{}', $file);
  252. $flag = true;
  253. }
  254. if(strpos($file, '"Site.DelSite.index_1"')!==false){ //aapanel-site
  255. $code = $this->getExtendCode($file, '"Site.DelSite.index_10"', 3, '(', ')');
  256. if($code){
  257. $code = $this->getExtendFunction($file, $code);
  258. $file = str_replace($code, '', $file);
  259. $file = preg_replace('@\w+\.value!==\w+\.value\+\w+\.value@', '!1', $file);
  260. $file = preg_replace('@null==\w+\.value\|\|null==\w+\.value@', '!1', $file);
  261. $file = str_replace('disabled:!0', 'disabled:!1', $file);
  262. $flag = true;
  263. }
  264. }
  265. if(strpos($file, '"Component.Confirm.index_4"')!==false){ //aapanel-public
  266. $code = $this->getExtendCode($file, '"Component.Confirm.index_4"', 2, '(', ')');
  267. if($code){
  268. $code = $this->getExtendFunction($file, $code);
  269. $file = str_replace($code, '', $file);
  270. $file = preg_replace('@\w+\.value===\w+\.value\+\w+\.value@', '!0', $file);
  271. $flag = true;
  272. }
  273. $code = $this->getExtendCode($file, '"Component.Confirm.index_1"', 1, '(', ')');
  274. if($code){
  275. $code = $this->getExtendFunction($file, $code);
  276. $file = str_replace($code, '', $file);
  277. $file = preg_replace('@\w+\.value===\w+\.value\?@', '!0?', $file);
  278. $flag = true;
  279. }
  280. }
  281. if(strpos($file, '"Component.Feedback.index_7"')!==false){ //aapanel-需求反馈
  282. $code = $this->getExtendCode($file, '"Component.Feedback.index_7"', 2);
  283. if($code){
  284. $code = $this->getExtendFunction($file, $code);
  285. $file = str_replace($code, '', $file);
  286. $flag = true;
  287. }
  288. }
  289. if(strpos($file, '"Soft.index_16"')!==false){ //aapanel-soft
  290. $code = $this->getExtendCode($file, '"Soft.index_16"', 2);
  291. if($code){
  292. $file = str_replace($code, '{}', $file);
  293. $flag = true;
  294. }
  295. }
  296. if(!$flag) return;
  297. if(file_put_contents($filepath, $file)){
  298. echo '文件:'.$filepath.' 处理成功'."\n";
  299. }else{
  300. echo '文件:'.$filepath.' 处理失败,可能无写入权限'."\n";
  301. }
  302. }
  303. }