PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/module/ModuleObject.class.php

http://xe-core.googlecode.com/
PHP | 480 lines | 306 code | 40 blank | 134 comment | 73 complexity | 18492d85513d90ab18aead9ccc6650da MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * @class ModuleObject
  4. * @author NHN (developers@xpressengine.com)
  5. * base class of ModuleHandler
  6. * */
  7. class ModuleObject extends Object
  8. {
  9. var $mid = NULL; ///< string to represent run-time instance of Module (XE Module)
  10. var $module = NULL; ///< Class name of Xe Module that is identified by mid
  11. var $module_srl = NULL; ///< integer value to represent a run-time instance of Module (XE Module)
  12. var $module_info = NULL; ///< an object containing the module information
  13. var $origin_module_info = NULL;
  14. var $xml_info = NULL; ///< an object containing the module description extracted from XML file
  15. var $module_path = NULL; ///< a path to directory where module source code resides
  16. var $act = NULL; ///< a string value to contain the action name
  17. var $template_path = NULL; ///< a path of directory where template files reside
  18. var $template_file = NULL; ///< name of template file
  19. var $layout_path = ''; ///< a path of directory where layout files reside
  20. var $layout_file = ''; ///< name of layout file
  21. var $edited_layout_file = ''; ///< name of temporary layout files that is modified in an admin mode
  22. var $stop_proc = false; ///< a flag to indicating whether to stop the execution of code.
  23. var $module_config = NULL;
  24. var $ajaxRequestMethod = array('XMLRPC', 'JSON');
  25. var $gzhandler_enable = TRUE;
  26. /**
  27. * setter to set the name of module
  28. * @param string $module name of module
  29. * @return void
  30. * */
  31. function setModule($module)
  32. {
  33. $this->module = $module;
  34. }
  35. /**
  36. * setter to set the name of module path
  37. * @param string $path the directory path to a module directory
  38. * @return void
  39. * */
  40. function setModulePath($path)
  41. {
  42. if(substr($path, -1) != '/')
  43. {
  44. $path.='/';
  45. }
  46. $this->module_path = $path;
  47. }
  48. /**
  49. * setter to set an url for redirection
  50. * @param string $url url for redirection
  51. * @remark redirect_url is used only for ajax requests
  52. * @return void
  53. * */
  54. function setRedirectUrl($url = './', $output = NULL)
  55. {
  56. $ajaxRequestMethod = array_flip($this->ajaxRequestMethod);
  57. if(!isset($ajaxRequestMethod[Context::getRequestMethod()]))
  58. {
  59. $this->add('redirect_url', $url);
  60. }
  61. if($output !== NULL && is_object($output))
  62. {
  63. return $output;
  64. }
  65. }
  66. /**
  67. * get url for redirection
  68. * @return string redirect_url
  69. * */
  70. function getRedirectUrl()
  71. {
  72. return $this->get('redirect_url');
  73. }
  74. /**
  75. * set message
  76. * @param string $message a message string
  77. * @param string $type type of message (error, info, update)
  78. * @return void
  79. * */
  80. function setMessage($message, $type = NULL)
  81. {
  82. parent::setMessage($message);
  83. $this->setMessageType($type);
  84. }
  85. /**
  86. * set type of message
  87. * @param string $type type of message (error, info, update)
  88. * @return void
  89. * */
  90. function setMessageType($type)
  91. {
  92. $this->add('message_type', $type);
  93. }
  94. /**
  95. * get type of message
  96. * @return string $type
  97. * */
  98. function getMessageType()
  99. {
  100. $type = $this->get('message_type');
  101. $typeList = array('error' => 1, 'info' => 1, 'update' => 1);
  102. if(!isset($typeList[$type]))
  103. {
  104. $type = $this->getError() ? 'error' : 'info';
  105. }
  106. return $type;
  107. }
  108. /**
  109. * sett to set the template path for refresh.html
  110. * refresh.html is executed as a result of method execution
  111. * Tpl as the common run of the refresh.html ..
  112. * @return void
  113. * */
  114. function setRefreshPage()
  115. {
  116. $this->setTemplatePath('./common/tpl');
  117. $this->setTemplateFile('refresh');
  118. }
  119. /**
  120. * sett to set the action name
  121. * @param string $act
  122. * @return void
  123. * */
  124. function setAct($act)
  125. {
  126. $this->act = $act;
  127. }
  128. /**
  129. * sett to set module information
  130. * @param object $module_info object containing module information
  131. * @param object $xml_info object containing module description
  132. * @return void
  133. * */
  134. function setModuleInfo($module_info, $xml_info)
  135. {
  136. // The default variable settings
  137. $this->mid = $module_info->mid;
  138. $this->module_srl = $module_info->module_srl;
  139. $this->module_info = $module_info;
  140. $this->origin_module_info = $module_info;
  141. $this->xml_info = $xml_info;
  142. $this->skin_vars = $module_info->skin_vars;
  143. // validate certificate info and permission settings necessary in Web-services
  144. $is_logged = Context::get('is_logged');
  145. $logged_info = Context::get('logged_info');
  146. // module model create an object
  147. $oModuleModel = getModel('module');
  148. // permission settings. access, manager(== is_admin) are fixed and privilege name in XE
  149. $module_srl = Context::get('module_srl');
  150. if(!$module_info->mid && !is_array($module_srl) && preg_match('/^([0-9]+)$/', $module_srl))
  151. {
  152. $request_module = $oModuleModel->getModuleInfoByModuleSrl($module_srl);
  153. if($request_module->module_srl == $module_srl)
  154. {
  155. $grant = $oModuleModel->getGrant($request_module, $logged_info);
  156. }
  157. }
  158. else
  159. {
  160. $grant = $oModuleModel->getGrant($module_info, $logged_info, $xml_info);
  161. // have at least access grant
  162. if(substr_count($this->act, 'Member') || substr_count($this->act, 'Communication'))
  163. {
  164. $grant->access = 1;
  165. }
  166. }
  167. // display no permission if the current module doesn't have an access privilege
  168. //if(!$grant->access) return $this->stop("msg_not_permitted");
  169. // checks permission and action if you don't have an admin privilege
  170. if(!$grant->manager)
  171. {
  172. // get permission types(guest, member, manager, root) of the currently requested action
  173. $permission_target = $xml_info->permission->{$this->act};
  174. // check manager if a permission in module.xml otherwise action if no permission
  175. if(!$permission_target && substr_count($this->act, 'Admin'))
  176. {
  177. $permission_target = 'manager';
  178. }
  179. // Check permissions
  180. switch($permission_target)
  181. {
  182. case 'root' :
  183. case 'manager' :
  184. $this->stop('msg_is_not_administrator');
  185. return;
  186. case 'member' :
  187. if(!$is_logged)
  188. {
  189. $this->stop('msg_not_permitted_act');
  190. return;
  191. }
  192. break;
  193. }
  194. }
  195. // permission variable settings
  196. $this->grant = $grant;
  197. Context::set('grant', $grant);
  198. $this->module_config = $oModuleModel->getModuleConfig($this->module, $module_info->site_srl);
  199. if(method_exists($this, 'init'))
  200. {
  201. $this->init();
  202. }
  203. }
  204. /**
  205. * set the stop_proc and approprate message for msg_code
  206. * @param string $msg_code an error code
  207. * @return ModuleObject $this
  208. * */
  209. function stop($msg_code)
  210. {
  211. // flag setting to stop the proc processing
  212. $this->stop_proc = TRUE;
  213. // Error handling
  214. $this->setError(-1);
  215. $this->setMessage($msg_code);
  216. // Error message display by message module
  217. $type = Mobile::isFromMobilePhone() ? 'mobile' : 'view';
  218. $oMessageObject = ModuleHandler::getModuleInstance('message', $type);
  219. $oMessageObject->setError(-1);
  220. $oMessageObject->setMessage($msg_code);
  221. $oMessageObject->dispMessage();
  222. $this->setTemplatePath($oMessageObject->getTemplatePath());
  223. $this->setTemplateFile($oMessageObject->getTemplateFile());
  224. return $this;
  225. }
  226. /**
  227. * set the file name of the template file
  228. * @param string name of file
  229. * @return void
  230. * */
  231. function setTemplateFile($filename)
  232. {
  233. if(substr($filename, -5) != '.html')
  234. {
  235. $filename .= '.html';
  236. }
  237. $this->template_file = $filename;
  238. }
  239. /**
  240. * retrieve the directory path of the template directory
  241. * @return string
  242. * */
  243. function getTemplateFile()
  244. {
  245. return $this->template_file;
  246. }
  247. /**
  248. * set the directory path of the template directory
  249. * @param string path of template directory.
  250. * @return void
  251. * */
  252. function setTemplatePath($path)
  253. {
  254. if(substr($path, 0, 1) != '/' && substr($path, 0, 2) != './')
  255. {
  256. $path = './' . $path;
  257. }
  258. if(substr($path, -1) != '/')
  259. {
  260. $path .= '/';
  261. }
  262. $this->template_path = $path;
  263. }
  264. /**
  265. * retrieve the directory path of the template directory
  266. * @return string
  267. * */
  268. function getTemplatePath()
  269. {
  270. return $this->template_path;
  271. }
  272. /**
  273. * set the file name of the temporarily modified by admin
  274. * @param string name of file
  275. * @return void
  276. * */
  277. function setEditedLayoutFile($filename)
  278. {
  279. if(substr($filename, -5) != '.html')
  280. {
  281. $filename .= '.html';
  282. }
  283. $this->edited_layout_file = $filename;
  284. }
  285. /**
  286. * retreived the file name of edited_layout_file
  287. * @return string
  288. * */
  289. function getEditedLayoutFile()
  290. {
  291. return $this->edited_layout_file;
  292. }
  293. /**
  294. * set the file name of the layout file
  295. * @param string name of file
  296. * @return void
  297. * */
  298. function setLayoutFile($filename)
  299. {
  300. if(substr($filename, -5) != '.html')
  301. {
  302. $filename .= '.html';
  303. }
  304. $this->layout_file = $filename;
  305. }
  306. /**
  307. * get the file name of the layout file
  308. * @return string
  309. * */
  310. function getLayoutFile()
  311. {
  312. return $this->layout_file;
  313. }
  314. /**
  315. * set the directory path of the layout directory
  316. * @param string path of layout directory.
  317. * */
  318. function setLayoutPath($path)
  319. {
  320. if(substr($path, 0, 1) != '/' && substr($path, 0, 2) != './')
  321. {
  322. $path = './' . $path;
  323. }
  324. if(substr($path, -1) != '/')
  325. {
  326. $path .= '/';
  327. }
  328. $this->layout_path = $path;
  329. }
  330. /**
  331. * set the directory path of the layout directory
  332. * @return string
  333. * */
  334. function getLayoutPath()
  335. {
  336. return $this->layout_path;
  337. }
  338. /**
  339. * excute the member method specified by $act variable
  340. * @return boolean true : success false : fail
  341. * */
  342. function proc()
  343. {
  344. // pass if stop_proc is true
  345. if($this->stop_proc)
  346. {
  347. return false;
  348. }
  349. // trigger call
  350. $triggerOutput = ModuleHandler::triggerCall('moduleObject.proc', 'before', $this);
  351. if(!$triggerOutput->toBool())
  352. {
  353. $this->setError($triggerOutput->getError());
  354. $this->setMessage($triggerOutput->getMessage());
  355. return false;
  356. }
  357. // execute an addon(call called_position as before_module_proc)
  358. $called_position = 'before_module_proc';
  359. $oAddonController = getController('addon');
  360. $addon_file = $oAddonController->getCacheFilePath(Mobile::isFromMobilePhone() ? "mobile" : "pc");
  361. @include($addon_file);
  362. if(isset($this->xml_info->action->{$this->act}) && method_exists($this, $this->act))
  363. {
  364. // Check permissions
  365. if($this->module_srl && !$this->grant->access)
  366. {
  367. $this->stop("msg_not_permitted_act");
  368. return FALSE;
  369. }
  370. // integrate skin information of the module(change to sync skin info with the target module only by seperating its table)
  371. $is_default_skin = ((!Mobile::isFromMobilePhone() && $this->module_info->is_skin_fix == 'N') || (Mobile::isFromMobilePhone() && $this->module_info->is_mskin_fix == 'N'));
  372. $usedSkinModule = !($this->module == 'page' && ($this->module_info->page_type == 'OUTSIDE' || $this->module_info->page_type == 'WIDGET'));
  373. if($usedSkinModule && $is_default_skin && $this->module != 'admin' && strpos($this->act, 'Admin') === false && $this->module == $this->module_info->module)
  374. {
  375. $dir = (Mobile::isFromMobilePhone()) ? 'm.skins' : 'skins';
  376. $valueName = (Mobile::isFromMobilePhone()) ? 'mskin' : 'skin';
  377. $oModuleModel = getModel('module');
  378. $skinType = (Mobile::isFromMobilePhone()) ? 'M' : 'P';
  379. $skinName = $oModuleModel->getModuleDefaultSkin($this->module, $skinType);
  380. if($this->module == 'page')
  381. {
  382. $this->module_info->{$valueName} = $skinName;
  383. }
  384. else
  385. {
  386. $isTemplatPath = (strpos($this->getTemplatePath(), '/tpl/') !== FALSE);
  387. if(!$isTemplatPath)
  388. {
  389. $this->setTemplatePath(sprintf('%s%s/%s/', $this->module_path, $dir, $skinName));
  390. }
  391. }
  392. }
  393. $oModuleModel = getModel('module');
  394. $oModuleModel->syncSkinInfoToModuleInfo($this->module_info);
  395. Context::set('module_info', $this->module_info);
  396. // Run
  397. $output = $this->{$this->act}();
  398. }
  399. else
  400. {
  401. return false;
  402. }
  403. // trigger call
  404. $triggerOutput = ModuleHandler::triggerCall('moduleObject.proc', 'after', $this);
  405. if(!$triggerOutput->toBool())
  406. {
  407. $this->setError($triggerOutput->getError());
  408. $this->setMessage($triggerOutput->getMessage());
  409. return false;
  410. }
  411. // execute an addon(call called_position as after_module_proc)
  412. $called_position = 'after_module_proc';
  413. $oAddonController = getController('addon');
  414. $addon_file = $oAddonController->getCacheFilePath(Mobile::isFromMobilePhone() ? "mobile" : "pc");
  415. @include($addon_file);
  416. if(is_a($output, 'Object') || is_subclass_of($output, 'Object'))
  417. {
  418. $this->setError($output->getError());
  419. $this->setMessage($output->getMessage());
  420. if(!$output->toBool())
  421. {
  422. return false;
  423. }
  424. }
  425. // execute api methos of the module if view action is and result is XMLRPC or JSON
  426. if($this->module_info->module_type == 'view')
  427. {
  428. if(Context::getResponseMethod() == 'XMLRPC' || Context::getResponseMethod() == 'JSON')
  429. {
  430. $oAPI = getAPI($this->module_info->module, 'api');
  431. if(method_exists($oAPI, $this->act))
  432. {
  433. $oAPI->{$this->act}($this);
  434. }
  435. }
  436. }
  437. return true;
  438. }
  439. }
  440. ?>