abstractMainDriver.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace MGModule\DNSManager2\mgLibs\process;
  3. use MGModule\DNSManager2 as main;
  4. use MGModule\DNSManager2\mgLibs;
  5. use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\getAdminDirName;
  6. use ModulesGarden\Servers\ZimbraEmail\Core\ModuleConstants;
  7. /**
  8. * Main Abstract Controller
  9. *
  10. * @author Michal Czech <michael@modulesgarden.com>
  11. */
  12. abstract class abstractMainDriver{
  13. /**
  14. * Single Ton Instance
  15. *
  16. * @var mainController
  17. */
  18. static private $_instance;
  19. /**
  20. * This Var define Debug Mode in Module
  21. *
  22. * @var boolean
  23. */
  24. public $_debug = false;
  25. /**
  26. * Mark
  27. *
  28. * $var boolean
  29. */
  30. public $isLoaded;
  31. /**
  32. * Load Configuration
  33. *
  34. * @var configuration
  35. */
  36. private $_configuration;
  37. /**
  38. * Is Loaded From Admin Area
  39. *
  40. * @var boolean
  41. */
  42. private $_isAdmin = false;
  43. /**
  44. * Key For Data Encryption
  45. *
  46. * @var string
  47. */
  48. private $encryptSecureKey;
  49. /**
  50. * Main Namespace
  51. *
  52. * @var string
  53. */
  54. private $_mainNamespace;
  55. private $_mainDIR;
  56. /**
  57. * Disable Contruct && Clone
  58. */
  59. private final function __construct() {;}
  60. private final function __clone() {;}
  61. public $page = 'home';
  62. /**
  63. * Get SingleTon Instance
  64. *
  65. * @return mainController
  66. */
  67. public static function I($force = false, $configs = array()){
  68. if(empty(self::$_instance) || $force)
  69. {
  70. $class = get_called_class();
  71. mainInstance::setInstanceName($class);
  72. self::$_instance = new $class();
  73. self::$_instance->_mainNamespace = substr(__NAMESPACE__,0, strpos(__NAMESPACE__, '\mgLibs'));
  74. self::$_instance->_mainDIR = call_user_func(array($class,'getMainDIR'));
  75. $class= self::$_instance->_mainNamespace.'\configuration';
  76. self::$_instance->_configuration = new $class();
  77. foreach($configs as $name => $value)
  78. {
  79. self::$_instance->_configuration->{$name} = $value;
  80. }
  81. self::$_instance->isLoaded = true;
  82. if(!empty(self::$_instance->_configuration->debug))
  83. {
  84. self::$_instance->_debug = true;
  85. }
  86. main\mgLibs\MySQL\query::useCurrentConnection();
  87. main\mgLibs\lang::getInstance(self::$_instance->_mainDIR.DS.'langs');
  88. }
  89. return self::$_instance;
  90. }
  91. function configuration(){
  92. return $this->_configuration;
  93. }
  94. public function isAdmin($status = null){
  95. if($status !== null)
  96. {
  97. $this->_isAdmin = $status;
  98. }
  99. return $this->_isAdmin;
  100. }
  101. public function isDebug(){
  102. return $this->_debug;
  103. }
  104. /**
  105. * Return Main Namespace
  106. *
  107. * @return string
  108. */
  109. function getMainNamespace(){
  110. return $this->_mainNamespace;
  111. }
  112. /**
  113. * Return Enrypt Key
  114. *
  115. * @return string
  116. */
  117. public function getEncryptKey(){
  118. if(empty($this->encryptSecureKey))
  119. {
  120. $this->encryptSecureKey = hash('sha256', $GLOBALS['cc_encryption_hash'].$this->configuration()->encryptHash,TRUE);
  121. }
  122. return $this->encryptSecureKey;
  123. }
  124. function setMainLangContext(){
  125. mgLibs\lang::setContext($this->getType().($this->isAdmin()?'AA':'CA'));
  126. }
  127. /**
  128. * Process Controllers
  129. *
  130. * @param string $controller controller name
  131. * @param array $input input array
  132. * @param string $type type of request
  133. * @return array
  134. * @throws main\mgLibs\exceptions\system
  135. * @throws main\mgLibs\exceptions\system
  136. */
  137. function runControler($controller,$action = 'index',$input = array(), $type = 'HTML'){
  138. $this->page = $controller;
  139. try{
  140. $className = $this->getMainNamespace()."\\controllers\\".$this->getType()."\\".($this->_isAdmin?'admin':'clientarea')."\\".$controller;
  141. try
  142. {
  143. if(!class_exists($className))
  144. {
  145. throw new main\mgLibs\exceptions\system("Unable to find page");
  146. }
  147. }
  148. catch (\Exception $exception)
  149. {
  150. if(!self::I()->isAdmin())
  151. {
  152. header('Location: index.php?m=DNSManager2');
  153. die;
  154. }
  155. throw $exception;
  156. }
  157. $controllerOBJ = new $className($input);
  158. if(!method_exists($controllerOBJ, $action.$type))
  159. {
  160. if(!self::I()->isAdmin())
  161. {
  162. header('Location: index.php?m=DNSManager2');
  163. die;
  164. }
  165. throw new main\mgLibs\exceptions\system("Unable to find Action: ".$action.$type);
  166. }
  167. main\mgLibs\lang::stagCurrentContext('generate'.$controller);
  168. main\mgLibs\lang::addToContext($controller);
  169. main\mgLibs\smarty::I()->setTemplateDir(self::I()->getModuleTemplatesDir().DS.'pages'.DS.$controller);
  170. $result = $controllerOBJ->{$action.$type}($input);
  171. switch ($type)
  172. {
  173. case 'HTML':
  174. if(empty($result['tpl']))
  175. {
  176. throw new main\mgLibs\exceptions\system("Provide Template Name");
  177. }
  178. $success = isset($result['vars']['success'])?$result['vars']['success']:false;
  179. $error = isset($result['vars']['error'])?$result['vars']['error']:false;
  180. $result = main\mgLibs\smarty::I()->view($result['tpl'], $result['vars']);
  181. break;
  182. default:
  183. $success = isset($result['success'])?$result['success']:false;
  184. $error = isset($result['error'])?$result['error']:false;
  185. }
  186. main\mgLibs\lang::unstagContext('generate'.$controller);
  187. return array(
  188. $result
  189. ,$success
  190. ,$error
  191. );
  192. } catch (\Exception $ex) {
  193. main\mgLibs\lang::unstagContext('generate'.$controller);
  194. $query = array_merge($_GET, $_POST);
  195. \logModuleCall(
  196. 'DNS Manager', __METHOD__, $ex->getMessage(), var_export($ex, true), var_export($query, true)
  197. );
  198. main\mgLibs\custom\manager\LogHelper::addFailLog('Load Page', $ex->getMessage(), "Render Page Action. Check module logs to see more");
  199. header('Location: index.php?m=DNSManager2');
  200. die;
  201. }
  202. }
  203. /**
  204. * @return bool
  205. */
  206. public static function isAdminArea()
  207. {
  208. if (isset($_SESSION['adminid']) && isset($_SERVER['REQUEST_URI']) && strpos($_SERVER['REQUEST_URI'], '/' . static::getAdminDirName() . '/') !== false)
  209. {
  210. return true;
  211. }
  212. return false;
  213. }
  214. /**
  215. * @return string
  216. */
  217. public static function getAdminDirName()
  218. {
  219. $fileName = 'configuration.php';
  220. $filePath = dirname(dirname(dirname(dirname(dirname(__DIR__)))));
  221. global $customadminpath;
  222. if (!$customadminpath && file_exists($filePath . DIRECTORY_SEPARATOR . $fileName))
  223. {
  224. include_once $filePath . DIRECTORY_SEPARATOR . $fileName;
  225. }
  226. if ($customadminpath && is_string($customadminpath))
  227. {
  228. return $customadminpath;
  229. }
  230. return 'admin';
  231. }
  232. /**
  233. * Dump data
  234. *
  235. * @param mixed $input
  236. */
  237. static function dump($input)
  238. {
  239. if(self::I()->isDebug())
  240. {
  241. echo "<pre>";
  242. print_r($input);
  243. echo "</pre>";
  244. }
  245. }
  246. abstract public function getAssetsURL();
  247. abstract public function getType();
  248. public static function getMainDIR(){
  249. return false;
  250. }
  251. public static function getUrl(){
  252. return false;
  253. }
  254. }