AddInstance.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Instance;
  5. use \Zttp\Zttp;
  6. class AddInstance extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'instance:add {domain}';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'Command description';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return mixed
  33. */
  34. public function handle()
  35. {
  36. $domain = $this->argument('domain');
  37. $validator = dns_get_record($domain);
  38. if(count($validator) == 0) {
  39. $this->error('invalid domain');
  40. return;
  41. }
  42. $url = "https://{$domain}/api/nodeinfo/2.0.json";
  43. $response = Zttp::get($url);
  44. $json = $response->json();
  45. if($response->status() != 200 || $json['software']['name'] != 'pixelfed') {
  46. $this->error('invalid response');
  47. return;
  48. }
  49. $instance = Instance::firstOrCreate(['name' => $domain, 'domain' => $domain]);
  50. if($this->confirm('Do you want to approve this instance?')) {
  51. $instance->approved_at = now();
  52. $instance->save();
  53. }
  54. $this->info("Added new instance: {$domain}!");
  55. $this->call('instance:fetch', ['domain' => $instance->domain]);
  56. }
  57. }