180 lines
6.2 KiB
PHP
180 lines
6.2 KiB
PHP
<?php
|
|
|
|
namespace kernel\modules\module_shop_client\controllers;
|
|
|
|
use GuzzleHttp\Client;
|
|
use GuzzleHttp\Exception\GuzzleException;
|
|
use JetBrains\PhpStorm\NoReturn;
|
|
use kernel\AdminController;
|
|
use kernel\Flash;
|
|
use kernel\helpers\Debug;
|
|
use kernel\helpers\Files;
|
|
use kernel\helpers\RESTClient;
|
|
use kernel\helpers\SMTP;
|
|
use kernel\Mailing;
|
|
use kernel\modules\module_shop_client\services\ModuleShopClientService;
|
|
use kernel\Request;
|
|
use kernel\services\ModuleService;
|
|
use kernel\services\ModuleShopService;
|
|
use kernel\services\TokenService;
|
|
use PHPMailer\PHPMailer\Exception;
|
|
|
|
class ModuleShopClientController extends AdminController
|
|
{
|
|
|
|
protected Client $client;
|
|
protected ModuleService $moduleService;
|
|
|
|
protected function init(): void
|
|
{
|
|
parent::init();
|
|
$this->cgView->viewPath = KERNEL_MODULES_DIR . "/module_shop_client/views/";
|
|
|
|
$this->client = new Client();
|
|
$this->moduleService = new ModuleService();
|
|
}
|
|
|
|
/**
|
|
* @throws GuzzleException
|
|
*/
|
|
public function actionIndex(int $page_number = 1): void
|
|
{
|
|
|
|
if ($this->moduleService->issetModuleShopToken()) {
|
|
if ($this->moduleService->isServerAvailable()) {
|
|
|
|
$per_page = 8;
|
|
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
|
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
|
$module_count = count($modules_info);
|
|
$modules_info = array_slice($modules_info, $per_page * ($page_number - 1), $per_page);
|
|
|
|
$this->cgView->render("index.php", [
|
|
'modules_info' => $modules_info,
|
|
'moduleService' => $this->moduleService,
|
|
'page_number' => $page_number,
|
|
'module_count' => $module_count,
|
|
'per_page' => $per_page,
|
|
]);
|
|
} else {
|
|
$this->cgView->render("module_shop_error_connection.php");
|
|
}
|
|
|
|
} else {
|
|
$this->cgView->render("login_at_module_shop.php");
|
|
}
|
|
}
|
|
|
|
public function actionView(int $id): void
|
|
{
|
|
$module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/' . $id);
|
|
$module_info = json_decode($module_info->getBody()->getContents(), true);
|
|
$this->cgView->render("view.php", ['data' => $module_info]);
|
|
}
|
|
|
|
/**
|
|
* @throws GuzzleException
|
|
*/
|
|
#[NoReturn] public function actionInstall(): void
|
|
{
|
|
$request = new Request();
|
|
$id = $request->get("id");
|
|
$module_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/install/' . $id);
|
|
|
|
$module_info = json_decode($module_info->getBody()->getContents(), true);
|
|
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $module_info['path_to_archive'], RESOURCES_DIR . "/tmp/modules");
|
|
$this->moduleService->installModule('/resources/tmp/modules/' . basename($module_info['path_to_archive']));
|
|
|
|
Flash::setMessage("success", "Модуль успешно установлен.");
|
|
$this->redirect('/admin/module_shop_client', 302);
|
|
}
|
|
|
|
#[NoReturn] public function actionUpdate(): void
|
|
{
|
|
$request = new Request();
|
|
$slug = $request->get("slug");
|
|
$modules_info = RESTClient::request($_ENV['MODULE_SHOP_URL'] . '/api/module_shop/gb_slug');
|
|
|
|
$modules_info = json_decode($modules_info->getBody()->getContents(), true);
|
|
foreach ($modules_info as $module) {
|
|
if ($module['slug'] === $slug) {
|
|
$path = $module['path_to_archive'];
|
|
}
|
|
}
|
|
if (isset($path)) {
|
|
Files::uploadByUrl($_ENV['MODULE_SHOP_URL'] . $path, RESOURCES_DIR . "/tmp/modules");
|
|
$this->moduleService->updateModule('/resources/tmp/modules/' . basename($path));
|
|
Flash::setMessage("success", "Модуль успешно обновлен.");
|
|
} else {
|
|
Flash::setMessage("error", "Ошибка обновления модуля.");
|
|
}
|
|
|
|
$this->redirect('/admin/module_shop_client', 302);
|
|
}
|
|
|
|
#[NoReturn] public function actionDelete(): void
|
|
{
|
|
$request = new Request();
|
|
$slug = $request->get("slug");
|
|
$module_info = $this->moduleService->getModuleInfoBySlug($slug);
|
|
$this->moduleService->uninstallModule($module_info['app_module_path']);
|
|
|
|
Flash::setMessage("success", "Модуль успешно удален.");
|
|
$this->redirect('/admin/module_shop_client', 302);
|
|
}
|
|
|
|
/**
|
|
* @throws Exception
|
|
*/
|
|
public function actionAuth(): void
|
|
{
|
|
$request = new Request();
|
|
$address = $request->post("email");
|
|
|
|
// $mailing = new Mailing();
|
|
// $mailing->send_html("login_by_code.php", ['code' => mt_rand(100000, 999999)], [
|
|
// 'address' => $address,
|
|
// 'subject' => "Код авторизации",
|
|
// "from_name" => $_ENV['APP_NAME']
|
|
// ]);
|
|
|
|
$moduleShopService = new ModuleShopService();
|
|
$result = $moduleShopService->email_auth($address);
|
|
|
|
if ($result['status'] == 'success'){
|
|
$this->cgView->render('enter_code.php', ['email' => $address]);
|
|
}
|
|
|
|
$this->cgView->render('module_shop_error_connection.php', ['email' => $address]);
|
|
}
|
|
|
|
public function actionCodeCheck(): void
|
|
{
|
|
$request = new Request();
|
|
$code = $request->post("code");
|
|
|
|
// $mailing = new Mailing();
|
|
// $mailing->send_html("login_by_code.php", ['code' => mt_rand(100000, 999999)], [
|
|
// 'address' => $address,
|
|
// 'subject' => "Код авторизации",
|
|
// "from_name" => $_ENV['APP_NAME']
|
|
// ]);
|
|
|
|
$moduleShopService = new ModuleShopService();
|
|
$result = $moduleShopService->code_check($code);
|
|
|
|
if (isset($result['access_token'])){
|
|
|
|
$envFile = \EnvEditor\EnvFile::loadFrom(ROOT_DIR . "/.env");
|
|
|
|
$envFile->setValue("MODULE_SHOP_TOKEN", $result['access_token']);
|
|
|
|
$envFile->saveTo(ROOT_DIR . "/.env");
|
|
|
|
$this->cgView->render('success_login.php');
|
|
}
|
|
|
|
$this->cgView->render('module_shop_error_connection.php');
|
|
}
|
|
|
|
} |