Tool.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Command;
  4. use App\Models\Setting;
  5. use App\Utils\QQWry;
  6. final class Tool extends Command
  7. {
  8. public $description = <<<EOL
  9. ├─=: php xcat Tool [选项]
  10. │ ├─ initQQWry - 下载 IP 解析库
  11. │ ├─ setTelegram - 设置 Telegram 机器人
  12. │ ├─ resetAllSettings - 使用默认值覆盖设置中心设置
  13. │ ├─ exportAllSettings - 导出所有设置
  14. │ ├─ importAllSettings - 导入所有设置
  15. │ ├─ upgradeDatabase - 升级(如果不存在的话初始化) 数据库
  16. EOL;
  17. public function boot(): void
  18. {
  19. if (count($this->argv) === 2) {
  20. echo $this->description;
  21. } else {
  22. $methodName = $this->argv[2];
  23. if (method_exists($this, $methodName)) {
  24. $this->$methodName();
  25. } else {
  26. echo '方法不存在.' . PHP_EOL;
  27. }
  28. }
  29. }
  30. public function upgradeDatabase(): void
  31. {
  32. $phinx = new \Phinx\Console\PhinxApplication();
  33. $phinx->run();
  34. }
  35. public function setTelegram(): void
  36. {
  37. $WebhookUrl = $_ENV['baseUrl'] . '/telegram_callback?token=' . $_ENV['telegram_request_token'];
  38. $telegram = new \Telegram\Bot\Api($_ENV['telegram_token']);
  39. $telegram->removeWebhook();
  40. if ($telegram->setWebhook(['url' => $WebhookUrl])) {
  41. echo 'Bot @' . $telegram->getMe()->getUsername() . ' 设置成功!' . PHP_EOL;
  42. } else {
  43. echo '设置失败!' . PHP_EOL;
  44. }
  45. }
  46. public function initQQWry(): void
  47. {
  48. echo '正在下载或更新纯真ip数据库...' . PHP_EOL;
  49. $path = BASE_PATH . '/storage/qqwry.dat';
  50. $qqwry = file_get_contents('https://qqwry.mirror.noc.one/QQWry.Dat?from=sspanel_uim');
  51. if ($qqwry !== '') {
  52. if (is_file($path)) {
  53. rename($path, $path . '.bak');
  54. }
  55. $fp = fopen($path, 'wb');
  56. if ($fp) {
  57. fwrite($fp, $qqwry);
  58. fclose($fp);
  59. echo '纯真ip数据库下载成功.' . PHP_EOL;
  60. $iplocation = new QQWry();
  61. $location = $iplocation->getlocation('8.8.8.8');
  62. $Userlocation = $location['country'];
  63. if (iconv('gbk', 'utf-8//IGNORE', $Userlocation) !== '美国') {
  64. unlink($path);
  65. if (is_file($path . '.bak')) {
  66. rename($path . '.bak', $path);
  67. }
  68. }
  69. } else {
  70. echo '纯真ip数据库保存失败,请检查权限' . PHP_EOL;
  71. }
  72. } else {
  73. echo '纯真ip数据库下载失败,请检查下载地址' . PHP_EOL;
  74. }
  75. }
  76. public function resetAllSettings(): void
  77. {
  78. $settings = Setting::all();
  79. foreach ($settings as $setting) {
  80. $setting->value = $setting->default;
  81. $setting->save();
  82. }
  83. echo '已使用默认值覆盖所有设置.' . PHP_EOL;
  84. }
  85. public function exportAllSettings(): void
  86. {
  87. $settings = Setting::all();
  88. foreach ($settings as $setting) {
  89. // 因为主键自增所以即便设置为 null 也会在导入时自动分配 id
  90. // 同时避免多位开发者 pull request 时 settings.json 文件 id 重复所可能导致的冲突
  91. $setting->id = null;
  92. // 避免开发者调试配置泄露
  93. $setting->value = $setting->default;
  94. }
  95. $json_settings = json_encode($settings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
  96. file_put_contents('./config/settings.json', $json_settings);
  97. echo '已导出所有设置.' . PHP_EOL;
  98. }
  99. public function importAllSettings(): void
  100. {
  101. $json_settings = file_get_contents('./config/settings.json');
  102. $settings = json_decode($json_settings, true);
  103. $counter = 0;
  104. foreach ($settings as $item) {
  105. $itemName = $item['item'];
  106. $query = Setting::where('item', '=', $item['item'])->first();
  107. if ($query === null) {
  108. $new_item = new Setting();
  109. $new_item->id = null;
  110. $new_item->item = $item['item'];
  111. $new_item->value = $item['value'];
  112. $new_item->class = $item['class'];
  113. $new_item->is_public = $item['is_public'];
  114. $new_item->type = $item['type'];
  115. $new_item->default = $item['default'];
  116. $new_item->mark = $item['mark'];
  117. $new_item->save();
  118. echo "添加新设置:${itemName}" . PHP_EOL;
  119. $counter += 1;
  120. }
  121. }
  122. if ($counter !== 0) {
  123. echo "总计添加了 ${counter} 条新设置." . PHP_EOL;
  124. } else {
  125. echo '没有任何新设置需要添加.' . PHP_EOL;
  126. }
  127. }
  128. }