/ipserver/FLEA/FLEA.php
PHP | 2031 lines | 1090 code | 158 blank | 783 comment | 274 complexity | 46fe5430720fb8971ef86a457cce6065 MD5 | raw file
- <?php
- /////////////////////////////////////////////////////////////////////////////
- // FleaPHP Framework
- //
- // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
- //
- // ??????????????? LICENSE.txt ???
- // ???? http://www.fleaphp.org/ ???????
- /////////////////////////////////////////////////////////////////////////////
-
- /**
- * ?? FLEA ??????????? FleaPHP ????
- *
- * ????? FleaPHP ???????????? FleaPHP ???
- * ??????????? require('FLEA.php') ??????
- * ???? FleaPHP ???????????
- *
- * @copyright Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
- * @author ???? (www.qeeyuan.com)
- * @package Core
- * @version $Id: FLEA.php,v 1.1 2008/09/10 03:00:15 jiang Exp $
- */
-
- /**
- * ?????????
- */
- global $___fleaphp_loaded_time;
- $___fleaphp_loaded_time = microtime();
-
- /**
- * ?????????
- */
-
- // ?? FleaPHP ?????
- define('FLEA_VERSION', '1.0.70.1017');
-
- // ???? PHP4 ? PHP5 ???
- if (substr(PHP_VERSION, 0, 1) == '5') {
- define('PHP5', true);
- define('PHP4', false);
- } else {
- define('PHP5', false);
- define('PHP4', true);
- }
-
- // ??? DIRECTORY_SEPARATOR
- define('DS', DIRECTORY_SEPARATOR);
-
- // ?? URL ??
- define('URL_STANDARD', 'URL_STANDARD');
-
- // PATHINFO ??
- define('URL_PATHINFO', 'URL_PATHINFO');
-
- // URL ????
- define('URL_REWRITE', 'URL_REWRITE');
-
- /**#@+
- * ?? RBAC ??????
- */
- // RBAC_EVERYONE ?????????????????????
- define('RBAC_EVERYONE', 'RBAC_EVERYONE');
-
- // RBAC_HAS_ROLE ???????????
- define('RBAC_HAS_ROLE', 'RBAC_HAS_ROLE');
-
- // RBAC_NO_ROLE ????????????
- define('RBAC_NO_ROLE', 'RBAC_NO_ROLE');
-
- // RBAC_NULL ????????
- define('RBAC_NULL', 'RBAC_NULL');
-
- // ACTION_ALL ???????????
- define('ACTION_ALL', 'ACTION_ALL');
- /**#@-*/
-
- /**
- * ??? FleaPHP ??
- */
- define('G_FLEA_VAR', '__FLEA_CORE__');
- $GLOBALS[G_FLEA_VAR] = array(
- 'APP_INF' => array(),
- 'OBJECTS' => array(),
- 'DBO' => array(),
- 'CLASS_PATH' => array(),
- 'FLEA_EXCEPTION_STACK' => array(),
- 'FLEA_EXCEPTION_HANDLER'=> null,
- );
-
- // ?? FleaPHP ???????????? CLASS_PATH
- $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][] = dirname(__FILE__);
- define('FLEA_DIR', $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][0] . DS . 'FLEA');
- define('FLEA_3RD_DIR', $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][0] . DS . '3rd');
-
- // ????? FleaPHP ????????
- if (!defined('NO_LEGACY_FLEAPHP') || NO_LEGACY_FLEAPHP == false) {
- require(FLEA_DIR . '/Compatibility.php');
- }
-
- /**
- * ????????
- *
- * ?????? DEPLOY_MODE ??? true??????????? FleaPHP
- */
- if (!defined('DEPLOY_MODE') || DEPLOY_MODE != true) {
- $GLOBALS[G_FLEA_VAR]['APP_INF'] = require(FLEA_DIR . '/Config/DEBUG_MODE_CONFIG.php');
- define('DEBUG_MODE', true);
- if (!defined('DEPLOY_MODE')) { define('DEPLOY_MODE', false); }
- } else {
- $GLOBALS[G_FLEA_VAR]['APP_INF'] = require(FLEA_DIR . '/Config/DEPLOY_MODE_CONFIG.php');
- define('DEBUG_MODE', false);
- if (!defined('DEPLOY_MODE')) { define('DEPLOY_MODE', true); }
- }
-
- // ??? PHP5 ???????????
- if (!defined('E_STRICT')) {
- define('E_STRICT', 2048);
- }
- if (DEBUG_MODE) {
- error_reporting(error_reporting(0) & ~E_STRICT);
- } else {
- error_reporting(0);
- }
-
- // ????????
- __SET_EXCEPTION_HANDLER('__FLEA_EXCEPTION_HANDLER');
-
- /**
- * FLEA ???? FleaPHP ???????
- *
- * ??????????????
- *
- * @package Core
- * @author ???? (www.qeeyuan.com)
- * @version 1.0
- */
- class FLEA
- {
- /**
- * ????????
- *
- * example:
- * <code>
- * FLEA::loadAppInf('./config/MyConfig.php');
- * </code>
- *
- * @param mixed $__config ??????????
- */
- function loadAppInf($__flea_internal_config = null)
- {
- if (!is_array($__flea_internal_config) && is_string($__flea_internal_config)) {
- if (!is_readable($__flea_internal_config)) {
- FLEA::loadClass('FLEA_Exception_ExpectedFile');
- return __THROW(new FLEA_Exception_ExpectedFile($__flea_internal_config));
- }
- $__flea_internal_config = require($__flea_internal_config);
- }
- if (is_array($__flea_internal_config)) {
- $GLOBALS[G_FLEA_VAR]['APP_INF'] = array_merge($GLOBALS[G_FLEA_VAR]['APP_INF'], $__flea_internal_config);
- }
- return null;
- }
-
- /**
- * ??????????
- *
- * example:
- * <code>
- * FLEA::setAppInf('siteTitle');
- * .....
- * $siteTitle = FLEA::getAppInf('siteTitle');
- * </code>
- *
- * @param string $option
- * @param mixed $default
- *
- * @return mixed
- */
- function getAppInf($option, $default = null)
- {
- return isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option]) ? $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] : $default;
- }
-
- /**
- * ?????????????????????????
- *
- * example:
- * <code>
- * $arr = array('min' => 1, 'max' => 100, 'step' => 2);
- * FLEA::setAppInf('rule', $arr);
- * .....
- * $min = FLEA::getAppInfValue('rule', 'min');
- * </code>
- *
- * @param string $option
- * @param string $keyname
- * @param mixed $default
- *
- * @return mixed
- */
- function getAppInfValue($option, $keyname, $default = null)
- {
- if (!isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option])) {
- $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] = array();
- }
- if (array_key_exists($keyname, $GLOBALS[G_FLEA_VAR]['APP_INF'][$option])) {
- return $GLOBALS[G_FLEA_VAR]['APP_INF'][$option][$keyname];
- } else {
- $GLOBALS[G_FLEA_VAR]['APP_INF'][$option][$keyname] = $default;
- return $default;
- }
- }
-
- /**
- * ??????????????????????????
- *
- * @param string $option
- * @param string $keyname
- * @param mixed $value
- */
- function setAppInfValue($option, $keyname, $value)
- {
- if (!isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option])) {
- $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] = array();
- }
- $GLOBALS[G_FLEA_VAR]['APP_INF'][$option][$keyname] = $value;
- }
-
- /**
- * ?????
- *
- * @param string $option
- * @param mixed $data
- */
- function setAppInf($option, $data = null)
- {
- if (is_array($option)) {
- $GLOBALS[G_FLEA_VAR]['APP_INF'] = array_merge($GLOBALS[G_FLEA_VAR]['APP_INF'], $option);
- } else {
- $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] = $data;
- }
- }
-
- /**
- * ????????
- *
- * FLEA::loadClass()?FLEA::getSingleton() ????????????????????
- * ?????? FLEA::import() ????????????????????
- * ?????????????????????????????????????????????
- *
- * ?????? Table_Posts???????? ./APP/Table/Posts.php?
- * ??????????? ./APP???? ./APP/Table ?
- *
- * example:
- * <code>
- * FLEA::import(APP_DIR . '/LIBS');
- * </code>
- *
- * @param string $dir
- */
- function import($dir)
- {
- if (array_search($dir, $GLOBALS[G_FLEA_VAR]['CLASS_PATH'], true)) { return; }
- if (DIRECTORY_SEPARATOR == '/') {
- $dir = str_replace('\\', DIRECTORY_SEPARATOR, $dir);
- } else {
- $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
- }
- $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][] = $dir;
- }
-
- /**
- * ???????
- *
- * FLEA::loadFile() ? $filename ???? _ ?????????
- *
- * example:
- * <code>
- * FLEA::loadFile('Table_Posts.php');
- * // ??? include 'Table/Posts.php';
- * </code>
- *
- * @param string $className
- * @param boolean $loadOnce ??? true ??FLEA::loadFile() ??? require_once
- *
- * @return boolean
- */
- function loadFile($filename, $loadOnce = false)
- {
- static $is_loaded = array();
-
- $path = FLEA::getFilePath($filename);
- if ($path != '') {
- if (isset($is_loaded[$path]) && $loadOnce) { return true; }
- $is_loaded[$path] = true;
- if ($loadOnce) {
- return require_once($path);
- } else {
- return require($path);
- }
- }
-
- FLEA::loadClass('FLEA_Exception_ExpectedFile');
- __THROW(new FLEA_Exception_ExpectedFile($filename));
- return false;
- }
-
- /**
- * ??????????
- *
- * ????? _ ??????????????????????????
- *
- * example:
- * <code>
- * FLEA::loadClass('Table_Posts');
- * // ?????? Table_Posts ?????? Table/Posts.php
- * // ?????????? Table/Posts.php ??
- * </code>
- *
- * @param string $filename
- * @param boolean $noException ??? true????????????????
- *
- * @return boolean
- */
- function loadClass($className, $noException = false)
- {
- if (PHP5) {
- if (class_exists($className, false) || interface_exists($className, false)) { return true; }
- } else {
- if (class_exists($className)) { return true; }
- }
-
- if (preg_match('/[^a-z0-9\-_.]/i', $className) === 0) {
- $filename = FLEA::getFilePath($className . '.php');
- if ($filename) {
- require($filename);
- if (PHP5) {
- if (class_exists($className, false) || interface_exists($className, false)) { return true; }
- } else {
- if (class_exists($className)) { return true; }
- }
- }
- }
-
- if ($noException) { return false; }
-
- $filename = FLEA::getFilePath($className . '.php', true);
- require_once(FLEA_DIR . '/Exception/ExpectedClass.php');
- __THROW(new FLEA_Exception_ExpectedClass($className, $filename, file_exists($filename)));
- return false;
- }
-
- /**
- * ?? FleaPHP ??????????
- *
- * FleaPHP ????????????_?????????
- *
- * @param string $filename
- * @param boolean $return ??????????????????????????
- *
- * @return string
- */
- function getFilePath($filename, $return = false)
- {
- $filename = str_replace('_', DIRECTORY_SEPARATOR, $filename);
- if (DIRECTORY_SEPARATOR == '/') {
- $filename = str_replace('\\', DIRECTORY_SEPARATOR, $filename);
- } else {
- $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
- }
-
- if (strtolower(substr($filename, -4)) != '.php') {
- $filename .= '.php';
- }
-
- // ????????
- if (is_file($filename)) { return $filename; }
-
- foreach ($GLOBALS[G_FLEA_VAR]['CLASS_PATH'] as $classdir) {
- $path = $classdir . DIRECTORY_SEPARATOR . $filename;
- if (is_file($path)) { return $path; }
- }
-
- if ($return) { return $filename; }
- return false;
- }
-
- /**
- * ????????????
- *
- * example:
- * <code>
- * $obj =& FLEA::getSingleton('Table_Posts);
- * ......
- * $obj2 =& FLEA::getSingleton('Table_Posts);
- * // ?????????????????
- * echo $obj === $obj2 ? 'Equals' : 'Not equals';
- * </code>
- *
- * @param string $className
- *
- * @return object
- */
- function & getSingleton($className)
- {
- static $instances = array();
- if (FLEA::isRegistered($className)) {
- // ???????????
- return FLEA::registry($className);
- }
- if (PHP5) {
- $classExists = class_exists($className, false);
- } else {
- $classExists = class_exists($className);
- }
- if (!$classExists) {
- if (!FLEA::loadClass($className)) {
- $return = false;
- return $return;
- }
- }
-
- $instances[$className] =& new $className();
- FLEA::register($instances[$className], $className);
- return $instances[$className];
- }
-
- /**
- * ???????????????????????
- *
- * example:
- * <code>
- * $obj =& new MyClass();
- * // ????????
- * FLEA::register($obj, 'MyClass');
- * .....
- * // ??????????
- * $obj2 =& FLEA::registry('MyClass');
- * // ??????????
- * echo $obj === $obj2 ? 'Equals' : 'Not equals';
- * </code>
- *
- * @param object $obj
- * @param string $name
- *
- * @return object
- */
- function & register(& $obj, $name = null)
- {
- if (!is_object($obj)) {
- FLEA::loadClass('FLEA_Exception_TypeMismatch');
- return __THROW(new FLEA_Exception_TypeMismatch($obj, 'object', gettype($obj)));
- }
-
- if (is_null($name)) {
- $name = get_class($obj);
- }
-
- if (isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name])) {
- FLEA::loadClass('FLEA_Exception_ExistsKeyName');
- return __THROW(new FLEA_Exception_ExistsKeyName($name));
- } else {
- $GLOBALS[G_FLEA_VAR]['OBJECTS'][$name] =& $obj;
- return $obj;
- }
- }
-
- /**
- * ????????????????????????????????????????
- *
- * example:?? FLEA::register()
- *
- * @param string $name
- *
- * @return object
- */
- function & registry($name = null)
- {
- if (is_null($name)) {
- return $GLOBALS[G_FLEA_VAR]['OBJECTS'];
- }
- if (isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name]) && is_object($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name])) {
- return $GLOBALS[G_FLEA_VAR]['OBJECTS'][$name];
- }
- FLEA::loadClass('FLEA_Exception_NotExistsKeyName');
- return __THROW(new FLEA_Exception_NotExistsKeyName($name));
- }
-
- /**
- * ???????????????
- *
- * example:
- * <code>
- * if (FLEA::isRegistered('MyClass')) {
- * $obj =& FLEA::registry('MyClass');
- * } else {
- * $obj =& new MyClass();
- * }
- * </code>
- *
- * @param string $name
- *
- * @return boolean
- */
- function isRegistered($name)
- {
- return isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name]);
- }
-
-
- /**
- * ?????????????????????????? false
- *
- * example:
- * <code>
- * $cacheId = 'my_cache_id';
- * if (!($data = FLEA::getCache($cacheId))) {
- * $data = 'Data';
- * FLEA::writeCache($cacheId, $data);
- * }
- * </code>
- *
- * ?? $cacheIdIsFilename ??? true??????????? $cacheId ????????
- * ???????????? $cacheIdIsFilename ????? true?
- *
- * $time ??????????????????????????????????????????????????????
- *
- * ?? $timeIsLifetime ? false?? $time ????????????????????????
- * ?? $time ??????????????????????????????
- *
- * @param string $cacheId ??ID???????????????ID
- * @param int $time ?????????????
- * @param boolean $timeIsLifetime ?? $time ?????
- * @param boolean $cacheIdIsFilename ????? $cacheId ?????
- *
- * @return mixed ??????????????????? false
- */
- function getCache($cacheId, $time = 900, $timeIsLifetime = true, $cacheIdIsFilename = false)
- {
- $cacheDir = FLEA::getAppInf('internalCacheDir');
- if (is_null($cacheDir)) {
- FLEA::loadClass('FLEA_Exception_CacheDisabled');
- __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
- return false;
- }
-
- if ($cacheIdIsFilename) {
- $cacheFile = $cacheDir . DS . preg_replace('/[^a-z0-9\-_]/i', '_', $cacheId);
- } else {
- $cacheFile = $cacheDir . DS . md5($cacheId);
- }
- if (!file_exists($cacheFile)) { return false; }
-
- if ($timeIsLifetime && $time == -1) {
- $data = safe_file_get_contents($cacheFile);
- return $data !== false ? unserialize($data) : false;
- }
-
- $filetime = filemtime($cacheFile);
- if ($timeIsLifetime) {
- if (time() >= $filetime + $time) { return false; }
- } else {
- if ($time >= $filetime) { return false; }
- }
- $data = safe_file_get_contents($cacheFile);
- return $data !== false ? unserialize($data) : false;
- }
-
- /**
- * ?????????
- *
- * example:
- * <code>
- * $data = .....; // ????????????????
- * // cache id ???????????????????????
- * $cacheId = 'data_cahce_1';
- * FLEA::writeCache($cacheId, $data);
- * </code>
- *
- * @param string $cacheId
- * @param mixed $data
- * @param boolean $cacheIdIsFilename
- *
- * @return boolean
- */
- function writeCache($cacheId, $data, $cacheIdIsFilename = false)
- {
- $cacheDir = FLEA::getAppInf('internalCacheDir');
- if (is_null($cacheDir)) {
- FLEA::loadClass('FLEA_Exception_CacheDisabled');
- __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
- return false;
- }
-
- if ($cacheIdIsFilename) {
- $cacheFile = $cacheDir . DS . preg_replace('/[^a-z0-9\-_]/i', '_', $cacheId);
- } else {
- $cacheFile = $cacheDir . DS . md5($cacheId);
- }
- if (!safe_file_put_contents($cacheFile, serialize($data))) {
- FLEA::loadClass('FLEA_Exception_CacheDisabled');
- __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
- return false;
- } else {
- return true;
- }
- }
-
- /**
- * ?????????
- *
- * @param string $cacheId
- * @param boolean $cacheIdIsFilename
- *
- * @return boolean
- */
- function purgeCache($cacheId, $cacheIdIsFilename = false)
- {
- $cacheDir = FLEA::getAppInf('internalCacheDir');
- if (is_null($cacheDir)) {
- FLEA::loadClass('FLEA_Exception_CacheDisabled');
- __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
- return false;
- }
-
- if ($cacheIdIsFilename) {
- $cacheFile = $cacheDir . DS . preg_replace('/[^a-z0-9\-_]/i', '_', $cacheId);
- } else {
- $cacheFile = $cacheDir . DS . md5($cacheId);
- }
-
- if (file_exists($cacheFile)) {
- return unlink($cacheFile);
- }
- return true;
- }
-
-
- /**
- * ??? WebControls??? FLEA_WebControls ????
- *
- * ?????????? webControlsClassName?????? WebControls ??
- *
- * @return FLEA_WebControls
- */
- function & initWebControls()
- {
- return FLEA::getSingleton(FLEA::getAppInf('webControlsClassName'));
- }
-
- /**
- * ??? Ajax??? FLEA_Ajax ????
- *
- * ?????????? ajaxClassName?????? Ajax ??
- *
- * @return FLEA_Ajax
- */
- function & initAjax()
- {
- return FLEA::getSingleton(FLEA::getAppInf('ajaxClassName'));
- }
-
- /**
- * ??????
- *
- * ???????????????????? helper. ???
- * ?? helper.array ??? FLEA_Helper_Array?helper.image ??? FLEA_Helper_Image?
- *
- * @param string $helperName
- */
- function loadHelper($helperName)
- {
- $settingName = 'helper.' . strtolower($helperName);
- $setting = FLEA::getAppInf($settingName);
- if ($setting) {
- return FLEA::loadFile($setting, true);
- } else {
- FLEA::loadClass('FLEA_Exception_NotExistsKeyName');
- return __THROW(new FLEA_Exception_NotExistsKeyName('helper.' . $helperName));
- }
- }
-
- /**
- * ???????????
- *
- * ????? $dsn ????? $dsn ??? 0????????? dbDSN ? DSN ???
- *
- * DSN ? Database Source Name ???????????????
- * ? FleaPHP ??DSN ???????????????????????????????????
- *
- * DSN ??????
- *
- * example:
- * <code>
- * $dsn = array(
- * 'driver' => 'mysql',
- * 'host' => 'localhost',
- * 'login' => 'username',
- * 'password' => 'password',
- * 'database' => 'test_db',
- * 'charset' => 'utf8',
- * );
- *
- * $dbo =& FLEA::getDBO($dsn);
- * </code>
- *
- * @param array|string|int $dsn
- *
- * @return FLEA_Db_Driver_Abstract
- */
- function & getDBO($dsn = 0)
- {
- if ($dsn == 0) {
- $dsn = FLEA::getAppInf('dbDSN');
- }
- $dsn = FLEA::parseDSN($dsn);
-
- if (!is_array($dsn) || !isset($dsn['driver'])) {
- FLEA::loadClass('FLEA_Db_Exception_InvalidDSN');
- return __THROW(new FLEA_Db_Exception_InvalidDSN($dsn));
- }
-
- $dsnid = $dsn['id'];
- if (isset($GLOBALS[G_FLEA_VAR]['DBO'][$dsnid])) {
- return $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid];
- }
-
- $driver = ucfirst(strtolower($dsn['driver']));
- $className = 'FLEA_Db_Driver_' . $driver;
- if ($driver == 'Mysql' || $driver == 'Mysqlt') {
- require_once(FLEA_DIR . '/Db/Driver/Mysql.php');
- } else {
- FLEA::loadClass($className);
- }
- $dbo =& new $className($dsn);
- /* @var $dbo FLEA_Db_Driver_Abstract */
- $dbo->connect();
-
- $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid] =& $dbo;
- return $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid];
- }
-
- /**
- * ?? DSN ??????????? DSN ???????????? false
- *
- * @param string|array $dsn
- *
- * @return array
- */
- function parseDSN($dsn)
- {
- if (is_array($dsn)) {
- $dsn['host'] = isset($dsn['host']) ? $dsn['host'] : '';
- $dsn['port'] = isset($dsn['port']) ? $dsn['port'] : '';
- $dsn['login'] = isset($dsn['login']) ? $dsn['login'] : '';
- $dsn['password'] = isset($dsn['password']) ? $dsn['password'] : '';
- $dsn['database'] = isset($dsn['database']) ? $dsn['database'] : '';
- $dsn['options'] = isset($dsn['options']) ? serialize($dsn['options']) : '';
- $dsn['prefix'] = isset($dsn['prefix']) ? $dsn['prefix'] : FLEA::getAppInf('dbTablePrefix');
- $dsn['schema'] = isset($dsn['schema']) ? $dsn['schema'] : '';
- } else {
- $dsn = str_replace('@/', '@localhost/', $dsn);
- $parse = parse_url($dsn);
- if (empty($parse['scheme'])) { return false; }
-
- $dsn = array();
- $dsn['host'] = isset($parse['host']) ? $parse['host'] : 'localhost';
- $dsn['port'] = isset($parse['port']) ? $parse['port'] : '';
- $dsn['login'] = isset($parse['user']) ? $parse['user'] : '';
- $dsn['password'] = isset($parse['pass']) ? $parse['pass'] : '';
- $dsn['driver'] = isset($parse['scheme']) ? strtolower($parse['scheme']) : '';
- $dsn['database'] = isset($parse['path']) ? substr($parse['path'], 1) : '';
- $dsn['options'] = isset($parse['query']) ? $parse['query'] : '';
- $dsn['prefix'] = FLEA::getAppInf('dbTablePrefix');
- $dsn['schema'] = '';
- }
- $dsnid = "{$dsn['driver']}://{$dsn['login']}:{$dsn['password']}@{$dsn['host']}_{$dsn['prefix']}/{$dsn['database']}/{$dsn['schema']}/{$dsn['options']}";
- $dsn['id'] = $dsnid;
- return $dsn;
- }
-
- /**
- * FleaPHP ???? MVC ????
- *
- * ?????????? FleaPHP ??? MVC ??????? FLEA.php ????????????????? FLEA::runMVC() ???????
- */
- function runMVC()
- {
- $MVCPackageFilename = FLEA::getAppInf('MVCPackageFilename');
- if ($MVCPackageFilename != '') {
- require_once($MVCPackageFilename);
- }
- FLEA::init();
-
- // ??????????????
- $dispatcherClass = FLEA::getAppInf('dispatcher');
- FLEA::loadClass($dispatcherClass);
-
- $dispatcher =& new $dispatcherClass($_GET);
- FLEA::register($dispatcher, $dispatcherClass);
- $dispatcher->dispatching();
- }
-
- /**
- * ??????
- *
- * @param boolean $loadMVC
- */
- function init($loadMVC = false)
- {
- static $firstTime = true;
-
- // ?????? FLEA::init()
- if (!$firstTime) { return; }
- $firstTime = false;
-
- /**
- * ???????????????
- */
- __SET_EXCEPTION_HANDLER(FLEA::getAppInf('exceptionHandler'));
- if (PHP5) {
- set_exception_handler(FLEA::getAppInf('exceptionHandler'));
- }
-
- /**
- * ??????????
- */
- if (FLEA::getAppInf('logEnabled') && FLEA::getAppInf('logProvider')) {
- FLEA::loadClass(FLEA::getAppInf('logProvider'));
- }
- if (!function_exists('log_message')) {
- // ?????????????????????? log_message() ??
- function log_message() {};
- }
-
- /**
- * ?????????????????????
- */
- $cachedir = FLEA::getAppInf('internalCacheDir');
- if (empty($cachedir)) {
- FLEA::setAppInf('internalCacheDir', dirname(__FILE__) . DS . '_Cache');
- }
-
- // ?? 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_magic_quotes_runtime(0);
-
- // ?? URL ???????????? URL ?????
- if (FLEA::getAppInf('urlMode') != URL_STANDARD) {
- require(FLEA_DIR . '/Filter/Uri.php');
- }
-
- // ?? requestFilters
- foreach ((array)FLEA::getAppInf('requestFilters') as $file) {
- FLEA::loadFile($file);
- }
-
- // ?? $loadMVC
- if ($loadMVC) {
- $MVCPackageFilename = FLEA::getAppInf('MVCPackageFilename');
- if ($MVCPackageFilename != '') {
- require_once($MVCPackageFilename);
- }
- }
-
- // ?? autoLoad
- foreach ((array)FLEA::getAppInf('autoLoad') as $file) {
- FLEA::loadFile($file);
- }
-
- // ????? session ??????
- if (FLEA::getAppInf('sessionProvider')) {
- FLEA::getSingleton(FLEA::getAppInf('sessionProvider'));
- }
- // ???? session ??
- if (FLEA::getAppInf('autoSessionStart')) {
- session_start();
- }
-
- // ?? I18N ?????
- define('RESPONSE_CHARSET', FLEA::getAppInf('responseCharset'));
- define('DATABASE_CHARSET', FLEA::getAppInf('databaseCharset'));
-
- // ???????????
- if (FLEA::getAppInf('multiLanguageSupport')) {
- FLEA::loadClass(FLEA::getAppInf('languageSupportProvider'));
- }
- if (!function_exists('_T')) {
- function _T() {};
- }
-
- // ?????????
- if (FLEA::getAppInf('autoResponseHeader')) {
- header('Content-Type: text/html; charset=' . FLEA::getAppInf('responseCharset'));
- }
- }
- }
-
- /**
- * ?? FleaPHP ???????
- */
-
- /**
- * ?????????? URL
- *
- * @param string $url ????? url
- * @param int $delay ?????????
- * @param bool $js ??????????? JavaScript ??
- * @param bool $jsWrapped ???? JavaScript ??????? <script> ??????
- * @param bool $return ????????? JavaScript ??
- */
- function redirect($url, $delay = 0, $js = false, $jsWrapped = true, $return = false)
- {
- $delay = (int)$delay;
- if (!$js) {
- if (headers_sent() || $delay > 0) {
- echo <<<EOT
- <html>
- <head>
- <meta http-equiv="refresh" content="{$delay};URL={$url}" />
- </head>
- </html>
- EOT;
- exit;
- } else {
- header("Location: {$url}");
- exit;
- }
- }
-
- $out = '';
- if ($jsWrapped) {
- $out .= '<script language="JavaScript" type="text/javascript">';
- }
- if ($delay > 0) {
- $out .= "window.setTimeout(function () { document.location='{$url}'; }, {$delay});";
- } else {
- $out .= "document.location='{$url}';";
- }
- if ($jsWrapped) {
- $out .= '</script>';
- }
-
- if ($return) {
- return $out;
- }
-
- echo $out;
- exit;
- }
-
- /**
- * ?? url
- *
- * ?? url ??????????????????????????????????????
- * ? url() ????????????????????????????????
- *
- * url() ????????? urlMode ????? URL ???
- * - URL_STANDARD - ??????????? index.php?url=Login&action=Reject&id=1
- * - URL_PATHINFO - PATHINFO ????? index.php/Login/Reject/id/1
- * - URL_REWRITE - URL ??????? /Login/Reject/id/1
- *
- * ??? url ??????????????????
- * - controllerAccessor
- * - defaultController
- * - actionAccessor
- * - defaultAction
- * - urlMode
- * - urlLowerChar
- *
- * ???
- * <code>
- * $url = url('Login', 'checkUser');
- * // $url ??? ?controller=Login&action=checkUser
- *
- * $url = url('Login', 'checkUser', array('username' => 'dualface'));
- * // $url ??? ?controller=Login&action=checkUser&username=dualface
- *
- * $url = url('Article', 'View', array('id' => 1'), '#details');
- * // $url ??? ?controller=Article&action=View&id=1#details
- * </code>
- *
- * @param string $controllerName
- * @param string $actionName
- * @param array $params
- * @param string $anchor
- * @param array $options
- *
- * @return string
- */
- function url($controllerName = null, $actionName = null, $params = null, $anchor = null, $options = null)
- {
- static $baseurl = null, $currentBootstrap = null;
-
- // ????? URL ??????????
- if (is_null($baseurl)) {
- $baseurl = detect_uri_base();
- $p = strrpos($baseurl, '/');
- $currentBootstrap = substr($baseurl, $p + 1);
- $baseurl = substr($baseurl, 0, $p);
- }
-
- // ???? url ???? bootstrap
- $options = (array)$options;
- if (isset($options['bootstrap'])) {
- $bootstrap = $options['bootstrap'];
- } else if ($currentBootstrap == '') {
- $bootstrap = FLEA::getAppInf('urlBootstrap');
- } else {
- $bootstrap = $currentBootstrap;
- }
-
- // ???????????
- $defaultController = FLEA::getAppInf('defaultController');
- $defaultAction = FLEA::getAppInf('defaultAction');
- $lowerChar = isset($options['lowerChar']) ? $options['lowerChar'] : FLEA::getAppInf('urlLowerChar');
- if ($lowerChar) {
- $defaultController = strtolower($defaultController);
- $defaultAction = strtolower($defaultAction);
- }
-
- if ($bootstrap != $currentBootstrap && $currentBootstrap != '') {
- $controllerName = !empty($controllerName) ? $controllerName : null;
- $actionName = !empty($actionName) ? $actionName : null;
- } else {
- $controllerName = !empty($controllerName) ? $controllerName : $defaultController;
- $actionName = !empty($actionName) ? $actionName : $defaultAction;
- }
- if ($lowerChar) {
- $controllerName = strtolower($controllerName);
- $actionName = strtolower($actionName);
- }
-
- $url = '';
- $mode = isset($options['mode']) ? $options['mode'] : FLEA::getAppInf('urlMode');
-
- // PATHINFO ? REWRITE ??
- if ($mode == URL_PATHINFO || $mode == URL_REWRITE) {
- $url = $baseurl;
- if ($mode == URL_PATHINFO) {
- $url .= '/' . $bootstrap;
- }
- if ($controllerName != '' && $actionName != '') {
- $pps = isset($options['parameterPairStyle']) ? $options['parameterPairStyle'] : FLEA::getAppInf('urlParameterPairStyle');
- $url .= '/' . rawurlencode($controllerName);
- if (is_array($params) && !empty($params)) {
- $url .= '/' . rawurlencode($actionName);
- $url .= '/' . encode_url_args($params, $mode, $pps);
- } else {
- if (FLEA::getAppInf('urlAlwaysUseAccessor') || $actionName != $defaultAction) {
- $url .= '/' . rawurlencode($actionName);
- }
- }
- }
- if ($anchor) { $url .= '#' . $anchor; }
- return $url;
- }
-
- // ????
- $alwaysUseBootstrap = isset($options['alwaysUseBootstrap']) ? $options['alwaysUseBootstrap'] : FLEA::getAppInf('urlAlwaysUseBootstrap');
- $url = $baseurl . '/';
-
- if ($alwaysUseBootstrap || $bootstrap != FLEA::getAppInf('urlBootstrap')) {
- $url .= $bootstrap;
- }
-
- $parajoin = '?';
- if (FLEA::getAppInf('urlAlwaysUseAccessor')) {
- $defaultController = '';
- $defaultAction = '';
- }
- if ($controllerName != $defaultController && !is_null($controllerName)) {
- $url .= $parajoin . FLEA::getAppInf('controllerAccessor'). '=' . $controllerName;
- $parajoin = '&';
- }
- if ($actionName != $defaultAction && !is_null($actionName)) {
- $url .= $parajoin . FLEA::getAppInf('actionAccessor') . '=' . $actionName;
- $parajoin = '&';
- }
-
- if (is_array($params) && !empty($params)) {
- $url .= $parajoin . encode_url_args($params, $mode);
- }
- if ($anchor) { $url .= '#' . $anchor; }
-
- return $url;
- }
-
- /**
- * ??????? URL ??
- *
- * ?? tsingson ?????????? FleaPHP ?? url() ?????? CGI ??????
- *
- * @param boolean $queryMode ??? URL ????????????
- *
- * @return string
- */
- function detect_uri_base($queryMode = false)
- {
- $aURL = array();
-
- // Try to get the request URL
- if (!empty($_SERVER['SCRIPT_NAME'])) {
- $arr = parse_url($_SERVER['SCRIPT_NAME']);
- $aURL['path'] = $arr['path'];
- } elseif (!empty($_SERVER['REQUEST_URI'])) {
- $_SERVER['REQUEST_URI'] = str_replace(array('"',"'",'<','>'), array('%22','%27','%3C','%3E'), $_SERVER['REQUEST_URI']);
- $p = strpos($_SERVER['REQUEST_URI'], ':');
- if ($p > 0 && substr($_SERVER['REQUEST_URI'], $p + 1, 2) != '//') {
- $aURL = array('path' => $_SERVER['REQUEST_URI']);
- } else {
- $aURL = parse_url($_SERVER['REQUEST_URI']);
- }
- if (isset($aURL['path']) && isset($_SERVER['PATH_INFO'])) {
- $aURL['path'] = substr(urldecode($aURL['path']), 0, - strlen($_SERVER['PATH_INFO']));
- }
- }
-
- // Fill in the empty values
- if (empty($aURL['scheme'])) {
- if (!empty($_SERVER['HTTP_SCHEME'])) {
- $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
- } else {
- $aURL['scheme'] = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http';
- }
- }
-
- if (empty($aURL['host'])) {
- if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
- $p = strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ':');
- if ($p > 0) {
- $aURL['host'] = substr($_SERVER['HTTP_X_FORWARDED_HOST'], 0, $p);
- $aURL['port'] = substr($_SERVER['HTTP_X_FORWARDED_HOST'], $p + 1);
- } else {
- $aURL['host'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
- }
- } else if (!empty($_SERVER['HTTP_HOST'])) {
- $p = strpos($_SERVER['HTTP_HOST'], ':');
- if ($p > 0) {
- $aURL['host'] = substr($_SERVER['HTTP_HOST'], 0, $p);
- $aURL['port'] = substr($_SERVER['HTTP_HOST'], $p + 1);
- } else {
- $aURL['host'] = $_SERVER['HTTP_HOST'];
- }
- } else if (!empty($_SERVER['SERVER_NAME'])) {
- $aURL['host'] = $_SERVER['SERVER_NAME'];
- }
- }
-
- if (empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) {
- $aURL['port'] = $_SERVER['SERVER_PORT'];
- }
-
- if (empty($aURL['path'])) {
- if (!empty($_SERVER['PATH_INFO'])) {
- $sPath = parse_url($_SERVER['PATH_INFO']);
- } else {
- $sPath = parse_url($_SERVER['PHP_SELF']);
- }
- $aURL['path'] = str_replace(array('"',"'",'<','>'), array('%22','%27','%3C','%3E'), $sPath['path']);
- unset($sPath);
- }
-
- // Build the URL: Start with scheme, user and pass
- $sURL = $aURL['scheme'].'://';
- if (!empty($aURL['user'])) {
- $sURL .= $aURL['user'];
- if (!empty($aURL['pass'])) {
- $sURL .= ':'.$aURL['pass'];
- }
- $sURL .= '@';
- }
-
- // Add the host
- $sURL .= $aURL['host'];
-
- // Add the port if needed
- if (!empty($aURL['port']) && (($aURL['scheme'] == 'http' && $aURL['port'] != 80) || ($aURL['scheme'] == 'https' && $aURL['port'] != 443))) {
- $sURL .= ':'.$aURL['port'];
- }
-
- $sURL .= $aURL['path'];
-
- // Add the path and the query string
- if ($queryMode && isset($aURL['query'])) {
- $sURL .= $aURL['query'];
- }
-
- unset($aURL);
- return $sURL;
- }
-
- /**
- * ????????? url ????????
- *
- * ???
- * <code>
- * $string = encode_url_args(array('username' => 'dualface', 'mode' => 'md5'));
- * // $string ??? username=dualface&mode=md5
- * </code>
- *
- * @param array $args
- * @param enum $urlMode
- * @param string $parameterPairStyle
- *
- * @return string
- */
- function encode_url_args($args, $urlMode = URL_STANDARD, $parameterPairStyle = null)
- {
- $str = '';
- switch ($urlMode) {
- case URL_STANDARD:
- if (is_null($parameterPairStyle)) {
- $parameterPairStyle = '=';
- }
- $sc = '&';
- break;
- case URL_PATHINFO:
- case URL_REWRITE:
- if (is_null($parameterPairStyle)) {
- $parameterPairStyle = FLEA::getAppInf('urlParameterPairStyle');
- }
- $sc = '/';
- break;
- }
-
- foreach ($args as $key => $value) {
- if (is_null($value) || $value === '') { continue; }
- if (is_array($value)) {
- $append = encode_url_args($value, $urlMode);
- } else {
- $append = rawurlencode($key) . $parameterPairStyle . rawurlencode($value);
- }
- if (substr($str, -1) != $sc) {
- $str .= $sc;
- }
- $str .= $append;
- }
- return substr($str, 1);
- }
-
- /**
- * ?? HTML ???????? htmlspecialchars()
- *
- * @param string $text
- *
- * @return string
- */
- function h($text)
- {
- return htmlspecialchars($text);
- }
-
- /**
- * ?? HTML ????????????
- *
- * ????? ??????? <br />?
- *
- * @param string $text
- *
- * @return string
- */
- function t($text)
- {
- return nl2br(str_replace(' ', ' ', htmlspecialchars($text)));
- }
-
- /**
- * ?? JavaScript ???????????????????????
- *
- * ???
- * <code>
- * js_alert('Dialog message', '', $url);
- * // ??
- * js_alert('Dialog message', 'window.close();');
- * </code>
- *
- * @param string $message ??????
- * @param string $after_action ???????????
- * @param string $url ?????
- */
- function js_alert($message = '', $after_action = '', $url = '')
- {
- $out = "<script language=\"javascript\" type=\"text/javascript\">\n";
- if (!empty($message)) {
- $out .= "alert(\"";
- $out .= str_replace("\\\\n", "\\n", t2js(addslashes($message)));
- $out .= "\");\n";
- }
- if (!empty($after_action)) {
- $out .= $after_action . "\n";
- }
- if (!empty($url)) {
- $out .= "document.location.href=\"";
- $out .= $url;
- $out .= "\";\n";
- }
- $out .= "</script>";
- echo $out;
- exit;
- }
-
- /**
- * ????????? JavaScript ??????????"?
- *
- * @param string $content
- *
- * @return string
- */
- function t2js($content)
- {
- return str_replace(array("\r", "\n"), array('', '\n'), addslashes($content));
- }
-
- /**
- * safe_file_put_contents() ???????????????????????????????????????
- *
- * @param string $filename
- * @param string $content
- * @param int $flag
- *
- * @return boolean
- */
- function safe_file_put_contents($filename, & $content)
- {
- $fp = fopen($filename, 'wb');
- if ($fp) {
- flock($fp, LOCK_EX);
- fwrite($fp, $content);
- flock($fp, LOCK_UN);
- fclose($fp);
- return true;
- } else {
- return false;
- }
- }
-
- /**
- * safe_file_get_contents() ???????????????????????????????????
- *
- * @param string $filename
- *
- * @return mixed
- */
- function safe_file_get_contents($filename)
- {
- $fp = fopen($filename, 'rb');
- if ($fp) {
- flock($fp, LOCK_SH);
- clearstatcache();
- $data = fread($fp, filesize($filename));
- flock($fp, LOCK_UN);
- fclose($fp);
- return $data;
- } else {
- return false;
- }
- }
-
- if (!function_exists('file_put_contents'))
- {
- function file_put_contents($filename, & $content)
- {
- return safe_file_put_contents($filename, $content);
- }
- }
-
- /**
- * ??????????????
- */
-
- /**
- * ??????
- *
- * FleaPHP ???? PHP4????????????????????????????????
- * FleaPHP ?????????????
- * - ? __TRY() ??? try ??????
- * - ? __CATCH() ???????? catch?
- * - ? __THROW() ?????
- * - __TRY() ? __CATCH() ?????? PHP5 ?? throw ??????
- * - ????? __THROW() ?????????? return false ????????????
- * - __TRY() ? __CATCH() ????????? __CATCH() ?????????
- * - ? __IS_EXCEPTION() ??? __CATCH() ????????????
- * - ?? __TRY() ?????? __CATCH() ???????? __CANCEL_TRY() ?????
- *
- * ?? __THROW() ??????????????? FLEA_Exception ??????????
- * ???? FleaPHP ??????????? FLEA_Exception ????????????
- * FLEA_Exception ???????????????????????
- *
- * ?????????????????????
- * <code>
- * __TRY();
- * $ret = doSomething(); // ????????????
- * $ex = __CATCH();
- * if (__IS_EXCEPTION($ex)) {
- * // ????
- * } else {
- * echo $ret;
- * }
- *
- * function doSomething() {
- * if (rand(0, 9) % 2) {
- * __THROW(new MyException());
- * return false;
- * }
- * return true;
- * }
- * </code>
- *
- * <strong>?????????? __THROW() ???????? return false</strong>
- *
- * ?? doSomething() ?? __THROW() ???????????????? doSomething() ?
- * ???????????????? doSomething() ?????????
- *
- * ???__TRY() ? __CATCH() ????????????
- *
- * <strong>?? __TRY() ? __CATCH() ??????</strong>
- *
- * FleaPHP ??? __TRY() ?????????????doSomething() ???????????
- * ???????? doSomething() ?????? __TRY() ? __CATCH() ??????
- *
- * <code>
- * function doSomething() {
- * if (rand(0, 9) % 2) {
- * __THROW(new MyException());
- * return false;
- * } else {
- * __TRY();
- * callAnotherFunc();
- * $ex = __CATCH();
- * if (__IS_EXCEPTION($ex)) {
- * // ?? callAnotherFunc() ???????
- * ...
- * // ?????????? __THROW() ?????????
- * // ??? doSomething() ?????????
- * __THROW($ex);
- * return false;
- * }
- * return true;
- * }
- * }
- * </code>
- *
- * ???? __TRY() ??????? __CATCH() ????????? __CANCEL_TRY()
- * ??? __TRY() ???????
- *
- * @package Core
- *
- * @param FLEA_Exception $exception
- *
- * @return boolean
- */
- function __THROW($exception)
- {
- // ????
- if (function_exists('log_message')) {
- log_message(get_class($exception) . ': ' . $exception->getMessage(), 'exception');
- }
-
- // ????????????
- if (isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']) && is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']))
- {
- $point = array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']);
- if ($point != null) {
- array_push($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'], $exception);
- $ret = false;
- return $ret;
- }
- }
-
- if (isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'])) {
- call_user_func_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'], array(& $exception));
- } else {
- __FLEA_EXCEPTION_HANDLER($exception);
- }
- exit;
- }
-
- /**
- * ???????
- *
- * @package Core
- */
- function __TRY()
- {
- static $point = 0;
- if (!isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']) ||
- !is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']))
- {
- $GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'] = array();
- }
-
- $point++;
- array_push($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'], $point);
- }
-
- /**
- * ??????????????????? false
- *
- * @package Core
- *
- * @return FLEA_Exception
- */
- function __CATCH()
- {
- if (!is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'])) {
- return false;
- }
- $exception = array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']);
- if (!is_object($exception)) {
- $exception = false;
- }
- return $exception;
- }
-
- /**
- * ?????? __TRY() ??????
- *
- * @package Core
- */
- function __CANCEL_TRY()
- {
- if (is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'])) {
- array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']);
- }
- }
-
- /**
- * ?????????
- *
- * $type ?????????????????
- *
- * @package Core
- *
- * @param FLEA_Exception $exception
- * @param string $type
- */
- function __IS_EXCEPTION($exception, $type = null)
- {
- if (!is_object($exception) || !is_a($exception, 'FLEA_Exception')) {
- return false;
- }
- if (is_null($type)) {
- return true;
- } else {
- return strtoupper($type) == strtoupper(get_class($exception));
- }
- }
-
- /**
- * ????????????????????????
- *
- * ?????????? __TRY() ??????????????FleaPHP ???
- * ????????????????????????????????????
- *
- * ???
- * <code>
- * // ?????????????
- * global $prevExceptionHandler;
- * $prevExceptionHandler = __SET_EXCEPTION_HANDLER('app_exception_handler');
- *
- * function app_exception_handler(& $ex) {
- * global $prevExceptionHandler;
- *
- * if (is_a($ex, 'APP_Exception')) {
- * // ?????
- * ...
- * } else {
- * // ???????????
- * if ($prevExceptionHandler) {
- * call_user_func_array($prevExceptionHandler, array(& $exception));
- * }
- * }
- * }
- * </code>
- *
- * ???????????????????????????????????????
- * ????????????????????????????????????????
- *
- * @package Core
- *
- * @param callback $callback
- *
- * @return mixed
- */
- function __SET_EXCEPTION_HANDLER($callback)
- {
- if (isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'])) {
- $current = $GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'];
- } else {
- $current = null;
- }
- $GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'] = $callback;
- return $current;
- }
-
- /**
- * FleaPHP ?????????
- *
- * @package Core
- *
- * @param FLEA_Exception $ex
- */
- function __FLEA_EXCEPTION_HANDLER($ex)
- {
- if (!FLEA::getAppInf('displayErrors')) { exit; }
- if (FLEA::getAppInf('friendlyErrorsMessage')) {
- $language = FLEA::getAppInf('defaultLanguage');
- $language = preg_replace('/[^a-z0-9\-_]+/i', '', $language);
-
- $exclass = strtoupper(get_class($ex));
- $template = FLEA_DIR . "/_Errors/{$language}/{$exclass}.php";
- if (!file_exists($template)) {
- $template = FLEA_DIR . "/_Errors/{$language}/FLEA_EXCEPTION.php";
- if (!file_exists($template)) {
- $template = FLEA_DIR . "/_Errors/default/FLEA_EXCEPTION.php";
- }
- }
- include($template);
- } else {
- print_ex($ex);
- }
- exit;
- }
-
- /**
- * ?????????
- *
- * @package Core
- *
- * @param FLEA_Exception $ex
- * @param boolean $return ? true ???????????????
- */
- function print_ex($ex, $return = false)
- {
- $out = "exception '" . get_class($ex) . "'";
- if ($ex->getMessage() != '') {
- $out .= " with message '" . $ex->getMessage() . "'";
- }
- if (defined('DEPLOY_MODE') && DEPLOY_MODE != false) {
- $out .= ' in ' . basename($ex->getFile()) . ':' . $ex->getLine() . "\n\n";
- } else {
- $out .= ' in ' . $ex->getFile() . ':' . $ex->getLine() . "\n\n";
- $out .= $ex->getTraceAsString();
- }
-
- if ($return) { return $out; }
-
- if (ini_get('html_errors')) {
- echo nl2br(htmlspecialchars($out));
- } else {
- echo $out;
- }
-
- return '';
- }
-
- /**
- * ??????????????
- *
- * @package Core
- *
- * @param mixed $vars ??????
- * @param string $label
- * @param boolean $return
- */
- function dump($vars, $label = '', $return = false)
- {
- if (ini_get('html_errors')) {
- $content = "<pre>\n";
- if ($label != '') {
- $content .= "<strong>{$label} :</strong>\n";
- }
- $content .= htmlspecialchars(print_r($vars, true));
- $content .= "\n</pre>\n";
- } else {
- $content = $label . " :\n" . print_r($vars, true);
- }
- if ($return) { return $content; }
- echo $content;
- return null;
- }
-
- /**
- * ?????????????????
- *
- * @package Core
- *
- * @return string
- */
- function dump_trace()
- {
- $debug = debug_backtrace();
- $lines = '';
- $index = 0;
- for ($i = 0; $i < count($debug); $i++) {
- if ($i == 0) { continue; }
- $file = $debug[$i];
- if ($file['file'] == '') { continue; }
- if (substr($file['file'], 0, strlen(FLEA_DIR)) != FLEA_DIR) {
- $line = "#<strong>{$index} {$file['file']}({$file['line']}): </strong>";
- } else {
- $line = "#{$index} {$file['file']}({$file['line']}): ";
- }
- if (isset($file['class'])) {
- $line .= "{$file['class']}{$file['type']}";
- }
- $line .= "{$file['function']}(";
- if (isset($file['args']) && count($file['args'])) {
- foreach ($file['args'] as $arg) {
- $line .= gettype($arg) . ', ';
- }
- $line = substr($line, 0, -2);
- }
- $line .= ')';
- $lines .= $line . "\n";
- $index++;
- } // for
- $lines .= "#{$index} {main}\n";
-
- if (ini_get('html_errors')) {
- echo nl2br(str_replace(' ', ' ', $lines));
- } else {
- echo $lines;
- }
- }
-
- /**
- * ??????????????
- *
- * @param mixed $time
- *
- * @return float
- */
- function microtime_float($time = null)
- {
- list($usec, $sec) = explode(' ', $time ? $time : microtime());
- return ((float)$usec + (float)$sec);
- }
-
- /**
- * ???????????????
- *
- * ???????????? 'defaultLanguage' ??????????????
- * ?????????????????????????
- *
- * ??????????????????????? default ??????
- *
- * ? $appError ? true ??_ET() ??????????
- * 'languageFilesDir' ?????????????
- *
- * @package Core
- *
- * @param int $errorCode
- * @param boolean $appError
- *
- * @return string
- */
- function _ET($errorCode, $appError = false)
- {
- static $message = array();
-
- $language = FLEA::getAppInf('defaultLanguage');
- $language = preg_replace('/[^a-z0-9\-_]+/i', '', $language);
-
- if (!isset($message[$language])) {
- if ($appError) {
- $filename = FLEA::getAppInf('languageFilesDir') . DS .
- $language . DS . 'ErrorMessage.php';
- } else {
- // ?? FleaPHP ?????????
- $filename = FLEA_DIR . "/_Errors/{$language}/ErrorMessage.php";
- }
- if (!is_readable($filename)) {
- $filename = FLEA_DIR . '/_Errors/default/ErrorMessage.php';
- }
- $message[$language] = include($filename);
- }
-
- return isset($message[$language][$errorCode]) ?
- $message[$language][$errorCode] :
- '';
- }
-
- /**
- * PHP4 ? PHP5 ????????
- */
- if (PHP5) {
-
- class FLEA_Exception extends Exception
- {
- function FLEA_Exception($message = '', $code = 0)
- {
- parent::__construct($message, $code);
- }
- }
-
- } else {
-
- /**
- * FLEA_Exception ????????
- *
- * ? PHP5 ??FLEA_Exception ??? PHP ??? Exception ??
- * ? PHP4 ???????????
- *
- * @package Exception
- * @author ???? (www.qeeyuan.com)
- * @version 1.0
- */
- class FLEA_Exception
- {
- /**
- * ????
- *
- * @var string
- */
- var $message = 'Unknown exception';
-
- /**
- * ????
- */
- var $code = 0;
-
- /**
- * ???????
- *
- * @var string
- */
- var $file;
-
- /**
- * ?????????
- *
- * @var int
- */
- var $line;
-
- /**
- * ????
- *
- * @var array
- */
- var $trac;
-
- /**
- * ????
- *
- * @param string $message
- * @param int $code
- *
- * @return FLEA_Exception
- */
- function FLEA_Exception($message = null, $code = 0)
- {
- $this->message = $message;
- $this->code = $code;
- $this->trac = debug_backtrace();
-
- // ??????????????
- $last = array_shift($this->trac);
- $this->file = $last['file'];
- $this->line = $last['line'];
- }
-
- /**
- * ????????
- *
- * @return string
- */
- function getMessage()
- {
- return $this->message;
- }
-
- /**
- * ????????
- *
- * @return int
- */
- function getCode()
- {
- return $this->code;
- }
-
- /**
- * ??????????
- *
- * @return string
- */
- function getFile()
- {
- return $this->file;
- }
-
- /**
- * ???????????
- *
- * @return int
- */
- function getLine()
- {
- return $this->line;
- }
-
- /**
- * ??????
- *
- * @return array
- */
- function getTrace()
- {
- return $this->trac;
- }
-
- /**
- * ????????????
- */
- function getTraceAsString()
- {
- $out = '';
- $ix = 0;
- foreach ($this->trac as $point) {
- $out .= "#{$ix} {$point['file']}({$point['line']}): {$point['function']}(";
- if (is_array($point['args']) && count($point['args']) > 0) {
- foreach ($point['args'] as $arg) {
- switch (gettype($arg)) {
- case 'array':
- case 'resource':
- $out .= gettype($arg);
- break;
- case 'object':
- $out .= get_class($arg);
- break;
- case 'string':
- if (strlen($arg) > 30) {
- $arg = substr($arg, 0, 27) . ' ...';
- }
- $out .= "'{$arg}'";
- break;
- default:
- $out .= $arg;
- }
- $out .= ', ';
- }
- $out = substr($out, 0, -2);
- }
- $out .= ")\n";
- $ix++;
- }
- $out .= "#{$ix} {main}\n";
-
- return $out;
- }
-
- /**
- * ??????????
- *
- * @return string
- */
- function __toString()
- {
- $out = "exception '" . get_class($this) . "'";
- if ($this->message != '') {
- $out .= " with message '{$this->message}'";
- }
- $out .= " in {$this->file}:{$this->line}\n\n";
- $out .= $this->getTraceAsString();
- return $out;
- }
- }
-
- }
-
- /**
- * Ajax ?????
- */
- /**
- * ????? PHP ??? JSON ?????? PEAR::Service_JSON ??? JSON ??????
- *
- * ?????? PHP ???? JSON ?????????????
- */
-
- if (!function_exists('json_encode')) {
- /**
- * ?????? JSON ???
- *
- * @param mixed $value
- *
- * @return string
- */
- function json_encode($value)
- {
- static $instance = array();
- if (!isset($instance[0])) {
- require_once(FLEA_DIR . '/Ajax/JSON.php');
- $instance[0] =& new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
- }
- return $instance[0]->encode($value);
- }
- }
-
- if (!function_exists('json_decode')) {
- /**
- * ? JSON ????????
- *
- * @param string $jsonString
- *
- * @return mixed
- */
- function json_decode($jsonString)
- {
- static $instance = array();
- if (!isset($instance[0])) {
- require_once(FLEA_DIR . '/Ajax/JSON.php');
- $instance[0] =& new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
- }
- return $instance[0]->decode($jsonString);
- }
- }