Response.php 9.2 KB

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