PageRenderTime 54ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/module/ModuleHandler.class.php

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