You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

223 lines
5.3 KiB

<?php
use base\Config;
use base\Route;
/**
* 获取设置配置
* @param string $key 配置项
* @param mixed $value 配置值
* @return array
*/
function C($key = NULL, $value = NULL){
if( func_num_args() <= 1 ){
return Config::get($key);
}else{
return Config::set($key, $value);
}
}
/**
* URL生成
* @param string $route 地址
* @param array $params 参数
* @return string
*/
function U($route = null, $params = array(), $domain = false){
$url = Route::url($route, $params);
if (true === $domain) {
$domain = $_SERVER['HTTP_HOST'];
$url = (is_ssl() ? 'https://' : 'http://') . $domain . $url;
}
return $url;
}
/**
* 对象调用函数
* @param string $class 模块名/类名
* @param string $layer 模块层
* @return object
*/
function A($class, $layer = 'models'){
static $objArr = array();
$param = explode('/', $class, 2);
$paramCount = count($param);
switch ($paramCount) {
case 1:
$app = APP_NAME;
$module = $param[0];
break;
case 2:
$app = $param[0];
$module = $param[1];
break;
}
$app = strtolower($app);
$class = "\\apps\\{$app}\\{$layer}\\{$module}".ucfirst(rtrim($layer, 's'));
if(!class_exists($class)){
$class = "\\apps\\base\\{$layer}\\{$module}".ucfirst(rtrim($layer, 's'));
}
if(isset($objArr[$class])){
return $objArr[$class];
}
if(!class_exists($class)){
throw new \Exception("Class '{$class}' not found'", 500);
}
$obj = new $class();
$objArr[$class] = $obj;
return $obj;
}
/**
* 获得 ECSHOP 当前环境的 HTTP 协议方式
*
* @access public
*
* @return void
*/
function http()
{
return (isset($_SERVER['HTTPS']) && (strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
}
/**
* 取得当前的域名
*
* @access public
*
* @return string 当前的域名
*/
function get_domain()
{
/* 协议 */
$protocol = 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; //IP
}
}
return $protocol . $host;
}
/**
* * 获取顶级域名
* * @param unknown $url
* * @return unknown
* */
function get_top_domain($url = '')
{
$url = empty($url) ? get_domain() : $url;
$host = strtolower($url);
if (strpos($host, '/') !== false) {
$parse = @parse_url($host);
$host = $parse['host'];
}
$topleveldomaindb = array(
'com',
'edu',
'gov',
'int',
'mil',
'net',
'org',
'biz',
'info',
'pro',
'name',
'museum',
'coop',
'aero',
'xxx',
'idv',
'mobi',
'cc',
'me'
);
$str = '';
foreach ($topleveldomaindb as $v) {
$str .= ($str ? '|' : '') . $v;
}
$matchstr = "[^\.]+\.(?:(" . $str . ")|\w{2}|((" . $str . ")\.\w{2}))$";
if (preg_match("/" . $matchstr . "/ies", $host, $matchs)) {
$domain = $matchs['0'];
} else {
$domain = $host;
}
return $domain;
}
/**
* 自动注册类
*/
spl_autoload_register(function($class){
static $fileList = array();
$prefixes =array(
'base' => BASE_PATH,
'libraries' => BASE_PATH,
'classes' => BASE_PATH,
'vendor' => BASE_PATH,
'apps' => BASE_PATH,
'*' => BASE_PATH,
);
$class = ltrim($class, '\\');
if (false !== ($pos = strrpos($class, '\\')) ){
$namespace = substr($class, 0, $pos);
$className = substr($class, $pos + 1);
foreach ($prefixes as $prefix => $baseDir){
if ( '*'!==$prefix && 0!==strpos($namespace, $prefix) ) continue;
//file path case-insensitive
$fileDIR = $baseDir.str_replace('\\', '/', $namespace).'/';
if( !isset($fileList[$fileDIR]) ){
$fileList[$fileDIR] = array();
foreach(glob($fileDIR.'*.php') as $file){
$fileList[$fileDIR][] = $file;
}
}
$fileBase = $baseDir.str_replace('\\', '/', $namespace).'/'.$className;
foreach($fileList[$fileDIR] as $file){
if( false!==stripos($file, $fileBase) ){
require $file;
return true;
}
}
}
}
return false;
});