| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301 |
- <?php
- namespace ModulesGarden\Servers\KerioEmail\Core\App\Controllers\Instances\Addon;
- use ModulesGarden\Servers\KerioEmail\Core\App\Controllers\ResponseResolver;
- use ModulesGarden\Servers\KerioEmail\Core\App\Installer\ModuleInstaller;
- use ModulesGarden\Servers\KerioEmail\Core\Http\JsonResponse;
- use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\Product;
- use ModulesGarden\Servers\KerioEmail\Core\Models\Whmcs\Server;
- use ModulesGarden\Servers\KerioEmail\Core\ModuleConstants;
- use ModulesGarden\Servers\KerioEmail\Core\ServiceLocator;
- use ModulesGarden\Servers\KerioEmail\Core\Traits\AppParams;
- use ModulesGarden\Servers\KerioEmail\Core\Traits\OutputBuffer;
- use ModulesGarden\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
- /**
- * ConfigOptions module actions
- */
- class ConfigOptions extends \ModulesGarden\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController
- {
- use AppParams;
- use RequestObjectHandler;
- use OutputBuffer;
- public function execute($params = null)
- {
- $productId = $this->getRequestValue('id');
- if (($this->getRequestValue('action') === 'module-settings' || ($this->getRequestValue('loadData') && $this->getRequestValue('ajax') == '1')))
- {
- try
- {
- //check storage
- $invalidStoragePermissions = $this->getInvalidStoragePermitions();
- if ($invalidStoragePermissions)
- {
- return $this->getInvalidStoragePermissionsError($invalidStoragePermissions);
- }
- //check server type
- if (!$this->isCorrectServerType())
- {
- return $this->getInvalidServerTypeError();
- }
- $requirementsHandler = new \ModulesGarden\Servers\KerioEmail\Core\App\Requirements\Checker();
- if ($requirementsHandler->getUnfulfilledRequirements())
- {
- $data = $this->buildErrorMessage(implode('<br>', $requirementsHandler->getUnfulfilledRequirements()));
- return $this->returnAjaxResponse($data);
- }
- $this->updateProductType();
- /**
- *
- * disabled because `packages` folder wasn't migrated
- */
- //$this->addRequiredCustomFields();
- //run installer
- $installer = new ModuleInstaller();
- $installer->makeInstall();
- if (!$installer->isInstallCorrect())
- {
- return $this->buildFailedQueriesMessage($installer->getFailedQueries());
- }
- }
- catch (\Exception $exc)
- {
- $data = $this->buildErrorMessage($exc->getMessage());
- return $this->returnAjaxResponse($data);
- }
- try
- {
- $this->setAppParam('IntegrationControlerName', \ModulesGarden\Servers\KerioEmail\App\Http\Actions\ConfigOptions::class);
- $this->setAppParam('IntegrationControlerMethod', 'runExecuteProcess');
- $configOptionsController = new \ModulesGarden\Servers\KerioEmail\App\Http\Actions\ConfigOptions();
- $result = $configOptionsController->execute();
- return $result;
- }
- catch (\Exception $exc)
- {
- $data = $this->buildErrorMessage($exc->getMessage());
- $response = new JsonResponse();
- $response->setData($data);
- $resolver = new ResponseResolver($response);
- $resolver->resolve();
- }
- }
- else if ($this->getRequestValue('action') === 'save')
- {
- if (!$this->isCorrectServerType())
- {
- return [];
- }
- return [\ModulesGarden\Servers\KerioEmail\App\Http\Actions\ConfigOptions::class, 'runExecuteProcess'];
- }
- return [];
- }
- public function getInvalidStoragePermitions()
- {
- $requiredPaths = [
- ModuleConstants::getFullPath('storage'),
- ModuleConstants::getFullPath('storage', 'app'),
- ModuleConstants::getFullPath('storage', 'crons'),
- ModuleConstants::getFullPath('storage', 'logs')
- ];
- $invalidPermissions = [];
- $lang = ServiceLocator::call('lang');
- foreach ($requiredPaths as $path)
- {
- if (!is_writable($path))
- {
- $invalidPermissions[] = $lang->addReplacementConstant('storage_path', $path)->absoluteT('permissionsStorage');
- }
- if (!is_readable($path))
- {
- $invalidPermissions[] = $lang->addReplacementConstant('storage_path', $path)->absoluteT('permissionsStorageReadable');
- }
- }
- return $invalidPermissions;
- }
- public function buildFailedQueriesMessage($failedQueries = [])
- {
- $content = '';
- foreach ($failedQueries as $query)
- {
- $content .= '<div class="panel panel-danger"><div class="panel-heading">Installation Error</div><div class="panel-body" style="padding:0px;"><ul class="list-group" style="margin-bottom: -5px;margin-top: -5px;">';
- //$content .= '<li class="list-group-item ">Status: ' . $query['status'] . '</li>';
- $content .= '<li class="list-group-item ">File: ' . $query['file'] . '</li>';
- $content .= '<li class="list-group-item ">Error Message: ' . $query['errorMessage'] . '</li>';
- $content .= '<li class="list-group-item ">Raw Query: ' . $query['rawQuery'] . '</li>';
- $content .= '</ul></div></div>';
- }
- $data = [
- 'content' =>
- '<tr><td class="fieldlabel" style="width:0%; display:none;"></td><td style="width=100%;" class="fieldarea">' . $content . '</td></tr>',
- 'mode' => 'advanced'
- ];
- return $this->returnAjaxResponse($data);
- }
- public function getInvalidStoragePermissionsError($permissions = [])
- {
- $data = $this->buildErrorMessage(implode('<br>', $permissions));
- return $this->returnAjaxResponse($data);
- }
- public function isCorrectServerType()
- {
- try
- {
- if (class_exists('\ModulesGarden\Servers\KerioEmail\App\Http\Actions\MetaData'))
- {
- $metaDataController = new \ModulesGarden\Servers\KerioEmail\App\Http\Actions\MetaData();
- $details = $metaDataController->execute(null);
- if ($details['RequiresServer'] !== true)
- {
- return true;
- }
- $serverGroupId = $this->getServerGroupId();
- $sModel = new Server();
- $server = $sModel
- ->select(['tblservers.type'])
- ->join('tblservergroupsrel', 'tblservergroupsrel.serverid', '=', 'tblservers.id')
- ->where('tblservergroupsrel.groupid', $serverGroupId)->first();
- if (!$server)
- {
- return false;
- }
- if ($server->type !== $this->getModuleName())
- {
- return false;
- }
- }
- }
- catch (\Exception $exception)
- {
- //todo log me
- return false;
- }
- return true;
- }
- public function getServerGroupId()
- {
- $gid = $this->getRequestValue('servergroup', false);
- if (!$gid && $gid !== '0' && $gid !== 0)
- {
- $pid = $this->getRequestValue('id', 0);
- $productModel = new Product();
- $product = $productModel->where('id', $pid)->first();
- if (!$product)
- {
- //can add first product here if needed
- return 0;
- }
- return $product->servergroup;
- }
- return (int)$gid;
- }
- public function getInvalidServerTypeError()
- {
- $lang = ServiceLocator::call('lang');
- $messaage = $lang->addReplacementConstant('provisioning_name', $this->getModuleDisplayName())->absoluteT('invalidServerType');
- $data = $this->buildErrorMessage($messaage);
- return $this->returnAjaxResponse($data);
- }
- public function buildErrorMessage($message)
- {
- $data = [
- 'content' =>
- '<tr><td class="fieldlabel" style="width:0%; display:none;"></td><td style="width=100%;" class="fieldarea"><div style="width=100%; margin-bottom: 0px;" class="alert alert-danger">' . $message . '</div></td></tr>',
- 'mode' => 'advanced'
- ];
- return $data;
- }
- public function returnAjaxResponse($data = [])
- {
- $response = new JsonResponse();
- $response->setData($data);
- return $response;
- }
- public function updateProductType()
- {
- if ($this->getRequestValue('action') !== 'module-settings'
- || $this->getAppParam('moduleAppType') !== 'server')
- {
- return false;
- }
- $moduleName = $this->getAppParam('systemName');
- $pid = $this->getRequestValue('id', false);
- $servergroup = $this->getRequestValue('servergroup', 0);
- if ($pid && $servergroup > 0)
- {
- $product = new Product();
- $product->where('id', $pid)->update(['servertype' => $moduleName, 'servergroup' => $servergroup]);
- }
- }
- public function getModuleName()
- {
- return $this->getAppParam('systemName');
- }
- public function getModuleDisplayName()
- {
- return $this->getAppParam('name');
- }
- public function addRequiredCustomFields()
- {
- // $pid = $this->getRequestValue('id', false);
- // if ($pid === false)
- // {
- // return;
- // }
- //
- // $product = new \ModulesGarden\Servers\KerioEmail\Packages\WhmcsService\Product($pid);
- //
- // $product->createCustomFieldsFromConfig();
- }
- }
|