mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-09-02 05:31:36 +08:00
核心文件规范调整
This commit is contained in:
@@ -76,9 +76,9 @@ class App {
|
||||
// 执行操作
|
||||
if(!preg_match('/^[A-Za-z](\/|\w)*$/',CONTROLLER_NAME)){ // 安全检测
|
||||
$instance = false;
|
||||
}elseif(isset($config['action_bind_class']) && $config['action_bind_class']){
|
||||
}elseif($config['action_bind_class']){
|
||||
// 操作绑定到类:模块\Controller\控制器\操作
|
||||
$layer = $config['controller_layer'];
|
||||
$layer = CONTROLLER_LAYER;
|
||||
if(is_dir(MODULE_PATH.$layer.'/'.CONTROLLER_NAME)){
|
||||
$namespace = MODULE_NAME.'\\'.$layer.'\\'.CONTROLLER_NAME.'\\';
|
||||
}else{
|
||||
@@ -279,7 +279,7 @@ class App {
|
||||
|
||||
static private function getModule($config){
|
||||
$module = strtolower(isset($_GET[$config['var_module']]) ? $_GET[$config['var_module']] : $config['default_module']);
|
||||
if(isset($config['url_module_map']) && $maps = $config['url_module_map']) {
|
||||
if($maps = $config['url_module_map']) {
|
||||
if(isset($maps[$module])) {
|
||||
// 记录当前别名
|
||||
define('MODULE_ALIAS',$module);
|
||||
@@ -290,6 +290,6 @@ class App {
|
||||
$module = '';
|
||||
}
|
||||
}
|
||||
return strip_tags(ucwords($module));
|
||||
return strip_tags($module);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,59 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Auto {
|
||||
|
||||
protected $auto = [];
|
||||
|
||||
public function rule($rule){
|
||||
$this->auto = $rule;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动表单处理
|
||||
* @access public
|
||||
* @param array $data 创建数据
|
||||
* @return mixed
|
||||
*/
|
||||
public function operate($data) {
|
||||
// 自动填充
|
||||
if($this->auto) {
|
||||
foreach ($this->auto as $auto){
|
||||
// 填充因子定义格式
|
||||
// array('field','填充内容','附加规则',[额外参数])
|
||||
switch(trim($auto[2])) {
|
||||
case 'callback': // 使用回调方法
|
||||
$args = isset($auto[3])?(array)$auto[3]:[];
|
||||
if(isset($data[$auto[0]])) {
|
||||
array_unshift($args,$data[$auto[0]]);
|
||||
}
|
||||
$data[$auto[0]] = call_user_func_array($auto[1], $args);
|
||||
break;
|
||||
case 'field': // 用其它字段的值进行填充
|
||||
$data[$auto[0]] = $data[$auto[1]];
|
||||
break;
|
||||
case 'ignore': // 为空忽略
|
||||
if(''===$data[$auto[0]])
|
||||
unset($data[$auto[0]]);
|
||||
break;
|
||||
case 'string':
|
||||
default: // 默认作为字符串填充
|
||||
$data[$auto[0]] = $auto[1];
|
||||
}
|
||||
if(false === $data[$auto[0]] ) unset($data[$auto[0]]);
|
||||
}
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Controller;
|
||||
abstract class Amf {
|
||||
|
||||
/**
|
||||
* PHPRpc控制器架构函数
|
||||
* @access public
|
||||
*/
|
||||
public function __construct() {
|
||||
//导入类库
|
||||
Think\Loader::import('Vendor.Zend.Amf.Server');
|
||||
//实例化AMF
|
||||
$server = new \Zend_Amf_Server();
|
||||
$server -> setClass($this);
|
||||
echo $server -> handle();
|
||||
return ;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Controller;
|
||||
|
||||
abstract class Phprpc {
|
||||
/**
|
||||
* PHPRpc控制器架构函数
|
||||
* @access public
|
||||
*/
|
||||
public function __construct() {
|
||||
//导入类库
|
||||
Think\Loader::import('Vendor.phpRPC.phprpc_server');
|
||||
//实例化phprpc
|
||||
$server = new \PHPRPC_Server();
|
||||
$server->add($this);
|
||||
if(APP_DEBUG) {
|
||||
$server->setDebugMode(true);
|
||||
}
|
||||
$server->setEnableGZIP(true);
|
||||
$server->start();
|
||||
//C('PHPRPC_COMMENT',$server->comment());
|
||||
echo $server->comment();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,215 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2012 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Controller;
|
||||
|
||||
abstract class Rest {
|
||||
|
||||
protected $_method = ''; // 当前请求类型
|
||||
protected $_type = ''; // 当前资源类型
|
||||
// 输出类型
|
||||
protected $restMethodList = 'get|post|put|delete';
|
||||
protected $restDefaultMethod = 'get';
|
||||
protected $restTypeList = 'html|xml|json|rss';
|
||||
protected $restDefaultType = 'html';
|
||||
protected $restOutputType = [ // REST允许输出的资源类型列表
|
||||
'xml' => 'application/xml',
|
||||
'json' => 'application/json',
|
||||
'html' => 'text/html',
|
||||
];
|
||||
|
||||
/**
|
||||
* 架构函数 取得模板对象实例
|
||||
* @access public
|
||||
*/
|
||||
public function __construct() {
|
||||
// 资源类型检测
|
||||
if(''==__EXT__) { // 自动检测资源类型
|
||||
$this->_type = $this->getAcceptType();
|
||||
}elseif(!preg_match('/\('.$this->restTypeList.')$/i',__EXT__)) {
|
||||
// 资源类型非法 则用默认资源类型访问
|
||||
$this->_type = $this->restDefaultType;
|
||||
}else{
|
||||
$this->_type = __EXT__;
|
||||
}
|
||||
// 请求方式检测
|
||||
$method = strtolower($_SERVER['REQUEST_METHOD']);
|
||||
if(false === stripos($this->restMethodList,$method)) {
|
||||
// 请求方式非法 则用默认请求方法
|
||||
$method = $this->restDefaultMethod;
|
||||
}
|
||||
$this->_method = $method;
|
||||
}
|
||||
|
||||
/**
|
||||
* REST 调用
|
||||
* @access public
|
||||
* @param string $method 方法名
|
||||
* @param array $args 参数
|
||||
* @return mixed
|
||||
*/
|
||||
public function _empty($method,$args) {
|
||||
if(method_exists($this,$method.'_'.$this->_method.'_'.$this->_type)) { // RESTFul方法支持
|
||||
$fun = $method.'_'.$this->_method.'_'.$this->_type;
|
||||
}elseif($this->_method == $this->restDefaultMethod && method_exists($this,$method.'_'.$this->_type) ){
|
||||
$fun = $method.'_'.$this->_type;
|
||||
}elseif($this->_type == $this->restDefaultType && method_exists($this,$method.'_'.$this->_method) ){
|
||||
$fun = $method.'_'.$this->_method;
|
||||
}
|
||||
if(isset($fun)) {
|
||||
$this->$fun();
|
||||
}else{
|
||||
// 抛出异常
|
||||
E(L('_ERROR_ACTION_:').ACTION_NAME);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置页面输出的CONTENT_TYPE和编码
|
||||
* @access public
|
||||
* @param string $type content_type 类型对应的扩展名
|
||||
* @param string $charset 页面输出编码
|
||||
* @return void
|
||||
*/
|
||||
public function setContentType($type, $charset='utf-8'){
|
||||
if(headers_sent()) return;
|
||||
$type = strtolower($type);
|
||||
if(isset($this->restOutputType[$type])) //过滤content_type
|
||||
header('Content-Type: '.$this->restOutputType[$type].'; charset='.$charset);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出返回数据
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param String $type 返回类型 JSON XML
|
||||
* @param integer $code HTTP状态
|
||||
* @return void
|
||||
*/
|
||||
protected function response($data,$type='',$code=200) {
|
||||
$this->sendHttpStatus($code);
|
||||
exit($this->encodeData($data,strtolower($type)));
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码数据
|
||||
* @access protected
|
||||
* @param mixed $data 要返回的数据
|
||||
* @param String $type 返回类型 JSON XML
|
||||
* @return void
|
||||
*/
|
||||
protected function encodeData($data,$type='') {
|
||||
if(empty($data)) return '';
|
||||
if('json' == $type) {
|
||||
// 返回JSON数据格式到客户端 包含状态信息
|
||||
$data = json_encode($data);
|
||||
}elseif('xml' == $type){
|
||||
// 返回xml格式数据
|
||||
$data = xml_encode($data);
|
||||
}elseif('php'==$type){
|
||||
$data = serialize($data);
|
||||
}// 默认直接输出
|
||||
$this->setContentType($type);
|
||||
header('Content-Length: ' . strlen($data));
|
||||
return $data;
|
||||
}
|
||||
|
||||
// 发送Http状态信息
|
||||
protected function sendHttpStatus($status) {
|
||||
static $_status = [
|
||||
// Informational 1xx
|
||||
100 => 'Continue',
|
||||
101 => 'Switching Protocols',
|
||||
// Success 2xx
|
||||
200 => 'OK',
|
||||
201 => 'Created',
|
||||
202 => 'Accepted',
|
||||
203 => 'Non-Authoritative Information',
|
||||
204 => 'No Content',
|
||||
205 => 'Reset Content',
|
||||
206 => 'Partial Content',
|
||||
// Redirection 3xx
|
||||
300 => 'Multiple Choices',
|
||||
301 => 'Moved Permanently',
|
||||
302 => 'Moved Temporarily ', // 1.1
|
||||
303 => 'See Other',
|
||||
304 => 'Not Modified',
|
||||
305 => 'Use Proxy',
|
||||
// 306 is deprecated but reserved
|
||||
307 => 'Temporary Redirect',
|
||||
// Client Error 4xx
|
||||
400 => 'Bad Request',
|
||||
401 => 'Unauthorized',
|
||||
402 => 'Payment Required',
|
||||
403 => 'Forbidden',
|
||||
404 => 'Not Found',
|
||||
405 => 'Method Not Allowed',
|
||||
406 => 'Not Acceptable',
|
||||
407 => 'Proxy Authentication Required',
|
||||
408 => 'Request Timeout',
|
||||
409 => 'Conflict',
|
||||
410 => 'Gone',
|
||||
411 => 'Length Required',
|
||||
412 => 'Precondition Failed',
|
||||
413 => 'Request Entity Too Large',
|
||||
414 => 'Request-URI Too Long',
|
||||
415 => 'Unsupported Media Type',
|
||||
416 => 'Requested Range Not Satisfiable',
|
||||
417 => 'Expectation Failed',
|
||||
// Server Error 5xx
|
||||
500 => 'Internal Server Error',
|
||||
501 => 'Not Implemented',
|
||||
502 => 'Bad Gateway',
|
||||
503 => 'Service Unavailable',
|
||||
504 => 'Gateway Timeout',
|
||||
505 => 'HTTP Version Not Supported',
|
||||
509 => 'Bandwidth Limit Exceeded'
|
||||
];
|
||||
if(isset($_status[$code])) {
|
||||
header('HTTP/1.1 '.$code.' '.$_status[$code]);
|
||||
// 确保FastCGI模式下正常
|
||||
header('Status:'.$code.' '.$_status[$code]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求的Accept头信息
|
||||
* @return string
|
||||
*/
|
||||
protected function getAcceptType(){
|
||||
$type = [
|
||||
'html' => 'text/html,application/xhtml+xml,*/*',
|
||||
'xml' => 'application/xml,text/xml,application/x-xml',
|
||||
'json' => 'application/json,text/x-json,application/jsonrequest,text/json',
|
||||
'js' => 'text/javascript,application/javascript,application/x-javascript',
|
||||
'css' => 'text/css',
|
||||
'rss' => 'application/rss+xml',
|
||||
'yaml' => 'application/x-yaml,text/yaml',
|
||||
'atom' => 'application/atom+xml',
|
||||
'pdf' => 'application/pdf',
|
||||
'text' => 'text/plain',
|
||||
'png' => 'image/png',
|
||||
'jpg' => 'image/jpg,image/jpeg,image/pjpeg',
|
||||
'gif' => 'image/gif',
|
||||
'csv' => 'text/csv'
|
||||
];
|
||||
|
||||
foreach($type as $key=>$val){
|
||||
$array = explode(',',$val);
|
||||
foreach($array as $k=>$v){
|
||||
if(stristr($_SERVER['HTTP_ACCEPT'], $v)) {
|
||||
return $key;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -46,19 +46,19 @@ class Create {
|
||||
}
|
||||
foreach($file as $val){
|
||||
switch($path) {
|
||||
case 'Controller':// 控制器
|
||||
case 'controller':// 控制器
|
||||
$filename = ucwords($val).$path;
|
||||
if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) {
|
||||
file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"<?php\nnamespace {$module}\\{$path};\nclass {$filename} {\n}");
|
||||
}
|
||||
break;
|
||||
case 'Model': // 模型
|
||||
case 'model': // 模型
|
||||
$filename = ucwords($val).$path;
|
||||
if(!is_file(APP_PATH.$module.'/'.$path.'/'.$filename.'.php')) {
|
||||
file_put_contents(APP_PATH.$module.'/'.$path.'/'.$filename.'.php',"<?php\nnamespace {$module}\\{$path};\nclass {$filename} extends \Think\Model{\n}");
|
||||
}
|
||||
break;
|
||||
case 'View': // 视图
|
||||
case 'view': // 视图
|
||||
break;
|
||||
default:
|
||||
$filename = ucwords($val).$path;
|
||||
@@ -77,13 +77,13 @@ class Create {
|
||||
|
||||
// 创建欢迎页面
|
||||
static public function buildHelloController($module) {
|
||||
if(!is_file(APP_PATH.$module.'/Controller/IndexController.php')) {
|
||||
$content = file_get_contents(THINK_PATH.'Tpl/default_index.tpl');
|
||||
if(!is_file(APP_PATH.$module.'/controller/index_controller.php')) {
|
||||
$content = file_get_contents(THINK_PATH.'tpl/default_index.tpl');
|
||||
$content = str_replace('{$module}',$module,$content);
|
||||
if(!is_dir(APP_PATH.$module.'/Controller')) {
|
||||
mkdir(APP_PATH.$module.'/Controller');
|
||||
if(!is_dir(APP_PATH.$module.'/controller')) {
|
||||
mkdir(APP_PATH.$module.'/controller');
|
||||
}
|
||||
file_put_contents(APP_PATH.$module.'/Controller/IndexController.php',$content);
|
||||
file_put_contents(APP_PATH.$module.'/controller/index_controller.php',$content);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -32,13 +32,9 @@ class Loader {
|
||||
}else{ // 项目命名空间
|
||||
$path = APP_PATH;
|
||||
}
|
||||
//$path = isset(self::$namespace[$name]) ? dirname(self::$namespace[$name]) . '/' : APP_PATH;
|
||||
$filename = $path . str_replace('\\', '/', $class) . EXT;
|
||||
$filename = $path . str_replace('\\', '/', str_replace('\\_','\\',strtolower(trim(preg_replace("/[A-Z]/", "_\\0", $class), "_")))) . EXT;
|
||||
var_dump($filename);
|
||||
if(is_file($filename)) {
|
||||
// Win环境下面严格区分大小写
|
||||
if (IS_WIN && false === strpos(str_replace('/', '\\', realpath($filename)), $class . EXT)){
|
||||
return ;
|
||||
}
|
||||
include $filename;
|
||||
}
|
||||
}
|
||||
@@ -60,7 +56,7 @@ class Loader {
|
||||
|
||||
// 注册自动加载机制
|
||||
static public function register($autoload = ''){
|
||||
spl_autoload_register($autoload ? $autoload : ['Think\Loader', 'autoload']);
|
||||
spl_autoload_register($autoload ? $autoload : ['think\loader', 'autoload']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,10 +79,10 @@ class Loader {
|
||||
//加载当前项目应用类库
|
||||
$class = substr_replace($class, '', 0, strlen($class_strut[0]) + 1);
|
||||
$baseUrl = MODULE_PATH;
|
||||
}elseif (in_array($class_strut[0], ['Think','Org', 'Com'])) {
|
||||
}elseif (in_array($class_strut[0], ['think','org', 'com'])) {
|
||||
// org 第三方公共类库 com 企业公共类库
|
||||
$baseUrl = LIB_PATH;
|
||||
}elseif(in_array($class_strut[0], ['Vendor', 'Traits'])){
|
||||
}elseif(in_array($class_strut[0], ['vendor', 'traits'])){
|
||||
$baseUrl = THINK_PATH;
|
||||
}else { // 加载其他项目应用类库
|
||||
$class = substr_replace($class, '', 0, strlen($class_strut[0]) + 1);
|
||||
@@ -115,7 +111,7 @@ class Loader {
|
||||
if(strpos($name, ':')) {
|
||||
list($class, $name) = explode(':', $name);
|
||||
}else{
|
||||
$class = 'Think\Model';
|
||||
$class = 'think\model';
|
||||
}
|
||||
$guid = $name . '_' . $class;
|
||||
if (!isset($_model[$guid]))
|
||||
@@ -142,6 +138,7 @@ class Loader {
|
||||
}else{
|
||||
$module = MODULE_NAME;
|
||||
}
|
||||
$layer = ucwords($layer);
|
||||
$class = $module . '\\' . $layer . '\\' . parse_name($name, 1). $layer;
|
||||
if(class_exists($class)) {
|
||||
$model = new $class($name);
|
||||
@@ -169,6 +166,7 @@ class Loader {
|
||||
}else{
|
||||
$module = MODULE_NAME;
|
||||
}
|
||||
$layer = ucwords($layer);
|
||||
$class = $module . '\\' . $layer . '\\' . parse_name($name, 1) . $layer;
|
||||
if(class_exists($class)) {
|
||||
$action = new $class;
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
// oauth登录接口
|
||||
// <code>
|
||||
// Oauth::connect('qq',['app_key'=>'','app_secret'=>'','callback'=>'','authorize'=>'']); // 链接QQ登录
|
||||
// Oauth::login(); // 跳转到授权登录页面 或者 Oauth::login($callbackUrl);
|
||||
// Oauth::call('api','params'); // 调用API接口
|
||||
// </code>
|
||||
class Oauth {
|
||||
|
||||
/**
|
||||
* 操作句柄
|
||||
* @var object
|
||||
* @access protected
|
||||
*/
|
||||
static protected $handler = null;
|
||||
|
||||
/**
|
||||
* 连接oauth
|
||||
* @access public
|
||||
* @param string $type Oauth类型
|
||||
* @param array $options 配置数组
|
||||
* @return object
|
||||
*/
|
||||
static public function connect($type,$options=[]) {
|
||||
$class = 'Think\\Oauth\\Driver\\'.ucwords($type);
|
||||
self::$handler = new $class($options);
|
||||
return self::$handler;
|
||||
}
|
||||
|
||||
// 跳转到授权登录页面
|
||||
static public function login($callback=''){
|
||||
self::$handler->login($callback);
|
||||
}
|
||||
|
||||
// 获取access_token
|
||||
static public function getAccessToken($code){
|
||||
self::$handler->getAccessToken($code);
|
||||
}
|
||||
|
||||
// 设置保存过的token信息
|
||||
static public function setToken($token){
|
||||
self::$handler->setToken($token);
|
||||
}
|
||||
|
||||
// 获取oauth用户信息
|
||||
static public function getOauthInfo(){
|
||||
return self::$handler->getOauthInfo();
|
||||
}
|
||||
|
||||
// 获取openid信息
|
||||
static public function getOpenId(){
|
||||
return self::$handler->getOpenId();
|
||||
}
|
||||
|
||||
// 调用oauth接口API
|
||||
static public function call($api,$param='',$method='GET'){
|
||||
return self::$handler->call($api,$param,$method);
|
||||
}
|
||||
}
|
||||
@@ -1,233 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth;
|
||||
abstract class Driver {
|
||||
|
||||
/**
|
||||
* oauth版本
|
||||
* @var string
|
||||
*/
|
||||
protected $version = '2.0';
|
||||
|
||||
/**
|
||||
* 申请应用时分配的app_key
|
||||
* @var string
|
||||
*/
|
||||
protected $appKey = '';
|
||||
|
||||
/**
|
||||
* 申请应用时分配的 app_secret
|
||||
* @var string
|
||||
*/
|
||||
protected $appSecret = '';
|
||||
|
||||
/**
|
||||
* 授权类型 response_type 目前只能为code
|
||||
* @var string
|
||||
*/
|
||||
protected $responseType = 'code';
|
||||
|
||||
/**
|
||||
* grant_type 目前只能为 authorization_code
|
||||
* @var string
|
||||
*/
|
||||
protected $grantType = 'authorization_code';
|
||||
|
||||
/**
|
||||
* 获取request_code请求的URL
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = '';
|
||||
|
||||
/**
|
||||
* 获取access_token请求的URL
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = '';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = '';
|
||||
|
||||
/**
|
||||
* 授权后获取到的TOKEN信息
|
||||
* @var array
|
||||
*/
|
||||
protected $token = null;
|
||||
|
||||
/**
|
||||
* 回调页面URL 可以通过配置文件配置
|
||||
* @var string
|
||||
*/
|
||||
protected $callback = '';
|
||||
|
||||
/**
|
||||
* 构造方法,配置应用信息
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct($config = []){
|
||||
$this->appKey = $config['app_key'];
|
||||
$this->appSecret = $config['app_secret'];
|
||||
$this->authorize = isset($config['authorize']) ? $config['authorize'] : '';
|
||||
$this->callback = isset($config['callback']) ? $config['callback'] : '';
|
||||
}
|
||||
|
||||
// 跳转到授权登录页面
|
||||
public function login($callback = ''){
|
||||
if($callback) {
|
||||
$this->callback = $callback;
|
||||
}
|
||||
//跳转到授权页面
|
||||
header('Location: ' . $this->getRequestCodeURL());
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求code
|
||||
*/
|
||||
public function getRequestCodeURL(){
|
||||
//Oauth 标准参数
|
||||
$params = array(
|
||||
'client_id' => $this->appKey,
|
||||
'redirect_uri' => $this->callback,
|
||||
'response_type' => $this->responseType,
|
||||
);
|
||||
|
||||
//获取额外参数
|
||||
if($this->authorize){
|
||||
parse_str($this->authorize, $_param);
|
||||
if(is_array($_param)){
|
||||
$params = array_merge($params, $_param);
|
||||
} else {
|
||||
throw new \Exception('AUTHORIZE配置不正确!');
|
||||
}
|
||||
}
|
||||
return $this->getRequestCodeURL . '?' . http_build_query($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取access_token
|
||||
* @param string $code 授权登录成功后得到的code信息
|
||||
*/
|
||||
public function getAccessToken($code){
|
||||
$params = array(
|
||||
'client_id' => $this->appKey,
|
||||
'client_secret' => $this->appSecret,
|
||||
'grant_type' => $this->grantType,
|
||||
'redirect_uri' => $this->callback,
|
||||
'code' => $code,
|
||||
);
|
||||
// 获取token信息
|
||||
$data = $this->http($this->getAccessTokenURL, $params, 'POST');
|
||||
// 解析token
|
||||
$this->token = $this->parseToken($data);
|
||||
return $this->token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置access_token
|
||||
* @param string $token
|
||||
*/
|
||||
public function setToken($token){
|
||||
$this->token = $token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并默认参数和额外参数
|
||||
* @param array $params 默认参数
|
||||
* @param array/string $param 额外参数
|
||||
* @return array:
|
||||
*/
|
||||
protected function param($params, $param){
|
||||
if(is_string($param))
|
||||
parse_str($param, $param);
|
||||
return array_merge($params, $param);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定API请求的URL
|
||||
* @param string $api API名称
|
||||
* @param string $fix api后缀
|
||||
* @return string 请求的完整URL
|
||||
*/
|
||||
protected function url($api, $fix = ''){
|
||||
return $this->apiBase . $api . $fix;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP请求方法,目前只支持CURL发送请求
|
||||
* @param string $url 请求URL
|
||||
* @param array $params 请求参数
|
||||
* @param string $method 请求方法GET/POST
|
||||
* @return array $data 响应数据
|
||||
*/
|
||||
protected function http($url, $params, $method = 'GET', $header = [], $multi = false){
|
||||
$opts = array(
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_RETURNTRANSFER => 1,
|
||||
CURLOPT_SSL_VERIFYPEER => false,
|
||||
CURLOPT_SSL_VERIFYHOST => false,
|
||||
CURLOPT_HTTPHEADER => $header
|
||||
);
|
||||
|
||||
/* 根据请求类型设置特定参数 */
|
||||
switch(strtoupper($method)){
|
||||
case 'GET':
|
||||
$opts[CURLOPT_URL] = $url . '?' . http_build_query($params);
|
||||
break;
|
||||
case 'POST':
|
||||
//判断是否传输文件
|
||||
+ $params = $multi ? $params : http_build_query($params);
|
||||
$opts[CURLOPT_URL] = $url;
|
||||
$opts[CURLOPT_POST] = 1;
|
||||
$opts[CURLOPT_POSTFIELDS] = $params;
|
||||
break;
|
||||
default:
|
||||
throw new \Exception('不支持的请求方式!');
|
||||
}
|
||||
|
||||
/* 初始化并执行curl请求 */
|
||||
$ch = curl_init();
|
||||
curl_setopt_array($ch, $opts);
|
||||
$data = curl_exec($ch);
|
||||
$error = curl_error($ch);
|
||||
curl_close($ch);
|
||||
if($error) throw new \Exception('请求发生错误:' . $error);
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* 抽象方法,在SNSSDK中实现
|
||||
* 组装接口调用参数 并调用接口
|
||||
*/
|
||||
abstract protected function call($api, $param = '', $method = 'GET', $multi = false);
|
||||
|
||||
/**
|
||||
* 抽象方法,在SNSSDK中实现
|
||||
* 解析access_token方法请求后的返回值
|
||||
*/
|
||||
abstract protected function parseToken($result);
|
||||
|
||||
/**
|
||||
* 抽象方法,在SNSSDK中实现
|
||||
* 获取当前授权用户的SNS标识
|
||||
*/
|
||||
abstract public function getOpenId();
|
||||
|
||||
/**
|
||||
* 抽象方法
|
||||
* 获取当前授权用户的用户信息
|
||||
*/
|
||||
abstract public function getOauthInfo();
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Baidu extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://openapi.baidu.com/oauth/2.0/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://openapi.baidu.com/oauth/2.0/token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://openapi.baidu.com/rest/2.0/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 百度API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 百度调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['refresh_token']){
|
||||
$data['openid'] = $this->openid();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取百度ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
$data = $this->call('passport/users/getLoggedInUser');
|
||||
return !empty($data['uid'])?$data['uid']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('passport/users/getLoggedInUser');
|
||||
|
||||
if(!empty($data['uid'])){
|
||||
$userInfo['type'] = 'BAIDU';
|
||||
$userInfo['name'] = $data['uid'];
|
||||
$userInfo['nick'] = $data['uname'];
|
||||
$userInfo['avatar'] = "http://tb.himg.baidu.com/sys/portrait/item/{$data['portrait']}";
|
||||
return $userInfo;
|
||||
} else {
|
||||
throw new \Exception("获取百度用户信息失败:{$data['error_msg']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 杨维杰 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Diandian extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://api.diandian.com/oauth/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://api.diandian.com/oauth/token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.diandian.com/v1/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 点点网API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 点点网调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['token_type'] && $data['uid']){
|
||||
$data['openid'] = $data['uid'];
|
||||
unset($data['uid']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取点点网ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('user/info');
|
||||
|
||||
if(!empty($data['meta']['status']) && $data['meta']['status'] == 200){
|
||||
$userInfo['type'] = 'DIANDIAN';
|
||||
$userInfo['name'] = $data['response']['name'];
|
||||
$userInfo['nick'] = $data['response']['name'];
|
||||
$userInfo['avatar'] = "https://api.diandian.com/v1/blog/{$data['response']['blogs'][0]['blogUuid']}/avatar/144";
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取点点用户信息失败:{$data}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,91 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Douban extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://www.douban.com/service/auth2/auth';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://www.douban.com/service/auth2/token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.douban.com/v2/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 豆瓣调用公共参数 */
|
||||
$params = [];
|
||||
$header = array("Authorization: Bearer {$this->token['access_token']}");
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method, $header);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['refresh_token'] && $data['douban_user_id']){
|
||||
$data['openid'] = $data['douban_user_id'];
|
||||
unset($data['douban_user_id']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取豆瓣ACCESS_TOKEN出错:{$data['msg']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string|null
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('user/~me');
|
||||
|
||||
if(empty($data['code'])){
|
||||
$userInfo['type'] = 'DOUBAN';
|
||||
$userInfo['name'] = $data['name'];
|
||||
$userInfo['nick'] = $data['name'];
|
||||
$userInfo['avatar'] = $data['avatar'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取豆瓣用户信息失败:{$data['msg']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Github extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://github.com/login/oauth/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://github.com/login/oauth/access_token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.github.com/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* Github 调用公共参数 */
|
||||
$params = [];
|
||||
$header = array("Authorization: bearer {$this->token['access_token']}");
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method, $header);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
parse_str($result, $data);
|
||||
if($data['access_token'] && $data['token_type']){
|
||||
$data['openid'] = $this->getOpenId();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取 Github ACCESS_TOKEN出错:未知错误");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('user');
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('user');
|
||||
|
||||
if(empty($data['code'])){
|
||||
$userInfo['type'] = 'GITHUB';
|
||||
$userInfo['name'] = $data['login'];
|
||||
$userInfo['nick'] = $data['name'];
|
||||
$userInfo['avatar'] = $data['avatar_url'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取Github用户信息失败:{$data}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,99 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Google extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://accounts.google.com/o/oauth2/auth';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://accounts.google.com/o/oauth2/token';
|
||||
|
||||
/**
|
||||
* 获取request_code的额外参数 URL查询字符串格式
|
||||
* @var srting
|
||||
*/
|
||||
protected $authorize = 'scope=https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://www.googleapis.com/oauth2/v1/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* Google 调用公共参数 */
|
||||
$params = [];
|
||||
$header = array("Authorization: Bearer {$this->token['access_token']}");
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method, $header);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['token_type'] && $data['expires_in']){
|
||||
$data['openid'] = $this->getOpenId();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取 Google ACCESS_TOKEN出错:未知错误");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('userinfo');
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('userinfo');
|
||||
|
||||
if(!empty($data['id'])){
|
||||
$userInfo['type'] = 'GOOGLE';
|
||||
$userInfo['name'] = $data['name'];
|
||||
$userInfo['nick'] = $data['name'];
|
||||
$userInfo['avatar'] = $data['picture'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取Google用户信息失败:{$data}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,94 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Kaixin extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'http://api.kaixin001.com/oauth2/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://api.kaixin001.com/oauth2/access_token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.kaixin001.com/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 开心网API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 开心网调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['refresh_token']){
|
||||
$data['openid'] = $this->getOpenId();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取开心网ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('users/me');
|
||||
return !empty($data['uid'])?$data['uid']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('users/me');
|
||||
|
||||
if(!empty($data['uid'])){
|
||||
$userInfo['type'] = 'KAIXIN';
|
||||
$userInfo['name'] = $data['uid'];
|
||||
$userInfo['nick'] = $data['name'];
|
||||
$userInfo['avatar'] = $data['logo50'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取开心网用户信息失败:{$data['error']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,100 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Msn extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://login.live.com/oauth20_authorize.srf';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://login.live.com/oauth20_token.srf';
|
||||
|
||||
/**
|
||||
* 获取request_code的额外参数 URL查询字符串格式
|
||||
* @var srting
|
||||
*/
|
||||
protected $authorize = 'scope=wl.basic wl.offline_access wl.signin wl.emails wl.photos';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://apis.live.net/v5.0/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* MSN 调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['token_type'] && $data['expires_in']){
|
||||
$data['openid'] = $this->getOpenId();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取 MSN ACCESS_TOKEN出错:未知错误");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('me');
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('me');
|
||||
|
||||
if(!empty($data['id'])){
|
||||
$userInfo['type'] = 'MSN';
|
||||
$userInfo['name'] = $data['name'];
|
||||
$userInfo['nick'] = $data['name'];
|
||||
$userInfo['avatar'] = '微软暂未提供头像URL,请通过 me/picture 接口下载';
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取msn用户信息失败:{$data}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,102 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2013 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Qq extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://graph.qq.com/oauth2.0/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://graph.qq.com/oauth2.0/token';
|
||||
|
||||
/**
|
||||
* 获取request_code的额外参数,可在配置中修改 URL查询字符串格式
|
||||
* @var srting
|
||||
*/
|
||||
protected $authorize = 'scope=get_user_info,add_share';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://graph.qq.com/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 腾讯QQ调用公共参数 */
|
||||
$params = array(
|
||||
'oauth_consumer_key' => $this->AppKey,
|
||||
'access_token' => $this->token['access_token'],
|
||||
'openid' => $this->openid(),
|
||||
'format' => 'json'
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
parse_str($result, $data);
|
||||
if($data['access_token'] && $data['expires_in']){
|
||||
$data['openid'] = $this->getOpenId();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取腾讯QQ ACCESS_TOKEN 出错:{$result}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
if($data['access_token']){
|
||||
$data = $this->http($this->url('oauth2.0/me'), array('access_token' => $data['access_token']));
|
||||
$data = json_decode(trim(substr($data, 9), " );\n"), true);
|
||||
if(isset($data['openid']))
|
||||
return $data['openid'];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('user/get_user_info');
|
||||
|
||||
if($data['ret'] == 0){
|
||||
$userInfo['type'] = 'QQ';
|
||||
$userInfo['name'] = $data['nickname'];
|
||||
$userInfo['nick'] = $data['nickname'];
|
||||
$userInfo['avatar'] = $data['figureurl_2'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取腾讯QQ用户信息失败:{$data['msg']}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Renren extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://graph.renren.com/oauth/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://graph.renren.com/oauth/token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'http://api.renren.com/restserver.do';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'POST'){
|
||||
/* 人人网调用公共参数 */
|
||||
$params = array(
|
||||
'method' => $api,
|
||||
'access_token' => $this->token['access_token'],
|
||||
'v' => '1.0',
|
||||
'format' => 'json',
|
||||
);
|
||||
|
||||
$data = $this->http($this->url(''), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 合并默认参数和额外参数
|
||||
* @param array $params 默认参数
|
||||
* @param array/string $param 额外参数
|
||||
* @return array:
|
||||
*/
|
||||
protected function param($params, $param){
|
||||
$params = parent::param($params, $param);
|
||||
|
||||
/* 签名 */
|
||||
ksort($params);
|
||||
$param = [];
|
||||
foreach ($params as $key => $value){
|
||||
$param[] = "{$key}={$value}";
|
||||
}
|
||||
$sign = implode('', $param).$this->AppSecret;
|
||||
$params['sig'] = md5($sign);
|
||||
|
||||
return $params;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['refresh_token'] && $data['user']['id']){
|
||||
$data['openid'] = $data['user']['id'];
|
||||
unset($data['user']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取人人网ACCESS_TOKEN出错:{$data['error_description']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('users.getInfo');
|
||||
|
||||
if(!isset($data['error_code'])){
|
||||
$userInfo['type'] = 'RENREN';
|
||||
$userInfo['name'] = $data[0]['name'];
|
||||
$userInfo['nick'] = $data[0]['name'];
|
||||
$userInfo['avatar'] = $data[0]['headurl'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取人人网用户信息失败:{$data['error_msg']}");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Sina extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://api.weibo.com/oauth2/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://api.weibo.com/oauth2/access_token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.weibo.com/2/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET', $multi = false){
|
||||
/* 新浪微博调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method, $multi);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['remind_in'] && $data['uid']){
|
||||
$data['openid'] = $data['uid'];
|
||||
unset($data['uid']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取新浪微博ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('users.getInfo');
|
||||
|
||||
if(!isset($data['error_code'])){
|
||||
$userInfo['type'] = 'RENREN';
|
||||
$userInfo['name'] = $data[0]['name'];
|
||||
$userInfo['nick'] = $data[0]['name'];
|
||||
$userInfo['avatar'] = $data[0]['headurl'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取人人网用户信息失败:{$data['error_msg']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Sohu extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://api.sohu.com/oauth2/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://api.sohu.com/oauth2/token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.sohu.com/rest/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 搜狐API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 搜狐调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['refresh_token'] && $data['open_id']){
|
||||
$data['openid'] = $data['open_id'];
|
||||
unset($data['open_id']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取搜狐ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('i/prv/1/user/get-basic-info');
|
||||
|
||||
if('success' == $data['message'] && !empty($data['data'])){
|
||||
$userInfo['type'] = 'SOHU';
|
||||
$userInfo['name'] = $data['data']['open_id'];
|
||||
$userInfo['nick'] = $data['data']['nick'];
|
||||
$userInfo['avatar'] = $data['data']['icon'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取搜狐用户信息失败:{$data['message']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,95 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class T163 extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://api.t.163.com/oauth2/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://api.t.163.com/oauth2/access_token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://api.t.163.com/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 新浪微博调用公共参数 */
|
||||
$params = array(
|
||||
'oauth_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['uid'] && $data['access_token'] && $data['expires_in'] && $data['refresh_token']){
|
||||
$data['openid'] = $data['uid'];
|
||||
unset($data['uid']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取网易微博ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
|
||||
$data = $this->call('users/show');
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('users/show');
|
||||
|
||||
if($data['error_code'] == 0){
|
||||
$userInfo['type'] = 'T163';
|
||||
$userInfo['name'] = $data['name'];
|
||||
$userInfo['nick'] = $data['screen_name'];
|
||||
$userInfo['avatar'] = str_replace('w=48&h=48', 'w=180&h=180', $data['profile_image_url']);
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取网易微博用户信息失败:{$data['error']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Taobao extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://oauth.taobao.com/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://oauth.taobao.com/token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://eco.taobao.com/router/rest';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 淘宝网调用公共参数 */
|
||||
$params = array(
|
||||
'method' => $api,
|
||||
'access_token' => $this->token['access_token'],
|
||||
'format' => 'json',
|
||||
'v' => '2.0',
|
||||
);
|
||||
$data = $this->http($this->url(''), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['taobao_user_id']){
|
||||
$data['openid'] = $data['taobao_user_id'];
|
||||
unset($data['taobao_user_id']);
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取淘宝网ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$fields = 'user_id,nick,sex,buyer_credit,avatar,has_shop,vip_info';
|
||||
$data = $this->call('taobao.user.buyer.get', "fields={$fields}");
|
||||
|
||||
if(!empty($data['user_buyer_get_response']['user'])){
|
||||
$user = $data['user_buyer_get_response']['user'];
|
||||
$userInfo['type'] = 'TAOBAO';
|
||||
$userInfo['name'] = $user['user_id'];
|
||||
$userInfo['nick'] = $user['nick'];
|
||||
$userInfo['avatar'] = $user['avatar'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取淘宝网用户信息失败:{$data['error_response']['msg']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class Tencent extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://open.t.qq.com/cgi-bin/oauth2/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://open.t.qq.com/cgi-bin/oauth2/access_token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://open.t.qq.com/api/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 微博API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET', $multi = false){
|
||||
/* 腾讯微博调用公共参数 */
|
||||
$params = array(
|
||||
'oauth_consumer_key' => $this->AppKey,
|
||||
'access_token' => $this->token['access_token'],
|
||||
'openid' => $this->openid(),
|
||||
'clientip' => get_client_ip(),
|
||||
'oauth_version' => '2.a',
|
||||
'scope' => 'all',
|
||||
'format' => 'json'
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api), $this->param($params, $param), $method, $multi);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
parse_str($result, $data);
|
||||
$data = array_merge($data, ['openid' => $_GET['openid'], 'openkey' => $_GET['openkey']]);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['openid'])
|
||||
return $data;
|
||||
else
|
||||
throw new \Exception("获取腾讯微博 ACCESS_TOKEN 出错:{$result}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('users.getInfo');
|
||||
|
||||
if(!isset($data['error_code'])){
|
||||
$userInfo['type'] = 'RENREN';
|
||||
$userInfo['name'] = $data[0]['name'];
|
||||
$userInfo['nick'] = $data[0]['name'];
|
||||
$userInfo['avatar'] = $data[0]['headurl'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取人人网用户信息失败:{$data['error_msg']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2010 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Oauth\Driver;
|
||||
use Think\Oauth\Driver;
|
||||
|
||||
class X360 extends Driver{
|
||||
/**
|
||||
* 获取requestCode的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getRequestCodeURL = 'https://openapi.360.cn/oauth2/authorize';
|
||||
|
||||
/**
|
||||
* 获取access_token的api接口
|
||||
* @var string
|
||||
*/
|
||||
protected $getAccessTokenURL = 'https://openapi.360.cn/oauth2/access_token';
|
||||
|
||||
/**
|
||||
* API根路径
|
||||
* @var string
|
||||
*/
|
||||
protected $apiBase = 'https://openapi.360.cn/';
|
||||
|
||||
/**
|
||||
* 组装接口调用参数 并调用接口
|
||||
* @param string $api 360开放平台API
|
||||
* @param string $param 调用API的额外参数
|
||||
* @param string $method HTTP请求方法 默认为GET
|
||||
* @return json
|
||||
*/
|
||||
public function call($api, $param = '', $method = 'GET'){
|
||||
/* 360开放平台调用公共参数 */
|
||||
$params = array(
|
||||
'access_token' => $this->token['access_token'],
|
||||
);
|
||||
|
||||
$data = $this->http($this->url($api, '.json'), $this->param($params, $param), $method);
|
||||
return json_decode($data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解析access_token方法请求后的返回值
|
||||
* @param string $result 获取access_token的方法的返回值
|
||||
*/
|
||||
protected function parseToken($result){
|
||||
$data = json_decode($result, true);
|
||||
if($data['access_token'] && $data['expires_in'] && $data['refresh_token']){
|
||||
$data['openid'] = $this->getOpenId();
|
||||
return $data;
|
||||
} else
|
||||
throw new \Exception("获取360开放平台ACCESS_TOKEN出错:{$data['error']}");
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前授权应用的openid
|
||||
* @return string
|
||||
*/
|
||||
public function getOpenId(){
|
||||
if(!empty($this->token['openid']))
|
||||
return $this->token['openid'];
|
||||
$data = $this->call('user/me');
|
||||
return !empty($data['id'])?$data['id']:null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前登录的用户信息
|
||||
* @return array
|
||||
*/
|
||||
public function getOauthInfo(){
|
||||
$data = $this->call('user/me');
|
||||
|
||||
if($data['error_code'] == 0){
|
||||
$userInfo['type'] = 'X360';
|
||||
$userInfo['name'] = $data['name'];
|
||||
$userInfo['nick'] = $data['name'];
|
||||
$userInfo['avatar'] = $data['avatar'];
|
||||
return $userInfo;
|
||||
} else {
|
||||
E("获取360用户信息失败:{$data['error']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -275,14 +275,16 @@ class Route {
|
||||
}
|
||||
$paths = explode('/', $url);
|
||||
|
||||
$_GET[$config['var_action']] = array_pop($paths);
|
||||
|
||||
if(!defined('BIND_CONTROLLER') && !isset($_GET[$config['var_controller']])) {
|
||||
$_GET[$config['var_controller']] = array_pop($paths);
|
||||
if(!defined('BIND_MODULE') && !isset($_GET[$config['var_module']])) {
|
||||
$_GET[$config['var_module']] = array_shift($paths);
|
||||
}
|
||||
|
||||
if(!defined('BIND_MODULE') && !isset($_GET[$config['var_module']])) {
|
||||
$_GET[$config['var_module']] = array_pop($paths);
|
||||
if(!defined('BIND_CONTROLLER') && !isset($_GET[$config['var_controller']])) {
|
||||
$_GET[$config['var_controller']] = array_shift($paths);
|
||||
}
|
||||
|
||||
if($paths){
|
||||
$_GET[$config['var_action']] = array_shift($paths);
|
||||
}
|
||||
|
||||
// 解析剩余的URL参数
|
||||
|
||||
@@ -1,84 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Tag {
|
||||
|
||||
static private $tags = [];
|
||||
|
||||
/**
|
||||
* 动态添加行为扩展到某个标签
|
||||
* @param string $tag 标签名称
|
||||
* @param mixed $behavior 行为名称
|
||||
* @return void
|
||||
*/
|
||||
static public function add($tag,$behavior) {
|
||||
if(is_array($behavior)) {
|
||||
self::$tags[$tag] = array_merge(self::$tags[$tag],$behavior);
|
||||
}else{
|
||||
self::$tags[$tag][] = $behavior;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量导入行为
|
||||
* @param array $tags 标签行为
|
||||
* @return void
|
||||
*/
|
||||
static public function import($tags) {
|
||||
self::$tags = array_merge(self::$tags,$tags);
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听标签的行为
|
||||
* @param string $tag 标签名称
|
||||
* @param mixed $params 传入参数
|
||||
* @return void
|
||||
*/
|
||||
static public function listen($tag, &$params=NULL) {
|
||||
if(isset(self::$tags[$tag])) {
|
||||
foreach (self::$tags[$tag] as $val) {
|
||||
Debug::remark('behavior_start','time');
|
||||
$result = self::exec($val, $params);
|
||||
Debug::remark('behavior_end','time');
|
||||
Log::record('Run '.$val.' Behavior [ RunTime:'.Debug::getUseTime('behavior_start','behavior_end').'s ]','INFO');
|
||||
if(false === $result) {
|
||||
// 如果返回false 则中断行为执行
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行某个行为
|
||||
* @param string $name 行为名称
|
||||
* @param Mixed $params 传人的参数
|
||||
* @return void
|
||||
*/
|
||||
static public function exec($name, &$params=NULL) {
|
||||
if($name instanceof \Closure) {
|
||||
return $name($params);
|
||||
}
|
||||
if(false === strpos($name,'\\')) {
|
||||
$class = '\\'.ucwords(MODULE_NAME).'\\Behavior\\'.$name;
|
||||
}else{
|
||||
$class = $name;
|
||||
}
|
||||
if(class_exists($class)) {
|
||||
$behavior = new $class();
|
||||
return $behavior->run($params);
|
||||
}
|
||||
return ;
|
||||
}
|
||||
}
|
||||
@@ -439,7 +439,7 @@ class Template {
|
||||
protected function parseTagLib($tagLib,&$content,$hide=false) {
|
||||
$begin = $this->config['taglib_begin'];
|
||||
$end = $this->config['taglib_end'];
|
||||
$className = '\\Think\\Template\\TagLib\\'.ucwords($tagLib);
|
||||
$className = '\\Think\\Template\\Taglib\\'.ucwords($tagLib);
|
||||
$tLib = new $className($this);
|
||||
foreach ($tLib->getTags() as $name=>$val){
|
||||
$tags = [$name];
|
||||
|
||||
@@ -9,8 +9,8 @@
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Template\TagLib;
|
||||
use Think\Template\TagLib;
|
||||
namespace Think\Template\Taglib;
|
||||
use Think\Template\Taglib;
|
||||
|
||||
/**
|
||||
* CX标签库解析类
|
||||
@@ -19,7 +19,7 @@ use Think\Template\TagLib;
|
||||
* @subpackage Driver.Taglib
|
||||
* @author liu21st <liu21st@gmail.com>
|
||||
*/
|
||||
class Cx extends TagLib {
|
||||
class Cx extends Taglib {
|
||||
|
||||
// 标签定义
|
||||
protected $tags = [
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Transform\Driver;
|
||||
|
||||
class Json{
|
||||
public function encode($data){
|
||||
return json_encode($data);
|
||||
}
|
||||
|
||||
public function decode($data, $assoc = true){
|
||||
return json_decode($data, $assoc);
|
||||
}
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: 麦当苗儿 <zuojiazi.cn@gmail.com> <http://www.zjzit.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think\Transform\Driver;
|
||||
|
||||
class Xml{
|
||||
/**
|
||||
* XML数据默认配置项
|
||||
* @var array
|
||||
*/
|
||||
private $config = [
|
||||
'root_name' => 'think', //根节点名称
|
||||
'root_attr' => [], //根节点属性
|
||||
'item_name' => 'item', //数字节点转换的名称
|
||||
'item_key' => 'id', //数字节点转换的属性名
|
||||
];
|
||||
|
||||
/**
|
||||
* 编码XML数据
|
||||
* @param mixed $data 被编码的数据
|
||||
* @param array $config 数据配置项
|
||||
* @return string 编码后的XML数据
|
||||
*/
|
||||
public function encode($data, array $config = []) {
|
||||
//初始化配置
|
||||
$config = array_merge($this->config, $config);
|
||||
|
||||
//创建XML对象
|
||||
$xml = new \SimpleXMLElement("<{$config['root_name']}></{$config['root_name']}>");
|
||||
self::data2xml($xml, $data, $config['item_name'], $config['item_key']);
|
||||
return $xml->asXML();
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码XML数据
|
||||
* @param string $str XML字符串
|
||||
* @param boolean $assoc 是否转换为数组
|
||||
* @param array $config 数据配置项
|
||||
* @return string 解码后的XML数据
|
||||
*/
|
||||
public function decode($str, $assoc = true, array $config = []){
|
||||
//初始化配置
|
||||
$config = array_merge($this->config, $config);
|
||||
|
||||
//创建XML对象
|
||||
$xml = new \SimpleXMLElement($str);
|
||||
if($assoc){
|
||||
self::xml2data($xml, $data, $config['item_name'], $config['item_key']);
|
||||
return $data;
|
||||
}
|
||||
|
||||
return $xml;
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据XML编码
|
||||
* @static
|
||||
* @access public
|
||||
* @param mixed $data 数据
|
||||
* @param string $item 数字索引时的节点名称
|
||||
* @param string $id 数字索引key转换为的属性名
|
||||
* @return string
|
||||
*/
|
||||
static public function data2xml(SimpleXMLElement $xml, $data, $item = 'item', $id = 'id') {
|
||||
foreach ($data as $key => $value) {
|
||||
//指定默认的数字key
|
||||
if(is_numeric($key)){
|
||||
$id && $val = $key;
|
||||
$key = $item;
|
||||
}
|
||||
|
||||
//添加子元素
|
||||
if(is_array($value) || is_object($value)){
|
||||
$child = $xml->addChild($key);
|
||||
self::data2xml($child, $value, $item, $id);
|
||||
} else {
|
||||
$child = $xml->addChild($key, $value);
|
||||
}
|
||||
|
||||
//记录原来的key
|
||||
isset($val) && $child->addAttribute($id, $val);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据XML解码
|
||||
* @static
|
||||
* @access public
|
||||
* @param SimpleXMLElement $xml xml对象
|
||||
* @param array $data 解码后的数据
|
||||
* @param string $item 数字索引时的节点名称
|
||||
* @param string $id 数字索引key转换为的属性名
|
||||
*/
|
||||
static public function xml2data(SimpleXMLElement $xml, &$data, $item = 'item', $id = 'id'){
|
||||
foreach ($xml->children() as $items) {
|
||||
$key = $items->getName();
|
||||
$attr = $items->attributes();
|
||||
if($key == $item && isset($attr[$id])){
|
||||
$key = strval($attr[$id]);
|
||||
}
|
||||
|
||||
if($items->count()){
|
||||
self::xml2data($items, $val);
|
||||
} else {
|
||||
$val = strval($items);
|
||||
}
|
||||
|
||||
$data[$key] = $val;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,514 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Upload {
|
||||
protected $config = [
|
||||
'max_size' => -1, // 上传文件的最大值
|
||||
'support_multi' => true, // 是否支持多文件上传
|
||||
'allow_exts' => [], // 允许上传的文件后缀 留空不作后缀检查
|
||||
'allow_types' => [], // 允许上传的文件类型 留空不做检查
|
||||
'thumb' => false, // 使用对上传图片进行缩略图处理
|
||||
'thumb_max_width' => '',// 缩略图最大宽度
|
||||
'thumb_max_height' => '',// 缩略图最大高度
|
||||
'thumb_prefix' => 'thumb_',// 缩略图前缀
|
||||
'thumb_suffix' => '',
|
||||
'thumb_path' => '',// 缩略图保存路径
|
||||
'thumb_file' => '',// 缩略图文件名
|
||||
'thumb_ext' => '',// 缩略图扩展名
|
||||
'thumb_remove_origin' => false,// 是否移除原图
|
||||
'zip_images' => false,// 压缩图片文件上传
|
||||
'auto_sub' => false,// 启用子目录保存文件
|
||||
'sub_type' => 'hash',// 子目录创建方式 可以使用hash date custom
|
||||
'sub_dir' => '', // 子目录名称 subType为custom方式后有效
|
||||
'date_format' => 'Ymd',
|
||||
'hash_level' => 1, // hash的目录层次
|
||||
'save_path' => '',// 上传文件保存路径
|
||||
'auto_check' => true, // 是否自动检查附件
|
||||
'upload_replace' => false,// 存在同名是否覆盖
|
||||
'save_rule' => 'uniqid',// 上传文件命名规则
|
||||
'hash_type' => 'md5_file',// 上传文件Hash规则函数名
|
||||
];
|
||||
|
||||
// 错误信息
|
||||
private $error = '';
|
||||
// 上传成功的文件信息
|
||||
private $uploadFileInfo ;
|
||||
|
||||
public function __get($name){
|
||||
if(isset($this->config[$name])) {
|
||||
return $this->config[$name];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public function __set($name,$value){
|
||||
if(isset($this->config[$name])) {
|
||||
$this->config[$name] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
public function __isset($name){
|
||||
return isset($this->config[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 架构函数
|
||||
* @access public
|
||||
* @param array $config 上传参数
|
||||
*/
|
||||
public function __construct($config=[]) {
|
||||
if(is_array($config)) {
|
||||
$this->config = array_merge($this->config,$config);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传一个文件
|
||||
* @access protected
|
||||
* @param mixed $name 数据
|
||||
* @param string $value 数据表名
|
||||
* @return string
|
||||
*/
|
||||
protected function save($file) {
|
||||
$filename = $file['save_path'].$file['savename'];
|
||||
if(!$this->upload_replace && is_file($filename)) {
|
||||
// 不覆盖同名文件
|
||||
$this->error = '文件已经存在!'.$filename;
|
||||
return false;
|
||||
}
|
||||
// 如果是图像文件 检测文件格式
|
||||
if( in_array(strtolower($file['extension']),['gif','jpg','jpeg','bmp','png','swf'])) {
|
||||
$info = getimagesize($file['tmp_name']);
|
||||
if(false === $info || ('gif' == strtolower($file['extension']) && empty($info['bits']))){
|
||||
$this->error = '非法图像文件';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if(!move_uploaded_file($file['tmp_name'], $this->autoCharset($filename,'utf-8','gbk'))) {
|
||||
$this->error = '文件上传保存错误!';
|
||||
return false;
|
||||
}
|
||||
if($this->thumb && in_array(strtolower($file['extension']),['gif','jpg','jpeg','bmp','png'])) {
|
||||
$image = getimagesize($filename);
|
||||
if(false !== $image) {
|
||||
//是图像文件生成缩略图
|
||||
$thumbWidth = explode(',',$this->thumb_max_width);
|
||||
$thumbHeight = explode(',',$this->thumb_max_height);
|
||||
$thumb_prefix = explode(',',$this->thumb_prefix);
|
||||
$thumb_suffix = explode(',',$this->thumb_suffix);
|
||||
$thumb_file = explode(',',$this->thumb_file);
|
||||
$thumb_path = $this->thumb_path?$this->thumb_path:dirname($filename).'/';
|
||||
$thumb_ext = $this->thumb_ext ? $this->thumb_ext : $file['extension']; //自定义缩略图扩展名
|
||||
// 生成图像缩略图
|
||||
for($i=0,$len=count($thumbWidth); $i<$len; $i++) {
|
||||
if(!empty($thumb_file[$i])) {
|
||||
$thumbname = $thumb_file[$i];
|
||||
}else{
|
||||
$prefix = isset($thumb_prefix[$i])?$thumb_prefix[$i]:$thumb_prefix[0];
|
||||
$suffix = isset($thumb_suffix[$i])?$thumb_suffix[$i]:$thumb_suffix[0];
|
||||
$thumbname = $prefix.basename($filename,'.'.$file['extension']).$suffix;
|
||||
}
|
||||
Image::thumb($filename,$thumb_path.$thumbname.'.'.$thumb_ext,'',$thumbWidth[$i],$thumbHeight[$i],true);
|
||||
}
|
||||
if($this->thumb_remove_origin) {
|
||||
// 生成缩略图之后删除原图
|
||||
unlink($filename);
|
||||
}
|
||||
}
|
||||
}
|
||||
if($this->zipImags) {
|
||||
// TODO 对图片压缩包在线解压
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传所有文件
|
||||
* @access public
|
||||
* @param string $savePath 上传文件保存路径
|
||||
* @return string
|
||||
*/
|
||||
public function upload($savePath ='') {
|
||||
//如果不指定保存文件名,则由系统默认
|
||||
if(empty($savePath))
|
||||
$savePath = $this->save_path;
|
||||
// 检查上传目录
|
||||
if(!is_dir($savePath)) {
|
||||
// 检查目录是否编码后的
|
||||
if(is_dir(base64_decode($savePath))) {
|
||||
$savePath = base64_decode($savePath);
|
||||
}else{
|
||||
// 尝试创建目录
|
||||
if(!mkdir($savePath)){
|
||||
$this->error = '上传目录'.$savePath.'不存在';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}else {
|
||||
if(!is_writeable($savePath)) {
|
||||
$this->error = '上传目录'.$savePath.'不可写';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
$fileInfo = [];
|
||||
$isUpload = false;
|
||||
|
||||
// 获取上传的文件信息
|
||||
// 对$_FILES数组信息处理
|
||||
$files = $this->dealFiles($_FILES);
|
||||
foreach($files as $key => $file) {
|
||||
//过滤无效的上传
|
||||
if(!empty($file['name'])) {
|
||||
//登记上传文件的扩展信息
|
||||
if(!isset($file['key'])) $file['key'] = $key;
|
||||
$file['extension'] = $this->getExt($file['name']);
|
||||
$file['savepath'] = $savePath;
|
||||
$file['savename'] = $this->getSaveName($file);
|
||||
|
||||
// 自动检查附件
|
||||
if($this->auto_check) {
|
||||
if(!$this->check($file))
|
||||
return false;
|
||||
}
|
||||
|
||||
//保存上传文件
|
||||
if(!$this->save($file)) return false;
|
||||
if(function_exists($this->hash_type)) {
|
||||
$fun = $this->hash_type;
|
||||
$file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
|
||||
}
|
||||
//上传成功后保存文件信息,供其他地方调用
|
||||
unset($file['tmp_name'],$file['error']);
|
||||
$fileInfo[] = $file;
|
||||
$isUpload = true;
|
||||
}
|
||||
}
|
||||
if($isUpload) {
|
||||
$this->uploadFileInfo = $fileInfo;
|
||||
return true;
|
||||
}else {
|
||||
$this->error = '没有选择上传文件';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传单个上传字段中的文件 支持多附件
|
||||
* @access public
|
||||
* @param array $file 上传文件信息
|
||||
* @param string $savePath 上传文件保存路径
|
||||
* @return string
|
||||
*/
|
||||
public function uploadOne($file,$savePath=''){
|
||||
//如果不指定保存文件名,则由系统默认
|
||||
if(empty($savePath))
|
||||
$savePath = $this->save_path;
|
||||
// 检查上传目录
|
||||
if(!is_dir($savePath)) {
|
||||
// 尝试创建目录
|
||||
if(!mkdir($savePath,0777,true)){
|
||||
$this->error = '上传目录'.$savePath.'不存在';
|
||||
return false;
|
||||
}
|
||||
}else {
|
||||
if(!is_writeable($savePath)) {
|
||||
$this->error = '上传目录'.$savePath.'不可写';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
//过滤无效的上传
|
||||
if(!empty($file['name'])) {
|
||||
$fileArray = [];
|
||||
if(is_array($file['name'])) {
|
||||
$keys = array_keys($file);
|
||||
$count = count($file['name']);
|
||||
for ($i=0; $i<$count; $i++) {
|
||||
foreach ($keys as $key)
|
||||
$fileArray[$i][$key] = $file[$key][$i];
|
||||
}
|
||||
}else{
|
||||
$fileArray[] = $file;
|
||||
}
|
||||
$info = [];
|
||||
foreach ($fileArray as $key=>$file){
|
||||
//登记上传文件的扩展信息
|
||||
$file['extension'] = $this->getExt($file['name']);
|
||||
$file['savepath'] = $savePath;
|
||||
$file['savename'] = $this->getSaveName($file);
|
||||
// 自动检查附件
|
||||
if($this->auto_check) {
|
||||
if(!$this->check($file))
|
||||
return false;
|
||||
}
|
||||
//保存上传文件
|
||||
if(!$this->save($file)) return false;
|
||||
if(function_exists($this->hash_type)) {
|
||||
$fun = $this->hash_type;
|
||||
$file['hash'] = $fun($this->autoCharset($file['savepath'].$file['savename'],'utf-8','gbk'));
|
||||
}
|
||||
unset($file['tmp_name'],$file['error']);
|
||||
$info[] = $file;
|
||||
}
|
||||
// 返回上传的文件信息
|
||||
return $info;
|
||||
}else {
|
||||
$this->error = '没有选择上传文件';
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 转换上传文件数组变量为正确的方式
|
||||
* @access protected
|
||||
* @param array $files 上传的文件变量
|
||||
* @return array
|
||||
*/
|
||||
protected function dealFiles($files) {
|
||||
$fileArray = [];
|
||||
$n = 0;
|
||||
foreach ($files as $key=>$file){
|
||||
if(is_array($file['name'])) {
|
||||
$keys = array_keys($file);
|
||||
$count = count($file['name']);
|
||||
for ($i=0; $i<$count; $i++) {
|
||||
$fileArray[$n]['key'] = $key;
|
||||
foreach ($keys as $_key){
|
||||
$fileArray[$n][$_key] = $file[$_key][$i];
|
||||
}
|
||||
$n++;
|
||||
}
|
||||
}else{
|
||||
$fileArray[$key] = $file;
|
||||
}
|
||||
}
|
||||
return $fileArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取错误代码信息
|
||||
* @access public
|
||||
* @param string $errorNo 错误号码
|
||||
* @return void
|
||||
*/
|
||||
protected function error($errorNo) {
|
||||
switch($errorNo) {
|
||||
case 1:
|
||||
$this->error = '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
|
||||
break;
|
||||
case 2:
|
||||
$this->error = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
|
||||
break;
|
||||
case 3:
|
||||
$this->error = '文件只有部分被上传';
|
||||
break;
|
||||
case 4:
|
||||
$this->error = '没有文件被上传';
|
||||
break;
|
||||
case 6:
|
||||
$this->error = '找不到临时文件夹';
|
||||
break;
|
||||
case 7:
|
||||
$this->error = '文件写入失败';
|
||||
break;
|
||||
default:
|
||||
$this->error = '未知上传错误!';
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据上传文件命名规则取得保存文件名
|
||||
* @access protected
|
||||
* @param string $filename 数据
|
||||
* @return string
|
||||
*/
|
||||
protected function getSaveName($filename) {
|
||||
$rule = $this->save_rule;
|
||||
if(empty($rule)) {//没有定义命名规则,则保持文件名不变
|
||||
$saveName = $filename['name'];
|
||||
}else {
|
||||
if(function_exists($rule)) {
|
||||
//使用函数生成一个唯一文件标识号
|
||||
$saveName = $rule().".".$filename['extension'];
|
||||
}else {
|
||||
//使用给定的文件名作为标识号
|
||||
$saveName = $rule.".".$filename['extension'];
|
||||
}
|
||||
}
|
||||
if($this->auto_sub) {
|
||||
// 使用子目录保存文件
|
||||
$filename['savename'] = $saveName;
|
||||
$saveName = $this->getSubName($filename).$saveName;
|
||||
}
|
||||
return $saveName;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取子目录的名称
|
||||
* @access protected
|
||||
* @param array $file 上传的文件信息
|
||||
* @return string
|
||||
*/
|
||||
protected function getSubName($file) {
|
||||
switch($this->sub_type) {
|
||||
case 'custom':
|
||||
$dir = $this->sub_dir;
|
||||
break;
|
||||
case 'date':
|
||||
$dir = date($this->date_format,time()).'/';
|
||||
break;
|
||||
case 'hash':
|
||||
default:
|
||||
$name = md5($file['savename']);
|
||||
$dir = '';
|
||||
for($i=0;$i<$this->hash_level;$i++) {
|
||||
$dir .= $name{$i}.'/';
|
||||
}
|
||||
break;
|
||||
}
|
||||
if(!is_dir($file['savepath'].$dir)) {
|
||||
mkdir($file['savepath'].$dir,0777,true);
|
||||
}
|
||||
return $dir;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查上传的文件
|
||||
* @access protected
|
||||
* @param array $file 文件信息
|
||||
* @return boolean
|
||||
*/
|
||||
protected function check($file) {
|
||||
if($file['error']!== 0) {
|
||||
//文件上传失败
|
||||
//捕获错误代码
|
||||
$this->error($file['error']);
|
||||
return false;
|
||||
}
|
||||
//文件上传成功,进行自定义规则检查
|
||||
//检查文件大小
|
||||
if(!$this->checkSize($file['size'])) {
|
||||
$this->error = '上传文件大小不符!';
|
||||
return false;
|
||||
}
|
||||
|
||||
//检查文件Mime类型
|
||||
if(!$this->checkType($file['type'])) {
|
||||
$this->error = '上传文件MIME类型不允许!';
|
||||
return false;
|
||||
}
|
||||
//检查文件类型
|
||||
if(!$this->checkExt($file['extension'])) {
|
||||
$this->error ='上传文件类型不允许';
|
||||
return false;
|
||||
}
|
||||
|
||||
//检查是否合法上传
|
||||
if(!$this->checkUpload($file['tmp_name'])) {
|
||||
$this->error = '非法上传文件!';
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// 自动转换字符集 支持数组转换
|
||||
protected function autoCharset($fContents, $from='gbk', $to='utf-8') {
|
||||
$from = strtoupper($from) == 'UTF8' ? 'utf-8' : $from;
|
||||
$to = strtoupper($to) == 'UTF8' ? 'utf-8' : $to;
|
||||
if (strtoupper($from) === strtoupper($to) || empty($fContents) || (is_scalar($fContents) && !is_string($fContents))) {
|
||||
//如果编码相同或者非字符串标量则不转换
|
||||
return $fContents;
|
||||
}
|
||||
if (function_exists('mb_convert_encoding')) {
|
||||
return mb_convert_encoding($fContents, $to, $from);
|
||||
} elseif (function_exists('iconv')) {
|
||||
return iconv($from, $to, $fContents);
|
||||
} else {
|
||||
return $fContents;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查上传的文件类型是否合法
|
||||
* @access protected
|
||||
* @param string $type 数据
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkType($type) {
|
||||
if(!empty($this->allow_types))
|
||||
return in_array(strtolower($type),$this->allow_types);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 检查上传的文件后缀是否合法
|
||||
* @access protected
|
||||
* @param string $ext 后缀名
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkExt($ext) {
|
||||
if(!empty($this->allow_exts))
|
||||
return in_array(strtolower($ext),$this->allow_exts,true);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查文件大小是否合法
|
||||
* @access protected
|
||||
* @param integer $size 数据
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkSize($size) {
|
||||
return !($size > $this->max_size) || (-1 == $this->max_size);
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查文件是否非法提交
|
||||
* @access protected
|
||||
* @param string $filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
protected function checkUpload($filename) {
|
||||
return is_uploaded_file($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得上传文件的后缀
|
||||
* @access protected
|
||||
* @param string $filename 文件名
|
||||
* @return boolean
|
||||
*/
|
||||
protected function getExt($filename) {
|
||||
return pathinfo($filename,PATHINFO_EXTENSION);
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得上传文件的信息
|
||||
* @access public
|
||||
* @return array
|
||||
*/
|
||||
public function getUploadFileInfo() {
|
||||
return $this->uploadFileInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 取得最后一次错误信息
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
public function getErrorMsg() {
|
||||
return $this->error;
|
||||
}
|
||||
}
|
||||
@@ -1,189 +0,0 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | TOPThink [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011 http://topthink.com All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: liu21st <liu21st@gmail.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace Think;
|
||||
|
||||
class Validate {
|
||||
|
||||
protected $validate = []; // 自动验证定义
|
||||
// 是否批处理验证
|
||||
protected $patchValidate = false;
|
||||
protected $error = '';
|
||||
|
||||
public function rule($rule){
|
||||
$this->validate = $rule;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getError(){
|
||||
return $this->error;
|
||||
}
|
||||
|
||||
/**
|
||||
* 自动表单验证
|
||||
* @access protected
|
||||
* @param array $data 创建数据
|
||||
* @param string $type 创建类型
|
||||
* @return boolean
|
||||
*/
|
||||
public function valid($data,$rule=[]) {
|
||||
$validate = $rule?$rule:$this->validate;
|
||||
// 属性验证
|
||||
if($validate) { // 如果设置了数据自动验证则进行数据验证
|
||||
if($this->patchValidate) { // 重置验证错误信息
|
||||
$this->error = [];
|
||||
}
|
||||
foreach($validate as $key=>$val) {
|
||||
// 验证因子定义格式
|
||||
// array(field,rule,message,condition,type,params)
|
||||
// 判断是否需要执行验证
|
||||
if(0==strpos($val[2],'{%') && strpos($val[2],'}'))
|
||||
// 支持提示信息的多语言 使用 {%语言定义} 方式
|
||||
$val[2] = L(substr($val[2],2,-1));
|
||||
$val[3] = isset($val[3])?$val[3]:0;
|
||||
$val[4] = isset($val[4])?$val[4]:'regex';
|
||||
// 判断验证条件
|
||||
if( 1 == $val[3] || (2 == $val[3] && '' != trim($data[$val[0]])) || (0 == $val[3] && isset($data[$val[0]])) ) {
|
||||
if(false === $this->_validationField($data,$val))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// 批量验证的时候最后返回错误
|
||||
if(!empty($this->error)) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证表单字段 支持批量验证
|
||||
* 如果批量验证返回错误的数组信息
|
||||
* @access protected
|
||||
* @param array $data 创建数据
|
||||
* @param array $val 验证因子
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _validationField($data,$val) {
|
||||
if(false === $this->_validationFieldItem($data,$val)){
|
||||
if($this->patchValidate) {
|
||||
$this->error[$val[0]] = $val[2];
|
||||
}else{
|
||||
$this->error = $val[2];
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return ;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据验证因子验证字段
|
||||
* @access protected
|
||||
* @param array $data 创建数据
|
||||
* @param array $val 验证因子
|
||||
* @return boolean
|
||||
*/
|
||||
protected function _validationFieldItem($data,$val) {
|
||||
switch(strtolower(trim($val[4]))) {
|
||||
case 'callback':// 调用方法进行验证
|
||||
$args = isset($val[5])?(array)$val[5]:[];
|
||||
if(is_string($val[0]) && strpos($val[0], ','))
|
||||
$val[0] = explode(',', $val[0]);
|
||||
if(is_array($val[0])){
|
||||
// 支持多个字段验证
|
||||
foreach($val[0] as $field)
|
||||
$_data[$field] = $data[$field];
|
||||
array_unshift($args, $_data);
|
||||
}else{
|
||||
array_unshift($args, $data[$val[0]]);
|
||||
}
|
||||
return call_user_func_array($val[1], $args);
|
||||
case 'confirm': // 验证两个字段是否相同
|
||||
return $data[$val[0]] == $data[$val[1]];
|
||||
default: // 检查附加规则
|
||||
return $this->check($data[$val[0]],$val[1],$val[4]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 验证数据 支持 in between equal length regex expire ip_allow ip_deny
|
||||
* @access public
|
||||
* @param string $value 验证数据
|
||||
* @param mixed $rule 验证表达式
|
||||
* @param string $type 验证方式 默认为正则验证
|
||||
* @return boolean
|
||||
*/
|
||||
public function check($value,$rule,$type='regex'){
|
||||
$type = strtolower(trim($type));
|
||||
switch($type) {
|
||||
case 'in': // 验证是否在某个指定范围之内 逗号分隔字符串或者数组
|
||||
case 'notin':
|
||||
$range = is_array($rule)? $rule : explode(',',$rule);
|
||||
return $type == 'in' ? in_array($value ,$range) : !in_array($value ,$range);
|
||||
case 'between': // 验证是否在某个范围
|
||||
case 'notbetween': // 验证是否不在某个范围
|
||||
if (is_array($rule)){
|
||||
$min = $rule[0];
|
||||
$max = $rule[1];
|
||||
}else{
|
||||
list($min,$max) = explode(',',$rule);
|
||||
}
|
||||
return $type == 'between' ? $value>=$min && $value<=$max : $value<$min || $value>$max;
|
||||
case 'equal': // 验证是否等于某个值
|
||||
case 'notequal': // 验证是否等于某个值
|
||||
return $type == 'equal' ? $value == $rule : $value != $rule;
|
||||
case 'length': // 验证长度
|
||||
$length = mb_strlen($value,'utf-8'); // 当前数据长度
|
||||
if(strpos($rule,',')) { // 长度区间
|
||||
list($min,$max) = explode(',',$rule);
|
||||
return $length >= $min && $length <= $max;
|
||||
}else{// 指定长度
|
||||
return $length == $rule;
|
||||
}
|
||||
case 'expire':
|
||||
list($start,$end) = explode(',',$rule);
|
||||
if(!is_numeric($start)) $start = strtotime($start);
|
||||
if(!is_numeric($end)) $end = strtotime($end);
|
||||
return NOW_TIME >= $start && NOW_TIME <= $end;
|
||||
case 'ip_allow': // IP 操作许可验证
|
||||
return in_array($_SERVER['REMOTE_ADDR'],explode(',',$rule));
|
||||
case 'ip_deny': // IP 操作禁止验证
|
||||
return !in_array($_SERVER['REMOTE_ADDR'],explode(',',$rule));
|
||||
case 'regex':
|
||||
default: // 默认使用正则验证 可以使用验证类中定义的验证名称
|
||||
// 检查附加规则
|
||||
return $this->regex($value,$rule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 使用正则验证数据
|
||||
* @access public
|
||||
* @param string $value 要验证的数据
|
||||
* @param string $rule 验证规则
|
||||
* @return boolean
|
||||
*/
|
||||
public function regex($value,$rule) {
|
||||
$validate = [
|
||||
'require' => '/.+/',
|
||||
'email' => '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
|
||||
'url' => '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
|
||||
'currency' => '/^\d+(\.\d+)?$/',
|
||||
'number' => '/^\d+$/',
|
||||
'zip' => '/^\d{6}$/',
|
||||
'integer' => '/^[-\+]?\d+$/',
|
||||
'double' => '/^[-\+]?\d+(\.\d+)?$/',
|
||||
'english' => '/^[A-Za-z]+$/',
|
||||
];
|
||||
// 检查是否有内置的正则表达式
|
||||
if(isset($validate[strtolower($rule)]))
|
||||
$rule = $validate[strtolower($rule)];
|
||||
return preg_match($rule,$value)===1;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user