PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/cubi/openbiz/bin/BizController.php

http://openbiz-cubi.googlecode.com/
PHP | 443 lines | 265 code | 53 blank | 125 comment | 54 complexity | c0462fb87206d11e07d021cb74eb12dc MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0
  1. <?PHP
  2. /**
  3. * PHPOpenBiz Framework
  4. *
  5. * This file contain BizController class, the C from MVC of phpOpenBiz framework,
  6. * and execute it. So bootstrap script simply include this file. For sample of
  7. * bootstrap script please see controller.php under baseapp/bin
  8. *
  9. * LICENSE
  10. *
  11. * This source file is subject to the BSD license that is bundled
  12. * with this package in the file LICENSE.txt.
  13. *
  14. * @package openbiz.bin
  15. * @copyright Copyright (c) 2005-2011, Rocky Swen
  16. * @license http://www.opensource.org/licenses/bsd-license.php
  17. * @link http://www.phpopenbiz.org/
  18. * @version $Id: BizController.php 5321 2013-03-21 07:20:24Z rockyswen@gmail.com $
  19. */
  20. // run controller
  21. //
  22. //session_cache_limiter('public');
  23. ob_start();
  24. header('Content-Type: text/html; charset=utf-8');
  25. include_once("sysheader_inc.php");
  26. // start session context object
  27. BizSystem::sessionContext();
  28. $bizController = new BizController();
  29. if ($bizController->processSecurityFilters() === true)
  30. {
  31. $bizController->dispatchRequest();
  32. }
  33. /**
  34. * BizController is the class that dispatches client requests to proper objects
  35. *
  36. * @package openbiz.bin
  37. * @author Rocky Swen <rocky@phpopenbiz.org>
  38. * @copyright Copyright (c) 2005-2011, Rocky Swen
  39. * @access public
  40. */
  41. class BizController
  42. {
  43. private $_userTimeoutView = USER_TIMEOUT_VIEW;
  44. private $_accessDeniedView = ACCESS_DENIED_VIEW;
  45. private $_securityDeniedView = SECURITY_DENIED_VIEW;
  46. /**
  47. * Process Security Filters
  48. *
  49. * @return boolean true if success, and false if have error
  50. */
  51. public function processSecurityFilters()
  52. {
  53. $securityService = BizSystem::getService(SECURITY_SERVICE);
  54. $securityService->processFilters();
  55. if ($err_msg = $securityService->getErrorMessage())
  56. {
  57. if ($this->_securityDeniedView)
  58. {
  59. $view = $this->_securityDeniedView;
  60. } else
  61. {
  62. $view = $this->_accessDeniedView;
  63. }
  64. $this->renderView($view);
  65. //BizSystem::clientProxy()->redirectView($view);
  66. return false;
  67. }
  68. return true;
  69. }
  70. /**
  71. * Dispatches client requests to proper objects, print the returned html text.
  72. *
  73. * @return void
  74. */
  75. public function dispatchRequest()
  76. {
  77. if ($this->_hasView())
  78. {
  79. return $this->_dispatchView();
  80. } else
  81. {
  82. if ($this->_isSessionTimeout()) // show timeout view
  83. {
  84. BizSystem::sessionContext()->destroy();
  85. //return $this->renderView($this->_userTimeoutView);
  86. return BizSystem::clientProxy()->redirectView($this->_userTimeoutView);
  87. }
  88. $this->_dispatchRPC();
  89. }
  90. }
  91. /**
  92. * Get the parameter from the url
  93. *
  94. * @return array parameter array
  95. */
  96. private function _getParameters()
  97. {
  98. $getKeys = array_keys($_GET);
  99. $params = null;
  100. // read parameters "param:name=value"
  101. foreach ($getKeys as $key)
  102. {
  103. if (substr($key, 0, 6) == "param:")
  104. {
  105. $paramName = substr($key, 6);
  106. $paramValue = $_GET[$key];
  107. $params[$paramName] = $paramValue;
  108. }
  109. }
  110. return $params;
  111. }
  112. /**
  113. * Get user profile array. Profile is provided by profileService
  114. *
  115. * @return array profile array
  116. */
  117. private function _getUserProfile()
  118. {
  119. return BizSystem::getUserProfile();
  120. }
  121. /**
  122. * Check if session timed out.
  123. *
  124. * @return boolean true - session timed out, false - session alive
  125. */
  126. private function _isSessionTimeout()
  127. {
  128. return BizSystem::sessionContext()->isTimeout();
  129. }
  130. /**
  131. * Check if the view can be accessed by current user. Call accessService to do the check
  132. *
  133. * @param string $viewName view name
  134. * @return boolean true= allow, false not allow
  135. */
  136. private function _canUserAccessView($viewName)
  137. {
  138. // load accessService
  139. $svcobj = BizSystem::getService(ACCESS_SERVICE);
  140. return $svcobj->allowViewAccess($viewName);
  141. }
  142. /**
  143. * Render a bizview
  144. *
  145. * @param string $viewName name of bizview
  146. * @param string $rule the search rule of a bizform who is not depent on (a subctrl of) another bizform
  147. * @return void
  148. */
  149. public function renderView($viewName, $form = "", $rule = "", $params = null, $hist = "")
  150. {
  151. $bizSystem = BizSystem::instance();
  152. /* @var $viewObj EasyView */
  153. if ($viewName == "__DynPopup")
  154. {
  155. $viewObj = BizSystem::getObject($viewName);
  156. $viewObj->render();
  157. return;
  158. }
  159. // if previous view is different with the to-be-loaded view,
  160. // clear the previous session objects
  161. $prevViewName = $bizSystem->getCurrentViewName();
  162. $prevViewSet = $bizSystem->getCurrentViewSet();
  163. // need to set current view before get view object
  164. $bizSystem->setCurrentViewName($viewName);
  165. $viewObj = BizSystem::getObject($viewName);
  166. if (!$viewObj)
  167. return;
  168. $viewSet = $viewObj->getViewSet();
  169. $bizSystem->setCurrentViewSet($viewSet);
  170. /*
  171. if ($prevViewSet && $viewSet && $prevViewSet == $viewSet) // keep prev view session objects if they have same viewset
  172. BizSystem::sessionContext()->clearSessionObjects(true);
  173. else
  174. BizSystem::sessionContext()->clearSessionObjects(false);
  175. */
  176. BizSystem::sessionContext()->clearSessionObjects(true);
  177. if ($hist == "N") // clean view history
  178. $viewObj->cleanViewHistory();
  179. if ($form != "" && $rule != "")
  180. $viewObj->processRule($form, $rule, TRUE);
  181. if ($params)
  182. $viewObj->setParameters($params);
  183. if (isset($_GET['mode'])) // can specify mode of form
  184. $viewObj->setFormMode($form, $_GET['mode']);
  185. $viewObj->render();
  186. //BizController::hidePageLoading();
  187. }
  188. /**
  189. * Invoke the action passed from browser
  190. *
  191. * @return HTML content
  192. */
  193. protected function invoke()
  194. {
  195. //patched by jixian for fix ajax post data
  196. if (isset($_POST['__url']))
  197. {
  198. $getUrl = parse_url($_POST['__url']);
  199. $query = $getUrl['query'];
  200. $parameter = explode('&', $query);
  201. foreach ($parameter as $param)
  202. {
  203. $data = explode('=', $param);
  204. $name = $data[0];
  205. $value = $data[1];
  206. $_GET[$name] = $value;
  207. }
  208. }
  209. // get invocation type
  210. $invocationType = (isset($_REQUEST['F']) ? $_REQUEST['F'] : "");
  211. if ($invocationType == '') // is invocation?
  212. return;
  213. // check is valid invocation?
  214. if ($invocationType != "RPCInvoke" && $invocationType != "Invoke")
  215. {
  216. trigger_error("$invocationType is not a valid invocation", E_USER_ERROR);
  217. return;
  218. }
  219. // read parameters
  220. $arg_list = array();
  221. $i = 0;
  222. eval("\$P$i = (isset(\$_REQUEST['P$i']) ? \$_REQUEST['P$i']:'');");
  223. $Ptmp = "P" . $i;
  224. eval("\$P$i = (isset(\$_REQUEST['P$i']) ? \$_REQUEST['P$i']:'');");
  225. if (strstr($P0, Popup_Suffix)) // _popupx_?
  226. {
  227. $name_len = strlen($P0);
  228. $suffix_len = strlen(Popup_Suffix);
  229. $P0 = substr($P0, 0, $name_len - $suffix_len - 1) . "]";
  230. }
  231. while ($$Ptmp != "")
  232. {
  233. $parm = $$Ptmp;
  234. $parm = substr($parm, 1, strlen($parm) - 2);
  235. $arg_list[] = $parm;
  236. $i++;
  237. eval("\$P$i = (isset(\$_REQUEST['P$i']) ? \$_REQUEST['P$i']:'');");
  238. $Ptmp = "P" . $i;
  239. }
  240. if ($invocationType == "RPCInvoke")
  241. BizSystem::clientProxy()->setRPCFlag(true);
  242. // invoke the function
  243. $num_arg = count($arg_list);
  244. if ($num_arg < 2)
  245. {
  246. $errmsg = BizSystem::getMessage("SYS_ERROR_RPCARG", array($class));
  247. trigger_error($errmsg, E_USER_ERROR);
  248. } else
  249. {
  250. $objName = array_shift($arg_list);
  251. $methodName = array_shift($arg_list);
  252. $obj = BizSystem::getObject($objName);
  253. if ($obj)
  254. {
  255. if (method_exists($obj, $methodName))
  256. {
  257. if (!$this->validateRequest($obj, $methodName))
  258. {
  259. $errmsg = BizSystem::getMessage("SYS_ERROR_REQUEST_REJECT", array($obj->m_Name, $methodName));
  260. trigger_error($errmsg, E_USER_ERROR);
  261. }
  262. switch (count($arg_list))
  263. {
  264. case 0: $rt_val = $obj->$methodName();
  265. break;
  266. case 1: $rt_val = $obj->$methodName($arg_list[0]);
  267. break;
  268. case 2: $rt_val = $obj->$methodName($arg_list[0], $arg_list[1]);
  269. break;
  270. case 3: $rt_val = $obj->$methodName($arg_list[0], $arg_list[1], $arg_list[2]);
  271. break;
  272. default: $rt_val = call_user_func_array(array($obj, $methodName), $arg_list);
  273. }
  274. } else
  275. {
  276. $errmsg = BizSystem::getMessage("SYS_ERROR_METHODNOTFOUND", array($objName, $methodName));
  277. trigger_error($errmsg, E_USER_ERROR);
  278. }
  279. } else
  280. {
  281. $errmsg = BizSystem::getMessage("SYS_ERROR_CLASSNOTFOUND", array($objName));
  282. trigger_error($errmsg, E_USER_ERROR);
  283. }
  284. if ($invocationType == "Invoke") // no RPC invoke, page reloaded -> rerender view
  285. {
  286. if (BizSystem::clientProxy()->hasOutput())
  287. BizSystem::clientProxy()->printOutput();
  288. }
  289. else if ($invocationType == "RPCInvoke") // RPC invoke
  290. {
  291. if (BizSystem::clientProxy()->hasOutput())
  292. {
  293. if ($_REQUEST['jsrs'] == 1)
  294. echo "<html><body><form name=\"jsrs_Form\"><textarea name=\"jsrs_Payload\" id=\"jsrs_Payload\">";
  295. BizSystem::clientProxy()->printOutput();
  296. if ($_REQUEST['jsrs'] == 1)
  297. echo "</textarea></form></body></html>";
  298. }
  299. else
  300. return $rt_val;
  301. }
  302. }
  303. }
  304. /**
  305. * Validate the request from client.
  306. *
  307. * @param object $obj the to be called object
  308. * @param string $methodName the to be called method name
  309. * @return boolean
  310. */
  311. protected function validateRequest($obj, $methodName)
  312. {
  313. if (is_a($obj, "EasyForm") || is_a($obj, "BaseForm"))
  314. {
  315. if (!$obj->validateRequest($methodName))
  316. {
  317. return false;
  318. }
  319. return true;
  320. }
  321. return false;
  322. }
  323. /**
  324. * Check whether the request in the form view
  325. *
  326. * @return boolean
  327. */
  328. private function _hasView()
  329. {
  330. return isset($_GET['view']);
  331. }
  332. /**
  333. * Dispatch request to view
  334. */
  335. private function _dispatchView()
  336. {
  337. // ?view=...&form=...&rule=...&mode=...&...
  338. //$getKeys = array_keys($_GET);
  339. //if ($getKeys[0] == "view")
  340. $form = isset($_GET['form']) ? $_GET['form'] : "";
  341. $rule = isset($_GET['rule']) ? $_GET['rule'] : "";
  342. $hist = isset($_GET['hist']) ? $_GET['hist'] : "";
  343. $viewName = $_GET['view'];
  344. $params = $this->_getParameters();
  345. if (defined('NOTFOUND_VIEW'))
  346. {
  347. if (!Resource::getXmlFileWithPath($viewName))
  348. {
  349. $this->renderView(NOTFOUND_VIEW, $form, $rule, $params, $hist);
  350. exit;
  351. }
  352. }
  353. if (!$this->_canUserAccessView($viewName)) //access denied error
  354. $this->renderView($this->_accessDeniedView);
  355. $this->renderView($viewName, $form, $rule, $params, $hist);
  356. }
  357. /**
  358. * Dispatch request as RPC (remote procedure call)
  359. */
  360. private function _dispatchRPC()
  361. {
  362. if ($this->_hasContainerView())
  363. {
  364. BizSystem::instance()->setCurrentViewName($this->_getContainerViewName());
  365. }
  366. $retval = $this->invoke();
  367. print($retval . " "); // why use space on end of data?
  368. exit();
  369. }
  370. /**
  371. * Check: remote procedure has container view?
  372. *
  373. * @see BizController::_hasView()
  374. * @return boolean
  375. */
  376. private function _hasContainerView()
  377. {
  378. return isset($_REQUEST['_thisView']) && !empty($_REQUEST['_thisView']);
  379. }
  380. /**
  381. * Get name of container view that call the remote procedure
  382. *
  383. * @return string name of view
  384. */
  385. private function _getContainerViewName()
  386. {
  387. return $_REQUEST['_thisView'];
  388. }
  389. }
  390. ?>