Response.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\Core\Http;
  3. use ThurData\Servers\KerioEmail\Core\Http\View\Smarty;
  4. use Symfony\Component\HttpFoundation\Response as SymfonyRespose;
  5. use ThurData\Servers\KerioEmail\Core\ServiceLocator;
  6. use ThurData\Servers\KerioEmail\Core\DependencyInjection;
  7. use ThurData\Servers\KerioEmail\Core\Http\View\MainMenu;
  8. use ThurData\Servers\KerioEmail\Core\Helper\BuildUrl;
  9. use ThurData\Servers\KerioEmail\Core\App\Controllers\Instances\Addon\Config;
  10. use ThurData\Servers\KerioEmail\Core\Helper\WhmcsVersionComparator;
  11. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  12. use ThurData\Servers\KerioEmail\Core\Helper\DnsHelper;
  13. /**
  14. * Description of Response
  15. *
  16. * @autor ThurData <info@thurdata.ch>
  17. */
  18. class Response extends SymfonyRespose
  19. {
  20. use \ThurData\Servers\KerioEmail\Core\Traits\Template;
  21. use \ThurData\Servers\KerioEmail\Core\Traits\IsAdmin;
  22. use \ThurData\Servers\KerioEmail\Core\UI\Traits\RequestObjectHandler;
  23. use \ThurData\Servers\KerioEmail\Core\UI\Traits\WhmcsParams;
  24. protected $data = [];
  25. protected $lang;
  26. protected $staticName;
  27. protected $isBreadcrumbs = true;
  28. protected $isDebug = null;
  29. /*
  30. * determines data return type as HTML only
  31. */
  32. protected $forceHtml = false;
  33. public function setLang($lang)
  34. {
  35. $this->lang = $lang;
  36. return $this;
  37. }
  38. public function setBreadcrumbs($isBreadcrumbs)
  39. {
  40. $this->isBreadcrumbs = $isBreadcrumbs;
  41. return $this;
  42. }
  43. public function isBreadcrumbs()
  44. {
  45. return $this->isBreadcrumbs;
  46. }
  47. public function setName($name)
  48. {
  49. $this->staticName = $name;
  50. return $this;
  51. }
  52. public function getName()
  53. {
  54. return $this->staticName;
  55. }
  56. public function getLang()
  57. {
  58. if (empty($this->lang))
  59. {
  60. $this->lang = ServiceLocator::call('lang');
  61. }
  62. return $this->lang;
  63. }
  64. public function setData(array $data)
  65. {
  66. $this->data = $data;
  67. return $this;
  68. }
  69. public function getError()
  70. {
  71. $data = $this->getData();
  72. if (isset($data['status']) && $data['status'] == 'error')
  73. {
  74. return $data['message'];
  75. }
  76. return false;
  77. }
  78. public function getSuccess()
  79. {
  80. $data = $this->getData();
  81. if (isset($data['status']) && $data['status'] == 'success')
  82. {
  83. return $data['message'];
  84. }
  85. return false;
  86. }
  87. public function getData($key = null, $dafault = null)
  88. {
  89. if ($key == null)
  90. {
  91. return $this->data;
  92. }
  93. if (isset($this->data[$key]) || array_key_exists($key, $this->data))
  94. {
  95. return $this->data[$key];
  96. }
  97. return $dafault;
  98. }
  99. public function withSuccess($message = '')
  100. {
  101. $data = $this->getData();
  102. $data['status'] = 'success';
  103. $data['message'] = $message;
  104. $this->setData($data);
  105. return $this;
  106. }
  107. public function withError($message = '')
  108. {
  109. $data = $this->getData();
  110. $data['status'] = 'error';
  111. $data['message'] = $message;
  112. $this->setData($data);
  113. return $this;
  114. }
  115. public function getPageContext()
  116. {
  117. $tpl = $this->getData('tpl', 'home');
  118. return ServiceLocator::call('smarty')
  119. ->setLang($this->getLang())
  120. ->view($tpl, $this->getData('data', []), $this->getData('tplDir', false));
  121. }
  122. /**
  123. * @param $responseResolver \ThurData\Servers\KerioEmail\Core\App\Controllers\ResponseResolver
  124. */
  125. public function getHtmlResponse($responseResolver)
  126. {
  127. $pageController = $responseResolver->getPageController();
  128. $path = $responseResolver->getTemplateDir();
  129. $fileName = $pageController->getTemplateName() ? : 'main';
  130. $controller = $pageController->getControllerClass(true);
  131. $action = $pageController->getControllerMethod();
  132. $mainMenu = DependencyInjection::create(MainMenu::class)->buildBreadcrumb($controller, $action, []);
  133. $menu = $mainMenu->getMenu();
  134. $addon = ServiceLocator::call(Config::class);
  135. $productManager = new ProductManager();
  136. $params = $this->getWhmcsParamsByKeys(['domain', 'userid', 'serverhostname', 'nameserver1ip']);
  137. $vars = [
  138. 'assetsURL' => BuildUrl::getAssetsURL(),
  139. 'customAssetsURL' => BuildUrl::getAssetsURL(true),
  140. 'isCustomIntegrationCss' => BuildUrl::isCustomIntegrationCss(),
  141. 'isCustomModuleCss' => BuildUrl::isCustomModuleCss(),
  142. 'mainURL' => BuildUrl::getUrl(),
  143. 'mainName' => ($this->staticName === null) ? $addon->getConfigValue('name') : $this->staticName,
  144. 'menu' => $menu,
  145. 'breadcrumbs' => ($this->isBreadcrumbs) ? $this->data['data']['breadcrumbs'] : null,
  146. 'JSONCurrentUrl' => BuildUrl::getUrl($controller),
  147. 'currentPageName' => $controller,
  148. 'mgWhmcsVersionComparator' => new WhmcsVersionComparator(),
  149. 'content' => $this->getPageContext(),
  150. 'moduleRequirementsErrors' => $this->checkModuleRequirements(),
  151. 'error' => $this->getData('status', false) == 'error' ? $this->getData('message', '') : false,
  152. 'success' => $this->getData('status', false) == 'success' ? $this->getData('message', '') : false,
  153. 'tagImageModule' => $addon->getConfigValue('moduleIcon'),
  154. 'isDebug' => (bool)((int)$addon->getConfigValue('debug', "0")),
  155. 'errorPageDetails' => $this->getErrorPageData($responseResolver),
  156. 'dnsvars' => DnsHelper::KerioEmailCheckDNS($params)
  157. ];
  158. logModuleCall(
  159. 'kerioEmail',
  160. __FUNCTION__,
  161. $params,
  162. 'Debug',
  163. $vars
  164. );
  165. try
  166. {
  167. $this->loadLangContext();
  168. if ((!$responseResolver->isAdmin() && !$this->forceHtml))
  169. {
  170. $vars['MGLANG'] = $this->lang;
  171. if (strpos(trim(self::class, '\\'), 'ThurData\Servers') === 0)
  172. {
  173. return $this->returnClientProvisioning($vars, $path, $fileName);
  174. }
  175. return $this->returnClientAddon($vars, $path, $fileName);
  176. }
  177. /**
  178. * @var Smarty $pageContent
  179. */
  180. $pageContent = ServiceLocator::call('smarty')
  181. ->setLang($this->lang)
  182. ->setTemplateDir($path)
  183. ->view($fileName, $vars);
  184. return $pageContent;
  185. }
  186. catch (\Exception $e)
  187. {
  188. ServiceLocator::call('errorManager')->addError(self::class, $e->getMessage(), $e->getTrace());
  189. }
  190. }
  191. public function returnClientAddon($vars, $path, $fileName)
  192. {
  193. return [
  194. 'vars' => $vars,
  195. //'pagetitle' => $this->lang->absoluteT("AddonCA","pagetitle","Reseller Area"), TODO!!!
  196. 'templatefile' => str_replace(\ThurData\Servers\KerioEmail\Core\ModuleConstants::getTemplateDir() . DIRECTORY_SEPARATOR,
  197. '', $path . DIRECTORY_SEPARATOR . $fileName),
  198. 'requirelogin' => true,
  199. 'breadcrumb' => ($this->isBreadcrumbs) ? $this->data['data']['breadcrumbs'] : null
  200. ];
  201. }
  202. public function returnClientProvisioning($vars, $path, $fileName)
  203. {
  204. // $templateVarName = ($this->getRequestValue('a', false) === 'management') ? 'tabOverviewReplacementTemplate' : 'templatefile';
  205. $templateVarName = 'tabOverviewReplacementTemplate';
  206. return [
  207. $templateVarName => str_replace(\ThurData\Servers\KerioEmail\Core\ModuleConstants::getTemplateDir() . DIRECTORY_SEPARATOR,
  208. '', $path . DIRECTORY_SEPARATOR . $fileName),
  209. 'templateVariables' => $vars
  210. ];
  211. }
  212. protected function checkModuleRequirements()
  213. {
  214. $requirementsHandler = new \ThurData\Servers\KerioEmail\Core\App\Requirements\Checker();
  215. $requirementsErrors = $requirementsHandler->getUnfulfilledRequirements();
  216. if ($requirementsErrors)
  217. {
  218. return implode('<br>', $requirementsErrors);
  219. }
  220. return $this->getData('status', false) == 'error' ? $this->getData('message', '') : false;
  221. }
  222. /**
  223. * Set context lang ( AdminArea or ClientArea )
  224. */
  225. protected function loadLangContext()
  226. {
  227. $this->lang->setContext(($this->getType() . ($this->isAdmin() ? 'AA' : 'CA')));
  228. }
  229. /**
  230. * @return string
  231. */
  232. protected function getType()
  233. {
  234. return 'addon';
  235. }
  236. public function setForceHtml()
  237. {
  238. $this->forceHtml = true;
  239. return $this;
  240. }
  241. public function unsetForceHtml()
  242. {
  243. $this->forceHtml = false;
  244. return $this;
  245. }
  246. public function isDebugOn()
  247. {
  248. if ($this->isDebug === null)
  249. {
  250. $addon = ServiceLocator::call(Config::class);
  251. $this->isDebug = (bool)((int)$addon->getConfigValue('debug', "0"));
  252. }
  253. return $this->isDebug;
  254. }
  255. public function getErrorPageData($responseResolver)
  256. {
  257. $pageController = $responseResolver->getPageController();
  258. $error = $pageController->getParam('mgErrorDetails');
  259. if (!$error)
  260. {
  261. return null;
  262. }
  263. $errorDetails = $error->getDetailsToDisplay();
  264. return $errorDetails;
  265. }
  266. }