diff --git a/composer.json b/composer.json index 0d227019..f022369e 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,9 @@ "nikic/fast-route": "^1.3", "matomo/device-detector": "^6.0", "illuminate/database": "^10.18", - "peppeocchi/php-cron-scheduler": "4.*" + "peppeocchi/php-cron-scheduler": "4.*", + "symfony/console": "^6.4", + "symfony/string": "^6.4" }, "require-dev": { "filp/whoops": "^2.15", diff --git a/ma b/ma new file mode 100644 index 00000000..1db3f566 --- /dev/null +++ b/ma @@ -0,0 +1,37 @@ +#!/usr/bin/env php +add(new ($commandPre . $name)); +} + +$pluginCommands = glob(PLUGINS . '*/commands/*.php'); +foreach ($pluginCommands as $item) { + $application->add(require $item); +} + +$application->setName('MyAAC'); +$application->setVersion(MYAAC_VERSION); + +$application->run(); diff --git a/system/bin/clear_cache.php b/system/bin/clear_cache.php deleted file mode 100644 index 9c10b793..00000000 --- a/system/bin/clear_cache.php +++ /dev/null @@ -1,18 +0,0 @@ -load(); - -use GO\Scheduler; - -// Create a new scheduler -$scheduler = new Scheduler(); - -$hooks->trigger(HOOK_CRONJOB, ['scheduler' => $scheduler]); - -// Let the scheduler execute jobs which are due. -$scheduler->run(); diff --git a/system/bin/execute_install_plugin.php b/system/bin/execute_install_plugin.php deleted file mode 100644 index b6127a66..00000000 --- a/system/bin/execute_install_plugin.php +++ /dev/null @@ -1,31 +0,0 @@ -> ' . SYSTEM . 'logs/cron.log 2>&1'; - -if (cronjob_exists($job)) { - echo 'MyAAC cronjob already installed.' . PHP_EOL; - exit(0); -} - -exec ('crontab -l', $content); - -$content = implode(' ', $content); -$content .= PHP_EOL . $job; - -file_put_contents(CACHE . 'cronjob', $content . PHP_EOL); -exec('crontab ' . CACHE. 'cronjob'); - -echo 'Installed crontab successfully.' . PHP_EOL; - -function cronjob_exists($command) -{ - $cronjob_exists=false; - - exec('crontab -l', $crontab); - if(isset($crontab)&&is_array($crontab)) { - - $crontab = array_flip($crontab); - - if(isset($crontab[$command])){ - $cronjob_exists = true; - } - - } - - return $cronjob_exists; -} diff --git a/system/bin/install_plugin.php b/system/bin/install_plugin.php deleted file mode 100644 index c727b782..00000000 --- a/system/bin/install_plugin.php +++ /dev/null @@ -1,42 +0,0 @@ -find($email_account_name); - } - else { - $account->load($email_account_name); - } - - if($account->isLoaded()) { - $email_account_name = $account->getEMail(); - } - else { - $player = new OTS_Player(); - $player->find($email_account_name); - if($player->isLoaded()) { - $email_account_name = $player->getAccount()->getEMail(); - } - else { - echo 'Cannot find player or account with name: ' . $email_account_name . '.' . PHP_EOL; - exit(3); - } - } -} - -if(!Validator::email($email_account_name)) { - echo 'Invalid E-Mail format.' . PHP_EOL; - exit(4); -} - -if(strlen($subject) > 255) { - echo 'Subject max length is 255 characters.' . PHP_EOL; - exit(5); -} - -if(!_mail($email_account_name, $subject, $message)) { - echo 'An error occurred while sending email. More info can be found in system/logs/mailer-error.log'; - exit(6); -} - -echo 'Mail sent to ' . $email_account_name . '.' . PHP_EOL; diff --git a/system/src/Commands/CacheClearCommand.php b/system/src/Commands/CacheClearCommand.php new file mode 100644 index 00000000..bdab1f21 --- /dev/null +++ b/system/src/Commands/CacheClearCommand.php @@ -0,0 +1,29 @@ +setName('cache:clear') + ->setDescription('This command clears the cache'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + if (!clearCache()) { + $io->error('Unknown error on clear cache'); + return Command::FAILURE; + } + + $io->success('Cache cleared'); + return Command::SUCCESS; + } +} diff --git a/system/src/Commands/Command.php b/system/src/Commands/Command.php new file mode 100644 index 00000000..7295acaa --- /dev/null +++ b/system/src/Commands/Command.php @@ -0,0 +1,18 @@ +hooks = new Hooks(); + $this->hooks->load(); + } +} diff --git a/system/src/Commands/CronjobCommand.php b/system/src/Commands/CronjobCommand.php new file mode 100644 index 00000000..81d3dd90 --- /dev/null +++ b/system/src/Commands/CronjobCommand.php @@ -0,0 +1,29 @@ +setName('cronjob') + ->setDescription('This command runs cron tasks'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + // Create a new scheduler + $scheduler = new Scheduler(); + + $this->hooks->trigger(HOOK_CRONJOB, ['scheduler' => $scheduler]); + + // Let the scheduler execute jobs which are due. + $scheduler->run(); + + return Command::SUCCESS; + } +} diff --git a/system/src/Commands/CronjobInstallCommand.php b/system/src/Commands/CronjobInstallCommand.php new file mode 100644 index 00000000..e683eeba --- /dev/null +++ b/system/src/Commands/CronjobInstallCommand.php @@ -0,0 +1,60 @@ +setName('cronjob:install') + ->setDescription('This command automatically registers into your crontab'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + if (MYAAC_OS !== 'LINUX') { + $io->error('This script can be run only on linux.'); + return 2; + } + + $job = '* * * * * /usr/bin/php ' . SYSTEM . 'bin/cronjob.php >> ' . SYSTEM . 'logs/cron.log 2>&1'; + + if ($this->cronjobExists($job)) { + $io->info('MyAAC cronjob already installed.'); + return Command::FAILURE; + } + + exec('crontab -l', $content); + + $content = implode(' ', $content); + $content .= PHP_EOL . $job; + + file_put_contents(CACHE . 'cronjob', $content . PHP_EOL); + exec('crontab ' . CACHE. 'cronjob'); + + $io->success('Installed crontab successfully.'); + return Command::SUCCESS; + } + + private function cronjobExists($command): bool + { + exec('crontab -l', $crontab); + + if(isset($crontab) && is_array($crontab)) { + $crontab = array_flip($crontab); + + if(isset($crontab[$command])){ + return true; + } + } + + return false; + } +} diff --git a/system/src/Commands/MailSendCommand.php b/system/src/Commands/MailSendCommand.php new file mode 100644 index 00000000..d0db7b69 --- /dev/null +++ b/system/src/Commands/MailSendCommand.php @@ -0,0 +1,78 @@ +setName('mail:send') + ->setDescription('This command sends E-Mail to single user. Message can be provided as follows: ' . PHP_EOL + . ' echo "Hello World" | php sa email:send --subject="This is the subject" test@test.com') + ->addArgument('recipient', InputArgument::REQUIRED, 'Email, Account Name, Account id or Player Name') + ->addOption('subject', 's', InputOption::VALUE_REQUIRED, 'Subject'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $email_account_name = $input->getArgument('recipient'); + $subject = $input->getOption('subject'); + if (!$subject) { + $io->error('Please specify subject via -s or --subject="" option'); + return 2; + } + + $message = file_get_contents('php://stdin'); + + if(!str_contains($email_account_name, '@')) { + $account = new \OTS_Account(); + if(USE_ACCOUNT_NAME) { + $account->find($email_account_name); + } + else { + $account->load($email_account_name); + } + + if($account->isLoaded()) { + $email_account_name = $account->getEMail(); + } + else { + $player = new \OTS_Player(); + $player->find($email_account_name); + if($player->isLoaded()) { + $email_account_name = $player->getAccount()->getEMail(); + } + else { + $io->error('Cannot find player or account with name: ' . $email_account_name); + return 3; + } + } + } + + if(!\Validator::email($email_account_name)) { + $io->error('Invalid E-Mail format'); + return 4; + } + + if(strlen($subject) > 255) { + $io->error('Subject max length is 255 characters'); + return 5; + } + + if(!_mail($email_account_name, $subject, $message)) { + $io->error('An error occurred while sending email. More info can be found in system/logs/mailer-error.log'); + return 6; + } + + $io->success('Mail sent to ' . $email_account_name . '.'); + return Command::SUCCESS; + } +} diff --git a/system/src/Commands/PluginInstallCommand.php b/system/src/Commands/PluginInstallCommand.php new file mode 100644 index 00000000..f40961e7 --- /dev/null +++ b/system/src/Commands/PluginInstallCommand.php @@ -0,0 +1,50 @@ +setName('plugin:install') + ->setDescription('This command installs plugin') + ->addArgument('plugin', InputArgument::REQUIRED, 'Path to zip file (plugin) that you want to install'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $pathToFile = $input->getArgument('plugin'); + + $ext = strtolower(pathinfo($pathToFile, PATHINFO_EXTENSION)); + if($ext !== 'zip') {// check if it is zipped/compressed file + $io->error('Please install only .zip files'); + return 2; + } + + if(!file_exists($pathToFile)) { + $io->error('File ' . $pathToFile . ' does not exist'); + return 3; + } + + if(!Plugins::install($pathToFile)){ + $io->error(Plugins::getError()); + return 4; + } + + foreach(Plugins::getWarnings() as $warning) { + $io->warning($warning); + } + + $info = Plugins::getPluginJson(); + $io->success((isset($info['name']) ? $info['name'] . ' p' : 'P') . 'lugin has been successfully installed.'); + return Command::SUCCESS; + } +} diff --git a/system/src/Commands/PluginInstallInstallCommand.php b/system/src/Commands/PluginInstallInstallCommand.php new file mode 100644 index 00000000..5f1a1cd6 --- /dev/null +++ b/system/src/Commands/PluginInstallInstallCommand.php @@ -0,0 +1,38 @@ +setName('plugin:install:install') + ->setDescription('This command executes the "install" part of the plugin') + ->addArgument('plugin', InputArgument::REQUIRED, 'Plugin name'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $io = new SymfonyStyle($input, $output); + + $pluginName = $input->getArgument('plugin'); + if(!Plugins::executeInstall($pluginName)) { + $io->error(Plugins::getError()); + return 2; + } + + foreach(Plugins::getWarnings() as $warning) { + $io->warning($warning); + } + + $info = Plugins::getPluginJson($pluginName); + $io->success('Script for install ' . (isset($info['name']) ? $info['name'] . ' p' : 'P') . 'lugin has been successfully executed.'); + return Command::SUCCESS; + } +}