mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-03 14:02:47 +08:00
核心文件规范调整
This commit is contained in:
59
Library/Org/auto.php
Normal file
59
Library/Org/auto.php
Normal file
@@ -0,0 +1,59 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
29
Library/Org/controller/Amf.php
Normal file
29
Library/Org/controller/Amf.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?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 ;
|
||||
}
|
||||
|
||||
}
|
||||
34
Library/Org/controller/Phprpc.php
Normal file
34
Library/Org/controller/Phprpc.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?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();
|
||||
}
|
||||
|
||||
}
|
||||
215
Library/Org/controller/Rest.php
Normal file
215
Library/Org/controller/Rest.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
74
Library/Org/crypt.php
Normal file
74
Library/Org/crypt.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?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 Crypt {
|
||||
/**
|
||||
* 加密字符串
|
||||
* @access public
|
||||
* @param string $str 字符串
|
||||
* @param string $key 加密key
|
||||
* @return string
|
||||
*/
|
||||
static public function encrypt($data,$key,$expire=0){
|
||||
$key = md5($key);
|
||||
$data = base64_encode($data);
|
||||
$x = 0;
|
||||
$len = strlen($data);
|
||||
$l = strlen($key);
|
||||
$char = '';
|
||||
for ($i = 0; $i< $len; $i++) {
|
||||
if ($x == $l) $x = 0;
|
||||
$char .=substr($key, $x, 1);
|
||||
$x++;
|
||||
}
|
||||
$str = sprintf('%010d', $expire ? $expire + time() : 0);
|
||||
for ($i=0; $i< $len; $i++) {
|
||||
$str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1))) % 256);
|
||||
}
|
||||
return str_replace('=', '', base64_encode($str));
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密字符串
|
||||
* @access public
|
||||
* @param string $str 字符串
|
||||
* @param string $key 加密key
|
||||
* @return string
|
||||
*/
|
||||
static public function decrypt($data,$key){
|
||||
$key = md5($key);
|
||||
$x = 0;
|
||||
$data = base64_decode($data);
|
||||
$expire = substr($data,0,10);
|
||||
$data = substr($data,10);
|
||||
if($expire > 0 && $expire<time()) {
|
||||
return '';
|
||||
}
|
||||
$len = strlen($data);
|
||||
$l = strlen($key);
|
||||
$char = $str = '';
|
||||
for ($i=0; $i< $len; $i++) {
|
||||
if ($x == $l) $x = 0;
|
||||
$char .= substr($key, $x, 1);
|
||||
$x++;
|
||||
}
|
||||
for ($i = 0; $i < $len; $i++) {
|
||||
if (ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
|
||||
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
|
||||
}else{
|
||||
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
|
||||
}
|
||||
}
|
||||
return base64_decode($str);
|
||||
}
|
||||
}
|
||||
222
Library/Org/filter.php
Normal file
222
Library/Org/filter.php
Normal file
@@ -0,0 +1,222 @@
|
||||
<?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 Filter {
|
||||
//html标签设置
|
||||
static public $htmlTags = [
|
||||
'allow' => 'table|td|th|tr|i|b|u|strong|img|p|br|div|strong|em|ul|ol|li|dl|dd|dt|a',
|
||||
'ban' => 'html|head|meta|link|base|basefont|body|bgsound|title|style|script|form|iframe|frame|frameset|applet|id|ilayer|layer|name|script|style|xml',
|
||||
];
|
||||
|
||||
static public function filter($data,$filter,$option=''){
|
||||
return filter_var($data,is_int($filter)?$filter:filter_id($filter),$option);
|
||||
}
|
||||
|
||||
static private function filter_input($type,$name,$filter,$options=''){
|
||||
return filter_input($type,$name,is_int($filter)?$filter:filter_id($filter),$option);
|
||||
}
|
||||
|
||||
static public function get($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_GET,$name,$filter,$option);
|
||||
}
|
||||
|
||||
static public function post($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_POST,$name,$filter,$option);
|
||||
}
|
||||
|
||||
static public function cookie($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_COOKIE,$name,$filter,$option);
|
||||
}
|
||||
|
||||
static public function server($name,$filter,$option=''){
|
||||
return self::filter_input(INPUT_SERVER,$name,$filter,$option);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理字符串,以便可以正常进行搜索
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forSearch($string) {
|
||||
return str_replace( ['%','_'], ['\%','\_'], $string );
|
||||
}
|
||||
|
||||
/**
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forShow($string) {
|
||||
return self::nl2Br( self::hsc($string) );
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理纯文本数据,以便在textarea标签中显示
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forTarea($string) {
|
||||
return str_ireplace(['<textarea>','</textarea>'], ['<textarea>','</textarea>'], $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 将数据中的单引号和双引号进行转义
|
||||
* @access public
|
||||
* @param string $text 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function forTag($string) {
|
||||
return str_replace(['"',"'"], ['"','''], $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把换行转换为<br />标签
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function nl2Br($string) {
|
||||
return nl2Br($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果 magic_quotes_gpc 为关闭状态,这个函数可以转义字符串
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static public function addSlashes($string) {
|
||||
return addslashes($string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于在textbox表单中显示html代码
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static function hsc($string) {
|
||||
return preg_replace(["/&/i", "/ /i"], ['&', '&nbsp;'], htmlspecialchars($string, ENT_QUOTES));
|
||||
}
|
||||
|
||||
/**
|
||||
* 是hsc()方法的逆操作
|
||||
* @access public
|
||||
* @param string $text 要处理的字符串
|
||||
* @return string
|
||||
*/
|
||||
static function undoHsc($text) {
|
||||
return preg_replace(["/>/i", "/</i", "/"/i", "/'/i", '/&nbsp;/i'], [">", "<", "\"", "'", " "], $text);
|
||||
}
|
||||
|
||||
/**
|
||||
* 输出安全的html,用于过滤危险代码
|
||||
* @access public
|
||||
* @param string $text 要处理的字符串
|
||||
* @param mixed $allowTags 允许的标签列表,如 table|td|th|td
|
||||
* @return string
|
||||
*/
|
||||
static public function safeHtml($text, $allowTags = null) {
|
||||
$text = trim($text);
|
||||
//完全过滤注释
|
||||
$text = preg_replace('/<!--?.*-->/','',$text);
|
||||
//完全过滤动态代码
|
||||
$text = preg_replace('/<\?|\?'.'>/','',$text);
|
||||
//完全过滤js
|
||||
$text = preg_replace('/<script?.*\/script>/','',$text);
|
||||
|
||||
$text = str_replace('[','[',$text);
|
||||
$text = str_replace(']',']',$text);
|
||||
$text = str_replace('|','|',$text);
|
||||
//过滤换行符
|
||||
$text = preg_replace('/\r?\n/','',$text);
|
||||
//br
|
||||
$text = preg_replace('/<br(\s\/)?'.'>/i','[br]',$text);
|
||||
$text = preg_replace('/(\[br\]\s*){10,}/i','[br]',$text);
|
||||
//过滤危险的属性,如:过滤on事件lang js
|
||||
while(preg_match('/(<[^><]+)(lang|on|action|background|codebase|dynsrc|lowsrc)[^><]+/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],$mat[1],$text);
|
||||
}
|
||||
while(preg_match('/(<[^><]+)(window\.|javascript:|js:|about:|file:|document\.|vbs:|cookie)([^><]*)/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],$mat[1].$mat[3],$text);
|
||||
}
|
||||
if( empty($allowTags) ) { $allowTags = self::$htmlTags['allow']; }
|
||||
//允许的HTML标签
|
||||
$text = preg_replace('/<('.$allowTags.')( [^><\[\]]*)>/i','[\1\2]',$text);
|
||||
//过滤多余html
|
||||
if ( empty($banTag) ) { $banTag = self::$htmlTags['ban']; }
|
||||
$text = preg_replace('/<\/?('.$banTag.')[^><]*>/i','',$text);
|
||||
//过滤合法的html标签
|
||||
while(preg_match('/<([a-z]+)[^><\[\]]*>[^><]*<\/\1>/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],str_replace('>',']',str_replace('<','[',$mat[0])),$text);
|
||||
}
|
||||
//转换引号
|
||||
while(preg_match('/(\[[^\[\]]*=\s*)(\"|\')([^\2=\[\]]+)\2([^\[\]]*\])/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],$mat[1].'|'.$mat[3].'|'.$mat[4],$text);
|
||||
}
|
||||
//空属性转换
|
||||
$text = str_replace('\'\'','||',$text);
|
||||
$text = str_replace('""','||',$text);
|
||||
//过滤错误的单个引号
|
||||
while(preg_match('/\[[^\[\]]*(\"|\')[^\[\]]*\]/i',$text,$mat)){
|
||||
$text=str_replace($mat[0],str_replace($mat[1],'',$mat[0]),$text);
|
||||
}
|
||||
//转换其它所有不合法的 < >
|
||||
$text = str_replace('<','<',$text);
|
||||
$text = str_replace('>','>',$text);
|
||||
$text = str_replace('"','"',$text);
|
||||
//反转换
|
||||
$text = str_replace('[','<',$text);
|
||||
$text = str_replace(']','>',$text);
|
||||
$text = str_replace('|','"',$text);
|
||||
//过滤多余空格
|
||||
$text = str_replace(' ',' ',$text);
|
||||
return $text;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除html标签,得到纯文本。可以处理嵌套的标签
|
||||
* @access public
|
||||
* @param string $string 要处理的html
|
||||
* @return string
|
||||
*/
|
||||
static public function deleteHtmlTags($string) {
|
||||
while(strstr($string, '>')) {
|
||||
$currentBeg = strpos($string, '<');
|
||||
$currentEnd = strpos($string, '>');
|
||||
$tmpStringBeg = @substr($string, 0, $currentBeg);
|
||||
$tmpStringEnd = @substr($string, $currentEnd + 1, strlen($string));
|
||||
$string = $tmpStringBeg.$tmpStringEnd;
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理文本中的换行
|
||||
* @access public
|
||||
* @param string $string 要处理的字符串
|
||||
* @param mixed $br 对换行的处理,
|
||||
* false:去除换行;true:保留原样;string:替换成string
|
||||
* @return string
|
||||
*/
|
||||
static public function nl2($string, $br = '<br />') {
|
||||
if ($br == false) {
|
||||
$string = preg_replace("/(\015\012)|(\015)|(\012)/", '', $string);
|
||||
} elseif ($br != true){
|
||||
$string = preg_replace("/(\015\012)|(\015)|(\012)/", $br, $string);
|
||||
}
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
61
Library/Org/image.php
Normal file
61
Library/Org/image.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?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;
|
||||
|
||||
/* 缩略图相关常量定义 */
|
||||
define('THINKIMAGE_THUMB_SCALING', 1); //常量,标识缩略图等比例缩放类型
|
||||
define('THINKIMAGE_THUMB_FILLED', 2); //常量,标识缩略图缩放后填充类型
|
||||
define('THINKIMAGE_THUMB_CENTER', 3); //常量,标识缩略图居中裁剪类型
|
||||
define('THINKIMAGE_THUMB_NORTHWEST', 4); //常量,标识缩略图左上角裁剪类型
|
||||
define('THINKIMAGE_THUMB_SOUTHEAST', 5); //常量,标识缩略图右下角裁剪类型
|
||||
define('THINKIMAGE_THUMB_FIXED', 6); //常量,标识缩略图固定尺寸缩放类型
|
||||
|
||||
/* 水印相关常量定义 */
|
||||
define('THINKIMAGE_WATER_NORTHWEST', 1); //常量,标识左上角水印
|
||||
define('THINKIMAGE_WATER_NORTH', 2); //常量,标识上居中水印
|
||||
define('THINKIMAGE_WATER_NORTHEAST', 3); //常量,标识右上角水印
|
||||
define('THINKIMAGE_WATER_WEST', 4); //常量,标识左居中水印
|
||||
define('THINKIMAGE_WATER_CENTER', 5); //常量,标识居中水印
|
||||
define('THINKIMAGE_WATER_EAST', 6); //常量,标识右居中水印
|
||||
define('THINKIMAGE_WATER_SOUTHWEST', 7); //常量,标识左下角水印
|
||||
define('THINKIMAGE_WATER_SOUTH', 8); //常量,标识下居中水印
|
||||
define('THINKIMAGE_WATER_SOUTHEAST', 9); //常量,标识右下角水印
|
||||
|
||||
/**
|
||||
* 图片处理驱动类,可配置图片处理库
|
||||
* 目前支持GD库和imagick
|
||||
* @author 麦当苗儿 <zuojiazi.cn@gmail.com>
|
||||
*/
|
||||
class Image {
|
||||
/**
|
||||
* 图片资源
|
||||
* @var resource
|
||||
*/
|
||||
private static $im;
|
||||
|
||||
/**
|
||||
* 初始化方法,用于实例化一个图片处理对象
|
||||
* @param string $type 要使用的类库,默认使用GD库
|
||||
*/
|
||||
static public function init($type = 'Gd', $imgname = null){
|
||||
/* 引入处理库,实例化图片处理对象 */
|
||||
$class = '\\Think\\Image\\Driver\\'.ucwords($type);
|
||||
self::$im = new $class($imgname);
|
||||
return self::$im;
|
||||
}
|
||||
|
||||
// 调用驱动类的方法
|
||||
static public function __callStatic($method, $params){
|
||||
self::$im || self::init();
|
||||
return call_user_func_array([self::$im, $method], $params);
|
||||
}
|
||||
}
|
||||
549
Library/Org/image/Driver/Gd.php
Normal file
549
Library/Org/image/Driver/Gd.php
Normal file
@@ -0,0 +1,549 @@
|
||||
<?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\Image\Driver;
|
||||
|
||||
class Gd{
|
||||
/**
|
||||
* 图像资源对象
|
||||
* @var resource
|
||||
*/
|
||||
private $im;
|
||||
private $gif;
|
||||
/**
|
||||
* 图像信息,包括width,height,type,mime,size
|
||||
* @var array
|
||||
*/
|
||||
private $info;
|
||||
|
||||
/**
|
||||
* 构造方法,可用于打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function __construct($imgname = null) {
|
||||
$imgname && $this->open($imgname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function open($imgname){
|
||||
//检测图像文件
|
||||
if(!is_file($imgname)) throw new \Exception('不存在的图像文件');
|
||||
|
||||
//获取图像信息
|
||||
$info = getimagesize($imgname);
|
||||
|
||||
//检测图像合法性
|
||||
if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){
|
||||
throw new \Exception('非法图像文件');
|
||||
}
|
||||
|
||||
//设置图像信息
|
||||
$this->info = [
|
||||
'width' => $info[0],
|
||||
'height' => $info[1],
|
||||
'type' => image_type_to_extension($info[2], false),
|
||||
'mime' => $info['mime'],
|
||||
];
|
||||
|
||||
//销毁已存在的图像
|
||||
empty($this->im) || imagedestroy($this->im);
|
||||
|
||||
//打开图像
|
||||
if('gif' == $this->info['type']){
|
||||
$class = '\\Think\\Image\\Driver\\Gif';
|
||||
$this->gif = new $class($imgname);
|
||||
$this->im = imagecreatefromstring($this->gif->image());
|
||||
} else {
|
||||
$fun = "imagecreatefrom{$this->info['type']}";
|
||||
$this->im = $fun($imgname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存图像
|
||||
* @param string $imgname 图像保存名称
|
||||
* @param string $type 图像类型
|
||||
* @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
|
||||
*/
|
||||
public function save($imgname, $type = null, $interlace = true){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被保存的图像资源');
|
||||
|
||||
//自动获取图像类型
|
||||
if(is_null($type)){
|
||||
$type = $this->info['type'];
|
||||
} else {
|
||||
$type = strtolower($type);
|
||||
}
|
||||
|
||||
//JPEG图像设置隔行扫描
|
||||
if('jpeg' == $type || 'jpg' == $type){
|
||||
$type = 'jpeg';
|
||||
imageinterlace($this->im, $interlace);
|
||||
}
|
||||
|
||||
//保存图像
|
||||
if('gif' == $type && !empty($this->gif)){
|
||||
$this->gif->save($imgname);
|
||||
} else {
|
||||
$fun = "image{$type}";
|
||||
$fun($this->im, $imgname);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像宽度
|
||||
* @return integer 图像宽度
|
||||
*/
|
||||
public function width(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['width'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像高度
|
||||
* @return integer 图像高度
|
||||
*/
|
||||
public function height(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['height'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像类型
|
||||
* @return string 图像类型
|
||||
*/
|
||||
public function type(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像MIME类型
|
||||
* @return string 图像MIME类型
|
||||
*/
|
||||
public function mime(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['mime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
|
||||
* @return array 图像尺寸
|
||||
*/
|
||||
public function size(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return [$this->info['width'], $this->info['height']];
|
||||
}
|
||||
|
||||
/**
|
||||
* 裁剪图像
|
||||
* @param integer $w 裁剪区域宽度
|
||||
* @param integer $h 裁剪区域高度
|
||||
* @param integer $x 裁剪区域x坐标
|
||||
* @param integer $y 裁剪区域y坐标
|
||||
* @param integer $width 图像保存宽度
|
||||
* @param integer $height 图像保存高度
|
||||
*/
|
||||
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被裁剪的图像资源');
|
||||
|
||||
//设置保存尺寸
|
||||
empty($width) && $width = $w;
|
||||
empty($height) && $height = $h;
|
||||
|
||||
do {
|
||||
//创建新图像
|
||||
$img = imagecreatetruecolor($width, $height);
|
||||
// 调整默认颜色
|
||||
$color = imagecolorallocate($img, 255, 255, 255);
|
||||
imagefill($img, 0, 0, $color);
|
||||
|
||||
//裁剪
|
||||
imagecopyresampled($img, $this->im, 0, 0, $x, $y, $width, $height, $w, $h);
|
||||
imagedestroy($this->im); //销毁原图
|
||||
|
||||
//设置新图像
|
||||
$this->im = $img;
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
|
||||
$this->info['width'] = $width;
|
||||
$this->info['height'] = $height;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缩略图
|
||||
* @param integer $width 缩略图最大宽度
|
||||
* @param integer $height 缩略图最大高度
|
||||
* @param integer $type 缩略图裁剪类型
|
||||
*/
|
||||
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被缩略的图像资源');
|
||||
|
||||
//原图宽度和高度
|
||||
$w = $this->info['width'];
|
||||
$h = $this->info['height'];
|
||||
|
||||
/* 计算缩略图生成的必要参数 */
|
||||
switch ($type) {
|
||||
/* 等比例缩放 */
|
||||
case THINKIMAGE_THUMB_SCALING:
|
||||
//原图尺寸小于缩略图尺寸则不进行缩略
|
||||
if($w < $width && $h < $height) return;
|
||||
|
||||
//计算缩放比例
|
||||
$scale = min($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$width = $w * $scale;
|
||||
$height = $h * $scale;
|
||||
break;
|
||||
|
||||
/* 居中裁剪 */
|
||||
case THINKIMAGE_THUMB_CENTER:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = ($this->info['width'] - $w)/2;
|
||||
$y = ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 左上角裁剪 */
|
||||
case THINKIMAGE_THUMB_NORTHWEST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
break;
|
||||
|
||||
/* 右下角裁剪 */
|
||||
case THINKIMAGE_THUMB_SOUTHEAST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = $this->info['width'] - $w;
|
||||
$y = $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 填充 */
|
||||
case THINKIMAGE_THUMB_FILLED:
|
||||
//计算缩放比例
|
||||
if($w < $width && $h < $height){
|
||||
$scale = 1;
|
||||
} else {
|
||||
$scale = min($width/$w, $height/$h);
|
||||
}
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$neww = $w * $scale;
|
||||
$newh = $h * $scale;
|
||||
$posx = ($width - $w * $scale)/2;
|
||||
$posy = ($height - $h * $scale)/2;
|
||||
|
||||
do{
|
||||
//创建新图像
|
||||
$img = imagecreatetruecolor($width, $height);
|
||||
// 调整默认颜色
|
||||
$color = imagecolorallocate($img, 255, 255, 255);
|
||||
imagefill($img, 0, 0, $color);
|
||||
|
||||
//裁剪
|
||||
imagecopyresampled($img, $this->im, $posx, $posy, $x, $y, $neww, $newh, $w, $h);
|
||||
imagedestroy($this->im); //销毁原图
|
||||
$this->im = $img;
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
|
||||
$this->info['width'] = $width;
|
||||
$this->info['height'] = $height;
|
||||
return $this;
|
||||
|
||||
/* 固定 */
|
||||
case THINKIMAGE_THUMB_FIXED:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('不支持的缩略图裁剪类型');
|
||||
}
|
||||
|
||||
/* 裁剪图像 */
|
||||
$this->crop($w, $h, $x, $y, $width, $height);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加水印
|
||||
* @param string $source 水印图片路径
|
||||
* @param integer $locate 水印位置
|
||||
* @param integer $alpha 水印透明度
|
||||
*/
|
||||
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被添加水印的图像资源');
|
||||
if(!is_file($source)) throw new \Exception('水印图像不存在');
|
||||
|
||||
//获取水印图像信息
|
||||
$info = getimagesize($source);
|
||||
if(false === $info || (IMAGETYPE_GIF === $info[2] && empty($info['bits']))){
|
||||
throw new \Exception('非法水印文件');
|
||||
}
|
||||
|
||||
//创建水印图像资源
|
||||
$fun = 'imagecreatefrom' . image_type_to_extension($info[2], false);
|
||||
$water = $fun($source);
|
||||
|
||||
//设定水印图像的混色模式
|
||||
imagealphablending($water, true);
|
||||
|
||||
/* 设定水印位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$x = 0;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
/* 右上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 居中水印 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 下居中水印 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 右居中水印 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 上居中水印 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 左居中水印 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$x = 0;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义水印坐标 */
|
||||
if(is_array($locate)){
|
||||
list($x, $y) = $locate;
|
||||
} else {
|
||||
throw new \Exception('不支持的水印位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
do{
|
||||
//添加水印
|
||||
$src = imagecreatetruecolor($info[0], $info[1]);
|
||||
// 调整默认颜色
|
||||
$color = imagecolorallocate($src, 255, 255, 255);
|
||||
imagefill($src, 0, 0, $color);
|
||||
|
||||
imagecopy($src, $this->im, 0, 0, $x, $y, $info[0], $info[1]);
|
||||
imagecopy($src, $water, 0, 0, 0, 0, $info[0], $info[1]);
|
||||
imagecopymerge($this->im, $src, $x, $y, 0, 0, $info[0], $info[1], 100);
|
||||
|
||||
//销毁零时图片资源
|
||||
imagedestroy($src);
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
|
||||
//销毁水印资源
|
||||
imagedestroy($water);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 图像添加文字
|
||||
* @param string $text 添加的文字
|
||||
* @param string $font 字体路径
|
||||
* @param integer $size 字号
|
||||
* @param string $color 文字颜色
|
||||
* @param integer $locate 文字写入位置
|
||||
* @param integer $offset 文字相对当前位置的偏移量
|
||||
* @param integer $angle 文字倾斜角度
|
||||
*/
|
||||
public function text($text, $font, $size, $color = '#00000000',
|
||||
$locate = THINKIMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被写入文字的图像资源');
|
||||
if(!is_file($font)) throw new \Exception("不存在的字体文件:{$font}");
|
||||
|
||||
//获取文字信息
|
||||
$info = imagettfbbox($size, $angle, $font, $text);
|
||||
$minx = min($info[0], $info[2], $info[4], $info[6]);
|
||||
$maxx = max($info[0], $info[2], $info[4], $info[6]);
|
||||
$miny = min($info[1], $info[3], $info[5], $info[7]);
|
||||
$maxy = max($info[1], $info[3], $info[5], $info[7]);
|
||||
|
||||
/* 计算文字初始坐标和尺寸 */
|
||||
$x = $minx;
|
||||
$y = abs($miny);
|
||||
$w = $maxx - $minx;
|
||||
$h = $maxy - $miny;
|
||||
|
||||
/* 设定文字位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
// 起始坐标即为左上角坐标,无需调整
|
||||
break;
|
||||
|
||||
/* 右上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
break;
|
||||
|
||||
/* 居中文字 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 下居中文字 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 右居中文字 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 上居中文字 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
break;
|
||||
|
||||
/* 左居中文字 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义文字坐标 */
|
||||
if(is_array($locate)){
|
||||
list($posx, $posy) = $locate;
|
||||
$x += $posx;
|
||||
$y += $posy;
|
||||
} else {
|
||||
throw new \Exception('不支持的文字位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
/* 设置偏移量 */
|
||||
if(is_array($offset)){
|
||||
$offset = array_map('intval', $offset);
|
||||
list($ox, $oy) = $offset;
|
||||
} else{
|
||||
$offset = intval($offset);
|
||||
$ox = $oy = $offset;
|
||||
}
|
||||
|
||||
/* 设置颜色 */
|
||||
if(is_string($color) && 0 === strpos($color, '#')){
|
||||
$color = str_split(substr($color, 1), 2);
|
||||
$color = array_map('hexdec', $color);
|
||||
if(empty($color[3]) || $color[3] > 127){
|
||||
$color[3] = 0;
|
||||
}
|
||||
} elseif (!is_array($color)) {
|
||||
throw new \Exception('错误的颜色值');
|
||||
}
|
||||
|
||||
do{
|
||||
/* 写入文字 */
|
||||
$col = imagecolorallocatealpha($this->im, $color[0], $color[1], $color[2], $color[3]);
|
||||
imagettftext($this->im, $size, $angle, $x + $ox, $y + $oy, $col, $font, $text);
|
||||
} while(!empty($this->gif) && $this->gifNext());
|
||||
return $this;
|
||||
}
|
||||
|
||||
/* 切换到GIF的下一帧并保存当前帧,内部使用 */
|
||||
private function gifNext(){
|
||||
ob_start();
|
||||
ob_implicit_flush(0);
|
||||
imagegif($this->im);
|
||||
$img = ob_get_clean();
|
||||
|
||||
$this->gif->image($img);
|
||||
$next = $this->gif->nextImage();
|
||||
|
||||
if($next){
|
||||
$this->im = imagecreatefromstring($next);
|
||||
return $next;
|
||||
} else {
|
||||
$this->im = imagecreatefromstring($this->gif->image());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 析构方法,用于销毁图像资源
|
||||
*/
|
||||
public function __destruct() {
|
||||
empty($this->im) || imagedestroy($this->im);
|
||||
}
|
||||
}
|
||||
570
Library/Org/image/Driver/Gif.php
Normal file
570
Library/Org/image/Driver/Gif.php
Normal file
@@ -0,0 +1,570 @@
|
||||
<?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\Image\Driver;
|
||||
|
||||
class Gif{
|
||||
/**
|
||||
* GIF帧列表
|
||||
* @var array
|
||||
*/
|
||||
private $frames = [];
|
||||
|
||||
/**
|
||||
* 每帧等待时间列表
|
||||
* @var array
|
||||
*/
|
||||
private $delays = [];
|
||||
|
||||
/**
|
||||
* 构造方法,用于解码GIF图片
|
||||
* @param string $src GIF图片数据
|
||||
* @param string $mod 图片数据类型
|
||||
*/
|
||||
public function __construct($src = null, $mod = 'url') {
|
||||
if(!is_null($src)){
|
||||
if('url' == $mod && is_file($src)){
|
||||
$src = file_get_contents($src);
|
||||
}
|
||||
|
||||
/* 解码GIF图片 */
|
||||
try{
|
||||
$de = new GIFDecoder($src);
|
||||
$this->frames = $de->GIFGetFrames();
|
||||
$this->delays = $de->GIFGetDelays();
|
||||
} catch(Exception $e){
|
||||
throw new \Exception("解码GIF图片出错");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置或获取当前帧的数据
|
||||
* @param string $stream 二进制数据流
|
||||
* @return boolean 获取到的数据
|
||||
*/
|
||||
public function image($stream = null){
|
||||
if(is_null($stream)){
|
||||
$current = current($this->frames);
|
||||
return false === $current ? reset($this->frames) : $current;
|
||||
} else {
|
||||
$this->frames[key($this->frames)] = $stream;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将当前帧移动到下一帧
|
||||
* @return string 当前帧数据
|
||||
*/
|
||||
public function nextImage(){
|
||||
return next($this->frames);
|
||||
}
|
||||
|
||||
/**
|
||||
* 编码并保存当前GIF图片
|
||||
* @param string $gifname 图片名称
|
||||
*/
|
||||
public function save($gifname){
|
||||
$gif = new GIFEncoder($this->frames, $this->delays, 0, 2, 0, 0, 0, 'bin');
|
||||
file_put_contents($gifname, $gif->GetAnimation());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFEncoder Version 2.0 by László Zsidi, http://gifs.hu
|
||||
::
|
||||
:: This class is a rewritten 'GifMerge.class.php' version.
|
||||
::
|
||||
:: Modification:
|
||||
:: - Simplified and easy code,
|
||||
:: - Ultra fast encoding,
|
||||
:: - Built-in errors,
|
||||
:: - Stable working
|
||||
::
|
||||
::
|
||||
:: Updated at 2007. 02. 13. '00.05.AM'
|
||||
::
|
||||
::
|
||||
::
|
||||
:: Try on-line GIFBuilder Form demo based on GIFEncoder.
|
||||
::
|
||||
:: http://gifs.hu/phpclasses/demos/GifBuilder/
|
||||
::
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
*/
|
||||
|
||||
Class GIFEncoder {
|
||||
var $GIF = "GIF89a"; /* GIF header 6 bytes */
|
||||
var $VER = "GIFEncoder V2.05"; /* Encoder version */
|
||||
|
||||
var $BUF = array ( );
|
||||
var $LOP = 0;
|
||||
var $DIS = 2;
|
||||
var $COL = -1;
|
||||
var $IMG = -1;
|
||||
|
||||
var $ERR = array (
|
||||
'ERR00'=>"Does not supported function for only one image!",
|
||||
'ERR01'=>"Source is not a GIF image!",
|
||||
'ERR02'=>"Unintelligible flag ",
|
||||
'ERR03'=>"Does not make animation from animated GIF source",
|
||||
);
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFEncoder...
|
||||
::
|
||||
*/
|
||||
function GIFEncoder (
|
||||
$GIF_src, $GIF_dly, $GIF_lop, $GIF_dis,
|
||||
$GIF_red, $GIF_grn, $GIF_blu, $GIF_mod
|
||||
) {
|
||||
if ( ! is_array ( $GIF_src ) && ! is_array ( $GIF_tim ) ) {
|
||||
printf ( "%s: %s", $this->VER, $this->ERR [ 'ERR00' ] );
|
||||
exit ( 0 );
|
||||
}
|
||||
$this->LOP = ( $GIF_lop > -1 ) ? $GIF_lop : 0;
|
||||
$this->DIS = ( $GIF_dis > -1 ) ? ( ( $GIF_dis < 3 ) ? $GIF_dis : 3 ) : 2;
|
||||
$this->COL = ( $GIF_red > -1 && $GIF_grn > -1 && $GIF_blu > -1 ) ?
|
||||
( $GIF_red | ( $GIF_grn << 8 ) | ( $GIF_blu << 16 ) ) : -1;
|
||||
|
||||
for ( $i = 0; $i < count ( $GIF_src ); $i++ ) {
|
||||
if ( strToLower ( $GIF_mod ) == "url" ) {
|
||||
$this->BUF [ ] = fread ( fopen ( $GIF_src [ $i ], "rb" ), filesize ( $GIF_src [ $i ] ) );
|
||||
}
|
||||
else if ( strToLower ( $GIF_mod ) == "bin" ) {
|
||||
$this->BUF [ ] = $GIF_src [ $i ];
|
||||
}
|
||||
else {
|
||||
printf ( "%s: %s ( %s )!", $this->VER, $this->ERR [ 'ERR02' ], $GIF_mod );
|
||||
exit ( 0 );
|
||||
}
|
||||
if ( substr ( $this->BUF [ $i ], 0, 6 ) != "GIF87a" && substr ( $this->BUF [ $i ], 0, 6 ) != "GIF89a" ) {
|
||||
printf ( "%s: %d %s", $this->VER, $i, $this->ERR [ 'ERR01' ] );
|
||||
exit ( 0 );
|
||||
}
|
||||
for ( $j = ( 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) ), $k = true; $k; $j++ ) {
|
||||
switch ( $this->BUF [ $i ] { $j } ) {
|
||||
case "!":
|
||||
if ( ( substr ( $this->BUF [ $i ], ( $j + 3 ), 8 ) ) == "NETSCAPE" ) {
|
||||
printf ( "%s: %s ( %s source )!", $this->VER, $this->ERR [ 'ERR03' ], ( $i + 1 ) );
|
||||
exit ( 0 );
|
||||
}
|
||||
break;
|
||||
case ";":
|
||||
$k = FALSE;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
GIFEncoder::GIFAddHeader ( );
|
||||
for ( $i = 0; $i < count ( $this->BUF ); $i++ ) {
|
||||
GIFEncoder::GIFAddFrames ( $i, $GIF_dly [ $i ] );
|
||||
}
|
||||
GIFEncoder::GIFAddFooter ( );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFAddHeader...
|
||||
::
|
||||
*/
|
||||
function GIFAddHeader ( ) {
|
||||
$cmap = 0;
|
||||
|
||||
if ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x80 ) {
|
||||
$cmap = 3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) );
|
||||
|
||||
$this->GIF .= substr ( $this->BUF [ 0 ], 6, 7 );
|
||||
$this->GIF .= substr ( $this->BUF [ 0 ], 13, $cmap );
|
||||
$this->GIF .= "!\377\13NETSCAPE2.0\3\1" . GIFEncoder::GIFWord ( $this->LOP ) . "\0";
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFAddFrames...
|
||||
::
|
||||
*/
|
||||
function GIFAddFrames ( $i, $d ) {
|
||||
|
||||
$Locals_str = 13 + 3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) );
|
||||
|
||||
$Locals_end = strlen ( $this->BUF [ $i ] ) - $Locals_str - 1;
|
||||
$Locals_tmp = substr ( $this->BUF [ $i ], $Locals_str, $Locals_end );
|
||||
|
||||
$Global_len = 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
|
||||
$Locals_len = 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
|
||||
|
||||
$Global_rgb = substr ( $this->BUF [ 0 ], 13,
|
||||
3 * ( 2 << ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 ) ) );
|
||||
$Locals_rgb = substr ( $this->BUF [ $i ], 13,
|
||||
3 * ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ) );
|
||||
|
||||
$Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
|
||||
chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . "\x0\x0";
|
||||
|
||||
if ( $this->COL > -1 && ord ( $this->BUF [ $i ] { 10 } ) & 0x80 ) {
|
||||
for ( $j = 0; $j < ( 2 << ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 ) ); $j++ ) {
|
||||
if (
|
||||
ord ( $Locals_rgb { 3 * $j + 0 } ) == ( ( $this->COL >> 16 ) & 0xFF ) &&
|
||||
ord ( $Locals_rgb { 3 * $j + 1 } ) == ( ( $this->COL >> 8 ) & 0xFF ) &&
|
||||
ord ( $Locals_rgb { 3 * $j + 2 } ) == ( ( $this->COL >> 0 ) & 0xFF )
|
||||
) {
|
||||
$Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 1 ) .
|
||||
chr ( ( $d >> 0 ) & 0xFF ) . chr ( ( $d >> 8 ) & 0xFF ) . chr ( $j ) . "\x0";
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
switch ( $Locals_tmp { 0 } ) {
|
||||
case "!":
|
||||
$Locals_img = substr ( $Locals_tmp, 8, 10 );
|
||||
$Locals_tmp = substr ( $Locals_tmp, 18, strlen ( $Locals_tmp ) - 18 );
|
||||
break;
|
||||
case ",":
|
||||
$Locals_img = substr ( $Locals_tmp, 0, 10 );
|
||||
$Locals_tmp = substr ( $Locals_tmp, 10, strlen ( $Locals_tmp ) - 10 );
|
||||
break;
|
||||
}
|
||||
if ( ord ( $this->BUF [ $i ] { 10 } ) & 0x80 && $this->IMG > -1 ) {
|
||||
if ( $Global_len == $Locals_len ) {
|
||||
if ( GIFEncoder::GIFBlockCompare ( $Global_rgb, $Locals_rgb, $Global_len ) ) {
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
|
||||
}
|
||||
else {
|
||||
$byte = ord ( $Locals_img { 9 } );
|
||||
$byte |= 0x80;
|
||||
$byte &= 0xF8;
|
||||
$byte |= ( ord ( $this->BUF [ 0 ] { 10 } ) & 0x07 );
|
||||
$Locals_img { 9 } = chr ( $byte );
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$byte = ord ( $Locals_img { 9 } );
|
||||
$byte |= 0x80;
|
||||
$byte &= 0xF8;
|
||||
$byte |= ( ord ( $this->BUF [ $i ] { 10 } ) & 0x07 );
|
||||
$Locals_img { 9 } = chr ( $byte );
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_rgb . $Locals_tmp );
|
||||
}
|
||||
}
|
||||
else {
|
||||
$this->GIF .= ( $Locals_ext . $Locals_img . $Locals_tmp );
|
||||
}
|
||||
$this->IMG = 1;
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFAddFooter...
|
||||
::
|
||||
*/
|
||||
function GIFAddFooter ( ) {
|
||||
$this->GIF .= ";";
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFBlockCompare...
|
||||
::
|
||||
*/
|
||||
function GIFBlockCompare ( $GlobalBlock, $LocalBlock, $Len ) {
|
||||
|
||||
for ( $i = 0; $i < $Len; $i++ ) {
|
||||
if (
|
||||
$GlobalBlock { 3 * $i + 0 } != $LocalBlock { 3 * $i + 0 } ||
|
||||
$GlobalBlock { 3 * $i + 1 } != $LocalBlock { 3 * $i + 1 } ||
|
||||
$GlobalBlock { 3 * $i + 2 } != $LocalBlock { 3 * $i + 2 }
|
||||
) {
|
||||
return ( 0 );
|
||||
}
|
||||
}
|
||||
|
||||
return ( 1 );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFWord...
|
||||
::
|
||||
*/
|
||||
function GIFWord ( $int ) {
|
||||
|
||||
return ( chr ( $int & 0xFF ) . chr ( ( $int >> 8 ) & 0xFF ) );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GetAnimation...
|
||||
::
|
||||
*/
|
||||
function GetAnimation ( ) {
|
||||
return ( $this->GIF );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFDecoder Version 2.0 by László Zsidi, http://gifs.hu
|
||||
::
|
||||
:: Created at 2007. 02. 01. '07.47.AM'
|
||||
::
|
||||
::
|
||||
::
|
||||
::
|
||||
:: Try on-line GIFBuilder Form demo based on GIFDecoder.
|
||||
::
|
||||
:: http://gifs.hu/phpclasses/demos/GifBuilder/
|
||||
::
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
*/
|
||||
|
||||
Class GIFDecoder {
|
||||
var $GIF_buffer = array ( );
|
||||
var $GIF_arrays = array ( );
|
||||
var $GIF_delays = array ( );
|
||||
var $GIF_stream = "";
|
||||
var $GIF_string = "";
|
||||
var $GIF_bfseek = 0;
|
||||
|
||||
var $GIF_screen = array ( );
|
||||
var $GIF_global = array ( );
|
||||
var $GIF_sorted;
|
||||
var $GIF_colorS;
|
||||
var $GIF_colorC;
|
||||
var $GIF_colorF;
|
||||
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFDecoder ( $GIF_pointer )
|
||||
::
|
||||
*/
|
||||
function GIFDecoder ( $GIF_pointer ) {
|
||||
$this->GIF_stream = $GIF_pointer;
|
||||
|
||||
GIFDecoder::GIFGetByte ( 6 ); // GIF89a
|
||||
GIFDecoder::GIFGetByte ( 7 ); // Logical Screen Descriptor
|
||||
|
||||
$this->GIF_screen = $this->GIF_buffer;
|
||||
$this->GIF_colorF = $this->GIF_buffer [ 4 ] & 0x80 ? 1 : 0;
|
||||
$this->GIF_sorted = $this->GIF_buffer [ 4 ] & 0x08 ? 1 : 0;
|
||||
$this->GIF_colorC = $this->GIF_buffer [ 4 ] & 0x07;
|
||||
$this->GIF_colorS = 2 << $this->GIF_colorC;
|
||||
|
||||
if ( $this->GIF_colorF == 1 ) {
|
||||
GIFDecoder::GIFGetByte ( 3 * $this->GIF_colorS );
|
||||
$this->GIF_global = $this->GIF_buffer;
|
||||
}
|
||||
/*
|
||||
*
|
||||
* 05.06.2007.
|
||||
* Made a little modification
|
||||
*
|
||||
*
|
||||
- for ( $cycle = 1; $cycle; ) {
|
||||
+ if ( GIFDecoder::GIFGetByte ( 1 ) ) {
|
||||
- switch ( $this->GIF_buffer [ 0 ] ) {
|
||||
- case 0x21:
|
||||
- GIFDecoder::GIFReadExtensions ( );
|
||||
- break;
|
||||
- case 0x2C:
|
||||
- GIFDecoder::GIFReadDescriptor ( );
|
||||
- break;
|
||||
- case 0x3B:
|
||||
- $cycle = 0;
|
||||
- break;
|
||||
- }
|
||||
- }
|
||||
+ else {
|
||||
+ $cycle = 0;
|
||||
+ }
|
||||
- }
|
||||
*/
|
||||
for ( $cycle = 1; $cycle; ) {
|
||||
if ( GIFDecoder::GIFGetByte ( 1 ) ) {
|
||||
switch ( $this->GIF_buffer [ 0 ] ) {
|
||||
case 0x21:
|
||||
GIFDecoder::GIFReadExtensions ( );
|
||||
break;
|
||||
case 0x2C:
|
||||
GIFDecoder::GIFReadDescriptor ( );
|
||||
break;
|
||||
case 0x3B:
|
||||
$cycle = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$cycle = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFReadExtension ( )
|
||||
::
|
||||
*/
|
||||
function GIFReadExtensions ( ) {
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
for ( ; ; ) {
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
if ( ( $u = $this->GIF_buffer [ 0 ] ) == 0x00 ) {
|
||||
break;
|
||||
}
|
||||
GIFDecoder::GIFGetByte ( $u );
|
||||
/*
|
||||
* 07.05.2007.
|
||||
* Implemented a new line for a new function
|
||||
* to determine the originaly delays between
|
||||
* frames.
|
||||
*
|
||||
*/
|
||||
if ( $u == 4 ) {
|
||||
$this->GIF_delays [ ] = ( $this->GIF_buffer [ 1 ] | $this->GIF_buffer [ 2 ] << 8 );
|
||||
}
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFReadExtension ( )
|
||||
::
|
||||
*/
|
||||
function GIFReadDescriptor ( ) {
|
||||
$GIF_screen = array ( );
|
||||
|
||||
GIFDecoder::GIFGetByte ( 9 );
|
||||
$GIF_screen = $this->GIF_buffer;
|
||||
$GIF_colorF = $this->GIF_buffer [ 8 ] & 0x80 ? 1 : 0;
|
||||
if ( $GIF_colorF ) {
|
||||
$GIF_code = $this->GIF_buffer [ 8 ] & 0x07;
|
||||
$GIF_sort = $this->GIF_buffer [ 8 ] & 0x20 ? 1 : 0;
|
||||
}
|
||||
else {
|
||||
$GIF_code = $this->GIF_colorC;
|
||||
$GIF_sort = $this->GIF_sorted;
|
||||
}
|
||||
$GIF_size = 2 << $GIF_code;
|
||||
$this->GIF_screen [ 4 ] &= 0x70;
|
||||
$this->GIF_screen [ 4 ] |= 0x80;
|
||||
$this->GIF_screen [ 4 ] |= $GIF_code;
|
||||
if ( $GIF_sort ) {
|
||||
$this->GIF_screen [ 4 ] |= 0x08;
|
||||
}
|
||||
$this->GIF_string = "GIF87a";
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_screen );
|
||||
if ( $GIF_colorF == 1 ) {
|
||||
GIFDecoder::GIFGetByte ( 3 * $GIF_size );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
}
|
||||
else {
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_global );
|
||||
}
|
||||
$this->GIF_string .= chr ( 0x2C );
|
||||
$GIF_screen [ 8 ] &= 0x40;
|
||||
GIFDecoder::GIFPutByte ( $GIF_screen );
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
for ( ; ; ) {
|
||||
GIFDecoder::GIFGetByte ( 1 );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
if ( ( $u = $this->GIF_buffer [ 0 ] ) == 0x00 ) {
|
||||
break;
|
||||
}
|
||||
GIFDecoder::GIFGetByte ( $u );
|
||||
GIFDecoder::GIFPutByte ( $this->GIF_buffer );
|
||||
}
|
||||
$this->GIF_string .= chr ( 0x3B );
|
||||
/*
|
||||
Add frames into $GIF_stream array...
|
||||
*/
|
||||
$this->GIF_arrays [ ] = $this->GIF_string;
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFGetByte ( $len )
|
||||
::
|
||||
*/
|
||||
|
||||
/*
|
||||
*
|
||||
* 05.06.2007.
|
||||
* Made a little modification
|
||||
*
|
||||
*
|
||||
- function GIFGetByte ( $len ) {
|
||||
- $this->GIF_buffer = array ( );
|
||||
-
|
||||
- for ( $i = 0; $i < $len; $i++ ) {
|
||||
+ if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) {
|
||||
+ return 0;
|
||||
+ }
|
||||
- $this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } );
|
||||
- }
|
||||
+ return 1;
|
||||
- }
|
||||
*/
|
||||
function GIFGetByte ( $len ) {
|
||||
$this->GIF_buffer = array ( );
|
||||
|
||||
for ( $i = 0; $i < $len; $i++ ) {
|
||||
if ( $this->GIF_bfseek > strlen ( $this->GIF_stream ) ) {
|
||||
return 0;
|
||||
}
|
||||
$this->GIF_buffer [ ] = ord ( $this->GIF_stream { $this->GIF_bfseek++ } );
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFPutByte ( $bytes )
|
||||
::
|
||||
*/
|
||||
function GIFPutByte ( $bytes ) {
|
||||
for ( $i = 0; $i < count ( $bytes ); $i++ ) {
|
||||
$this->GIF_string .= chr ( $bytes [ $i ] );
|
||||
}
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: PUBLIC FUNCTIONS
|
||||
::
|
||||
::
|
||||
:: GIFGetFrames ( )
|
||||
::
|
||||
*/
|
||||
function GIFGetFrames ( ) {
|
||||
return ( $this->GIF_arrays );
|
||||
}
|
||||
/*
|
||||
:::::::::::::::::::::::::::::::::::::::::::::::::::
|
||||
::
|
||||
:: GIFGetDelays ( )
|
||||
::
|
||||
*/
|
||||
function GIFGetDelays ( ) {
|
||||
return ( $this->GIF_delays );
|
||||
}
|
||||
}
|
||||
591
Library/Org/image/Driver/Imagick.php
Normal file
591
Library/Org/image/Driver/Imagick.php
Normal file
@@ -0,0 +1,591 @@
|
||||
<?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\Image\Driver;
|
||||
|
||||
class Imagick{
|
||||
/**
|
||||
* 图像资源对象
|
||||
* @var resource
|
||||
*/
|
||||
private $im;
|
||||
|
||||
/**
|
||||
* 图像信息,包括width,height,type,mime,size
|
||||
* @var array
|
||||
*/
|
||||
private $info;
|
||||
|
||||
/**
|
||||
* 构造方法,可用于打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function __construct($imgname = null) {
|
||||
if ( !extension_loaded('Imagick') ) {
|
||||
E(L('_NOT_SUPPERT_').':Imagick');
|
||||
}
|
||||
$imgname && $this->open($imgname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开一张图像
|
||||
* @param string $imgname 图像路径
|
||||
*/
|
||||
public function open($imgname){
|
||||
//检测图像文件
|
||||
if(!is_file($imgname)) throw new \Exception('不存在的图像文件');
|
||||
|
||||
//销毁已存在的图像
|
||||
empty($this->im) || $this->im->destroy();
|
||||
|
||||
//载入图像
|
||||
$this->im = new \Imagick(realpath($imgname));
|
||||
|
||||
//设置图像信息
|
||||
$this->info = [
|
||||
'width' => $this->im->getImageWidth(),
|
||||
'height' => $this->im->getImageHeight(),
|
||||
'type' => strtolower($this->im->getImageFormat()),
|
||||
'mime' => $this->im->getImageMimeType(),
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存图像
|
||||
* @param string $imgname 图像保存名称
|
||||
* @param string $type 图像类型
|
||||
* @param boolean $interlace 是否对JPEG类型图像设置隔行扫描
|
||||
*/
|
||||
public function save($imgname, $type = null, $interlace = true){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被保存的图像资源');
|
||||
|
||||
//设置图片类型
|
||||
if(is_null($type)){
|
||||
$type = $this->info['type'];
|
||||
} else {
|
||||
$type = strtolower($type);
|
||||
$this->im->setImageFormat($type);
|
||||
}
|
||||
|
||||
//JPEG图像设置隔行扫描
|
||||
if('jpeg' == $type || 'jpg' == $type){
|
||||
$this->im->setImageInterlaceScheme(1);
|
||||
}
|
||||
|
||||
//去除图像配置信息
|
||||
$this->im->stripImage();
|
||||
|
||||
//保存图像
|
||||
$imgname = realpath(dirname($imgname)) . '/' . basename($imgname); //强制绝对路径
|
||||
if ('gif' == $type) {
|
||||
$this->im->writeImages($imgname, true);
|
||||
} else {
|
||||
$this->im->writeImage($imgname);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像宽度
|
||||
* @return integer 图像宽度
|
||||
*/
|
||||
public function width(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['width'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像高度
|
||||
* @return integer 图像高度
|
||||
*/
|
||||
public function height(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['height'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像类型
|
||||
* @return string 图像类型
|
||||
*/
|
||||
public function type(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['type'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像MIME类型
|
||||
* @return string 图像MIME类型
|
||||
*/
|
||||
public function mime(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return $this->info['mime'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回图像尺寸数组 0 - 图像宽度,1 - 图像高度
|
||||
* @return array 图像尺寸
|
||||
*/
|
||||
public function size(){
|
||||
if(empty($this->im)) throw new \Exception('没有指定图像资源');
|
||||
return [$this->info['width'], $this->info['height']];
|
||||
}
|
||||
|
||||
/**
|
||||
* 裁剪图像
|
||||
* @param integer $w 裁剪区域宽度
|
||||
* @param integer $h 裁剪区域高度
|
||||
* @param integer $x 裁剪区域x坐标
|
||||
* @param integer $y 裁剪区域y坐标
|
||||
* @param integer $width 图像保存宽度
|
||||
* @param integer $height 图像保存高度
|
||||
*/
|
||||
public function crop($w, $h, $x = 0, $y = 0, $width = null, $height = null){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被裁剪的图像资源');
|
||||
|
||||
//设置保存尺寸
|
||||
empty($width) && $width = $w;
|
||||
empty($height) && $height = $h;
|
||||
|
||||
//裁剪图片
|
||||
if('gif' == $this->info['type']){
|
||||
$img = $this->im->coalesceImages();
|
||||
$this->im->destroy(); //销毁原图
|
||||
|
||||
//循环裁剪每一帧
|
||||
do {
|
||||
$this->_crop($w, $h, $x, $y, $width, $height, $img);
|
||||
} while ($img->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im = $img->deconstructImages();
|
||||
$img->destroy(); //销毁零时图片
|
||||
} else {
|
||||
$this->_crop($w, $h, $x, $y, $width, $height);
|
||||
}
|
||||
}
|
||||
|
||||
/* 裁剪图片,内部调用 */
|
||||
private function _crop($w, $h, $x, $y, $width, $height, $img = null){
|
||||
is_null($img) && $img = $this->im;
|
||||
|
||||
//裁剪
|
||||
$info = $this->info;
|
||||
if($x != 0 || $y != 0 || $w != $info['width'] || $h != $info['height']){
|
||||
$img->cropImage($w, $h, $x, $y);
|
||||
$img->setImagePage($w, $h, 0, 0); //调整画布和图片一致
|
||||
}
|
||||
|
||||
//调整大小
|
||||
if($w != $width || $h != $height){
|
||||
$img->scaleImage($width, $height);
|
||||
}
|
||||
|
||||
//设置缓存尺寸
|
||||
$this->info['width'] = $w;
|
||||
$this->info['height'] = $h;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成缩略图
|
||||
* @param integer $width 缩略图最大宽度
|
||||
* @param integer $height 缩略图最大高度
|
||||
* @param integer $type 缩略图裁剪类型
|
||||
*/
|
||||
public function thumb($width, $height, $type = THINKIMAGE_THUMB_SCALE){
|
||||
if(empty($this->im)) throw new \Exception('没有可以被缩略的图像资源');
|
||||
|
||||
//原图宽度和高度
|
||||
$w = $this->info['width'];
|
||||
$h = $this->info['height'];
|
||||
|
||||
/* 计算缩略图生成的必要参数 */
|
||||
switch ($type) {
|
||||
/* 等比例缩放 */
|
||||
case THINKIMAGE_THUMB_SCALING:
|
||||
//原图尺寸小于缩略图尺寸则不进行缩略
|
||||
if($w < $width && $h < $height) return;
|
||||
|
||||
//计算缩放比例
|
||||
$scale = min($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$width = $w * $scale;
|
||||
$height = $h * $scale;
|
||||
break;
|
||||
|
||||
/* 居中裁剪 */
|
||||
case THINKIMAGE_THUMB_CENTER:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = ($this->info['width'] - $w)/2;
|
||||
$y = ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 左上角裁剪 */
|
||||
case THINKIMAGE_THUMB_NORTHWEST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$x = $y = 0;
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
break;
|
||||
|
||||
/* 右下角裁剪 */
|
||||
case THINKIMAGE_THUMB_SOUTHEAST:
|
||||
//计算缩放比例
|
||||
$scale = max($width/$w, $height/$h);
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$w = $width/$scale;
|
||||
$h = $height/$scale;
|
||||
$x = $this->info['width'] - $w;
|
||||
$y = $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 填充 */
|
||||
case THINKIMAGE_THUMB_FILLED:
|
||||
//计算缩放比例
|
||||
if($w < $width && $h < $height){
|
||||
$scale = 1;
|
||||
} else {
|
||||
$scale = min($width/$w, $height/$h);
|
||||
}
|
||||
|
||||
//设置缩略图的坐标及宽度和高度
|
||||
$neww = $w * $scale;
|
||||
$newh = $h * $scale;
|
||||
$posx = ($width - $w * $scale)/2;
|
||||
$posy = ($height - $h * $scale)/2;
|
||||
|
||||
//创建一张新图像
|
||||
$newimg = new Imagick();
|
||||
$newimg->newImage($width, $height, 'white', $this->info['type']);
|
||||
|
||||
|
||||
if('gif' == $this->info['type']){
|
||||
$imgs = $this->im->coalesceImages();
|
||||
$img = new Imagick();
|
||||
$this->im->destroy(); //销毁原图
|
||||
|
||||
//循环填充每一帧
|
||||
do {
|
||||
//填充图像
|
||||
$image = $this->_fill($newimg, $posx, $posy, $neww, $newh, $imgs);
|
||||
|
||||
$img->addImage($image);
|
||||
$img->setImageDelay($imgs->getImageDelay());
|
||||
$img->setImagePage($width, $height, 0, 0);
|
||||
|
||||
$image->destroy(); //销毁零时图片
|
||||
|
||||
} while ($imgs->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im->destroy();
|
||||
$this->im = $img->deconstructImages();
|
||||
$imgs->destroy(); //销毁零时图片
|
||||
$img->destroy(); //销毁零时图片
|
||||
|
||||
} else {
|
||||
//填充图像
|
||||
$img = $this->_fill($newimg, $posx, $posy, $neww, $newh);
|
||||
//销毁原图
|
||||
$this->im->destroy();
|
||||
$this->im = $img;
|
||||
}
|
||||
|
||||
//设置新图像属性
|
||||
$this->info['width'] = $width;
|
||||
$this->info['height'] = $height;
|
||||
return;
|
||||
|
||||
/* 固定 */
|
||||
case THINKIMAGE_THUMB_FIXED:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('不支持的缩略图裁剪类型');
|
||||
}
|
||||
|
||||
/* 裁剪图像 */
|
||||
$this->crop($w, $h, $x, $y, $width, $height);
|
||||
}
|
||||
|
||||
/* 填充指定图像,内部使用 */
|
||||
private function _fill($newimg, $posx, $posy, $neww, $newh, $img = null){
|
||||
is_null($img) && $img = $this->im;
|
||||
|
||||
/* 将指定图片绘入空白图片 */
|
||||
$draw = new ImagickDraw();
|
||||
$draw->composite($img->getImageCompose(), $posx, $posy, $neww, $newh, $img);
|
||||
$image = $newimg->clone();
|
||||
$image->drawImage($draw);
|
||||
$draw->destroy();
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加水印
|
||||
* @param string $source 水印图片路径
|
||||
* @param integer $locate 水印位置
|
||||
* @param integer $alpha 水印透明度
|
||||
*/
|
||||
public function water($source, $locate = THINKIMAGE_WATER_SOUTHEAST){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被添加水印的图像资源');
|
||||
if(!is_file($source)) throw new \Exception('水印图像不存在');
|
||||
|
||||
//创建水印图像资源
|
||||
$water = new Imagick(realpath($source));
|
||||
$info = [$water->getImageWidth(), $water->getImageHeight()];
|
||||
|
||||
/* 设定水印位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左下角水印 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$x = 0;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 左上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
$x = $y = 0;
|
||||
break;
|
||||
|
||||
/* 右上角水印 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 居中水印 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 下居中水印 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = $this->info['height'] - $info[1];
|
||||
break;
|
||||
|
||||
/* 右居中水印 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x = $this->info['width'] - $info[0];
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
/* 上居中水印 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x = ($this->info['width'] - $info[0])/2;
|
||||
$y = 0;
|
||||
break;
|
||||
|
||||
/* 左居中水印 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$x = 0;
|
||||
$y = ($this->info['height'] - $info[1])/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义水印坐标 */
|
||||
if(is_array($locate)){
|
||||
list($x, $y) = $locate;
|
||||
} else {
|
||||
throw new \Exception('不支持的水印位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
//创建绘图资源
|
||||
$draw = new ImagickDraw();
|
||||
$draw->composite($water->getImageCompose(), $x, $y, $info[0], $info[1], $water);
|
||||
|
||||
if('gif' == $this->info['type']){
|
||||
$img = $this->im->coalesceImages();
|
||||
$this->im->destroy(); //销毁原图
|
||||
|
||||
do{
|
||||
//添加水印
|
||||
$img->drawImage($draw);
|
||||
} while ($img->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im = $img->deconstructImages();
|
||||
$img->destroy(); //销毁零时图片
|
||||
|
||||
} else {
|
||||
//添加水印
|
||||
$this->im->drawImage($draw);
|
||||
}
|
||||
|
||||
//销毁水印资源
|
||||
$draw->destroy();
|
||||
$water->destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 图像添加文字
|
||||
* @param string $text 添加的文字
|
||||
* @param string $font 字体路径
|
||||
* @param integer $size 字号
|
||||
* @param string $color 文字颜色
|
||||
* @param integer $locate 文字写入位置
|
||||
* @param integer $offset 文字相对当前位置的偏移量
|
||||
* @param integer $angle 文字倾斜角度
|
||||
*/
|
||||
public function text($text, $font, $size, $color = '#00000000',
|
||||
$locate = THINKIMAGE_WATER_SOUTHEAST, $offset = 0, $angle = 0){
|
||||
//资源检测
|
||||
if(empty($this->im)) throw new \Exception('没有可以被写入文字的图像资源');
|
||||
if(!is_file($font)) throw new \Exception("不存在的字体文件:{$font}");
|
||||
|
||||
//获取颜色和透明度
|
||||
if(is_array($color)){
|
||||
$color = array_map('dechex', $color);
|
||||
foreach ($color as &$value) {
|
||||
$value = str_pad($value, 2, '0', STR_PAD_LEFT);
|
||||
}
|
||||
$color = '#' . implode('', $color);
|
||||
} elseif(!is_string($color) || 0 !== strpos($color, '#')) {
|
||||
throw new \Exception('错误的颜色值');
|
||||
}
|
||||
$col = substr($color, 0, 7);
|
||||
$alp = strlen($color) == 9 ? substr($color, -2) : 0;
|
||||
|
||||
|
||||
//获取文字信息
|
||||
$draw = new ImagickDraw();
|
||||
$draw->setFont(realpath($font));
|
||||
$draw->setFontSize($size);
|
||||
$draw->setFillColor($col);
|
||||
$draw->setFillAlpha(1-hexdec($alp)/127);
|
||||
$draw->setTextAntialias(true);
|
||||
$draw->setStrokeAntialias(true);
|
||||
|
||||
$metrics = $this->im->queryFontMetrics($draw, $text);
|
||||
|
||||
/* 计算文字初始坐标和尺寸 */
|
||||
$x = 0;
|
||||
$y = $metrics['ascender'];
|
||||
$w = $metrics['textWidth'];
|
||||
$h = $metrics['textHeight'];
|
||||
|
||||
/* 设定文字位置 */
|
||||
switch ($locate) {
|
||||
/* 右下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左下角文字 */
|
||||
case THINKIMAGE_WATER_SOUTHWEST:
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 左上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHWEST:
|
||||
// 起始坐标即为左上角坐标,无需调整
|
||||
break;
|
||||
|
||||
/* 右上角文字 */
|
||||
case THINKIMAGE_WATER_NORTHEAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
break;
|
||||
|
||||
/* 居中文字 */
|
||||
case THINKIMAGE_WATER_CENTER:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 下居中文字 */
|
||||
case THINKIMAGE_WATER_SOUTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
$y += $this->info['height'] - $h;
|
||||
break;
|
||||
|
||||
/* 右居中文字 */
|
||||
case THINKIMAGE_WATER_EAST:
|
||||
$x += $this->info['width'] - $w;
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
/* 上居中文字 */
|
||||
case THINKIMAGE_WATER_NORTH:
|
||||
$x += ($this->info['width'] - $w)/2;
|
||||
break;
|
||||
|
||||
/* 左居中文字 */
|
||||
case THINKIMAGE_WATER_WEST:
|
||||
$y += ($this->info['height'] - $h)/2;
|
||||
break;
|
||||
|
||||
default:
|
||||
/* 自定义文字坐标 */
|
||||
if(is_array($locate)){
|
||||
list($posx, $posy) = $locate;
|
||||
$x += $posx;
|
||||
$y += $posy;
|
||||
} else {
|
||||
throw new \Exception('不支持的文字位置类型');
|
||||
}
|
||||
}
|
||||
|
||||
/* 设置偏移量 */
|
||||
if(is_array($offset)){
|
||||
$offset = array_map('intval', $offset);
|
||||
list($ox, $oy) = $offset;
|
||||
} else{
|
||||
$offset = intval($offset);
|
||||
$ox = $oy = $offset;
|
||||
}
|
||||
|
||||
/* 写入文字 */
|
||||
if('gif' == $this->info['type']){
|
||||
$img = $this->im->coalesceImages();
|
||||
$this->im->destroy(); //销毁原图
|
||||
do{
|
||||
$img->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
|
||||
} while ($img->nextImage());
|
||||
|
||||
//压缩图片
|
||||
$this->im = $img->deconstructImages();
|
||||
$img->destroy(); //销毁零时图片
|
||||
|
||||
} else {
|
||||
$this->im->annotateImage($draw, $x + $ox, $y + $oy, $angle, $text);
|
||||
}
|
||||
$draw->destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 析构方法,用于销毁图像资源
|
||||
*/
|
||||
public function __destruct() {
|
||||
empty($this->im) || $this->im->destroy();
|
||||
}
|
||||
}
|
||||
71
Library/Org/oauth.php
Normal file
71
Library/Org/oauth.php
Normal file
@@ -0,0 +1,71 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
233
Library/Org/oauth/Driver.php
Normal file
233
Library/Org/oauth/Driver.php
Normal file
@@ -0,0 +1,233 @@
|
||||
<?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();
|
||||
}
|
||||
93
Library/Org/oauth/Driver/Baidu.php
Normal file
93
Library/Org/oauth/Driver/Baidu.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
93
Library/Org/oauth/Driver/Diandian.php
Normal file
93
Library/Org/oauth/Driver/Diandian.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
91
Library/Org/oauth/Driver/Douban.php
Normal file
91
Library/Org/oauth/Driver/Douban.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
93
Library/Org/oauth/Driver/Github.php
Normal file
93
Library/Org/oauth/Driver/Github.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
99
Library/Org/oauth/Driver/Google.php
Normal file
99
Library/Org/oauth/Driver/Google.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?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}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
94
Library/Org/oauth/Driver/Kaixin.php
Normal file
94
Library/Org/oauth/Driver/Kaixin.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
100
Library/Org/oauth/Driver/Msn.php
Normal file
100
Library/Org/oauth/Driver/Msn.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?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}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
102
Library/Org/oauth/Driver/Qq.php
Normal file
102
Library/Org/oauth/Driver/Qq.php
Normal file
@@ -0,0 +1,102 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
}
|
||||
116
Library/Org/oauth/Driver/Renren.php
Normal file
116
Library/Org/oauth/Driver/Renren.php
Normal file
@@ -0,0 +1,116 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
}
|
||||
93
Library/Org/oauth/Driver/Sina.php
Normal file
93
Library/Org/oauth/Driver/Sina.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
93
Library/Org/oauth/Driver/Sohu.php
Normal file
93
Library/Org/oauth/Driver/Sohu.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
95
Library/Org/oauth/Driver/T163.php
Normal file
95
Library/Org/oauth/Driver/T163.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
97
Library/Org/oauth/Driver/Taobao.php
Normal file
97
Library/Org/oauth/Driver/Taobao.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
98
Library/Org/oauth/Driver/Tencent.php
Normal file
98
Library/Org/oauth/Driver/Tencent.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
93
Library/Org/oauth/Driver/X360.php
Normal file
93
Library/Org/oauth/Driver/X360.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?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']}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
22
Library/Org/transform/Driver/Json.php
Normal file
22
Library/Org/transform/Driver/Json.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
119
Library/Org/transform/Driver/Xml.php
Normal file
119
Library/Org/transform/Driver/Xml.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
514
Library/Org/upload.php
Normal file
514
Library/Org/upload.php
Normal file
@@ -0,0 +1,514 @@
|
||||
<?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;
|
||||
}
|
||||
}
|
||||
189
Library/Org/validate.php
Normal file
189
Library/Org/validate.php
Normal file
@@ -0,0 +1,189 @@
|
||||
<?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