Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update

  • Loading branch information...
commit 35c3c0283e13e4150386e56837587fc294415ec3 1 parent 6bd58f0
@akira-cn authored
View
2  classes/jkit.php
@@ -40,8 +40,10 @@ class JKit extends Kohana{
public static $security = array(
'xss' => false,
'csrf' => false,
+ 'non-ajax access' => true, //json的非ajax访问
);
+
/**
* 读取配置的内容
*
View
32 classes/jkit/controller.php
@@ -25,7 +25,18 @@
*/
protected static $_template;
- protected $jsonp = false;
+ protected static $_controller;
+
+ /**
+ * 增加 debug 跟踪的对象
+ * $this->trace($key, $value)
+ * 将要设置的变量存入以便在debug中显示出来
+ *
+ * @var mixed
+ */
+ protected $_trace = array();
+
+ protected $auto_jsonp = false;
/**
* 获得当前框架的默认模板
@@ -38,6 +49,10 @@ public static function template(){
return self::$_template;
}
+ public static function current_controller(){
+ return self::$_controller;
+ }
+
/**
* 在 Controller 的 action 被调用前自动运行: 重载了 [Kohana_Controller::before]
* 如果设置了 `JKit::$environment=JKit::DEVELOPMENT` 并且在 url 中传递了 rdtest 参数,那么设置 [View::$debugging] 为 true,打开调试信息
@@ -48,6 +63,7 @@ function before(){
parent::before();
self::$_template = View::factory(str_replace('_', '/', $this->request->controller()) . '/' . $this->request->action());
+ self::$_controller = $this;
if (JKit::$security['csrf'] && count($this->request->post()))
{ //防止跨站请求伪造
@@ -84,13 +100,21 @@ function after(){
//追加调试信息
if(JKit::$environment == JKit::DEVELOPMENT && $this->request->param('rdtest')){
- $this->response->debug(array('_requested_params' => $this->request->param()));
+ $this->trace('_requested_params', $this->request->param());
+ $this->response->debug($this->_trace);
}
parent::after();
}
/**
+ * 设置 _trace 变量到模板中便于调试跟踪
+ */
+ protected function trace($key, $value){
+ $this->_trace[$key] = $value;
+ }
+
+ /**
* 重载魔术方法 __get,如果未创建过`$this->template`,那么创建之
*
* functions action_bar(){
@@ -159,7 +183,7 @@ protected function err($data=null, $msg=null, $forward=null, $callback=null, $ca
$result = Logic::parseResult($data, $msg, $forward, $catch_errs[0]);
//如果配置为默认支持jsonp
- if(!$callback && $this->jsonp){
+ if(!$callback && $this->auto_jsonp){
$callback = $this->request->param('cb'); //这个支持Route的callback
}
@@ -167,7 +191,7 @@ protected function err($data=null, $msg=null, $forward=null, $callback=null, $ca
if( !$catch_errs[0] && $result['err'] != 'ok' //如果不捕获特定状态,那么捕获除了ok之外的所有状态
|| in_array($result['err'], $catch_errs)){
//如果是ajax请求
- if($callback || $this->request->is_ajax()){ //如果有callback说明是jsonp,那么不一定用ajax
+ if($callback || JKit::$security['non-ajax access'] || $this->request->is_ajax()){ //如果有callback说明是jsonp,那么不一定用ajax
//直接返回json结果
$this->response->json($result, $callback)->send();
}
View
3  classes/jkit/response.php
@@ -56,7 +56,7 @@ public function json($data, $callback=null){
* @return Response
*/
public function jsonp($data){
- $cb = $_REQUEST['cb'];
+ $cb = Controller::current_controller()->request->param('cb');
return $this->json($data, $cb);
}
@@ -131,6 +131,7 @@ public function __toString()
* @uses Profiler::stop_by_group
*/
public function send(){
+ Controller::current_controller()->after();
Profiler::stop_by_group('Requests'); //结束Request的Profiler
echo $this->send_headers()->body();
exit;
View
22 classes/jkit/rpc/http.php
@@ -37,17 +37,17 @@ public function __construct($strServerName, $arrServers = array()) {
/**
* 进行HTTP交互
- *
- * $param = $arrInput {
+ *
+ * $param = $arrInput {
* action : $url,
* method : 'POST',
* post_vars : array(...),
* curl_opts : array(...),
* cookie : array(...),
* }
- * $strContent = $objRPC->call($param, 2);
- * $this->response->('content-type','text/html;charset=gbk')->send_headers()->body($strContent);
- *
+ * $strContent = $objRPC->call($param, 2);
+ * $this->response->('content-type','text/html;charset=gbk')->send_headers()->body($strContent);
+ *
* @param array 调用参数
* @param int 重试次数
* @return mixed 成功返回HTTP请求的响应字符串,失败返回false
@@ -64,13 +64,23 @@ public function call($arrInput, $intRetry = 1) {
}
curl_setopt($this->_resConn, CURLOPT_COOKIE, $strCookie);
}
+ $strUrl = empty($arrInput['action']) ? '/' : $arrInput['action'];
if (isset($arrInput['method']) && (strtolower($arrInput['method']) == 'post') ) {
curl_setopt($this->_resConn, CURLOPT_POST, 1);
if(isset($arrInput['post_vars'])) {
curl_setopt($this->_resConn, CURLOPT_POSTFIELDS, $arrInput['post_vars']);
}
+ } else {
+ if(isset($arrInput['post_vars'])) {
+ $strParam = http_build_query($arrInput['post_vars']);
+ if (strpos($strUrl, '?') === false) {
+ $strUrl .= "?{$strParam}";
+ } else {
+ $strUrl .= "&{$strParam}";
+ }
+ }
}
- $strUrl = isset($arrInput['action']) ? $arrInput['action'] : '/index.html';
+
$arrOutput = false;
while ($intRetry--) {
$this->connect();
View
11 classes/jkit/validation.php
@@ -221,21 +221,22 @@ public function parse_rules($rules){
//先解析 $datatype
if($section = $rule['datatype']){ //如果有datatype
- list($section_name, $pattern) = explode('-', $section);
+ $arrTmp = explode('-', $section);
+ $section_name = $arrTmp[0];
//解析跟在datatype后面的pattern,现在有三种格式 -m.n -/reg/ -a||b
- if($pattern){
- $rule[$section_name.'-pattern'] = $pattern;
+ if (isset($arrTmp[1])) {
+ $rule[$section_name.'-pattern'] = $arrTmp[1];
}
//解析datatype
- if($trans_rules = $this->datetype($section_name, $rule, $field_name))
+ if($trans_rules = @$this->datetype($section_name, $rule, $field_name))
{
$sections = array_merge($sections, $trans_rules);
}
//解析reqmsg
- if($rule['reqmsg']){
+ if(isset($rule['reqmsg'])){
array_push($sections, array('not_empty'));
unset($rule['reqmsg']);
}
View
6 classes/jkit/view.php
@@ -121,9 +121,6 @@ protected static function capture($kohana_view_file, array $kohana_view_data)
return $template->fetch($file);
}
else{
- if(strpos($file, 'string:') === 0){ //字符串模板
- return $template->fetch($file);
- }
//先把php的内容解析了
//$kohana_view_source = Kohana_View::capture($file, $kohana_view_data);
$kohana_view_source = self::capturePHP($file, $kohana_view_data);
@@ -131,6 +128,9 @@ protected static function capture($kohana_view_file, array $kohana_view_data)
self::$_temp_local_data = NULL;
try{
+ if(strpos($file, 'string:') === 0){ //字符串模板
+ return $template->fetch($file);
+ }
return $template->fetch('string:'.$kohana_view_source);
}catch(Exception $ex){
return $kohana_view_source;
View
40 config/rpc.php
@@ -0,0 +1,40 @@
+<?php
+/**
+ 配置示例
+$_arrRpcConfig = array(
+ 'baidu_news' => array(
+ 'type' => RPC_TYPE_HTTP,
+ 'option' => array(
+ 'ctimeout' => 200, //连接超时,单位毫秒
+ 'rtimeout' => 1000, //读超时,单位毫秒
+ 'wtimeout' => 1000, //写超时,单位毫秒
+ 'balance' => 'Rpc_Balance_RoundRobin',
+ ),
+ 'server' => array(
+ array('host' => 'news.baidu.com', 'port' => 80),
+ array('host' => '220.181.112.138', 'port' => 80),
+ ),
+ ),
+ 'reco_video' => array(
+ 'type' => RPC_TYPE_THRIFT,
+ 'option' => array(
+ 'balance' => 'Rpc_Balance_RoundRobin',
+ 'transport' => 'TBufferedTransport',
+ ),
+ 'server' => array(
+ array('host' => '10.34.7.172', 'port' => 10021),
+ array('host' => '10.1.121.81', 'port' => 10021),
+ ),
+ ),
+ 'example_ttserver' => array(
+ 'type' => RPC_TYPE_TTSERVER,
+ 'option' => array(
+ 'balance' => 'Rpc_Balance_RoundRobin',
+ ),
+ 'server' => array(
+ array('host' => '10.34.7.172', 'port' => 10021),
+ ),
+ ),
+);
+*/
+return array();
View
1  init.php
@@ -23,6 +23,7 @@
//安全设置
JKit::$security['csrf'] = true;
JKit::$security['xss'] = true;
+JKit::$security['non-ajax access'] = true;
/**
* Attach a file reader to config. Multiple readers are supported.
Please sign in to comment.
Something went wrong with that request. Please try again.