Skip to content

Commit

Permalink
config配置增加 $config['hide_x_powered_by_header'] 和
Browse files Browse the repository at this point in the history
$config['ajax_cross_domain'] 参数
breath-co2 committed Aug 13, 2013
1 parent d382c11 commit 60c0c77
Showing 3 changed files with 175 additions and 2 deletions.
35 changes: 35 additions & 0 deletions config.new.php
Original file line number Diff line number Diff line change
@@ -296,6 +296,41 @@
$config['asset_allow_suffix'] = 'js|css|jpg|jpeg|png|gif|bmp|pdf|html|htm|mp4|swf';


/**
* HTML5自动跨越请求支持
*
* 开启后,如果遇到AJAX跨越请求,则会自动加上 Access-Control-Allow-Origin 的支持
* 注意,只有支持HTML5的此协议的浏览器有用,IE6,7等浏览器这个
*
* header("Access-Control-Allow-Origin: http://.../');
*
* none - 不自动处理
* auto - 自动(可自动允许相同主域名下的所有的请求)
* 也可设置一个数组,指定允许的域名,支持通配符*。例如:
*
* $config['ajax_cross_domain'] = array
* (
* '*.myqee.com',
* '*.myqee.sinaapp.com',
* 'www.queyang.com',
* );
*
* @var auto | array
*/
$config['ajax_cross_domain'] = 'auto';


/**
* 是否隐藏 X-Powered-By 头部版本输出
*
* true - 隐藏
* false - 显示
* string - 自定义输出的头信息
*
* @var boolean | string
*/
$config['hide_x_powered_by_header'] = false;

/**
* nodejs 执行文件默认路径
* 此功能在devassets等处理css时用到,通常不用改,除非你的node安装目录不是默认目录
20 changes: 19 additions & 1 deletion core/classes/core.class.php
Original file line number Diff line number Diff line change
@@ -178,7 +178,25 @@ public static function setup($auto_execute = true)
if (!IS_CLI)
{
# 输出powered by信息
header('X-Powered-By: PHP/' . PHP_VERSION . ' MyQEE/' . Core::VERSION .'/'. Core::RELEASE);
$x_powered_by = (isset(Core::$config['hide_x_powered_by_header']) && Core::$config['hide_x_powered_by_header']) ? Core::$config['hide_x_powered_by_header'] : false;

if (is_string($x_powered_by))
{
$str = 'X-Powered-By: ' . trim(str_replace(array("\r", "\n", $x_powered_by), '', $x_powered_by));
}
else if (!$x_powered_by)
{
$str = 'X-Powered-By: PHP/' . PHP_VERSION . ' MyQEE/' . Core::VERSION .'('. Core::RELEASE .')';
}
else
{
$str = null;
}

if ($str)
{
header($str);
}
}

if (IS_DEBUG)
122 changes: 121 additions & 1 deletion core/classes/httpio.class.php
Original file line number Diff line number Diff line change
@@ -242,7 +242,7 @@ public function __construct()
public static function setup()
{
static $run = null;
if ( null === $run )
if (null===$run)
{
$run = true;
if (!IS_CLI)
@@ -267,6 +267,64 @@ public static function setup()

HttpIO::$uri =& Core::$path_info;
}

// 自动支持子域名AJAX请求
if (HttpIO::IS_AJAX && isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'])
{
HttpIO::auto_add_ajax_control_allow_origin();
}

}
}

/**
* 自动添加HTML5的AJAX跨越支持
*/
protected static function auto_add_ajax_control_allow_origin()
{
$ajax_cross_domain = Core::config('ajax_cross_domain');

if (false!==$ajax_cross_domain)
{
if ('nono'==$ajax_cross_domain)return ;

$info = parse_url($_SERVER['HTTP_REFERER']);
$host = $info['host'];

$add_allow_origin = false;

if (is_array($ajax_cross_domain))
{
foreach ($ajax_cross_domain as $item)
{
if (strpos($item, '*')!==false)
{
$preg = '#^'. str_replace('\\*', '*', preg_quote($item)) .'#$i';
if (preg_match($preg, $host))
{
$add_allow_origin = true;
break;
}
}
elseif ($host==$item)
{
$add_allow_origin = true;
break;
}
}
}
elseif ($ajax_cross_domain)
{
if ($_SERVER['HTTP_HOST']!=$host && HttpIO::get_primary_domain($_SERVER['HTTP_HOST']) == HttpIO::get_primary_domain($host))
{
$add_allow_origin = true;
}
}

if ($add_allow_origin)
{
header('Access-Control-Allow-Origin: http://' . $host . '/');
}
}
}

@@ -692,4 +750,66 @@ public static function url(array $params = null, $protocol = null)
return Core::url(HttpIO::uri($params), $protocol);
}

/**
* 获取一个域名的主域名
*
* 支持传入URL
*
* HttpIO::get_primary_domain('test.myqee.com'); //myqee.com
*
* HttpIO::get_primary_domain('http://v3.myqee.com/docs/'); //myqee.com
*
* @param string $host
* @return string
*/
public static function get_primary_domain($host)
{
$host = strtolower($host);
if(false!==strpos($host, '/'))
{
$parse = @parse_url($host);
$host = $parse['host'];
}

$top_level_domain = array
(
'com',
'edu',
'gov',
'int',
'mil',
'net',
'org',
'biz',
'info',
'pro',
'name',
'museum',
'coop',
'aero',
'xxx',
'idv',
'mobi',
'cc',
'me'
);

$str='';
foreach($top_level_domain 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;
}
}

0 comments on commit 60c0c77

Please sign in to comment.