ConfigOptions.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Addon;
  3. use ThurData\Servers\KerioEmail\Core\App\Controllers\ResponseResolver;
  4. use ThurData\Servers\KerioEmail\Core\App\Installer\ModuleInstaller;
  5. use ThurData\Servers\KerioEmail\Core\Http\JsonResponse;
  6. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Product;
  7. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Server;
  8. use ThurData\Servers\KerioEmail\Core\ModuleConstants;
  9. use ThurData\Servers\KerioEmail\Core\ServiceLocator;
  10. use ThurData\Servers\KerioEmail\Core\Traits\AppParams;
  11. use ThurData\Servers\KerioEmail\Core\Traits\OutputBuffer;
  12. use ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  13. /**
  14. * ConfigOptions module actions
  15. */
  16. class ConfigOptions extends \ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\AddonController
  17. {
  18. use AppParams;
  19. use RequestObjectHandler;
  20. use OutputBuffer;
  21. public function execute($params = null)
  22. {
  23. $productId = $this->getRequestValue('id');
  24. if (($this->getRequestValue('action') === 'module-settings' || ($this->getRequestValue('loadData') && $this->getRequestValue('ajax') == '1')))
  25. {
  26. try
  27. {
  28. //check storage
  29. $invalidStoragePermissions = $this->getInvalidStoragePermitions();
  30. if ($invalidStoragePermissions)
  31. {
  32. return $this->getInvalidStoragePermissionsError($invalidStoragePermissions);
  33. }
  34. //check server type
  35. if (!$this->isCorrectServerType())
  36. {
  37. return $this->getInvalidServerTypeError();
  38. }
  39. $requirementsHandler = new \ThurData\Servers\KerioEmail\Core\App\Requirements\Checker();
  40. if ($requirementsHandler->getUnfulfilledRequirements())
  41. {
  42. $data = $this->buildErrorMessage(implode('<br>', $requirementsHandler->getUnfulfilledRequirements()));
  43. return $this->returnAjaxResponse($data);
  44. }
  45. $this->updateProductType();
  46. /**
  47. *
  48. * disabled because `packages` folder wasn't migrated
  49. */
  50. //$this->addRequiredCustomFields();
  51. //run installer
  52. $installer = new ModuleInstaller();
  53. $installer->makeInstall();
  54. if (!$installer->isInstallCorrect())
  55. {
  56. return $this->buildFailedQueriesMessage($installer->getFailedQueries());
  57. }
  58. }
  59. catch (\Exception $exc)
  60. {
  61. $data = $this->buildErrorMessage($exc->getMessage());
  62. return $this->returnAjaxResponse($data);
  63. }
  64. try
  65. {
  66. $this->setAppParam('IntegrationControlerName', \ThurData\Servers\KerioEmail\App\Http\Actions\ConfigOptions::class);
  67. $this->setAppParam('IntegrationControlerMethod', 'runExecuteProcess');
  68. $configOptionsController = new \ThurData\Servers\KerioEmail\App\Http\Actions\ConfigOptions();
  69. $result = $configOptionsController->execute();
  70. return $result;
  71. }
  72. catch (\Exception $exc)
  73. {
  74. $data = $this->buildErrorMessage($exc->getMessage());
  75. $response = new JsonResponse();
  76. $response->setData($data);
  77. $resolver = new ResponseResolver($response);
  78. $resolver->resolve();
  79. }
  80. }
  81. else if ($this->getRequestValue('action') === 'save')
  82. {
  83. if (!$this->isCorrectServerType())
  84. {
  85. return [];
  86. }
  87. return [\ThurData\Servers\KerioEmail\App\Http\Actions\ConfigOptions::class, 'runExecuteProcess'];
  88. }
  89. return [];
  90. }
  91. public function getInvalidStoragePermitions()
  92. {
  93. $requiredPaths = [
  94. ModuleConstants::getFullPath('storage'),
  95. ModuleConstants::getFullPath('storage', 'app'),
  96. ModuleConstants::getFullPath('storage', 'crons'),
  97. ModuleConstants::getFullPath('storage', 'logs')
  98. ];
  99. $invalidPermissions = [];
  100. $lang = ServiceLocator::call('lang');
  101. foreach ($requiredPaths as $path)
  102. {
  103. if (!is_writable($path))
  104. {
  105. $invalidPermissions[] = $lang->addReplacementConstant('storage_path', $path)->absoluteT('permissionsStorage');
  106. }
  107. if (!is_readable($path))
  108. {
  109. $invalidPermissions[] = $lang->addReplacementConstant('storage_path', $path)->absoluteT('permissionsStorageReadable');
  110. }
  111. }
  112. return $invalidPermissions;
  113. }
  114. public function buildFailedQueriesMessage($failedQueries = [])
  115. {
  116. $content = '';
  117. foreach ($failedQueries as $query)
  118. {
  119. $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;">';
  120. //$content .= '<li class="list-group-item ">Status: ' . $query['status'] . '</li>';
  121. $content .= '<li class="list-group-item ">File: ' . $query['file'] . '</li>';
  122. $content .= '<li class="list-group-item ">Error Message: ' . $query['errorMessage'] . '</li>';
  123. $content .= '<li class="list-group-item ">Raw Query: ' . $query['rawQuery'] . '</li>';
  124. $content .= '</ul></div></div>';
  125. }
  126. $data = [
  127. 'content' =>
  128. // '<tr><td class="fieldlabel" style="width:0%; display:none;"></td><td style="width=100%;" class="fieldarea">' . $content . '</td></tr>',
  129. '<div style="border: 3px solid #e2e7e9; margin: 12px 0 12px 0; border-collapse: separate; border-radius: 4px;">
  130. <div style="padding: 1px 3px 1px 3px; margin: 2px; background-color: #efefef;">
  131. <div style="width: 100%; margin: 2px;" class="alert alert-danger">' . $message . '</div>
  132. </div>
  133. </div>',
  134. 'mode' => 'advanced'
  135. ];
  136. return $this->returnAjaxResponse($data);
  137. }
  138. public function getInvalidStoragePermissionsError($permissions = [])
  139. {
  140. $data = $this->buildErrorMessage(implode('<br>', $permissions));
  141. return $this->returnAjaxResponse($data);
  142. }
  143. public function isCorrectServerType()
  144. {
  145. try
  146. {
  147. if (class_exists('\ThurData\Servers\KerioEmail\App\Http\Actions\MetaData'))
  148. {
  149. $metaDataController = new \ThurData\Servers\KerioEmail\App\Http\Actions\MetaData();
  150. $details = $metaDataController->execute(null);
  151. if ($details['RequiresServer'] !== true)
  152. {
  153. return true;
  154. }
  155. $serverGroupId = $this->getServerGroupId();
  156. $sModel = new Server();
  157. $server = $sModel
  158. ->select(['tblservers.type'])
  159. ->join('tblservergroupsrel', 'tblservergroupsrel.serverid', '=', 'tblservers.id')
  160. ->where('tblservergroupsrel.groupid', $serverGroupId)->first();
  161. if (!$server)
  162. {
  163. return false;
  164. }
  165. if ($server->type !== $this->getModuleName())
  166. {
  167. return false;
  168. }
  169. }
  170. }
  171. catch (\Exception $exception)
  172. {
  173. //todo log me
  174. return false;
  175. }
  176. return true;
  177. }
  178. public function getServerGroupId()
  179. {
  180. $gid = $this->getRequestValue('servergroup', false);
  181. if (!$gid && $gid !== '0' && $gid !== 0)
  182. {
  183. $pid = $this->getRequestValue('id', 0);
  184. $productModel = new Product();
  185. $product = $productModel->where('id', $pid)->first();
  186. if (!$product)
  187. {
  188. //can add first product here if needed
  189. return 0;
  190. }
  191. return $product->servergroup;
  192. }
  193. return (int)$gid;
  194. }
  195. public function getInvalidServerTypeError()
  196. {
  197. $lang = ServiceLocator::call('lang');
  198. $messaage = $lang->addReplacementConstant('provisioning_name', $this->getModuleDisplayName())->absoluteT('invalidServerType');
  199. $data = $this->buildErrorMessage($messaage);
  200. return $this->returnAjaxResponse($data);
  201. }
  202. public function buildErrorMessage($message)
  203. {
  204. $data = [
  205. 'content' =>
  206. '<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>',
  207. 'mode' => 'advanced'
  208. ];
  209. return $data;
  210. }
  211. public function returnAjaxResponse($data = [])
  212. {
  213. $response = new JsonResponse();
  214. $response->setData($data);
  215. return $response;
  216. }
  217. public function updateProductType()
  218. {
  219. if ($this->getRequestValue('action') !== 'module-settings'
  220. || $this->getAppParam('moduleAppType') !== 'server')
  221. {
  222. return false;
  223. }
  224. $moduleName = $this->getAppParam('systemName');
  225. $pid = $this->getRequestValue('id', false);
  226. $servergroup = $this->getRequestValue('servergroup', 0);
  227. if ($pid && $servergroup > 0)
  228. {
  229. $product = new Product();
  230. $product->where('id', $pid)->update(['servertype' => $moduleName, 'servergroup' => $servergroup]);
  231. }
  232. }
  233. public function getModuleName()
  234. {
  235. return $this->getAppParam('systemName');
  236. }
  237. public function getModuleDisplayName()
  238. {
  239. return $this->getAppParam('name');
  240. }
  241. public function addRequiredCustomFields()
  242. {
  243. // $pid = $this->getRequestValue('id', false);
  244. // if ($pid === false)
  245. // {
  246. // return;
  247. // }
  248. //
  249. // $product = new \ThurData\Servers\KerioEmail\Packages\WhmcsService\Product($pid);
  250. //
  251. // $product->createCustomFieldsFromConfig();
  252. }
  253. }