Files
OMS/app/ome/statics/lib/oauth.php
2025-12-28 23:13:25 +08:00

168 lines
4.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Copyright © ShopeX http://www.shopex.cn. All rights reserved.
* See LICENSE file for license details.
*/
include(dirname(__FILE__)."/oauth/response.php");
include(dirname(__FILE__)."/oauth/request.php");
class oauth2{
protected $token_type = 'Bearer';
private $__request;
private $__response;
private $__config;
function __construct($config){
$this->key = $config['key'];
$this->secret = $config['secret'];
$this->site = $config['site'];
$this->oauth = $config['oauth'];
$this->__config = $config;
}
public function get_token($code)
{
$url = "{$this->oauth}/token";
$data = array(
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => "",
'client_id'=>$this->key,
'client_secret'=>$this->secret,
);
$this->request()->get_info($url, $data);
$params = $this->request()->parsed();
$token = $params['access_token'];
unset($params['expires_in'], $params['token_type'], $params['access_token']);
$this->token = $token;
return array('token'=>$token, 'params'=>$params);
}
public function authorize_url($callback){
$data = array(
'response_type' => 'code',
'client_id' => $this->key,
'redirect_uri'=>$callback,
);
$query = http_build_query($data);
$url = "{$this->oauth}/authorize?{$query}";
return $url;
}
public function logout_url($callback)
{
$data = array(
'redirect_uri'=>$callback,
);
$query = http_build_query($data);
$url = "{$this->oauth}/logout?{$query}";
return $url;
}
public function request($token=null)
{
if (!$this->__request)
$this->__request = new oauth2_request($this->__config, $token);
return $this->__request;
}
public function response()
{
if (!$this->__response)
$this->__response = new oauth2_response($this->__config);
return $this->__response;
}
public function verify_api_params()
{
$arr_get = $_GET;
$arr_post = $_POST;
$sign = $arr_get['sign'];
$arr_get = $arr_get['sign'];
echo "<pre>";
print_r($_GET);
print_r($_POST);
return true;
}
public function sign($action, $url, $data=array(), $signtime=null)
{
$parse_url = parse_url($url);
$path = $parse_url['path'];
$get_data['sign_method'] = 'md5';
$get_data['sign_time'] = $signtime ? $signtime : time();
$get_data['client_id'] = $this->key;
$post_data = array();
if(strtolower($action)=='get') {
$get_data = array_merge($data, $get_data);
} else {
$post_data = $data;
}
$get_data = $this->ksort($get_data);
$post_data = $this->ksort($post_data);
$get_params = rawurlencode(urldecode(http_build_query($get_data)));
$post_params = rawurlencode(urldecode(http_build_query($post_data)));
$header_params = rawurlencode(urldecode(http_build_query($header_data)));
#$get_params = $post_params = $header_params = null;
#foreach(array('get_params' =>$get_data, 'post_params'=>$post_data) as $key => $val){
# $$key = $this->build_query($val);
#}
#$get_params = rawurlencode(ltrim($get_params, '&'));
#$post_params = rawurlencode(ltrim($post_params, '&'));
#$header_params = rawurlencode(ltrim($header_params, '&'));
$path = rawurlencode('/'.ltrim($path, '/'));
#$orgsign = "{$this->secret}&".strtoupper($action)."&{$path}&{$get_params}&{$this->secret}";
$orgsign = "{$this->secret}&".strtoupper($action)."&{$path}&{$header_params}&{$get_params}&{$post_params}&{$this->secret}";
$sign = strtoupper(md5($orgsign));
#echo "\r\n", $orgsign, "\r\n", $sign, "\r\n";
$get_data['sign'] = $sign;
return $get_data;
}
private function build_query($row, $pre=null)
{
$r = null;
foreach($row as $k => $v) {
if(is_array($v)){
$r .= '&'. $this->build_query($v, ($pre ? "{$pre}[$k]" : $k));
} else {
$r .= '&'. ($pre ? "{$pre}[{$k}]" : $k) . "=" .$v;
}
}
return $r;
}
private function ksort($data)
{
ksort($data);
foreach($data as $key => &$val){
if (is_array($val)) {
$val = $this->ksort($val);
}
}
return $data;
}
}