?php /** * ECMall框架核心文件,包含最基础的类与函数 * Streamlining comes from Sparrow PHP @ Garbin * * Garbin */ /*---------------------以下是系统常量-----------------------*/ /* 记录程序启动时间 */ define('START_TIME', ecm_microtime()); /* 判断请求方式 */ define('IS_POST', (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')); /* 判断请求方式 */ define('IN_ECM', true); /* 定义PHP_SELF常量 */ define('PHP_SELF', htmlentities(isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'])); /* 当前ECMall程序版本 */ define('VERSION', '2.0 final'); /* 当前ECMall程序Release */ define('RELEASE', '20090821'); /*---------------------以下是PHP在不同版本,不同服务器上的兼容处理-----------------------*/ /* 在部分IIS上会没有REQUEST_URI变量 */ $query_string = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : $_SERVER['QUERY_STRING']; if (!isset($_SERVER['REQUEST_URI'])) { $_SERVER['REQUEST_URI'] = PHP_SELF . '?' . $query_string; } else { if (strpos($_SERVER['REQUEST_URI'], '?') === false && $query_string) { $_SERVER['REQUEST_URI'] .= '?' . $query_string; } } function view($mess){ if(is_array($mess)===true){ $mess = highlight_string('<?php '.var_export($mess,true),true); } exit($mess); } /*---------------------以下是系统底层基础类及工具-----------------------*/ class ECMall { /* 启动 */ function startup($config = array()) { /* ECMall::startup(array( 'default_app' => 'default', //默认执行文件, 'default_act' => 'index', //默认执行方法 'app_root' => ROOT_PATH . '/app', //默认执行文件的路径 'external_libs' => array( //通用引入文件 ROOT_PATH . '/includes/global.lib.php', ROOT_PATH . '/includes/libraries/time.lib.php', ROOT_PATH . '/includes/ecapp.base.php', ROOT_PATH . '/includes/plugin.base.php', ROOT_PATH . '/app/frontend.base.php', ROOT_PATH . '/includes/subdomain.inc.php', ), )); */ /* 加载初始化文件 */ require(ROOT_PATH . '/eccore/controller/app.base.php'); //基础控制器类 require(ROOT_PATH . '/eccore/model/model.base.php'); //模型基础类 //每次都有一堆的文件引入. if (!empty($config['external_libs'])) { foreach ($config['external_libs'] as $lib) { require($lib); } } /* 数据过滤 */ if (!get_magic_quotes_gpc()) { $_GET = addslashes_deep($_GET); $_POST = addslashes_deep($_POST); $_COOKIE= addslashes_deep($_COOKIE); } /* 请求转发 作用不大, 可直接写死*/ $default_app = $config['default_app'] ? $config['default_app'] : 'default'; $default_act = $config['default_act'] ? $config['default_act'] : 'index'; // 处理GET,抓取app及act参数. // http://127.0.0.1/ecmall/index.php?app=message&act=inbox
很容易看得出来. $app = isset($_REQUEST['app']) ? trim($_REQUEST['app']) : $default_app; $act = isset($_REQUEST['act']) ? trim($_REQUEST['act']) : $default_act; //根据app参数来引入相应的文件. $config['app_root']变量是路径. $app_file = $config['app_root'] . "/{$app}.app.php"; //文件判断 if (!is_file($app_file)) { exit('Missing controller'); } // 引入文件. require($app_file); //定义两个常量, 估计日后常用.. define('APP', $app); define('ACT', $act); // ucfirst,假设第一个字为小写,即转大写. // 也是组合成相应的类名, class MessageApp extends MemberbaseApp 可以看得出来. $app_class_name = ucfirst($app) . 'App'; /* 实例化控制器 */ $app = new $app_class_name(); // c()函数即将当前的实例引用给$GLOBALS['ECMALL_APP'] c($app); //执行$act方法 $app->do_action($act); //转发至对应的Action //空白方法,未添加功能. $app->destruct(); } } /** * 所有类的基础类 * * Garbin * @usage none */ class Object { var $_errors = array(); var $_errnum = 0; function __construct() { $this->Object(); } function Object() { #TODO } /** * 触发错误 * * Garbin * @param string $errmsg * void */ function _error($msg, $obj = '') { if(is_array($msg)) { $this->_errors = array_merge($this->_errors, $msg); $this->_errnum += count($msg); } else { $this->_errors[] = compact('msg', 'obj'); $this->_errnum++; } } /** * 检查是否存在错误 * * Garbin * int */ function has_error() { return $this->_errnum; } /** * 获取错误列表 * * Garbin * array */ function get_error() { return $this->_errors; } } /** * 语言项管理 * * Garbin * @param none * void */ class Lang { /** * 获取指定键的语言项 * * Garbin * @param none * mixed */ function &get($key = '') { $vkey = $key ? strtokey("{$key}", '$GLOBALS[\'__ECLANG__\']') : '$GLOBALS[\'__ECLANG__\']'; $tmp = eval('if(isset(' . $vkey . '))return ' . $vkey . ';else{ return $key; }'); return $tmp; } /** * 加载指定的语言项至全局语言数据中 * * Garbin * @param none * void */ function load($lang_file) { static $loaded = array(); $old_lang = $new_lang = array(); $file_md5 = md5($lang_file); if (!isset($loaded[$file_md5])) { $new_lang = Lang::fetch($lang_file); $loaded[$file_md5] = $lang_file; } else { return; } $old_lang =& $GLOBALS['__ECLANG__']; if (is_array($old_lang)) { $new_lang = array_merge($old_lang, $new_lang); } $GLOBALS['__ECLANG__'] = $new_lang; } /** * 获取一个语言文件的内容 * * Garbin * @param string $lang_file * array */ function fetch($lang_file) { return include($lang_file); } } function lang_file($file) { return ROOT_PATH . '/languages/' . LANG . '/' . $file . '.lang.php'; } /** * 配置管理器 * * Garbin * @usage none */ class Conf { /** * 加载配置项 * * Garbin * @param mixed $conf * bool */ function load($conf) { $old_conf = isset($GLOBALS['ECMALL_CONFIG']) ? $GLOBALS['ECMALL_CONFIG'] : array(); if (is_string($conf)) { $conf = include($conf); } if (is_array($old_conf)) { $GLOBALS['ECMALL_CONFIG'] = array_merge($old_conf, $conf); } else { $GLOBALS['ECMALL_CONFIG'] = $conf; } } /** * 获取配置项 * * Garbin * @param string $k * mixed */ function get($key = '') { $vkey = $key ? strtokey("{$key}", '$GLOBALS[\'ECMALL_CONFIG\']') : '$GLOBALS[\'ECMALL_CONFIG\']'; return eval('if(isset(' . $vkey . '))return ' . $vkey . ';else{ return null; }'); } } /** * 获取视图链接 * * Garbin * @param string $engine * object */ function &v($is_new = false, $engine = 'default') { include_once(ROOT_PATH . '/eccore/view/template.php'); if ($is_new) { return new ecsTemplate(); } else { static $v = null; if ($v === null) { switch ($engine) { case 'default': $v = new ecsTemplate(); break; } } return $v; } } /** * 获取一个模型 * * Garbin * @param string $model_name * @param array $params * @param book $is_new * object */ function &m($model_name, $params = array(), $is_new = false) { static $models = array(); $model_hash = md5($model_name . var_export($params, true)); if ($is_new || !isset($models[$model_hash])) { $model_file = ROOT_PATH . '/includes/models/' . $model_name . '.model.php'; if (!is_file($model_file)) { /* 不存在该文件,则无法获取模型 */ return false; } include_once($model_file); $model_name = ucfirst($model_name) . 'Model'; if ($is_new) { return new $model_name($params, db()); } $models[$model_hash] = new $model_name($params, db()); } return $models[$model_hash]; } /** * 获取一个业务模型 * * @param string $model_name * @param array $params * @param bool $is_new * object */ function &bm($model_name, $params = array(), $is_new = false) { static $models = array(); $model_hash = md5($model_name . var_export($params, true)); if ($is_new || !isset($models[$model_hash])) { $model_file = ROOT_PATH . '/includes/models/' . $model_name . '.model.php'; if (!is_file($model_file)) { /* 不存在该文件,则无法获取模型 */ return false; } include_once($model_file); $model_name = ucfirst($model_name) . 'BModel'; if ($is_new) { return new $model_name($params, db()); } $models[$model_hash] = new $model_name($params, db()); } return $models[$model_hash]; } /** * 获取当前控制器实例 * * Garbin * void */ function c(&$app) { $GLOBALS['ECMALL_APP'] =& $app; } /** * 获取当前控制器 * * Garbin * Object */ function &cc() { return $GLOBALS['ECMALL_APP']; } /** * 导入一个类 * * Garbin * void */ function import() { $c = func_get_args(); if (empty($c)) { return; } array_walk($c, create_function('$item, $key', 'include_once(ROOT_PATH . \'/includes/libraries/\' . $item . \'.php\');')); } /** * 将default.abc类的字符串转为$default['abc'] * * Garbin * @param string $str * string */ function strtokey($str, $owner = '') { if (!$str) { return ''; } if ($owner) { return $owner . '[\'' . str_replace('.', '\'][\'', $str) . '\']'; } else { $parts = explode('.', $str); $owner = '$' . $parts[0]; unset($parts[0]); return strtokey(implode('.', $parts), $owner); } } /** * 跟踪调试 * * Garbin * @param mixed $var * void */ function trace($var) { static $i = 0; echo $i, '.', var_dump($var), '<br />'; $i++; } /** * rdump的别名 * * Garbin * @param any * void */ function dump($arr) { $args = func_get_args(); call_user_func_array('rdump', $args); } /** * 格式化显示出变量 * * Garbin * @param any * void */ function rdump($arr) { echo '<pre>'; array_walk(func_get_args(), create_function('&$item, $key', 'print_r($item);')); echo '</pre>'; exit(); } /** * 格式化并显示出变量类型 * * Garbin * @param any * void */ function vdump($arr) { echo '<pre>'; array_walk(func_get_args(), create_function('&$item, $key', 'var_dump($item);')); echo '</pre>'; exit(); } /** * 创建MySQL数据库对象实例 * * @author wj * @return object */ function &db() { include_once(ROOT_PATH . '/eccore/model/mysql.php'); static $db = null; if ($db === null) { $cfg = parse_url(DB_CONFIG); if ($cfg['scheme'] == 'mysql') { if (empty($cfg['pass'])) { $cfg['pass'] = ''; } else { $cfg['pass'] = urldecode($cfg['pass']); } $cfg ['user'] = urldecode($cfg['user']); if (empty($cfg['path'])) { trigger_error('Invalid database name.', E_USER_ERROR); } else { $cfg['path'] = str_replace('/', '', $cfg['path']); } $charset = (CHARSET == 'utf-8') ? 'utf8' : CHARSET; $db = new cls_mysql(); $db->cache_dir = ROOT_PATH. '/temp/query_caches/'; $db->connect($cfg['host']. ':' .$cfg['port'], $cfg['user'], $cfg['pass'], $cfg['path'], $charset); } else { trigger_error('Unkown database type.', E_USER_ERROR); } } return $db; } /** * 获得当前的域名 * * @return string */ function get_domain() { /* 协议 */ $protocol = (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://'; /* 域名或IP地址 */ if (isset($_SERVER['HTTP_X_FORWARDED_HOST'])) { $host = $_SERVER['HTTP_X_FORWARDED_HOST']; } elseif (isset($_SERVER['HTTP_HOST'])) { $host = $_SERVER['HTTP_HOST']; } else { /* 端口 */ if (isset($_SERVER['SERVER_PORT'])) { $port = ':' . $_SERVER['SERVER_PORT']; if ((':80' == $port && 'http://' == $protocol) || (':443' == $port && 'https://' == $protocol)) { $port = ''; } } else { $port = ''; } if (isset($_SERVER['SERVER_NAME'])) { $host = $_SERVER['SERVER_NAME'] . $port; } elseif (isset($_SERVER['SERVER_ADDR'])) { $host = $_SERVER['SERVER_ADDR'] . $port; } } return $protocol . $host; } /** * 获得网站的URL地址 * * @return string */ function site_url() { return get_domain() . substr(PHP_SELF, 0, strrpos(PHP_SELF, '/')); } /** * 截取UTF-8编码下字符串的函数 * * @param string $str 被截取的字符串 * @param int $length 截取的长度 * @param bool $append 是否附加省略号 * * @return string */ function sub_str($string, $length = 0, $append = true) { if(strlen($string) <= $length) { return $string; } $string = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $string); $strcut = ''; if(strtolower(CHARSET) == 'utf-8') { $n = $tn = $noc = 0; while($n < strlen($string)) { $t = ord($string[$n]); if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) { $tn = 1; $n++; $noc++; } elseif(194 <= $t && $t <= 223) { $tn = 2; $n += 2; $noc += 2; } elseif(224 <= $t && $t < 239) { $tn = 3; $n += 3; $noc += 2; } elseif(240 <= $t && $t <= 247) { $tn = 4; $n += 4; $noc += 2; } elseif(248 <= $t && $t <= 251) { $tn = 5; $n += 5; $noc += 2; } elseif($t == 252 || $t == 253) { $tn = 6; $n += 6; $noc += 2; } else { $n++; } if($noc >= $length) { break; } } if($noc > $length) { $n -= $tn; } $strcut = substr($string, 0, $n); } else { for($i = 0; $i < $length; $i++) { $strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i]; } } $strcut = str_replace(array('&', '"', '<', '>'), array('&', '"', '<', '>'), $strcut); if ($append && $string != $strcut) { $strcut .= '...'; } return $strcut; } /** * 获得用户的真实IP地址 * * @return string */ function real_ip() { static $realip = NULL; if ($realip !== NULL) { return $realip; } if (isset($_SERVER)) { if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) { $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']); /* 取X-Forwarded-For中第一个非unknown的有效IP字符串 */ foreach ($arr AS $ip) { $ip = trim($ip); if ($ip != 'unknown') { $realip = $ip; break; } } } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) { $realip = $_SERVER['HTTP_CLIENT_IP']; } else { if (isset($_SERVER['REMOTE_ADDR'])) { $realip = $_SERVER['REMOTE_ADDR']; } else { $realip = '0.0.0.0'; } } } else { if (getenv('HTTP_X_FORWARDED_FOR')) { $realip = getenv('HTTP_X_FORWARDED_FOR'); } elseif (getenv('HTTP_CLIENT_IP')) { $realip = getenv('HTTP_CLIENT_IP'); } else { $realip = getenv('REMOTE_ADDR'); } } preg_match("/[\d\.]{7,15}/", $realip, $onlineip); $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0'; return $realip; } /** * 验证输入的邮件地址是否合法 * * @param string $email 需要验证的邮件地址 * * bool */ function is_email($user_email) { $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,5}\$/i"; if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) { if (preg_match($chars, $user_email)) { return true; } else { return false; } } else { return false; } } /** * 检查是否为一个合法的时间格式 * * @param string $time * @return void */ function is_time($time) { $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/'; return preg_match($pattern, $time); }