/Maco/m-examples/blog/app/myapp.php
PHP | 453 lines | 300 code | 56 blank | 97 comment | 39 complexity | b21ee84a746cd66659d4620d2f39c0c8 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
- <?php
-
- class ExampleBlog
- {
- /**
- * ????????
- *
- * @var array
- */
- protected $_app_config;
-
- public function __construct(array $app_config)
- {
- ## IFDEF DEBUG
- global $g_boot_time;
- MLog::log('--- STARTUP TIME ---' . $g_boot_time, MLog::DEBUG);
- ## ENDIF
-
- // ?? magic quotes
- set_magic_quotes_runtime(0);
-
- // ??? magic quotes ????????
- if (get_magic_quotes_gpc())
- {
- $in = array(& $_GET, & $_POST, & $_COOKIE, & $_REQUEST);
- while (list($k, $v) = each($in))
- {
- foreach ($v as $key => $val)
- {
- if (!is_array($val))
- {
- $in[$k][$key] = stripslashes($val);
- continue;
- }
- $in[] = & $in[$k][$key];
- }
- }
- unset($in);
- }
-
- // ????????
- set_exception_handler(array($this, 'exception_handler'));
-
- // ???????
- $this->_app_config = $app_config;
- $this->_initConfig();
- M::replaceIni('app_config', $app_config);
-
- /**
- * ???????
- */
- // ???????
- date_default_timezone_set(M::getIni('l10n_default_timezone'));
-
- // ?? session ??
- if (M::getIni('runtime_session_provider'))
- {
- M::loadClass(M::getIni('runtime_session_provider'));
- }
-
- // ?? session
- if (M::getIni('runtime_session_start'))
- {
- session_start();
- // #IFDEF DEBUG
- MLog::log('session_start()', MLog::DEBUG);
- MLog::log('session_id: ' . session_id(), MLog::DEBUG);
- // #ENDIF
- }
-
- // ???????
- M::import($app_config['APP_DIR']);
- M::import($app_config['APP_DIR'] . '/model');
- M::import($app_config['MODULE_DIR']);
-
- // ????????
- M::register($this, 'app');
- }
-
- /**
- * ????
- */
- function __destruct()
- {
- global $g_boot_time;
- $shutdown_time = microtime(true);
- $length = $shutdown_time - $g_boot_time;
- MLog::log("--- SHUTDOWN TIME --- {$shutdown_time} ({$length})sec", MLog::DEBUG);
- }
-
- /**
- * ????????
- *
- * @param array $app_config
- * @return unknown
- */
- static function instance(array $app_config = null)
- {
- static $instance;
- if (is_null($instance))
- {
- $instance = new ExampleBlog($app_config);
- }
- return $instance;
- }
-
- /**
- * ??????????????????????
- *
- * @param MContext $context
- * @param array $args
- *
- * @return mixed
- */
- function run(array $args = array())
- {
- $context = MContext::instance();
- $udi = $context->getRequestUDI('array');
-
- #IFDEF DEBUG
- MLog::log('REQUEST UDI: ' . $context->UDIString($udi), MLog::DEBUG);
- #ENDIF
-
- if (!$this->authorizedUDI($this->currentUserRoles(), $udi))
- {
- // ????
- $response = $this->_on_access_denied();
- }
- else
- {
- if (!empty($udi[MContext::UDI_MODULE]))
- {
- $module_name = $udi[MContext::UDI_MODULE];
- $dir = "{$this->_app_config['MODULE_DIR']}/{$module_name}/controller";
- $class_name = "{$module_name}_controller_";
- }
- else
- {
- $dir = "{$this->_app_config['APP_DIR']}/controller";
- $class_name = 'controller_';
- }
-
- if (!empty($udi[MContext::UDI_NAMESPACE]))
- {
- $class_name .= $udi[MContext::UDI_NAMESPACE] . '_';
- }
- $controller_name = strtolower($udi[MContext::UDI_CONTROLLER]);
- $class_name .= $controller_name;
- $filename = "{$controller_name}_controller.php";
-
- do
- {
- try
- {
- if (!class_exists($class_name, false))
- {
- M::loadClassFile($filename, array($dir), $class_name);
- }
- }
- catch (MException_FileNotFound $ex)
- {
- $response = $this->_on_action_not_defined($context);
- break;
- }
-
- $controller = new $class_name($this);
- $action_name = $udi[MContext::UDI_ACTION];
- if ($controller->actionExists($action_name))
- {
- $response = $controller->execute($action_name, $args);
- }
- else
- {
- $response = $controller->_on_action_not_defined($action_name);
- if (is_null($response))
- {
- $response = $this->_on_action_not_defined();
- }
- }
- } while (false);
- }
-
- if (is_object($response) && method_exists($response, 'execute')){
- $response = $response->execute();
- }
- elseif ($response instanceof MController_Forward)
- {
- $response = $this->run($response->args);
- }
-
- return $response;
- }
-
- /**
- * ?????
- */
- protected function _initConfig()
- {
- #IFDEF DEBUG
- MLog::log(__METHOD__, MLog::DEBUG);
- #ENDIF
-
- if ($this->_app_config['CONFIG_CACHED'])
- {
- // ????????
- $backend = $this->_app_config['CONFIG_CACHE_BACKEND'];
-
- $settings = isset($this->_app_config['CONFIG_CACHE_SETTINGS'][$backend]) ? $this->_app_config['CONFIG_CACHE_SETTINGS'][$backend] : null;
- $cache = new $backend($settings);
-
- // ??????
- $cache_id = $this->_app_config['APPID'] . '_app_config';
- $config = $cache->get($cache_id);
-
- if (!empty($config))
- {
- M::replaceIni($config);
- return;
- }
- }
-
- $config = $this->_loadConfigFiles();
-
- if ($this->_app_config['CONFIG_CACHED'])
- {
- $cache->set($cache_id, $config);
- }
-
- M::replaceIni($config);
- }
-
- /**
- * ????? session ??????
- *
- * @return array
- */
- function currentUser()
- {
- $key = M::getIni('acl_session_key');
- return isset($_SESSION[$key]) ? $_SESSION[$key] : null;
- }
-
- /**
- * ?? session ??????????
- *
- * @return array
- */
- function currentUserRoles()
- {
- $user = $this->currentUser();
- return isset($_SESSION[$user['roles']]) ? M::normalize($user['roles']): array();
- }
-
- /**
- * ? session ???????
- */
- function unsetUser()
- {
- unset($_SESSION[M::getIni('acl_session_key')]);
- }
-
- /**
- * ??????????????????????
- *
- * @param array $roles
- * @param string|array $udi
- *
- * @return boolean
- */
- function authorizedUDI($roles, $udi)
- {
- $roles = M::normalize($roles);
- /**
- * ? UDI ???????
- * ?????? ACL????????
- * ?? MACL ????????
- */
- if (!is_array($udi))
- {
- $udi = MContext::instance()->UDIString($udi);
- }
-
- $controller_acl = $this->controllerACL($udi);
-
- // ????
- $acl = M::singleton('MACL');
- $action_name = strtolower($udi[MContext::UDI_ACTION]);
- if (isset($controller_acl['actions'][$action_name]))
- {
- // ????? ACL ??
- return $acl->checkBasedRoles($roles, $controller_acl['actions'][$action_name]);
- }
-
- if (isset($controller_acl['actions'][MACL::ALL_ACTIONS]))
- {
- // ??? ACL
- return $acl->checkBasedRoles($roles, $controller_acl['actions'][MACL::ALL_ACTIONS]);
- }
-
- // ??????????????
- return $acl->checkBasedRoles($roles, $controller_acl);
- }
-
- /**
- * ???????? ACL
- *
- * @param string|array $udi
- *
- * @return array
- */
- function controllerACL($udi)
- {
- if (!is_array($udi))
- {
- $udi = MContext::instance()->UDIArray($udi);
- }
-
- $path = 'acl_global';
- if ($udi[MContext::UDI_MODULE])
- {
- $path .= '/' . $udi[MContext::UDI_MODULE];
- }
-
- if ($udi[MContext::UDI_NAMESPACE])
- {
- $path .= '/' . $udi[MContext::UDI_NAMESPACE];
- }
-
- $acl = M::getIni("{$path}/{$udi[MContext::UDI_CONTROLLER]}");
-
- if (is_array($acl))
- {
- return $acl;
- }
- else
- {
- // ????????? ACL????? ALL_CONTROLLERS ?????
- $acl = M::getIni($path . '/' . MACL::ALL_CONTROLLERS);
- if (is_array($acl))
- {
- return $acl;
- }
-
- // ????? ACL
- return M::getIni('acl_default');
- }
- }
-
- /**
- * ??????
- *
- * @return array
- */
- protected function _loadConfigFiles()
- {
- $extname = !empty($this->_app_config['CONFIG_FILE_EXTNAME']) ? $this->_app_config['CONFIG_FILE_EXTNAME'] : 'php';
- $config_dir = $this->_app_config['CONFIG_DIR'];
- $run_mode = strtolower($this->_app_config['RUN_MODE']);
-
- $files = array
- (
- "{$config_dir}/environment.{$extname}" => 'global',
- "{$config_dir}/database.{$extname}" => 'db_dsn_pool',
- "{$config_dir}/acl.{$extname}" => 'acl_global',
- "{$config_dir}/environments/{$run_mode}.{$extname}" => 'global',
- "{$config_dir}/app.{$extname}" => 'appini',
- "{$config_dir}/routes.{$extname}" => 'routes',
- );
-
- $replace = array();
- foreach ($this->_app_config as $key => $value)
- {
- if (!is_array($value))
- {
- $replace["%{$key}%"] = $value;
- }
- }
-
- $config = require(M_DIR . '/_config/default_config.php');
- foreach ($files as $filename => $scope)
- {
- if (!file_exists($filename)) continue;
- $contents = $this->_parse(include($filename), $replace);
- if ($scope == 'global')
- {
- $config = array_merge($config, $contents);
- }
- else
- {
- if (!isset($config[$scope]))
- {
- $config[$scope] = array();
- }
- $config[$scope] = array_merge($config[$scope], $contents);
- }
- }
-
- if (!empty($config['db_dsn_pool'][$run_mode]))
- {
- $config['db_dsn_pool']['default'] = $config['db_dsn_pool'][$run_mode];
- }
-
- return $config;
- }
-
- // ?????? ?? %XXX% ????
- protected function _parse($input, array $replace = null)
- {
- if (!is_array($input))
- {
- $input = array();
- }
-
- if (!empty($replace))
- {
- array_walk_recursive($input, array(__CLASS__, '_replace'), $replace);
- }
-
- return $input;
- }
-
- // ? %% ?????????
- static function _replace(&$string, $key, array $replace)
- {
- foreach ($replace as $macro => $value)
- {
- if (strpos($string, $macro) !== false)
- {
- $string = str_replace($macro, $value, $string);
- break;
- }
- }
- }
-
- // ????
- protected function _on_access_denied()
- {
- require $this->_app_config['ROOT_DIR'] . '/public/403.php';
- }
-
- // ?????
- protected function _on_action_not_defined()
- {
- MDebug::dump(MContext::instance()->getRequestUDI());
- require $this->_app_config['ROOT_DIR'] . '/public/404.php';
- }
-
- // ??
- function exception_handler(Exception $ex)
- {
- MException::dump($ex);
- }
- }