PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/module/ModuleHandler.class.php

https://github.com/prologos/xe-core
PHP | 1234 lines | 951 code | 139 blank | 144 comment | 245 complexity | af18a44e16d8fcf99168109ecfc1004f MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /* Copyright (C) NAVER <http://www.navercorp.com> */
  3. /**
  4. * @class ModuleHandler
  5. * @author NAVER (developers@xpressengine.com)
  6. * Handling modules
  7. *
  8. * @remarks This class is to excute actions of modules.
  9. * Constructing an instance without any parameterconstructor, it finds the target module based on Context.
  10. * If there is no act on the found module, excute an action referencing action_forward.
  11. * */
  12. class ModuleHandler extends Handler
  13. {
  14. var $module = NULL; ///< Module
  15. var $act = NULL; ///< action
  16. var $mid = NULL; ///< Module ID
  17. var $document_srl = NULL; ///< Document Number
  18. var $module_srl = NULL; ///< Module Number
  19. var $module_info = NULL; ///< Module Info. Object
  20. var $error = NULL; ///< an error code.
  21. var $httpStatusCode = NULL; ///< http status code.
  22. /**
  23. * prepares variables to use in moduleHandler
  24. * @param string $module name of module
  25. * @param string $act name of action
  26. * @param int $mid
  27. * @param int $document_srl
  28. * @param int $module_srl
  29. * @return void
  30. * */
  31. function ModuleHandler($module = '', $act = '', $mid = '', $document_srl = '', $module_srl = '')
  32. {
  33. // If XE has not installed yet, set module as install
  34. if(!Context::isInstalled())
  35. {
  36. $this->module = 'install';
  37. $this->act = Context::get('act');
  38. return;
  39. }
  40. $oContext = Context::getInstance();
  41. if($oContext->isSuccessInit == FALSE)
  42. {
  43. $logged_info = Context::get('logged_info');
  44. if($logged_info->is_admin != "Y")
  45. {
  46. $this->error = 'msg_invalid_request';
  47. return;
  48. }
  49. }
  50. // Set variables from request arguments
  51. $this->module = $module ? $module : Context::get('module');
  52. $this->act = $act ? $act : Context::get('act');
  53. $this->mid = $mid ? $mid : Context::get('mid');
  54. $this->document_srl = $document_srl ? (int) $document_srl : (int) Context::get('document_srl');
  55. $this->module_srl = $module_srl ? (int) $module_srl : (int) Context::get('module_srl');
  56. $this->entry = Context::convertEncodingStr(Context::get('entry'));
  57. // Validate variables to prevent XSS
  58. $isInvalid = NULL;
  59. if($this->module && !preg_match("/^([a-z0-9\_\-]+)$/i", $this->module))
  60. {
  61. $isInvalid = TRUE;
  62. }
  63. if($this->mid && !preg_match("/^([a-z0-9\_\-]+)$/i", $this->mid))
  64. {
  65. $isInvalid = TRUE;
  66. }
  67. if($this->act && !preg_match("/^([a-z0-9\_\-]+)$/i", $this->act))
  68. {
  69. $isInvalid = TRUE;
  70. }
  71. if($isInvalid)
  72. {
  73. htmlHeader();
  74. echo Context::getLang("msg_invalid_request");
  75. htmlFooter();
  76. Context::close();
  77. exit;
  78. }
  79. if(isset($this->act) && (strlen($this->act) >= 4 && substr_compare($this->act, 'disp', 0, 4) === 0))
  80. {
  81. if(Context::get('_use_ssl') == 'optional' && Context::isExistsSSLAction($this->act) && $_SERVER['HTTPS'] != 'on')
  82. {
  83. if(Context::get('_https_port')!=null) {
  84. header('location:https://' . $_SERVER['HTTP_HOST'] . ':' . Context::get('_https_port') . $_SERVER['REQUEST_URI']);
  85. } else {
  86. header('location:https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']);
  87. }
  88. return;
  89. }
  90. }
  91. // call a trigger before moduleHandler init
  92. ModuleHandler::triggerCall('moduleHandler.init', 'before', $this);
  93. // execute addon (before module initialization)
  94. $called_position = 'before_module_init';
  95. $oAddonController = getController('addon');
  96. $addon_file = $oAddonController->getCacheFilePath(Mobile::isFromMobilePhone() ? 'mobile' : 'pc');
  97. if(file_exists($addon_file)) include($addon_file);
  98. }
  99. /**
  100. * Initialization. It finds the target module based on module, mid, document_srl, and prepares to execute an action
  101. * @return boolean true: OK, false: redirected
  102. * */
  103. function init()
  104. {
  105. $oModuleModel = getModel('module');
  106. $site_module_info = Context::get('site_module_info');
  107. // if success_return_url and error_return_url is incorrect
  108. $urls = array(Context::get('success_return_url'), Context::get('error_return_url'));
  109. foreach($urls as $url)
  110. {
  111. if(empty($url))
  112. {
  113. continue;
  114. }
  115. $urlInfo = parse_url($url);
  116. $host = $urlInfo['host'];
  117. $dbInfo = Context::getDBInfo();
  118. $defaultUrlInfo = parse_url($dbInfo->default_url);
  119. $defaultHost = $defaultUrlInfo['host'];
  120. if($host && ($host != $defaultHost && $host != $site_module_info->domain))
  121. {
  122. throw new Exception('msg_default_url_is_null');
  123. }
  124. }
  125. if(!$this->document_srl && $this->mid && $this->entry)
  126. {
  127. $oDocumentModel = getModel('document');
  128. $this->document_srl = $oDocumentModel->getDocumentSrlByAlias($this->mid, $this->entry);
  129. if($this->document_srl)
  130. {
  131. Context::set('document_srl', $this->document_srl);
  132. }
  133. }
  134. // Get module's information based on document_srl, if it's specified
  135. if($this->document_srl)
  136. {
  137. $module_info = $oModuleModel->getModuleInfoByDocumentSrl($this->document_srl);
  138. // If the document does not exist, remove document_srl
  139. if(!$module_info)
  140. {
  141. unset($this->document_srl);
  142. }
  143. else
  144. {
  145. // If it exists, compare mid based on the module information
  146. // if mids are not matching, set it as the document's mid
  147. if(!$this->mid || ($this->mid != $module_info->mid))
  148. {
  149. if(Context::getRequestMethod() == 'GET')
  150. {
  151. $this->mid = $module_info->mid;
  152. header('location:' . getNotEncodedSiteUrl($site_info->domain, 'mid', $this->mid, 'document_srl', $this->document_srl));
  153. return FALSE;
  154. }
  155. else
  156. {
  157. $this->mid = $module_info->mid;
  158. Context::set('mid', $this->mid);
  159. }
  160. }
  161. // if requested module is different from one of the document, remove the module information retrieved based on the document number
  162. if($this->module && $module_info->module != $this->module)
  163. {
  164. unset($module_info);
  165. }
  166. }
  167. }
  168. // If module_info is not set yet, and there exists mid information, get module information based on the mid
  169. if(!$module_info && $this->mid)
  170. {
  171. $module_info = $oModuleModel->getModuleInfoByMid($this->mid, $site_module_info->site_srl);
  172. //if($this->module && $module_info->module != $this->module) unset($module_info);
  173. }
  174. // redirect, if module_site_srl and site_srl are different
  175. if(!$this->module && !$module_info && $site_module_info->site_srl == 0 && $site_module_info->module_site_srl > 0)
  176. {
  177. $site_info = $oModuleModel->getSiteInfo($site_module_info->module_site_srl);
  178. header("location:" . getNotEncodedSiteUrl($site_info->domain, 'mid', $site_module_info->mid));
  179. return FALSE;
  180. }
  181. // If module_info is not set still, and $module does not exist, find the default module
  182. if(!$module_info && !$this->module && !$this->mid)
  183. {
  184. $module_info = $site_module_info;
  185. }
  186. if(!$module_info && !$this->module && $site_module_info->module_site_srl)
  187. {
  188. $module_info = $site_module_info;
  189. }
  190. // redirect, if site_srl of module_info is different from one of site's module_info
  191. if($module_info && $module_info->site_srl != $site_module_info->site_srl && !isCrawler())
  192. {
  193. // If the module is of virtual site
  194. if($module_info->site_srl)
  195. {
  196. $site_info = $oModuleModel->getSiteInfo($module_info->site_srl);
  197. $redirect_url = getNotEncodedSiteUrl($site_info->domain, 'mid', Context::get('mid'), 'document_srl', Context::get('document_srl'), 'module_srl', Context::get('module_srl'), 'entry', Context::get('entry'));
  198. // If it's called from a virtual site, though it's not a module of the virtual site
  199. }
  200. else
  201. {
  202. $db_info = Context::getDBInfo();
  203. if(!$db_info->default_url)
  204. {
  205. return Context::getLang('msg_default_url_is_not_defined');
  206. }
  207. else
  208. {
  209. $redirect_url = getNotEncodedSiteUrl($db_info->default_url, 'mid', Context::get('mid'), 'document_srl', Context::get('document_srl'), 'module_srl', Context::get('module_srl'), 'entry', Context::get('entry'));
  210. }
  211. }
  212. header("location:" . $redirect_url);
  213. return FALSE;
  214. }
  215. // If module info was set, retrieve variables from the module information
  216. if($module_info)
  217. {
  218. $this->module = $module_info->module;
  219. $this->mid = $module_info->mid;
  220. $this->module_info = $module_info;
  221. Context::setBrowserTitle($module_info->browser_title);
  222. $viewType = (Mobile::isFromMobilePhone()) ? 'M' : 'P';
  223. $targetSrl = (Mobile::isFromMobilePhone()) ? 'mlayout_srl' : 'layout_srl';
  224. // use the site default layout.
  225. if($module_info->{$targetSrl} == -1)
  226. {
  227. $oLayoutAdminModel = getAdminModel('layout');
  228. $layoutSrl = $oLayoutAdminModel->getSiteDefaultLayout($viewType, $module_info->site_srl);
  229. }
  230. else
  231. {
  232. $layoutSrl = $module_info->{$targetSrl};
  233. }
  234. // reset a layout_srl in module_info.
  235. $module_info->{$targetSrl} = $layoutSrl;
  236. $part_config = $oModuleModel->getModulePartConfig('layout', $layoutSrl);
  237. Context::addHtmlHeader($part_config->header_script);
  238. }
  239. // Set module and mid into module_info
  240. if(!isset($this->module_info))
  241. {
  242. $this->module_info = new stdClass();
  243. }
  244. $this->module_info->module = $this->module;
  245. $this->module_info->mid = $this->mid;
  246. // Set site_srl add 2011 08 09
  247. $this->module_info->site_srl = $site_module_info->site_srl;
  248. // Still no module? it's an error
  249. if(!$this->module)
  250. {
  251. $this->error = 'msg_module_is_not_exists';
  252. $this->httpStatusCode = '404';
  253. }
  254. // If mid exists, set mid into context
  255. if($this->mid)
  256. {
  257. Context::set('mid', $this->mid, TRUE);
  258. }
  259. // Call a trigger after moduleHandler init
  260. $output = ModuleHandler::triggerCall('moduleHandler.init', 'after', $this->module_info);
  261. if(!$output->toBool())
  262. {
  263. $this->error = $output->getMessage();
  264. return TRUE;
  265. }
  266. // Set current module info into context
  267. Context::set('current_module_info', $this->module_info);
  268. return TRUE;
  269. }
  270. /**
  271. * get a module instance and execute an action
  272. * @return ModuleObject executed module instance
  273. * */
  274. function procModule()
  275. {
  276. $oModuleModel = getModel('module');
  277. // If error occurred while preparation, return a message instance
  278. if($this->error)
  279. {
  280. $this->_setInputErrorToContext();
  281. $type = Mobile::isFromMobilePhone() ? 'mobile' : 'view';
  282. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  283. $oMessageObject->setError(-1);
  284. $oMessageObject->setMessage($this->error);
  285. $oMessageObject->dispMessage();
  286. if($this->httpStatusCode)
  287. {
  288. $oMessageObject->setHttpStatusCode($this->httpStatusCode);
  289. }
  290. return $oMessageObject;
  291. }
  292. // Get action information with conf/module.xml
  293. $xml_info = $oModuleModel->getModuleActionXml($this->module);
  294. // If not installed yet, modify act
  295. if($this->module == "install")
  296. {
  297. if(!$this->act || !$xml_info->action->{$this->act})
  298. {
  299. $this->act = $xml_info->default_index_act;
  300. }
  301. }
  302. // if act exists, find type of the action, if not use default index act
  303. if(!$this->act)
  304. {
  305. $this->act = $xml_info->default_index_act;
  306. }
  307. // still no act means error
  308. if(!$this->act)
  309. {
  310. $this->error = 'msg_module_is_not_exists';
  311. $this->httpStatusCode = '404';
  312. $this->_setInputErrorToContext();
  313. $type = Mobile::isFromMobilePhone() ? 'mobile' : 'view';
  314. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  315. $oMessageObject->setError(-1);
  316. $oMessageObject->setMessage($this->error);
  317. $oMessageObject->dispMessage();
  318. if($this->httpStatusCode)
  319. {
  320. $oMessageObject->setHttpStatusCode($this->httpStatusCode);
  321. }
  322. return $oMessageObject;
  323. }
  324. // get type, kind
  325. $type = $xml_info->action->{$this->act}->type;
  326. $ruleset = $xml_info->action->{$this->act}->ruleset;
  327. $kind = stripos($this->act, 'admin') !== FALSE ? 'admin' : '';
  328. if(!$kind && $this->module == 'admin')
  329. {
  330. $kind = 'admin';
  331. }
  332. // check REQUEST_METHOD in controller
  333. if($type == 'controller')
  334. {
  335. $allowedMethod = $xml_info->action->{$this->act}->method;
  336. if(!$allowedMethod)
  337. {
  338. $allowedMethodList[0] = 'POST';
  339. }
  340. else
  341. {
  342. $allowedMethodList = explode('|', strtoupper($allowedMethod));
  343. }
  344. if(!in_array(strtoupper($_SERVER['REQUEST_METHOD']), $allowedMethodList))
  345. {
  346. $this->error = "msg_invalid_request";
  347. $oMessageObject = ModuleHandler::getModuleInstance('message', 'view');
  348. $oMessageObject->setError(-1);
  349. $oMessageObject->setMessage($this->error);
  350. $oMessageObject->dispMessage();
  351. return $oMessageObject;
  352. }
  353. }
  354. if($this->module_info->use_mobile != "Y")
  355. {
  356. Mobile::setMobile(FALSE);
  357. }
  358. // Admin ip
  359. $logged_info = Context::get('logged_info');
  360. if($kind == 'admin' && $_SESSION['denied_admin'] == 'Y')
  361. {
  362. $this->_setInputErrorToContext();
  363. $this->error = "msg_not_permitted_act";
  364. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  365. $oMessageObject->setError(-1);
  366. $oMessageObject->setMessage($this->error);
  367. $oMessageObject->dispMessage();
  368. return $oMessageObject;
  369. }
  370. // if(type == view, and case for using mobilephone)
  371. if($type == "view" && Mobile::isFromMobilePhone() && Context::isInstalled())
  372. {
  373. $orig_type = "view";
  374. $type = "mobile";
  375. // create a module instance
  376. $oModule = $this->getModuleInstance($this->module, $type, $kind);
  377. if(!is_object($oModule) || !method_exists($oModule, $this->act))
  378. {
  379. $type = $orig_type;
  380. Mobile::setMobile(FALSE);
  381. $oModule = $this->getModuleInstance($this->module, $type, $kind);
  382. }
  383. }
  384. else
  385. {
  386. // create a module instance
  387. $oModule = $this->getModuleInstance($this->module, $type, $kind);
  388. }
  389. if(!is_object($oModule))
  390. {
  391. $this->_setInputErrorToContext();
  392. $type = Mobile::isFromMobilePhone() ? 'mobile' : 'view';
  393. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  394. $oMessageObject->setError(-1);
  395. $oMessageObject->setMessage($this->error);
  396. $oMessageObject->dispMessage();
  397. if($this->httpStatusCode)
  398. {
  399. $oMessageObject->setHttpStatusCode($this->httpStatusCode);
  400. }
  401. return $oMessageObject;
  402. }
  403. // If there is no such action in the module object
  404. if(!isset($xml_info->action->{$this->act}) || !method_exists($oModule, $this->act))
  405. {
  406. if(!Context::isInstalled())
  407. {
  408. $this->_setInputErrorToContext();
  409. $this->error = 'msg_invalid_request';
  410. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  411. $oMessageObject->setError(-1);
  412. $oMessageObject->setMessage($this->error);
  413. $oMessageObject->dispMessage();
  414. if($this->httpStatusCode)
  415. {
  416. $oMessageObject->setHttpStatusCode($this->httpStatusCode);
  417. }
  418. return $oMessageObject;
  419. }
  420. $forward = NULL;
  421. // 1. Look for the module with action name
  422. if(preg_match('/^([a-z]+)([A-Z])([a-z0-9\_]+)(.*)$/', $this->act, $matches))
  423. {
  424. $module = strtolower($matches[2] . $matches[3]);
  425. $xml_info = $oModuleModel->getModuleActionXml($module);
  426. if($xml_info->action->{$this->act} && ((stripos($this->act, 'admin') !== FALSE) || $xml_info->action->{$this->act}->standalone != 'false'))
  427. {
  428. $forward = new stdClass();
  429. $forward->module = $module;
  430. $forward->type = $xml_info->action->{$this->act}->type;
  431. $forward->ruleset = $xml_info->action->{$this->act}->ruleset;
  432. $forward->act = $this->act;
  433. }
  434. else
  435. {
  436. $this->error = 'msg_invalid_request';
  437. $oMessageObject = ModuleHandler::getModuleInstance('message', 'view');
  438. $oMessageObject->setError(-1);
  439. $oMessageObject->setMessage($this->error);
  440. $oMessageObject->dispMessage();
  441. return $oMessageObject;
  442. }
  443. }
  444. if(!$forward)
  445. {
  446. $forward = $oModuleModel->getActionForward($this->act);
  447. }
  448. if($forward->module && $forward->type && $forward->act && $forward->act == $this->act)
  449. {
  450. $kind = stripos($forward->act, 'admin') !== FALSE ? 'admin' : '';
  451. $type = $forward->type;
  452. $ruleset = $forward->ruleset;
  453. $tpl_path = $oModule->getTemplatePath();
  454. $orig_module = $oModule;
  455. if($type == "view" && Mobile::isFromMobilePhone())
  456. {
  457. $orig_type = "view";
  458. $type = "mobile";
  459. // create a module instance
  460. $oModule = $this->getModuleInstance($forward->module, $type, $kind);
  461. if(!is_object($oModule) || !method_exists($oModule, $this->act))
  462. {
  463. $type = $orig_type;
  464. Mobile::setMobile(FALSE);
  465. $oModule = $this->getModuleInstance($forward->module, $type, $kind);
  466. }
  467. }
  468. else
  469. {
  470. $oModule = $this->getModuleInstance($forward->module, $type, $kind);
  471. }
  472. if(!is_object($oModule))
  473. {
  474. $type = Mobile::isFromMobilePhone() ? 'mobile' : 'view';
  475. $this->_setInputErrorToContext();
  476. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  477. $oMessageObject->setError(-1);
  478. $oMessageObject->setMessage('msg_module_is_not_exists');
  479. $oMessageObject->dispMessage();
  480. if($this->httpStatusCode)
  481. {
  482. $oMessageObject->setHttpStatusCode($this->httpStatusCode);
  483. }
  484. return $oMessageObject;
  485. }
  486. $xml_info = $oModuleModel->getModuleActionXml($forward->module);
  487. $oMemberModel = getModel('member');
  488. if($this->module == "admin" && $type == "view")
  489. {
  490. if($logged_info->is_admin == 'Y')
  491. {
  492. if($this->act != 'dispLayoutAdminLayoutModify')
  493. {
  494. $oAdminView = getAdminView('admin');
  495. $oAdminView->makeGnbUrl($forward->module);
  496. $oModule->setLayoutPath("./modules/admin/tpl");
  497. $oModule->setLayoutFile("layout.html");
  498. }
  499. }
  500. else
  501. {
  502. $this->_setInputErrorToContext();
  503. $this->error = 'msg_is_not_administrator';
  504. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  505. $oMessageObject->setError(-1);
  506. $oMessageObject->setMessage($this->error);
  507. $oMessageObject->dispMessage();
  508. return $oMessageObject;
  509. }
  510. }
  511. if($kind == 'admin')
  512. {
  513. $grant = $oModuleModel->getGrant($this->module_info, $logged_info);
  514. if(!$grant->is_admin && !$grant->manager)
  515. {
  516. $this->_setInputErrorToContext();
  517. $this->error = 'msg_is_not_manager';
  518. $oMessageObject = ModuleHandler::getModuleInstance('message', 'view');
  519. $oMessageObject->setError(-1);
  520. $oMessageObject->setMessage($this->error);
  521. $oMessageObject->dispMessage();
  522. return $oMessageObject;
  523. }
  524. }
  525. }
  526. else if($xml_info->default_index_act && method_exists($oModule, $xml_info->default_index_act))
  527. {
  528. $this->act = $xml_info->default_index_act;
  529. }
  530. else
  531. {
  532. $this->error = 'msg_invalid_request';
  533. $oModule->setError(-1);
  534. $oModule->setMessage($this->error);
  535. return $oModule;
  536. }
  537. }
  538. // ruleset check...
  539. if(!empty($ruleset))
  540. {
  541. $rulesetModule = $forward->module ? $forward->module : $this->module;
  542. $rulesetFile = $oModuleModel->getValidatorFilePath($rulesetModule, $ruleset, $this->mid);
  543. if(!empty($rulesetFile))
  544. {
  545. if($_SESSION['XE_VALIDATOR_ERROR_LANG'])
  546. {
  547. $errorLang = $_SESSION['XE_VALIDATOR_ERROR_LANG'];
  548. foreach($errorLang as $key => $val)
  549. {
  550. Context::setLang($key, $val);
  551. }
  552. unset($_SESSION['XE_VALIDATOR_ERROR_LANG']);
  553. }
  554. $Validator = new Validator($rulesetFile);
  555. $result = $Validator->validate();
  556. if(!$result)
  557. {
  558. $lastError = $Validator->getLastError();
  559. $returnUrl = Context::get('error_return_url');
  560. $errorMsg = $lastError['msg'] ? $lastError['msg'] : 'validation error';
  561. //for xml response
  562. $oModule->setError(-1);
  563. $oModule->setMessage($errorMsg);
  564. //for html redirect
  565. $this->error = $errorMsg;
  566. $_SESSION['XE_VALIDATOR_ERROR'] = -1;
  567. $_SESSION['XE_VALIDATOR_MESSAGE'] = $this->error;
  568. $_SESSION['XE_VALIDATOR_MESSAGE_TYPE'] = 'error';
  569. $_SESSION['XE_VALIDATOR_RETURN_URL'] = $returnUrl;
  570. $_SESSION['XE_VALIDATOR_ID'] = Context::get('xe_validator_id');
  571. $this->_setInputValueToSession();
  572. return $oModule;
  573. }
  574. }
  575. }
  576. $oModule->setAct($this->act);
  577. $this->module_info->module_type = $type;
  578. $oModule->setModuleInfo($this->module_info, $xml_info);
  579. $skipAct = array(
  580. 'dispEditorConfigPreview' => 1,
  581. 'dispLayoutPreviewWithModule' => 1
  582. );
  583. if($type == "view" && $this->module_info->use_mobile == "Y" && Mobile::isMobileCheckByAgent() && !isset($skipAct[Context::get('act')]))
  584. {
  585. global $lang;
  586. $header = '<style>div.xe_mobile{opacity:0.7;margin:1em 0;padding:.5em;background:#333;border:1px solid #666;border-left:0;border-right:0}p.xe_mobile{text-align:center;margin:1em 0}a.xe_mobile{color:#ff0;font-weight:bold;font-size:24px}@media only screen and (min-width:500px){a.xe_mobile{font-size:15px}}</style>';
  587. $footer = '<div class="xe_mobile"><p class="xe_mobile"><a class="xe_mobile" href="' . getUrl('m', '1') . '">' . $lang->msg_pc_to_mobile . '</a></p></div>';
  588. Context::addHtmlHeader($header);
  589. Context::addHtmlFooter($footer);
  590. }
  591. if($type == "view" && $kind != 'admin')
  592. {
  593. $module_config = $oModuleModel->getModuleConfig('module');
  594. if($module_config->htmlFooter)
  595. {
  596. Context::addHtmlFooter($module_config->htmlFooter);
  597. }
  598. if($module_config->siteTitle)
  599. {
  600. $siteTitle = Context::getBrowserTitle();
  601. if(!$siteTitle)
  602. {
  603. Context::setBrowserTitle($module_config->siteTitle);
  604. }
  605. }
  606. }
  607. // if failed message exists in session, set context
  608. $this->_setInputErrorToContext();
  609. $procResult = $oModule->proc();
  610. $methodList = array('XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1);
  611. if(!$oModule->stop_proc && !isset($methodList[Context::getRequestMethod()]))
  612. {
  613. $error = $oModule->getError();
  614. $message = $oModule->getMessage();
  615. $messageType = $oModule->getMessageType();
  616. $redirectUrl = $oModule->getRedirectUrl();
  617. if($messageType == 'error') debugPrint($message, 'ERROR');
  618. if(!$procResult)
  619. {
  620. $this->error = $message;
  621. if(!$redirectUrl && Context::get('error_return_url'))
  622. {
  623. $redirectUrl = Context::get('error_return_url');
  624. }
  625. $this->_setInputValueToSession();
  626. }
  627. else
  628. {
  629. }
  630. $_SESSION['XE_VALIDATOR_ERROR'] = $error;
  631. $_SESSION['XE_VALIDATOR_ID'] = Context::get('xe_validator_id');
  632. if($message != 'success')
  633. {
  634. $_SESSION['XE_VALIDATOR_MESSAGE'] = $message;
  635. }
  636. $_SESSION['XE_VALIDATOR_MESSAGE_TYPE'] = $messageType;
  637. if(Context::get('xeVirtualRequestMethod') != 'xml')
  638. {
  639. $_SESSION['XE_VALIDATOR_RETURN_URL'] = $redirectUrl;
  640. }
  641. }
  642. unset($logged_info);
  643. return $oModule;
  644. }
  645. /**
  646. * set error message to Session.
  647. * @return void
  648. * */
  649. function _setInputErrorToContext()
  650. {
  651. if($_SESSION['XE_VALIDATOR_ERROR'] && !Context::get('XE_VALIDATOR_ERROR'))
  652. {
  653. Context::set('XE_VALIDATOR_ERROR', $_SESSION['XE_VALIDATOR_ERROR']);
  654. }
  655. if($_SESSION['XE_VALIDATOR_MESSAGE'] && !Context::get('XE_VALIDATOR_MESSAGE'))
  656. {
  657. Context::set('XE_VALIDATOR_MESSAGE', $_SESSION['XE_VALIDATOR_MESSAGE']);
  658. }
  659. if($_SESSION['XE_VALIDATOR_MESSAGE_TYPE'] && !Context::get('XE_VALIDATOR_MESSAGE_TYPE'))
  660. {
  661. Context::set('XE_VALIDATOR_MESSAGE_TYPE', $_SESSION['XE_VALIDATOR_MESSAGE_TYPE']);
  662. }
  663. if($_SESSION['XE_VALIDATOR_RETURN_URL'] && !Context::get('XE_VALIDATOR_RETURN_URL'))
  664. {
  665. Context::set('XE_VALIDATOR_RETURN_URL', $_SESSION['XE_VALIDATOR_RETURN_URL']);
  666. }
  667. if($_SESSION['XE_VALIDATOR_ID'] && !Context::get('XE_VALIDATOR_ID'))
  668. {
  669. Context::set('XE_VALIDATOR_ID', $_SESSION['XE_VALIDATOR_ID']);
  670. }
  671. if(count($_SESSION['INPUT_ERROR']))
  672. {
  673. Context::set('INPUT_ERROR', $_SESSION['INPUT_ERROR']);
  674. }
  675. $this->_clearErrorSession();
  676. }
  677. /**
  678. * clear error message to Session.
  679. * @return void
  680. * */
  681. function _clearErrorSession()
  682. {
  683. $_SESSION['XE_VALIDATOR_ERROR'] = '';
  684. $_SESSION['XE_VALIDATOR_MESSAGE'] = '';
  685. $_SESSION['XE_VALIDATOR_MESSAGE_TYPE'] = '';
  686. $_SESSION['XE_VALIDATOR_RETURN_URL'] = '';
  687. $_SESSION['XE_VALIDATOR_ID'] = '';
  688. $_SESSION['INPUT_ERROR'] = '';
  689. }
  690. /**
  691. * occured error when, set input values to session.
  692. * @return void
  693. * */
  694. function _setInputValueToSession()
  695. {
  696. $requestVars = Context::getRequestVars();
  697. unset($requestVars->act, $requestVars->mid, $requestVars->vid, $requestVars->success_return_url, $requestVars->error_return_url);
  698. foreach($requestVars AS $key => $value)
  699. {
  700. $_SESSION['INPUT_ERROR'][$key] = $value;
  701. }
  702. }
  703. /**
  704. * display contents from executed module
  705. * @param ModuleObject $oModule module instance
  706. * @return void
  707. * */
  708. function displayContent($oModule = NULL)
  709. {
  710. // If the module is not set or not an object, set error
  711. if(!$oModule || !is_object($oModule))
  712. {
  713. $this->error = 'msg_module_is_not_exists';
  714. $this->httpStatusCode = '404';
  715. }
  716. // If connection to DB has a problem even though it's not install module, set error
  717. if($this->module != 'install' && isset($GLOBALS['__DB__']) && $GLOBALS['__DB__'][Context::getDBType()]->isConnected() == FALSE)
  718. {
  719. $this->error = 'msg_dbconnect_failed';
  720. }
  721. // Call trigger after moduleHandler proc
  722. $output = ModuleHandler::triggerCall('moduleHandler.proc', 'after', $oModule);
  723. if(!$output->toBool())
  724. {
  725. $this->error = $output->getMessage();
  726. }
  727. // Use message view object, if HTML call
  728. $methodList = array('XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1);
  729. if(!isset($methodList[Context::getRequestMethod()]))
  730. {
  731. if($_SESSION['XE_VALIDATOR_RETURN_URL'])
  732. {
  733. $display_handler = new DisplayHandler();
  734. $display_handler->_debugOutput();
  735. header('location:' . $_SESSION['XE_VALIDATOR_RETURN_URL']);
  736. return;
  737. }
  738. // If error occurred, handle it
  739. if($this->error)
  740. {
  741. // display content with message module instance
  742. $type = Mobile::isFromMobilePhone() ? 'mobile' : 'view';
  743. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  744. $oMessageObject->setError(-1);
  745. $oMessageObject->setMessage($this->error);
  746. $oMessageObject->dispMessage();
  747. if($oMessageObject->getHttpStatusCode() && $oMessageObject->getHttpStatusCode() != '200')
  748. {
  749. $this->_setHttpStatusMessage($oMessageObject->getHttpStatusCode());
  750. $oMessageObject->setTemplateFile('http_status_code');
  751. }
  752. // If module was called normally, change the templates of the module into ones of the message view module
  753. if($oModule)
  754. {
  755. $oModule->setTemplatePath($oMessageObject->getTemplatePath());
  756. $oModule->setTemplateFile($oMessageObject->getTemplateFile());
  757. // Otherwise, set message instance as the target module
  758. }
  759. else
  760. {
  761. $oModule = $oMessageObject;
  762. }
  763. $this->_clearErrorSession();
  764. }
  765. // Check if layout_srl exists for the module
  766. if(Mobile::isFromMobilePhone())
  767. {
  768. $layout_srl = $oModule->module_info->mlayout_srl;
  769. }
  770. else
  771. {
  772. $layout_srl = $oModule->module_info->layout_srl;
  773. }
  774. // if layout_srl is rollback by module, set default layout
  775. if($layout_srl == -1)
  776. {
  777. $viewType = (Mobile::isFromMobilePhone()) ? 'M' : 'P';
  778. $oLayoutAdminModel = getAdminModel('layout');
  779. $layout_srl = $oLayoutAdminModel->getSiteDefaultLayout($viewType, $oModule->module_info->site_srl);
  780. }
  781. if($layout_srl && !$oModule->getLayoutFile())
  782. {
  783. // If layout_srl exists, get information of the layout, and set the location of layout_path/ layout_file
  784. $oLayoutModel = getModel('layout');
  785. $layout_info = $oLayoutModel->getLayout($layout_srl);
  786. if($layout_info)
  787. {
  788. // Input extra_vars into $layout_info
  789. if($layout_info->extra_var_count)
  790. {
  791. foreach($layout_info->extra_var as $var_id => $val)
  792. {
  793. if($val->type == 'image')
  794. {
  795. if(strncmp('./files/attach/images/', $val->value, 22) === 0)
  796. {
  797. $val->value = Context::getRequestUri() . substr($val->value, 2);
  798. }
  799. }
  800. $layout_info->{$var_id} = $val->value;
  801. }
  802. }
  803. // Set menus into context
  804. if($layout_info->menu_count)
  805. {
  806. foreach($layout_info->menu as $menu_id => $menu)
  807. {
  808. // set default menu set(included home menu)
  809. if(!$menu->menu_srl || $menu->menu_srl == -1)
  810. {
  811. $oMenuAdminController = getAdminController('menu');
  812. $homeMenuCacheFile = $oMenuAdminController->getHomeMenuCacheFile();
  813. if(FileHandler::exists($homeMenuCacheFile))
  814. {
  815. include($homeMenuCacheFile);
  816. }
  817. if(!$menu->menu_srl)
  818. {
  819. $menu->xml_file = str_replace('.xml.php', $homeMenuSrl . '.xml.php', $menu->xml_file);
  820. $menu->php_file = str_replace('.php', $homeMenuSrl . '.php', $menu->php_file);
  821. $layout_info->menu->{$menu_id}->menu_srl = $homeMenuSrl;
  822. }
  823. else
  824. {
  825. $menu->xml_file = str_replace($menu->menu_srl, $homeMenuSrl, $menu->xml_file);
  826. $menu->php_file = str_replace($menu->menu_srl, $homeMenuSrl, $menu->php_file);
  827. }
  828. }
  829. $php_file = FileHandler::exists($menu->php_file);
  830. if($php_file)
  831. {
  832. include($php_file);
  833. }
  834. Context::set($menu_id, $menu);
  835. }
  836. }
  837. // Set layout information into context
  838. Context::set('layout_info', $layout_info);
  839. $oModule->setLayoutPath($layout_info->path);
  840. $oModule->setLayoutFile('layout');
  841. // If layout was modified, use the modified version
  842. $edited_layout = $oLayoutModel->getUserLayoutHtml($layout_info->layout_srl);
  843. if(file_exists($edited_layout))
  844. {
  845. $oModule->setEditedLayoutFile($edited_layout);
  846. }
  847. }
  848. }
  849. $isLayoutDrop = Context::get('isLayoutDrop');
  850. if($isLayoutDrop)
  851. {
  852. $kind = stripos($this->act, 'admin') !== FALSE ? 'admin' : '';
  853. if($kind == 'admin')
  854. {
  855. $oModule->setLayoutFile('popup_layout');
  856. }
  857. else
  858. {
  859. $oModule->setLayoutPath('common/tpl');
  860. $oModule->setLayoutFile('default_layout');
  861. }
  862. }
  863. }
  864. // Display contents
  865. $oDisplayHandler = new DisplayHandler();
  866. $oDisplayHandler->printContent($oModule);
  867. }
  868. /**
  869. * returns module's path
  870. * @param string $module module name
  871. * @return string path of the module
  872. * */
  873. function getModulePath($module)
  874. {
  875. return sprintf('./modules/%s/', $module);
  876. }
  877. /**
  878. * It creates a module instance
  879. * @param string $module module name
  880. * @param string $type instance type, (e.g., view, controller, model)
  881. * @param string $kind admin or svc
  882. * @return ModuleObject module instance (if failed it returns null)
  883. * @remarks if there exists a module instance created before, returns it.
  884. * */
  885. function &getModuleInstance($module, $type = 'view', $kind = '')
  886. {
  887. if(__DEBUG__ == 3)
  888. {
  889. $start_time = getMicroTime();
  890. }
  891. $parent_module = $module;
  892. $kind = strtolower($kind);
  893. $type = strtolower($type);
  894. $kinds = array('svc' => 1, 'admin' => 1);
  895. if(!isset($kinds[$kind]))
  896. {
  897. $kind = 'svc';
  898. }
  899. $key = $module . '.' . ($kind != 'admin' ? '' : 'admin') . '.' . $type;
  900. if(is_array($GLOBALS['__MODULE_EXTEND__']) && array_key_exists($key, $GLOBALS['__MODULE_EXTEND__']))
  901. {
  902. $module = $extend_module = $GLOBALS['__MODULE_EXTEND__'][$key];
  903. }
  904. // if there is no instance of the module in global variable, create a new one
  905. if(!isset($GLOBALS['_loaded_module'][$module][$type][$kind]))
  906. {
  907. ModuleHandler::_getModuleFilePath($module, $type, $kind, $class_path, $high_class_file, $class_file, $instance_name);
  908. if($extend_module && (!is_readable($high_class_file) || !is_readable($class_file)))
  909. {
  910. $module = $parent_module;
  911. ModuleHandler::_getModuleFilePath($module, $type, $kind, $class_path, $high_class_file, $class_file, $instance_name);
  912. }
  913. // Get base class name and load the file contains it
  914. if(!class_exists($module, false))
  915. {
  916. $high_class_file = sprintf('%s%s%s.class.php', _XE_PATH_, $class_path, $module);
  917. if(!file_exists($high_class_file))
  918. {
  919. return NULL;
  920. }
  921. require_once($high_class_file);
  922. }
  923. // Get the name of the class file
  924. if(!is_readable($class_file))
  925. {
  926. return NULL;
  927. }
  928. // Create an instance with eval function
  929. require_once($class_file);
  930. if(!class_exists($instance_name, false))
  931. {
  932. return NULL;
  933. }
  934. $tmp_fn = create_function('', "return new {$instance_name}();");
  935. $oModule = $tmp_fn();
  936. if(!is_object($oModule))
  937. {
  938. return NULL;
  939. }
  940. // Load language files for the class
  941. Context::loadLang($class_path . 'lang');
  942. if($extend_module)
  943. {
  944. Context::loadLang(ModuleHandler::getModulePath($parent_module) . 'lang');
  945. }
  946. // Set variables to the instance
  947. $oModule->setModule($module);
  948. $oModule->setModulePath($class_path);
  949. // If the module has a constructor, run it.
  950. if(!isset($GLOBALS['_called_constructor'][$instance_name]))
  951. {
  952. $GLOBALS['_called_constructor'][$instance_name] = TRUE;
  953. if(@method_exists($oModule, $instance_name))
  954. {
  955. $oModule->{$instance_name}();
  956. }
  957. }
  958. // Store the created instance into GLOBALS variable
  959. $GLOBALS['_loaded_module'][$module][$type][$kind] = $oModule;
  960. }
  961. if(__DEBUG__ == 3)
  962. {
  963. $GLOBALS['__elapsed_class_load__'] += getMicroTime() - $start_time;
  964. }
  965. // return the instance
  966. return $GLOBALS['_loaded_module'][$module][$type][$kind];
  967. }
  968. function _getModuleFilePath($module, $type, $kind, &$classPath, &$highClassFile, &$classFile, &$instanceName)
  969. {
  970. $classPath = ModuleHandler::getModulePath($module);
  971. $highClassFile = sprintf('%s%s%s.class.php', _XE_PATH_, $classPath, $module);
  972. $highClassFile = FileHandler::getRealPath($highClassFile);
  973. $types = array('view','controller','model','api','wap','mobile','class');
  974. if(!in_array($type, $types))
  975. {
  976. $type = $types[0];
  977. }
  978. if($type == 'class')
  979. {
  980. $instanceName = '%s';
  981. $classFile = '%s%s.%s.php';
  982. }
  983. elseif($kind == 'admin' && array_search($type, $types) < 3)
  984. {
  985. $instanceName = '%sAdmin%s';
  986. $classFile = '%s%s.admin.%s.php';
  987. }
  988. else
  989. {
  990. $instanceName = '%s%s';
  991. $classFile = '%s%s.%s.php';
  992. }
  993. $instanceName = sprintf($instanceName, $module, ucfirst($type));
  994. $classFile = FileHandler::getRealPath(sprintf($classFile, $classPath, $module, $type));
  995. }
  996. /**
  997. * call a trigger
  998. * @param string $trigger_name trigger's name to call
  999. * @param string $called_position called position
  1000. * @param object $obj an object as a parameter to trigger
  1001. * @return Object
  1002. * */
  1003. function triggerCall($trigger_name, $called_position, &$obj)
  1004. {
  1005. // skip if not installed
  1006. if(!Context::isInstalled())
  1007. {
  1008. return new Object();
  1009. }
  1010. $oModuleModel = getModel('module');
  1011. $triggers = $oModuleModel->getTriggers($trigger_name, $called_position);
  1012. if(!$triggers || count($triggers) < 1)
  1013. {
  1014. return new Object();
  1015. }
  1016. foreach($triggers as $item)
  1017. {
  1018. $module = $item->module;
  1019. $type = $item->type;
  1020. $called_method = $item->called_method;
  1021. // todo why don't we call a normal class object ?
  1022. $oModule = getModule($module, $type);
  1023. if(!$oModule || !method_exists($oModule, $called_method))
  1024. {
  1025. continue;
  1026. }
  1027. $output = $oModule->{$called_method}($obj);
  1028. if(is_object($output) && method_exists($output, 'toBool') && !$output->toBool())
  1029. {
  1030. return $output;
  1031. }
  1032. unset($oModule);
  1033. }
  1034. return new Object();
  1035. }
  1036. /**
  1037. * get http status message by http status code
  1038. * @param string $code
  1039. * @return string
  1040. * */
  1041. function _setHttpStatusMessage($code)
  1042. {
  1043. $statusMessageList = array(
  1044. '100' => 'Continue',
  1045. '101' => 'Switching Protocols',
  1046. '201' => 'OK', // todo check array key '201'
  1047. '201' => 'Created',
  1048. '202' => 'Accepted',
  1049. '203' => 'Non-Authoritative Information',
  1050. '204' => 'No Content',
  1051. '205' => 'Reset Content',
  1052. '206' => 'Partial Content',
  1053. '300' => 'Multiple Choices',
  1054. '301' => 'Moved Permanently',
  1055. '302' => 'Found',
  1056. '303' => 'See Other',
  1057. '304' => 'Not Modified',
  1058. '305' => 'Use Proxy',
  1059. '307' => 'Temporary Redirect',
  1060. '400' => 'Bad Request',
  1061. '401' => 'Unauthorized',
  1062. '402' => 'Payment Required',
  1063. '403' => 'Forbidden',
  1064. '404' => 'Not Found',
  1065. '405' => 'Method Not Allowed',
  1066. '406' => 'Not Acceptable',
  1067. '407' => 'Proxy Authentication Required',
  1068. '408' => 'Request Timeout',
  1069. '409' => 'Conflict',
  1070. '410' => 'Gone',
  1071. '411' => 'Length Required',
  1072. '412' => 'Precondition Failed',
  1073. '413' => 'Request Entity Too Large',
  1074. '414' => 'Request-URI Too Long',
  1075. '415' => 'Unsupported Media Type',
  1076. '416' => 'Requested Range Not Satisfiable',
  1077. '417' => 'Expectation Failed',
  1078. '500' => 'Internal Server Error',
  1079. '501' => 'Not Implemented',
  1080. '502' => 'Bad Gateway',
  1081. '503' => 'Service Unavailable',
  1082. '504' => 'Gateway Timeout',
  1083. '505' => 'HTTP Version Not Supported',
  1084. );
  1085. $statusMessage = $statusMessageList[$code];
  1086. if(!$statusMessage)
  1087. {
  1088. $statusMessage = 'OK';
  1089. }
  1090. Context::set('http_status_code', $code);
  1091. Context::set('http_status_message', $statusMessage);
  1092. }
  1093. }
  1094. /* End of file ModuleHandler.class.php */
  1095. /* Location: ./classes/module/ModuleHandler.class.php */