ConfigOptions.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. 'mode' => 'advanced'
  130. ];
  131. return $this->returnAjaxResponse($data);
  132. }
  133. public function getInvalidStoragePermissionsError($permissions = [])
  134. {
  135. $data = $this->buildErrorMessage(implode('<br>', $permissions));
  136. return $this->returnAjaxResponse($data);
  137. }
  138. public function isCorrectServerType()
  139. {
  140. try
  141. {
  142. if (class_exists('\ThurData\Servers\KerioEmail\App\Http\Actions\MetaData'))
  143. {
  144. $metaDataController = new \ThurData\Servers\KerioEmail\App\Http\Actions\MetaData();
  145. $details = $metaDataController->execute(null);
  146. if ($details['RequiresServer'] !== true)
  147. {
  148. return true;
  149. }
  150. $serverGroupId = $this->getServerGroupId();
  151. $sModel = new Server();
  152. $server = $sModel
  153. ->select(['tblservers.type'])
  154. ->join('tblservergroupsrel', 'tblservergroupsrel.serverid', '=', 'tblservers.id')
  155. ->where('tblservergroupsrel.groupid', $serverGroupId)->first();
  156. if (!$server)
  157. {
  158. return false;
  159. }
  160. if ($server->type !== $this->getModuleName())
  161. {
  162. return false;
  163. }
  164. }
  165. }
  166. catch (\Exception $exception)
  167. {
  168. //todo log me
  169. return false;
  170. }
  171. return true;
  172. }
  173. public function getServerGroupId()
  174. {
  175. $gid = $this->getRequestValue('servergroup', false);
  176. if (!$gid && $gid !== '0' && $gid !== 0)
  177. {
  178. $pid = $this->getRequestValue('id', 0);
  179. $productModel = new Product();
  180. $product = $productModel->where('id', $pid)->first();
  181. if (!$product)
  182. {
  183. //can add first product here if needed
  184. return 0;
  185. }
  186. return $product->servergroup;
  187. }
  188. return (int)$gid;
  189. }
  190. public function getInvalidServerTypeError()
  191. {
  192. $lang = ServiceLocator::call('lang');
  193. $messaage = $lang->addReplacementConstant('provisioning_name', $this->getModuleDisplayName())->absoluteT('invalidServerType');
  194. $data = $this->buildErrorMessage($messaage);
  195. return $this->returnAjaxResponse($data);
  196. }
  197. public function buildErrorMessage($message)
  198. {
  199. $data = [
  200. 'content' =>
  201. '<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>',
  202. 'mode' => 'advanced'
  203. ];
  204. return $data;
  205. }
  206. public function returnAjaxResponse($data = [])
  207. {
  208. $response = new JsonResponse();
  209. $response->setData($data);
  210. return $response;
  211. }
  212. public function updateProductType()
  213. {
  214. if ($this->getRequestValue('action') !== 'module-settings'
  215. || $this->getAppParam('moduleAppType') !== 'server')
  216. {
  217. return false;
  218. }
  219. $moduleName = $this->getAppParam('systemName');
  220. $pid = $this->getRequestValue('id', false);
  221. $servergroup = $this->getRequestValue('servergroup', 0);
  222. if ($pid && $servergroup > 0)
  223. {
  224. $product = new Product();
  225. $product->where('id', $pid)->update(['servertype' => $moduleName, 'servergroup' => $servergroup]);
  226. }
  227. }
  228. public function getModuleName()
  229. {
  230. return $this->getAppParam('systemName');
  231. }
  232. public function getModuleDisplayName()
  233. {
  234. return $this->getAppParam('name');
  235. }
  236. public function addRequiredCustomFields()
  237. {
  238. // $pid = $this->getRequestValue('id', false);
  239. // if ($pid === false)
  240. // {
  241. // return;
  242. // }
  243. //
  244. // $product = new \ThurData\Servers\KerioEmail\Packages\WhmcsService\Product($pid);
  245. //
  246. // $product->createCustomFieldsFromConfig();
  247. }
  248. }