Skip to content

Commit

Permalink
Merge pull request #6 from liu21st/master
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
vus520 committed May 28, 2013
2 parents 31b3606 + cc5a1b5 commit 37d5ec9
Show file tree
Hide file tree
Showing 11 changed files with 800 additions and 819 deletions.
27 changes: 16 additions & 11 deletions Extend/Behavior/CheckLangBehavior.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,23 @@ private function checkLanguage() {
define('LANG_SET',strtolower($langSet));

$group = '';
$lang_path = (defined('GROUP_NAME') && C('APP_GROUP_MODE')==1) ? BASE_LIB_PATH.'Lang/'.LANG_SET.'/' : LANG_PATH.LANG_SET.'/';
// 读取项目或者独立分组公共语言包
if (is_file($lang_path.'common.php'))
L(include $lang_path.'common.php');
// 普通分组公共语言包
if (defined('GROUP_NAME') && C('APP_GROUP_MODE')==0){
if (is_file($lang_path.GROUP_NAME.'.php'))
L(include $lang_path.GROUP_NAME.'.php');
$group = GROUP_NAME.C('TMPL_FILE_DEPR');
$path = (defined('GROUP_NAME') && C('APP_GROUP_MODE')==1) ? BASE_LIB_PATH.'Lang/'.LANG_SET.'/' : LANG_PATH.LANG_SET.'/';
// 读取项目公共语言包
if(is_file(LANG_PATH.LANG_SET.'/common.php'))
L(include LANG_PATH.LANG_SET.'/common.php');
// 读取分组公共语言包
if(defined('GROUP_NAME')){
if(C('APP_GROUP_MODE')==1){ // 独立分组
$file = $path.'common.php';
}else{ // 普通分组
$file = $path.GROUP_NAME.'.php';
$group = GROUP_NAME.C('TMPL_FILE_DEPR');
}
if(is_file($file))
L(include $file);
}
// 读取当前模块语言包
if (is_file($lang_path.$group.strtolower(MODULE_NAME).'.php'))
L(include $lang_path.$group.strtolower(MODULE_NAME).'.php');
if (is_file($path.$group.strtolower(MODULE_NAME).'.php'))
L(include $path.$group.strtolower(MODULE_NAME).'.php');
}
}
108 changes: 98 additions & 10 deletions Extend/Engine/Sae/Common/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,85 @@
* @author liu21st <liu21st@gmail.com>
*/

/**
* 获取输入参数 支持过滤和默认值
* 使用方法:
* <code>
* I('id',0); 获取id参数 自动判断get或者post
* I('post.name','','htmlspecialchars'); 获取$_POST['name']
* I('get.'); 获取$_GET
* </code>
* @param string $name 变量的名称 支持指定类型
* @param mixed $default 不存在的时候默认值
* @param mixed $filter 参数过滤方法
* @return mixed
*/
function I($name,$default='',$filter=null) {
if(strpos($name,'.')) { // 指定参数来源
list($method,$name) = explode('.',$name);
}else{ // 默认为自动判断
$method = 'param';
}
switch(strtolower($method)) {
case 'get' : $input =& $_GET;break;
case 'post' : $input =& $_POST;break;
case 'put' : parse_str(file_get_contents('php://input'), $input);break;
case 'param' :
switch($_SERVER['REQUEST_METHOD']) {
case 'POST':
$input = $_POST;
break;
case 'PUT':
parse_str(file_get_contents('php://input'), $input);
break;
default:
$input = $_GET;
}
if(C('VAR_URL_PARAMS') && isset($_GET[C('VAR_URL_PARAMS')])){
$input = array_merge($input,$_GET[C('VAR_URL_PARAMS')]);
}
break;
case 'request' : $input =& $_REQUEST; break;
case 'session' : $input =& $_SESSION; break;
case 'cookie' : $input =& $_COOKIE; break;
case 'server' : $input =& $_SERVER; break;
case 'globals' : $input =& $GLOBALS; break;
default:
return NULL;
}
// 全局过滤
// array_walk_recursive($input,'filter_exp');
if(C('VAR_FILTERS')) {
$_filters = explode(',',C('VAR_FILTERS'));
foreach($_filters as $_filter){
// 全局参数过滤
array_walk_recursive($input,$_filter);
}
}
if(empty($name)) { // 获取全部变量
$data = $input;
}elseif(isset($input[$name])) { // 取值操作
$data = $input[$name];
$filters = isset($filter)?$filter:C('DEFAULT_FILTER');
if($filters) {
$filters = explode(',',$filters);
foreach($filters as $filter){
if(function_exists($filter)) {
$data = is_array($data)?array_map($filter,$data):$filter($data); // 参数过滤
}else{
$data = filter_var($data,is_int($filter)?$filter:filter_id($filter));
if(false === $data) {
return isset($default)?$default:NULL;
}
}
}
}
}else{ // 变量默认值
$data = isset($default)?$default:NULL;
}
return $data;
}

/**
* 记录和统计时间(微秒)和内存使用情况
* 使用方法:
Expand Down Expand Up @@ -157,7 +236,7 @@ function file_exists_case($filename) {
* @param string $class 类库命名空间字符串
* @param string $baseUrl 起始路径
* @param string $ext 导入的文件扩展名
* @return boolen
* @return boolean
*/
function import($class, $baseUrl = '', $ext='.class.php') {
static $_file = array();
Expand Down Expand Up @@ -274,10 +353,16 @@ function D($name='',$layer='') {
$name = C('DEFAULT_APP').'/'.$layer.'/'.$name;
}
if(isset($_model[$name])) return $_model[$name];
import($name.$layer);
$path = explode('/',$name);
if(count($path)>3 && 1 == C('APP_GROUP_MODE')) { // 独立分组
$baseUrl = $path[0]== '@' ? dirname(BASE_LIB_PATH) : APP_PATH.'../'.$path[0].'/'.C('APP_GROUP_PATH').'/';
import($path[2].'/'.$path[1].'/'.$path[3].$layer,$baseUrl);
}else{
import($name.$layer);
}
$class = basename($name.$layer);
if(class_exists($class)) {
$model = new $class();
$model = new $class(basename($name));
}else {
$model = new Model(basename($name));
}
Expand Down Expand Up @@ -309,6 +394,7 @@ function M($name='', $tablePrefix='',$connection='') {
* A函数用于实例化Action 格式:[项目://][分组/]模块
* @param string $name Action资源地址
* @param string $layer 控制层名称
* @param boolean $common 是否公共目录
* @return Action|false
*/
function A($name,$layer='',$common=false) {
Expand All @@ -320,15 +406,19 @@ function A($name,$layer='',$common=false) {
$name = '@/'.$layer.'/'.$name;
}
if(isset($_action[$name])) return $_action[$name];
if($common){ // 独立分组情况下 加载公共目录类库
$path = explode('/',$name);
if(count($path)>3 && 1 == C('APP_GROUP_MODE')) { // 独立分组
$baseUrl = $path[0]== '@' ? dirname(BASE_LIB_PATH) : APP_PATH.'../'.$path[0].'/'.C('APP_GROUP_PATH').'/';
import($path[2].'/'.$path[1].'/'.$path[3].$layer,$baseUrl);
}elseif($common) { // 加载公共类库目录
import(str_replace('@/','',$name).$layer,LIB_PATH);
}else{
import($name.$layer);
}
import($name.$layer);
}
$class = basename($name.$layer);
if(class_exists($class,false)) {
$action = new $class();
$_action[$name] = $action;
$action = new $class();
$_action[$name] = $action;
return $action;
}else {
return false;
Expand Down Expand Up @@ -505,8 +595,6 @@ function B($name, &$params=NULL) {
}
}



/**
* 去除代码中的空白和注释
* @param string $content 代码内容
Expand Down
83 changes: 46 additions & 37 deletions Extend/Engine/Sae/Common/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ function halt($error) {
$trace = debug_backtrace();
$e['message'] = $error;
$e['file'] = $trace[0]['file'];
$e['class'] = isset($trace[0]['class'])?$trace[0]['class']:'';
$e['function'] = isset($trace[0]['function'])?$trace[0]['function']:'';
$e['line'] = $trace[0]['line'];
$traceInfo = '';
$time = date('y-m-d H:i:m');
foreach ($trace as $t) {
$traceInfo .= '[' . $time . '] ' . $t['file'] . ' (' . $t['line'] . ') ';
$traceInfo .= $t['class'] . $t['type'] . $t['function'] . '(';
$traceInfo .= implode(', ', $t['args']);
$traceInfo .=')<br/>';
}
$e['trace'] = $traceInfo;
ob_start();
debug_print_backtrace();
$e['trace'] = ob_get_clean();
} else {
$e = $error;
}
Expand Down Expand Up @@ -70,7 +62,7 @@ function halt($error) {
*/
function throw_exception($msg, $type='ThinkException', $code=0) {
if (class_exists($type, false))
throw new $type($msg, $code, true);
throw new $type($msg, $code);
else
halt($msg); // 异常类型不存在则输出错误信息字串
}
Expand Down Expand Up @@ -265,7 +257,7 @@ function U($url='',$vars='',$suffix=true,$redirect=false,$domain=false) {
}
if(!empty($vars)) { // 添加参数
foreach ($vars as $var => $val){
if('' !== trim($val)) $url .= $depr . $var . $depr . $val;
if('' !== trim($val)) $url .= $depr . $var . $depr . urlencode($val);
}
}
if($suffix) {
Expand Down Expand Up @@ -295,11 +287,13 @@ function U($url='',$vars='',$suffix=true,$redirect=false,$domain=false) {
* @param string $name Widget名称
* @param array $data 传人的参数
* @param boolean $return 是否返回内容
* @param string $path Widget所在路径
* @return void
*/
function W($name, $data=array(), $return=false) {
$class = $name . 'Widget';
require_cache(BASE_LIB_PATH . 'Widget/' . $class . '.class.php');
$path = empty($path) ? BASE_LIB_PATH : $path;
require_cache($path . 'Widget/' . $class . '.class.php');
if (!class_exists($class))
throw_exception(L('_CLASS_NOT_EXIST_') . ':' . $class);
$widget = Think::instance($class);
Expand Down Expand Up @@ -365,13 +359,10 @@ function redirect($url, $time=0, $msg='') {
}
}


/**
* 全局缓存设置和读取
* 缓存管理
* @param string $name 缓存名称
* @param mixed $value 缓存值
* @param integer $expire 缓存有效期(秒)
* @param string $type 缓存类型
* @param array $options 缓存参数
* @return mixed
*/
Expand All @@ -394,17 +385,19 @@ function S($name,$value='',$options=null) {
}elseif(is_null($value)) { // 删除缓存
return $cache->rm($name);
}else { // 缓存数据
$expire = is_numeric($options)?$options:NULL;
if(is_array($options)) {
$expire = isset($options['expire'])?$options['expire']:NULL;
}else{
$expire = is_numeric($options)?$options:NULL;
}
return $cache->set($name, $value, $expire);
}
}

// S方法的别名 已经废除 不再建议使用
function cache($name,$value='',$options=null){
return S($name,$value,$options);
}


/**
* 快速文件数据读取和保存 针对简单类型数据 字符串、数组
* @param string $name 缓存名称
Expand Down Expand Up @@ -495,31 +488,47 @@ function to_guid_string($mix) {
/**
* XML编码
* @param mixed $data 数据
* @param string $encoding 数据编码
* @param string $root 根节点名
* @param string $item 数字索引的子节点名
* @param string $attr 根节点属性
* @param string $id 数字索引子节点key转换的属性名
* @param string $encoding 数据编码
* @return string
*/
function xml_encode($data, $encoding='utf-8', $root='think') {
$xml = '<?xml version="1.0" encoding="' . $encoding . '"?>';
$xml .= '<' . $root . '>';
$xml .= data_to_xml($data);
$xml .= '</' . $root . '>';
function xml_encode($data, $root='think', $item='item', $attr='', $id='id', $encoding='utf-8') {
if(is_array($attr)){
$_attr = array();
foreach ($attr as $key => $value) {
$_attr[] = "{$key}=\"{$value}\"";
}
$attr = implode(' ', $_attr);
}
$attr = trim($attr);
$attr = empty($attr) ? '' : " {$attr}";
$xml = "<?xml version=\"1.0\" encoding=\"{$encoding}\"?>";
$xml .= "<{$root}{$attr}>";
$xml .= data_to_xml($data, $item, $id);
$xml .= "</{$root}>";
return $xml;
}

/**
* 数据XML编码
* @param mixed $data 数据
* @param mixed $data 数据
* @param string $item 数字索引时的节点名称
* @param string $id 数字索引key转换为的属性名
* @return string
*/
function data_to_xml($data) {
$xml = '';
function data_to_xml($data, $item='item', $id='id') {
$xml = $attr = '';
foreach ($data as $key => $val) {
is_numeric($key) && $key = "item id=\"$key\"";
$xml .= "<$key>";
$xml .= ( is_array($val) || is_object($val)) ? data_to_xml($val) : $val;
list($key, ) = explode(' ', $key);
$xml .= "</$key>";
if(is_numeric($key)){
$id && $attr = " {$id}=\"{$key}\"";
$key = $item;
}
$xml .= "<{$key}{$attr}>";
$xml .= (is_array($val) || is_object($val)) ? data_to_xml($val, $item, $id) : $val;
$xml .= "</{$key}>";
}
return $xml;
}
Expand Down Expand Up @@ -711,7 +720,7 @@ function load_ext_file() {
* @return mixed
*/
function get_client_ip($type = 0) {
$type = $type ? 1 : 0;
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
Expand Down Expand Up @@ -762,4 +771,4 @@ function filter_exp(&$value){
if (in_array(strtolower($value),array('exp','or'))){
$value .= ' ';
}
}
}
10 changes: 5 additions & 5 deletions Extend/Engine/Sae/Common/runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
if(version_compare(PHP_VERSION,'5.2.0','<')) die('require PHP > 5.2.0 !');

// 版本信息
define('THINK_VERSION', '3.1.2');
define('THINK_VERSION', '3.1.3');

// 系统信息
if(version_compare(PHP_VERSION,'5.4.0','<')) {
Expand Down Expand Up @@ -101,7 +101,7 @@ function load_runtime_file() {
if(is_file($file)) require_cache($file);
}
//[sae] 加载系统类库别名定义
//alias_import(include SAE_PATH.'Conf/alias.php');
alias_import(include SAE_PATH.'Conf/alias.php');
//[sae]在sae下不对目录结构进行检查
if(APP_DEBUG){
//[sae] 调试模式切换删除编译缓存
Expand Down Expand Up @@ -139,8 +139,8 @@ function build_runtime_cache($append='') {
// 系统行为扩展文件统一编译
$content .= build_tags_cache();
//[sae] 编译SAE的alias
//$alias = include SAE_PATH.'Conf/alias.php';
//$content .= 'alias_import('.var_export($alias,true).');';
$alias = include SAE_PATH.'Conf/alias.php';
$content .= 'alias_import('.var_export($alias,true).');';
// 编译框架默认语言包和配置参数
$content .= $append."\nL(".var_export(L(),true).");C(".var_export(C(),true).');G(\'loadTime\');Think::Start();';
//[sae] 生成编译缓存文件
Expand All @@ -167,4 +167,4 @@ function build_tags_cache() {
// 记录加载文件时间
G('loadTime');
// 执行入口
Think::Start();
Think::Start();
Loading

0 comments on commit 37d5ec9

Please sign in to comment.