PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Maco/m-examples/blog/app/myapp.php

http://devj.googlecode.com/
PHP | 453 lines | 300 code | 56 blank | 97 comment | 39 complexity | b21ee84a746cd66659d4620d2f39c0c8 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. class ExampleBlog
  3. {
  4. /**
  5. * ????????
  6. *
  7. * @var array
  8. */
  9. protected $_app_config;
  10. public function __construct(array $app_config)
  11. {
  12. ## IFDEF DEBUG
  13. global $g_boot_time;
  14. MLog::log('--- STARTUP TIME ---' . $g_boot_time, MLog::DEBUG);
  15. ## ENDIF
  16. // ?? magic quotes
  17. set_magic_quotes_runtime(0);
  18. // ??? magic quotes ????????
  19. if (get_magic_quotes_gpc())
  20. {
  21. $in = array(& $_GET, & $_POST, & $_COOKIE, & $_REQUEST);
  22. while (list($k, $v) = each($in))
  23. {
  24. foreach ($v as $key => $val)
  25. {
  26. if (!is_array($val))
  27. {
  28. $in[$k][$key] = stripslashes($val);
  29. continue;
  30. }
  31. $in[] = & $in[$k][$key];
  32. }
  33. }
  34. unset($in);
  35. }
  36. // ????????
  37. set_exception_handler(array($this, 'exception_handler'));
  38. // ???????
  39. $this->_app_config = $app_config;
  40. $this->_initConfig();
  41. M::replaceIni('app_config', $app_config);
  42. /**
  43. * ???????
  44. */
  45. // ???????
  46. date_default_timezone_set(M::getIni('l10n_default_timezone'));
  47. // ?? session ??
  48. if (M::getIni('runtime_session_provider'))
  49. {
  50. M::loadClass(M::getIni('runtime_session_provider'));
  51. }
  52. // ?? session
  53. if (M::getIni('runtime_session_start'))
  54. {
  55. session_start();
  56. // #IFDEF DEBUG
  57. MLog::log('session_start()', MLog::DEBUG);
  58. MLog::log('session_id: ' . session_id(), MLog::DEBUG);
  59. // #ENDIF
  60. }
  61. // ???????
  62. M::import($app_config['APP_DIR']);
  63. M::import($app_config['APP_DIR'] . '/model');
  64. M::import($app_config['MODULE_DIR']);
  65. // ????????
  66. M::register($this, 'app');
  67. }
  68. /**
  69. * ????
  70. */
  71. function __destruct()
  72. {
  73. global $g_boot_time;
  74. $shutdown_time = microtime(true);
  75. $length = $shutdown_time - $g_boot_time;
  76. MLog::log("--- SHUTDOWN TIME --- {$shutdown_time} ({$length})sec", MLog::DEBUG);
  77. }
  78. /**
  79. * ????????
  80. *
  81. * @param array $app_config
  82. * @return unknown
  83. */
  84. static function instance(array $app_config = null)
  85. {
  86. static $instance;
  87. if (is_null($instance))
  88. {
  89. $instance = new ExampleBlog($app_config);
  90. }
  91. return $instance;
  92. }
  93. /**
  94. * ??????????????????????
  95. *
  96. * @param MContext $context
  97. * @param array $args
  98. *
  99. * @return mixed
  100. */
  101. function run(array $args = array())
  102. {
  103. $context = MContext::instance();
  104. $udi = $context->getRequestUDI('array');
  105. #IFDEF DEBUG
  106. MLog::log('REQUEST UDI: ' . $context->UDIString($udi), MLog::DEBUG);
  107. #ENDIF
  108. if (!$this->authorizedUDI($this->currentUserRoles(), $udi))
  109. {
  110. // ????
  111. $response = $this->_on_access_denied();
  112. }
  113. else
  114. {
  115. if (!empty($udi[MContext::UDI_MODULE]))
  116. {
  117. $module_name = $udi[MContext::UDI_MODULE];
  118. $dir = "{$this->_app_config['MODULE_DIR']}/{$module_name}/controller";
  119. $class_name = "{$module_name}_controller_";
  120. }
  121. else
  122. {
  123. $dir = "{$this->_app_config['APP_DIR']}/controller";
  124. $class_name = 'controller_';
  125. }
  126. if (!empty($udi[MContext::UDI_NAMESPACE]))
  127. {
  128. $class_name .= $udi[MContext::UDI_NAMESPACE] . '_';
  129. }
  130. $controller_name = strtolower($udi[MContext::UDI_CONTROLLER]);
  131. $class_name .= $controller_name;
  132. $filename = "{$controller_name}_controller.php";
  133. do
  134. {
  135. try
  136. {
  137. if (!class_exists($class_name, false))
  138. {
  139. M::loadClassFile($filename, array($dir), $class_name);
  140. }
  141. }
  142. catch (MException_FileNotFound $ex)
  143. {
  144. $response = $this->_on_action_not_defined($context);
  145. break;
  146. }
  147. $controller = new $class_name($this);
  148. $action_name = $udi[MContext::UDI_ACTION];
  149. if ($controller->actionExists($action_name))
  150. {
  151. $response = $controller->execute($action_name, $args);
  152. }
  153. else
  154. {
  155. $response = $controller->_on_action_not_defined($action_name);
  156. if (is_null($response))
  157. {
  158. $response = $this->_on_action_not_defined();
  159. }
  160. }
  161. } while (false);
  162. }
  163. if (is_object($response) && method_exists($response, 'execute')){
  164. $response = $response->execute();
  165. }
  166. elseif ($response instanceof MController_Forward)
  167. {
  168. $response = $this->run($response->args);
  169. }
  170. return $response;
  171. }
  172. /**
  173. * ?????
  174. */
  175. protected function _initConfig()
  176. {
  177. #IFDEF DEBUG
  178. MLog::log(__METHOD__, MLog::DEBUG);
  179. #ENDIF
  180. if ($this->_app_config['CONFIG_CACHED'])
  181. {
  182. // ????????
  183. $backend = $this->_app_config['CONFIG_CACHE_BACKEND'];
  184. $settings = isset($this->_app_config['CONFIG_CACHE_SETTINGS'][$backend]) ? $this->_app_config['CONFIG_CACHE_SETTINGS'][$backend] : null;
  185. $cache = new $backend($settings);
  186. // ??????
  187. $cache_id = $this->_app_config['APPID'] . '_app_config';
  188. $config = $cache->get($cache_id);
  189. if (!empty($config))
  190. {
  191. M::replaceIni($config);
  192. return;
  193. }
  194. }
  195. $config = $this->_loadConfigFiles();
  196. if ($this->_app_config['CONFIG_CACHED'])
  197. {
  198. $cache->set($cache_id, $config);
  199. }
  200. M::replaceIni($config);
  201. }
  202. /**
  203. * ????? session ??????
  204. *
  205. * @return array
  206. */
  207. function currentUser()
  208. {
  209. $key = M::getIni('acl_session_key');
  210. return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
  211. }
  212. /**
  213. * ?? session ??????????
  214. *
  215. * @return array
  216. */
  217. function currentUserRoles()
  218. {
  219. $user = $this->currentUser();
  220. return isset($_SESSION[$user['roles']]) ? M::normalize($user['roles']): array();
  221. }
  222. /**
  223. * ? session ???????
  224. */
  225. function unsetUser()
  226. {
  227. unset($_SESSION[M::getIni('acl_session_key')]);
  228. }
  229. /**
  230. * ??????????????????????
  231. *
  232. * @param array $roles
  233. * @param string|array $udi
  234. *
  235. * @return boolean
  236. */
  237. function authorizedUDI($roles, $udi)
  238. {
  239. $roles = M::normalize($roles);
  240. /**
  241. * ? UDI ???????
  242. * ?????? ACL????????
  243. * ?? MACL ????????
  244. */
  245. if (!is_array($udi))
  246. {
  247. $udi = MContext::instance()->UDIString($udi);
  248. }
  249. $controller_acl = $this->controllerACL($udi);
  250. // ????
  251. $acl = M::singleton('MACL');
  252. $action_name = strtolower($udi[MContext::UDI_ACTION]);
  253. if (isset($controller_acl['actions'][$action_name]))
  254. {
  255. // ????? ACL ??
  256. return $acl->checkBasedRoles($roles, $controller_acl['actions'][$action_name]);
  257. }
  258. if (isset($controller_acl['actions'][MACL::ALL_ACTIONS]))
  259. {
  260. // ??? ACL
  261. return $acl->checkBasedRoles($roles, $controller_acl['actions'][MACL::ALL_ACTIONS]);
  262. }
  263. // ??????????????
  264. return $acl->checkBasedRoles($roles, $controller_acl);
  265. }
  266. /**
  267. * ???????? ACL
  268. *
  269. * @param string|array $udi
  270. *
  271. * @return array
  272. */
  273. function controllerACL($udi)
  274. {
  275. if (!is_array($udi))
  276. {
  277. $udi = MContext::instance()->UDIArray($udi);
  278. }
  279. $path = 'acl_global';
  280. if ($udi[MContext::UDI_MODULE])
  281. {
  282. $path .= '/' . $udi[MContext::UDI_MODULE];
  283. }
  284. if ($udi[MContext::UDI_NAMESPACE])
  285. {
  286. $path .= '/' . $udi[MContext::UDI_NAMESPACE];
  287. }
  288. $acl = M::getIni("{$path}/{$udi[MContext::UDI_CONTROLLER]}");
  289. if (is_array($acl))
  290. {
  291. return $acl;
  292. }
  293. else
  294. {
  295. // ????????? ACL????? ALL_CONTROLLERS ?????
  296. $acl = M::getIni($path . '/' . MACL::ALL_CONTROLLERS);
  297. if (is_array($acl))
  298. {
  299. return $acl;
  300. }
  301. // ????? ACL
  302. return M::getIni('acl_default');
  303. }
  304. }
  305. /**
  306. * ??????
  307. *
  308. * @return array
  309. */
  310. protected function _loadConfigFiles()
  311. {
  312. $extname = !empty($this->_app_config['CONFIG_FILE_EXTNAME']) ? $this->_app_config['CONFIG_FILE_EXTNAME'] : 'php';
  313. $config_dir = $this->_app_config['CONFIG_DIR'];
  314. $run_mode = strtolower($this->_app_config['RUN_MODE']);
  315. $files = array
  316. (
  317. "{$config_dir}/environment.{$extname}" => 'global',
  318. "{$config_dir}/database.{$extname}" => 'db_dsn_pool',
  319. "{$config_dir}/acl.{$extname}" => 'acl_global',
  320. "{$config_dir}/environments/{$run_mode}.{$extname}" => 'global',
  321. "{$config_dir}/app.{$extname}" => 'appini',
  322. "{$config_dir}/routes.{$extname}" => 'routes',
  323. );
  324. $replace = array();
  325. foreach ($this->_app_config as $key => $value)
  326. {
  327. if (!is_array($value))
  328. {
  329. $replace["%{$key}%"] = $value;
  330. }
  331. }
  332. $config = require(M_DIR . '/_config/default_config.php');
  333. foreach ($files as $filename => $scope)
  334. {
  335. if (!file_exists($filename)) continue;
  336. $contents = $this->_parse(include($filename), $replace);
  337. if ($scope == 'global')
  338. {
  339. $config = array_merge($config, $contents);
  340. }
  341. else
  342. {
  343. if (!isset($config[$scope]))
  344. {
  345. $config[$scope] = array();
  346. }
  347. $config[$scope] = array_merge($config[$scope], $contents);
  348. }
  349. }
  350. if (!empty($config['db_dsn_pool'][$run_mode]))
  351. {
  352. $config['db_dsn_pool']['default'] = $config['db_dsn_pool'][$run_mode];
  353. }
  354. return $config;
  355. }
  356. // ?????? ?? %XXX% ????
  357. protected function _parse($input, array $replace = null)
  358. {
  359. if (!is_array($input))
  360. {
  361. $input = array();
  362. }
  363. if (!empty($replace))
  364. {
  365. array_walk_recursive($input, array(__CLASS__, '_replace'), $replace);
  366. }
  367. return $input;
  368. }
  369. // ? %% ?????????
  370. static function _replace(&$string, $key, array $replace)
  371. {
  372. foreach ($replace as $macro => $value)
  373. {
  374. if (strpos($string, $macro) !== false)
  375. {
  376. $string = str_replace($macro, $value, $string);
  377. break;
  378. }
  379. }
  380. }
  381. // ????
  382. protected function _on_access_denied()
  383. {
  384. require $this->_app_config['ROOT_DIR'] . '/public/403.php';
  385. }
  386. // ?????
  387. protected function _on_action_not_defined()
  388. {
  389. MDebug::dump(MContext::instance()->getRequestUDI());
  390. require $this->_app_config['ROOT_DIR'] . '/public/404.php';
  391. }
  392. // ??
  393. function exception_handler(Exception $ex)
  394. {
  395. MException::dump($ex);
  396. }
  397. }