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