|
@@ -6,21 +6,31 @@ class Base
|
|
|
protected $info;
|
|
|
protected $domain;
|
|
|
protected $param = array();
|
|
|
- protected $signname = 'signature';
|
|
|
|
|
|
|
|
|
- protected function setting($type, $channel, $param = array())
|
|
|
+ protected function setting($type, $cate_id, $channel, $param = array())
|
|
|
{
|
|
|
$this->connect = Dever::db('info', 'connect')->find($channel['connect_id']);
|
|
|
if (!$this->connect) {
|
|
|
return false;
|
|
|
}
|
|
|
+ $this->connect['channel_id'] = $channel['id'];
|
|
|
$this->connect['host'] = $channel['host'];
|
|
|
$this->connect['appkey'] = $channel['appkey'];
|
|
|
$this->connect['appsecret'] = $channel['appsecret'];
|
|
|
- $this->info = Dever::db('api', 'connect')->find(array('type' => $type, 'connect_id' => $this->connect['id']));
|
|
|
+ $set = Dever::db('set', 'channel')->select(array('channel_id' => $channel['id']));
|
|
|
+ $this->connect['set'] = array();
|
|
|
+ if ($set) {
|
|
|
+ foreach ($set as $k => $v) {
|
|
|
+ $this->connect['set'][$v['key']] = $v['value'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $this->info = Dever::db($this->type, 'connect')->find(array('type' => $type, 'cate_id' => $cate_id, 'connect_id' => $this->connect['id']));
|
|
|
if (!$this->info) {
|
|
|
- return false;
|
|
|
+ $this->info = Dever::db($this->type, 'connect')->find(array('type' => $type, 'connect_id' => $this->connect['id']));
|
|
|
+ if (!$this->info) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
$this->param = $param;
|
|
|
return $this;
|
|
@@ -60,21 +70,23 @@ class Base
|
|
|
$this->requestBody($body);
|
|
|
}
|
|
|
$this->sign($body);
|
|
|
-
|
|
|
- $log['type'] = 'request';
|
|
|
- $log['url'] = $url;
|
|
|
- $log['body'] = $body;
|
|
|
- $log['method'] = $method;
|
|
|
- $log['json'] = $json;
|
|
|
- $log['header'] = $header;
|
|
|
- Dever::log($log, 'api');
|
|
|
-
|
|
|
- print_r($body);die;
|
|
|
-
|
|
|
- $response = Dever::curl($url, $body, $method, $json, $header);
|
|
|
+ $response = Dever::curl($url, $body, $method, $json, $header)->result();
|
|
|
+ $test = Dever::input('test');
|
|
|
+ if ($test && $test == 1) {
|
|
|
+ $result = array
|
|
|
+ (
|
|
|
+ 'url' => $url,
|
|
|
+ 'header' => $header,
|
|
|
+ 'method' => $method,
|
|
|
+ 'json' => $json,
|
|
|
+ 'body' => $body,
|
|
|
+ 'response' => $response,
|
|
|
+ );
|
|
|
+ echo Dever::json_encode($result);die;
|
|
|
+ }
|
|
|
+
|
|
|
$response = $this->response($response);
|
|
|
- $response['request'] = $log;
|
|
|
-
|
|
|
+ $response['request'] = $body;
|
|
|
return $response;
|
|
|
}
|
|
|
|
|
@@ -84,6 +96,34 @@ class Base
|
|
|
if (strstr($this->info['uri'], 'http')) {
|
|
|
$this->connect['host'] = '';
|
|
|
}
|
|
|
+ $uri = Dever::db('api_uri', 'connect')->select(array('api_id' => $this->info['id']));
|
|
|
+ if ($uri) {
|
|
|
+ $path = array();
|
|
|
+ $param = array();
|
|
|
+ foreach ($uri as $k => $v) {
|
|
|
+ $v['value'] = $this->value(array(), $v['key'], $v['value']);
|
|
|
+ if ($v['type'] == 1) {
|
|
|
+ $path[] = $v['value'];
|
|
|
+ } elseif ($v['type'] == 2) {
|
|
|
+ $path[] = $v['key'] . '/' . $v['value'];
|
|
|
+ } elseif ($v['type'] == 3) {
|
|
|
+ $path[] = $v['key'] . '=' . $v['value'];
|
|
|
+ } elseif ($v['type'] == 4) {
|
|
|
+ $param[] = $v['value'];
|
|
|
+ } elseif ($v['type'] == 5) {
|
|
|
+ $param[] = $v['key'] . '=' . $v['value'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if ($path) {
|
|
|
+ $this->info['uri'] .= implode('/', $path);
|
|
|
+ }
|
|
|
+ if ($param) {
|
|
|
+ if (!strstr($this->info['uri'], '?')) {
|
|
|
+ $this->info['uri'] .= '?';
|
|
|
+ }
|
|
|
+ $this->info['uri'] .= implode('&', $param);
|
|
|
+ }
|
|
|
+ }
|
|
|
return $this->domain = $this->connect['host'] . $this->info['uri'];
|
|
|
}
|
|
|
|
|
@@ -94,7 +134,7 @@ class Base
|
|
|
if ($body) {
|
|
|
foreach ($body as $k => $v) {
|
|
|
$value = $this->value($data, $v['key'], $v['default'], $v['type']);
|
|
|
- if ($value || Dever::zero($value)) {
|
|
|
+ if ($value) {
|
|
|
$data[$v['key']] = $value;
|
|
|
}
|
|
|
}
|
|
@@ -104,36 +144,31 @@ class Base
|
|
|
|
|
|
protected function response($response)
|
|
|
{
|
|
|
- $log['type'] = 'response';
|
|
|
- $log['data'] = $response;
|
|
|
- Dever::log($log, 'api');
|
|
|
-
|
|
|
if ($this->connect['response_type'] == 1) {
|
|
|
$response = $this->filter($response);
|
|
|
- return array
|
|
|
- (
|
|
|
- 'status' => 1,
|
|
|
- 'msg' => 'ok',
|
|
|
- 'response' => false,
|
|
|
- 'request' => array(),
|
|
|
- 'data' => $response,
|
|
|
- );
|
|
|
+ return array
|
|
|
+ (
|
|
|
+ 'status' => 1,
|
|
|
+ 'msg' => 'ok',
|
|
|
+ 'response' => false,
|
|
|
+ 'request' => array(),
|
|
|
+ 'data' => $response,
|
|
|
+ );
|
|
|
}
|
|
|
if ($this->connect['response_type'] == 2) {
|
|
|
$response = Dever::json_decode($response);
|
|
|
} elseif ($this->connect['response_type'] == 3) {
|
|
|
$response = (array) simplexml_load_string($response);
|
|
|
} else {
|
|
|
- if (strstr($response, ',')) {
|
|
|
- $response = explode(',', $response);
|
|
|
- } elseif (strstr($response, ' ')) {
|
|
|
- $response = explode(' ', $response);
|
|
|
- } elseif (strstr($response, '|')) {
|
|
|
- $response = explode('|', $response);
|
|
|
- } else {
|
|
|
- $response = explode("\n", $response);
|
|
|
- }
|
|
|
-
|
|
|
+ if (strstr($response, ',')) {
|
|
|
+ $response = explode(',', $response);
|
|
|
+ } elseif (strstr($response, ' ')) {
|
|
|
+ $response = explode(' ', $response);
|
|
|
+ } elseif (strstr($response, '|')) {
|
|
|
+ $response = explode('|', $response);
|
|
|
+ } else {
|
|
|
+ $response = explode("\n", $response);
|
|
|
+ }
|
|
|
}
|
|
|
$msg = '';
|
|
|
$status = 2;
|
|
@@ -143,7 +178,7 @@ class Base
|
|
|
$status = 1;
|
|
|
}
|
|
|
}
|
|
|
- $msg = $response[$this->connect['response_msg']] ?? : 'no';
|
|
|
+ $msg = $response[$this->connect['response_msg']] ?? 'no';
|
|
|
$data = '';
|
|
|
if (strstr($this->connect['response_data'], ',')) {
|
|
|
$temp = explode(',', $this->connect['response_data']);
|
|
@@ -176,102 +211,125 @@ class Base
|
|
|
|
|
|
protected function sign(&$body)
|
|
|
{
|
|
|
- $config = Dever::db('sign', 'connect')->find(array('connect_id' => $this->connect['id']));
|
|
|
- if (!$config || ($config && $config['method'] == 1)) {
|
|
|
+ if ($this->connect['sign_method'] == 1) {
|
|
|
return;
|
|
|
}
|
|
|
+ if (!$this->connect['sign_name']) {
|
|
|
+ $this->connect['sign_name'] = 'signature';
|
|
|
+ }
|
|
|
$sign = array();
|
|
|
- if ($config['col']) {
|
|
|
- $col = explode('+', $config['col']);
|
|
|
+ if ($this->info['sign_col']) {
|
|
|
+ $col = explode('+', $this->info['sign_col']);
|
|
|
foreach ($col as $k => $v) {
|
|
|
$sign[$v] = $this->value($body, $v, $v, 1);
|
|
|
}
|
|
|
- }
|
|
|
- if ($this->info && isset($this->info['sign_col']) && $this->info['sign_col']) {
|
|
|
- $col = explode('+', $this->info['sign_col']);
|
|
|
+ } elseif ($this->connect['sign_col']) {
|
|
|
+ $col = explode('+', $this->connect['sign_col']);
|
|
|
foreach ($col as $k => $v) {
|
|
|
$sign[$v] = $this->value($body, $v, $v, 1);
|
|
|
}
|
|
|
+ } else {
|
|
|
+ $sign = $body;
|
|
|
}
|
|
|
|
|
|
- if ($config['sort'] == 2) {
|
|
|
+ if ($this->connect['sign_appsecret'] && $this->connect['sign_appsecret_location'] == 1) {
|
|
|
+ $sign[$this->connect['sign_appsecret']] = $this->connect['appsecret'];
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($this->connect['sign_sort'] == 2) {
|
|
|
ksort($sign);
|
|
|
}
|
|
|
|
|
|
$string = '';
|
|
|
foreach ($sign as $k => $v) {
|
|
|
- if ($config['empty'] == 2 && !Dever::zero($v) && !$v) {
|
|
|
+ if ($this->connect['sign_empty'] == 2 && !$v) {
|
|
|
continue;
|
|
|
}
|
|
|
- if ($config['encode'] == 2 && strstr($v, 'http')) {
|
|
|
+ if ($this->connect['sign_encode'] == 2 && strstr($v, 'http')) {
|
|
|
$v = urlencode($v);
|
|
|
}
|
|
|
- if ($config['type'] == 1) {
|
|
|
+ if ($this->connect['sign_type'] == 1) {
|
|
|
$string .= $v;
|
|
|
- } else {
|
|
|
+ } elseif ($this->connect['sign_type'] == 2) {
|
|
|
$string .= $k . '=' . $v . '&';
|
|
|
+ } elseif ($this->connect['sign_type'] == 3) {
|
|
|
+ $string .= $k . $v;
|
|
|
+ } elseif ($this->connect['sign_type'] == 4) {
|
|
|
+ $string .= $k . $v . '&';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
$sign = rtrim($string, '&');
|
|
|
|
|
|
+ if ($this->connect['sign_appsecret'] && $this->connect['sign_appsecret_location'] == 2) {
|
|
|
+ if ($this->connect['sign_type'] == 1) {
|
|
|
+ $sign .= $this->connect['appsecret'];
|
|
|
+ } elseif ($this->connect['sign_type'] == 2) {
|
|
|
+ $sign .= '&' . $this->connect['sign_appsecret'] . '=' . $this->connect['appsecret'];
|
|
|
+ } elseif ($this->connect['sign_type'] == 3) {
|
|
|
+ $sign .= $this->connect['sign_appsecret'] . $this->connect['appsecret'];
|
|
|
+ } elseif ($this->connect['sign_type'] == 4) {
|
|
|
+ $sign .= '&' . $this->connect['sign_appsecret'] . $this->connect['appsecret'];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
|
|
|
- if ($config['method'] == 2) {
|
|
|
+ if ($this->connect['sign_method'] == 2) {
|
|
|
$sign = md5($sign);
|
|
|
- } elseif ($config['method'] == 3) {
|
|
|
+ } elseif ($this->connect['sign_method'] == 3) {
|
|
|
$sign = hash("sha256", $sign);
|
|
|
- } elseif ($config['method'] == 4) {
|
|
|
+ } elseif ($this->connect['sign_method'] == 4) {
|
|
|
$sign = sha1($sign);
|
|
|
}
|
|
|
|
|
|
- if ($config['after'] == 2) {
|
|
|
+ if ($this->connect['sign_after'] == 2) {
|
|
|
$sign = strtoupper($sign);
|
|
|
- } elseif ($config['after'] == 2) {
|
|
|
+ } elseif ($this->connect['sign_after'] == 2) {
|
|
|
$sign = strtolower($sign);
|
|
|
}
|
|
|
|
|
|
- $body[$this->signname] = $sign;
|
|
|
+ $body[$this->connect['sign_name']] = $sign;
|
|
|
+ return $sign;
|
|
|
}
|
|
|
|
|
|
- protected function value(&$data, $key, $value, $type = 1)
|
|
|
+ protected function value($data, $key, $value, $type = 1)
|
|
|
{
|
|
|
- $key = trim($key);
|
|
|
$value = trim($value);
|
|
|
|
|
|
if ($this->param && isset($this->param[$value])) {
|
|
|
$value = $this->param[$value];
|
|
|
} elseif ($data && isset($data[$value])) {
|
|
|
$value = $data[$value];
|
|
|
+ } elseif (isset($this->connect['set'][$value]) && $this->connect['set'][$value]) {
|
|
|
+ $value = $this->connect['set'][$value];
|
|
|
} elseif (isset($this->connect[$value]) && $this->connect[$value]) {
|
|
|
$value = $this->connect[$value];
|
|
|
- } elseif (isset($this->channel[$value]) && $this->channel[$value]) {
|
|
|
- $value = $this->channel[$value];
|
|
|
- } elseif ($value == 'signature') {
|
|
|
- $this->signname = $key;
|
|
|
} elseif ($value == 'notify') {
|
|
|
- $value = $this->createNotify();
|
|
|
+ $value = $this->createNotify($this->param['order_id'] ?? '');
|
|
|
} elseif ($value == 'api_request') {
|
|
|
$value = $data;
|
|
|
} elseif ($value == 'timestamp') {
|
|
|
- $value = Dever::timestamp();
|
|
|
+ $value = \Dever\Helper\Secure::timestamp();
|
|
|
} elseif ($value == 'nonce') {
|
|
|
- $value = Dever::nonce();
|
|
|
+ $value = \Dever\Helper\Secure::nonce();
|
|
|
} elseif ($value == 'token') {
|
|
|
$value = $this->token(1, 'token');
|
|
|
} elseif ($value == 'ticket') {
|
|
|
$value = $this->token(2, 'ticket');
|
|
|
- } elseif (strstr($value, 'Dever::')) {
|
|
|
+ } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
|
|
|
+ if ($a) {
|
|
|
+ $func = function ($r) use($data) {
|
|
|
+ return $this->value($data, $r[1], $r[1]);
|
|
|
+ };
|
|
|
+ $value = preg_replace_callback('/{(.*?)}/', $func, $value);
|
|
|
+ }
|
|
|
$value = '$value = '.$value.';';
|
|
|
eval($value);
|
|
|
- } else {
|
|
|
- $set = Dever::db('channel/info_set')->find(array('info_id' => $this->channel['id'], 'key' => $value));
|
|
|
- if ($set) {
|
|
|
- $value = $set['value'];
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
- if (!Dever::zero($value) && !$value) {
|
|
|
+ if (!$value) {
|
|
|
+ $key = trim($key);
|
|
|
return Dever::input($key);
|
|
|
}
|
|
|
|
|
@@ -294,7 +352,7 @@ class Base
|
|
|
|
|
|
protected function service($data)
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
$convert = Dever::db('convert', 'connect')->select(array('connect_id' => $this->connect['id']));
|
|
|
|
|
|
$param = array();
|
|
@@ -318,26 +376,46 @@ class Base
|
|
|
$param = $data;
|
|
|
}
|
|
|
|
|
|
- Dever::load('channel/lib/service')->act($this->channel['id'], $this->info['service_id'], $this->type, $this->info['id'], $param);
|
|
|
+
|
|
|
+
|
|
|
return $param;
|
|
|
}
|
|
|
|
|
|
protected function token($id = 1, $type = 'token')
|
|
|
{
|
|
|
- $api = Dever::db('api', 'connect')->find(array('connect_id' => $this->connect['id'], 'service_id' => $id));
|
|
|
- if ($api) {
|
|
|
- return Dever::load('channel/lib/api')->token($this->channel, $this->connect, $api, $type);
|
|
|
- } else {
|
|
|
- return 'error';
|
|
|
+ return '';
|
|
|
+ $db = Dever::db($type, 'connect');
|
|
|
+ $data['connect_id'] = $connect['id'];
|
|
|
+ $data['api_id'] = $api['id'];
|
|
|
+ $info = $db->find($data);
|
|
|
+ if ($info && time() - $info['mdate'] >= $info['expires']) {
|
|
|
+
|
|
|
+ } elseif ($info) {
|
|
|
+ return $info['value'];
|
|
|
+ }
|
|
|
+ $result = $this->init()->curl();
|
|
|
+ if (isset($result[$type])) {
|
|
|
+ $data['value'] = $result[$type];
|
|
|
+ $data['expires'] = '7200';
|
|
|
+ if (isset($result['expires'])) {
|
|
|
+ $data['expires'] = $result['expires'];
|
|
|
+ }
|
|
|
+ if ($info) {
|
|
|
+ $data['where_id'] = $info['id'];
|
|
|
+ $db->update($data);
|
|
|
+ } else {
|
|
|
+ $db->insert($data);
|
|
|
+ }
|
|
|
+ return $result[$type];
|
|
|
}
|
|
|
+ return '';
|
|
|
}
|
|
|
|
|
|
protected function encrypt($value)
|
|
|
{
|
|
|
- $config = Dever::db('ssl', 'connect')->find(array('connect_id' => $this->connect['id']));
|
|
|
- if ($config && $config['method'] && $config['key']) {
|
|
|
- $value = openssl_encrypt($value, $config['method'], $config['key'], $config['option'], $config['iv']);
|
|
|
- if ($config['after'] == 2) {
|
|
|
+ if ($this->connect['ssl_method'] && $this->connect['ssl_key']) {
|
|
|
+ $value = openssl_encrypt($value, $this->connect['ssl_method'], $this->connect['ssl_key'], $this->connect['ssl_option'], $this->connect['ssl_iv']);
|
|
|
+ if ($this->connect['ssl_after'] == 2) {
|
|
|
$value = base64_encode($value);
|
|
|
}
|
|
|
}
|
|
@@ -347,12 +425,12 @@ class Base
|
|
|
|
|
|
protected function decrypt($value)
|
|
|
{
|
|
|
- if ($config['method'] && $config['key']) {
|
|
|
+ if ($this->connect['ssl_method'] && $this->connect['ssl_key']) {
|
|
|
if (is_array($value)) {
|
|
|
$value = Dever::json_encode($value);
|
|
|
}
|
|
|
- $value = openssl_decrypt($value, $config['method'], $config['key'], $config['option'], $config['iv']);
|
|
|
- if ($config['after'] == 2) {
|
|
|
+ $value = openssl_decrypt($value, $this->connect['ssl_method'], $this->connect['ssl_key'], $this->connect['ssl_option'], $this->connect['ssl_iv']);
|
|
|
+ if ($this->connect['ssl_after'] == 2) {
|
|
|
$value = base64_decode($value);
|
|
|
}
|
|
|
}
|