| 2 |
global $___fleaphp_loaded_time; $___fleaphp_loaded_time = microtime(); define('FLEA_VERSION', '1.0.70.1017'); if (substr(PHP_VERSION, 0, 1) == '5') { define('PHP5', true); define('PHP4', false); } else { define('PHP5', false); define('PHP4', true); } define('DS', DIRECTORY_SEPARATOR); define('URL_STANDARD', 'URL_STANDARD'); define('URL_PATHINFO', 'URL_PATHINFO'); define('URL_REWRITE', 'URL_REWRITE'); define('RBAC_EVERYONE', 'RBAC_EVERYONE'); define('RBAC_HAS_ROLE', 'RBAC_HAS_ROLE'); define('RBAC_NO_ROLE', 'RBAC_NO_ROLE'); define('RBAC_NULL', 'RBAC_NULL'); define('ACTION_ALL', 'ACTION_ALL'); 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, ); $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'); if (!defined('NO_LEGACY_FLEAPHP') || NO_LEGACY_FLEAPHP == false) { require(FLEA_DIR . '/Compatibility.php'); } 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); } } 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'); class FLEA { 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; } function getAppInf($option, $default = null) { return isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option]) ? $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] : $default; } 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; } } 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; } 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; } } 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; } 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; } 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; } 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; } 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]; } 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; } } 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)); } function isRegistered($name) { return isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name]); } 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; } 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; } } 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; } function & initWebControls() { return FLEA::getSingleton(FLEA::getAppInf('webControlsClassName')); } function & initAjax() { return FLEA::getSingleton(FLEA::getAppInf('ajaxClassName')); } 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)); } } 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); $dbo->connect(); $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid] =& $dbo; return $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid]; } 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; } 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(); } function init($loadMVC = false) { static $firstTime = true; 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')) { function log_message() {}; } $cachedir = FLEA::getAppInf('internalCacheDir'); if (empty($cachedir)) { FLEA::setAppInf('internalCacheDir', dirname(__FILE__) . DS . '_Cache'); } 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); if (FLEA::getAppInf('urlMode') != URL_STANDARD) { require(FLEA_DIR . '/Filter/Uri.php'); } foreach ((array)FLEA::getAppInf('requestFilters') as $file) { FLEA::loadFile($file); } if ($loadMVC) { $MVCPackageFilename = FLEA::getAppInf('MVCPackageFilename'); if ($MVCPackageFilename != '') { require_once($MVCPackageFilename); } } foreach ((array)FLEA::getAppInf('autoLoad') as $file) { FLEA::loadFile($file); } if (FLEA::getAppInf('sessionProvider')) { FLEA::getSingleton(FLEA::getAppInf('sessionProvider')); } if (FLEA::getAppInf('autoSessionStart')) { session_start(); } 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')); } } } function redirect($url, $delay = 0, $js = false, $jsWrapped = true, $return = false) { $delay = (int)$delay; if (!$js) { if (headers_sent() || $delay > 0) { echo <<<EOT
|
| 9 |
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; } function url($controllerName = null, $actionName = null, $params = null, $anchor = null, $options = null) { static $baseurl = null, $currentBootstrap = null; if (is_null($baseurl)) { $baseurl = detect_uri_base(); $p = strrpos($baseurl, '/'); $currentBootstrap = substr($baseurl, $p + 1); $baseurl = substr($baseurl, 0, $p); } $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'); 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; } function detect_uri_base($queryMode = false) { $aURL = array(); 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'])); } } 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); } $sURL = $aURL['scheme'].'://'; if (!empty($aURL['user'])) { $sURL .= $aURL['user']; if (!empty($aURL['pass'])) { $sURL .= ':'.$aURL['pass']; } $sURL .= '@'; } $sURL .= $aURL['host']; if (!empty($aURL['port']) && (($aURL['scheme'] == 'http' && $aURL['port'] != 80) || ($aURL['scheme'] == 'https' && $aURL['port'] != 443))) { $sURL .= ':'.$aURL['port']; } $sURL .= $aURL['path']; if ($queryMode && isset($aURL['query'])) { $sURL .= $aURL['query']; } unset($aURL); return $sURL; } 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); } function h($text) { return htmlspecialchars($text); } function t($text) { return nl2br(str_replace(' ', ' ', htmlspecialchars($text))); } 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; } function t2js($content) { return str_replace(array("\r", "\n"), array('', '\n'), addslashes($content)); } 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; } } 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); } } 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; } 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); } 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; } function __CANCEL_TRY() { if (is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'])) { array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']); } } 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)); } } 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; } 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; } 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 ''; } 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; } 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++; } $lines .= "#{$index} {main}\n"; if (ini_get('html_errors')) { echo nl2br(str_replace(' ', ' ', $lines)); } else { echo $lines; } } function microtime_float($time = null) { list($usec, $sec) = explode(' ', $time ? $time : microtime()); return ((float)$usec + (float)$sec); } 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 { $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] : ''; } if (PHP5) { class FLEA_Exception extends Exception { function FLEA_Exception($message = '', $code = 0) { parent::__construct($message, $code); } } } else { class FLEA_Exception { var $message = 'Unknown exception'; var $code = 0; var $file; var $line; var $trac; 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']; } function getMessage() { return $this->message; } function getCode() { return $this->code; } function getFile() { return $this->file; } function getLine() { return $this->line; } 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; } 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; } } } if (!function_exists('json_encode')) { 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')) { 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); } } |