|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by Error202
|
|
|
|
* Date: 17.08.2018
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace backend\controllers;
|
|
|
|
|
|
|
|
use core\entities\ModuleRecord;
|
|
|
|
use core\helpers\FileHelper;
|
|
|
|
use core\services\ModuleService;
|
|
|
|
use yii\web\Controller;
|
|
|
|
use yii\filters\VerbFilter;
|
|
|
|
use yii\filters\AccessControl;
|
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Modules Management
|
|
|
|
* Class ModuleController
|
|
|
|
* @package backend\controllers
|
|
|
|
*/
|
|
|
|
class ModuleController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ModuleService Modules management service
|
|
|
|
*/
|
|
|
|
private $_service;
|
|
|
|
|
|
|
|
public function __construct(string $id, $module, ModuleService $service, array $config = [])
|
|
|
|
{
|
|
|
|
parent::__construct($id, $module, $config);
|
|
|
|
$this->_service = $service;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function behaviors(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'access' => [
|
|
|
|
'class' => AccessControl::class,
|
|
|
|
'rules' => [
|
|
|
|
[
|
|
|
|
'actions' => ['list', 'disable', 'enable', 'delete', 'search', 'install'],
|
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['ModuleManagement'],
|
|
|
|
],
|
|
|
|
[ // all the action are accessible to admin
|
|
|
|
'allow' => true,
|
|
|
|
'roles' => ['admin'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'verbs' => [
|
|
|
|
'class' => VerbFilter::class,
|
|
|
|
'actions' => [
|
|
|
|
'delete' => ['POST'],
|
|
|
|
'disable' => ['POST'],
|
|
|
|
'enable' => ['POST'],
|
|
|
|
'install' => ['POST'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of modules
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function actionList()
|
|
|
|
{
|
|
|
|
$modules = \Yii::$app->moduleManager->getModules();
|
|
|
|
|
|
|
|
return $this->render('list', [
|
|
|
|
'modules' => $modules,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionDelete($id)
|
|
|
|
{
|
|
|
|
$module = $this->findModel($id);
|
|
|
|
$this->_service->delete($module);
|
|
|
|
|
|
|
|
return $this->redirect(['module/list']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionDisable($id)
|
|
|
|
{
|
|
|
|
$module = $this->findModel($id);
|
|
|
|
$this->_service->disable($module);
|
|
|
|
|
|
|
|
return $this->redirect(['module/list']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionEnable($id)
|
|
|
|
{
|
|
|
|
$module = $this->findModel($id);
|
|
|
|
$this->_service->enable($module);
|
|
|
|
|
|
|
|
return $this->redirect(['module/list']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionInstall($name)
|
|
|
|
{
|
|
|
|
$needed_manifest = [];
|
|
|
|
// check installed extension
|
|
|
|
if (!extension_loaded('zip')) {
|
|
|
|
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'PHP extension "zip" not installed, Need ZipArchive class'));
|
|
|
|
return $this->redirect(['search']);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
$list = file_get_contents('http://zertex.ru/zxcms/modules.txt');
|
|
|
|
$links = array_filter(explode('|', $list));
|
|
|
|
$modules = [];
|
|
|
|
foreach ($links as $link) {
|
|
|
|
$manifest = file_get_contents($link);
|
|
|
|
$manifest = eval(str_replace('<?php', '', $manifest));
|
|
|
|
if ($manifest['name'] == $name) {
|
|
|
|
$needed_manifest = $manifest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Service not available'));
|
|
|
|
return $this->redirect(['search']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($needed_manifest) && !isset($needed_manifest['git_path']) && !isset($needed_manifest['git_nameh'])) {
|
|
|
|
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Module cannot be installed'));
|
|
|
|
return $this->redirect(['search']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get git link, download
|
|
|
|
$download_link = $needed_manifest['git_path'] . '/-/archive/master/' . $needed_manifest['git_name'] . '-master.zip';
|
|
|
|
if (FileHelper::downloadFile($download_link, \Yii::getAlias('@runtime/module.zip'))) {
|
|
|
|
// unzip
|
|
|
|
$zip = new \ZipArchive;
|
|
|
|
$res = $zip->open(\Yii::getAlias('@runtime/module.zip'));
|
|
|
|
if ($res === true) {
|
|
|
|
mkdir(\Yii::getAlias('@runtime/_module'));
|
|
|
|
$zip->extractTo(\Yii::getAlias('@runtime/_module/'));
|
|
|
|
$zip->close();
|
|
|
|
|
|
|
|
$module_path = \Yii::getAlias('@common/modules/') . $needed_manifest['name'];
|
|
|
|
if (!file_exists($module_path)) {
|
|
|
|
mkdir($module_path);
|
|
|
|
}
|
|
|
|
FileHelper::copyDirectory(\Yii::getAlias('@runtime/_module/' . $needed_manifest['git_name'] . '-master'), $module_path);
|
|
|
|
} else {
|
|
|
|
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Error module archive'));
|
|
|
|
return $this->redirect(['search']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
\Yii::$app->session->setFlash('danger', \Yii::t('main', 'Error while install module'));
|
|
|
|
return $this->redirect(['search']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove zip
|
|
|
|
$this->cleanInstallationFiles();
|
|
|
|
|
|
|
|
\Yii::$app->session->setFlash('success', \Yii::t('main', 'Module installed successful'));
|
|
|
|
return $this->redirect(['search']);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function cleanInstallationFiles()
|
|
|
|
{
|
|
|
|
if (file_exists(\Yii::getAlias('@runtime/_module'))) {
|
|
|
|
FileHelper::removeDirectory(\Yii::getAlias('@runtime/_module'));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_exists(\Yii::getAlias('@runtime/module.zip'))) {
|
|
|
|
unlink(\Yii::getAlias('@runtime/module.zip'));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function actionSearch()
|
|
|
|
{
|
|
|
|
//print_r(file_get_contents('https://gitlab.com/zertex/zxcms-blog.git')); die;
|
|
|
|
//https://gitlab.com/zertex/zxcms-blog/-/archive/master/zxcms-blog-master.zip
|
|
|
|
|
|
|
|
$message = null;
|
|
|
|
try {
|
|
|
|
$list = file_get_contents('http://zertex.ru/zxcms/modules.txt');
|
|
|
|
$links = array_filter(explode('|', $list));
|
|
|
|
$modules = [];
|
|
|
|
foreach ($links as $link) {
|
|
|
|
$manifest = file_get_contents($link);
|
|
|
|
$manifest = eval(str_replace('<?php', '', $manifest));
|
|
|
|
|
|
|
|
$languageFile = str_replace('manifest.php', 'messages/' . \Yii::$app->language . '/' . $manifest['name'] . '.php', $link);
|
|
|
|
try {
|
|
|
|
$language = file_get_contents($languageFile);
|
|
|
|
$language = eval(str_replace('<?php', '', $language));
|
|
|
|
$manifest['locale_name'] = isset($language[$manifest['name']]) ? $language[$manifest['name']] : $manifest['name'];
|
|
|
|
$manifest['locale_description'] = isset($language[$manifest['description']]) ? $language[$manifest['description']] : $manifest['description'];
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$manifest['locale_name'] = $manifest['name'];
|
|
|
|
$manifest['locale_description'] = $manifest['description'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$modules[] = $manifest;
|
|
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$modules = [];
|
|
|
|
$message = \Yii::t('main', 'Available modules not found');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->render('remote-list', [
|
|
|
|
'modules' => $modules,
|
|
|
|
'message' => $message,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function findModel($id): ModuleRecord
|
|
|
|
{
|
|
|
|
if (($model = ModuleRecord::findOne($id)) !== null) {
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
throw new NotFoundHttpException('The requested module does not exist.');
|
|
|
|
}
|
|
|
|
}
|