|
@@ -0,0 +1,387 @@
|
|
|
+<?php namespace Connect\Lib\Func;
|
|
|
+use Dever;
|
|
|
+class Base
|
|
|
+{
|
|
|
+ protected $connect;
|
|
|
+ protected $info;
|
|
|
+ protected $domain;
|
|
|
+ protected $param = array();
|
|
|
+ protected $signname = 'signature';
|
|
|
+
|
|
|
+
|
|
|
+ protected function setting($type, $channel, $param = array())
|
|
|
+ {
|
|
|
+ $this->connect = Dever::db('info', 'connect')->find($channel['connect_id']);
|
|
|
+ if (!$this->connect) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $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']));
|
|
|
+ if (!$this->info) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ $this->param = $param;
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected function curl($url = false)
|
|
|
+ {
|
|
|
+ if (!$url) {
|
|
|
+ $url = $this->url();
|
|
|
+ }
|
|
|
+ $header = $body = array();
|
|
|
+ $method = 'get';
|
|
|
+ $json = false;
|
|
|
+ if ($this->info['method'] == -1) {
|
|
|
+ $this->info['method'] = $this->connect['method'];
|
|
|
+ $this->info['post_method'] = $this->connect['post_method'];
|
|
|
+ }
|
|
|
+ if ($this->info['post_method'] == 2) {
|
|
|
+ $method = 'file';
|
|
|
+ } elseif ($this->info['method'] == 2) {
|
|
|
+ $method = 'post';
|
|
|
+ }
|
|
|
+ if ($this->info['post_method'] == 3) {
|
|
|
+ $json = true;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $this->request($header, 'request_header', array('connect_id' => $this->connect['id']));
|
|
|
+
|
|
|
+ $this->request($body, 'request_body', array('connect_id' => $this->connect['id']));
|
|
|
+
|
|
|
+ if (method_exists($this, 'requestHeader')) {
|
|
|
+ $this->requestHeader($header);
|
|
|
+ }
|
|
|
+ if (method_exists($this, 'requestBody')) {
|
|
|
+ $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 = $this->response($response);
|
|
|
+ $response['request'] = $log;
|
|
|
+
|
|
|
+ return $response;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected function url()
|
|
|
+ {
|
|
|
+ if (strstr($this->info['uri'], 'http')) {
|
|
|
+ $this->connect['host'] = '';
|
|
|
+ }
|
|
|
+ return $this->domain = $this->connect['host'] . $this->info['uri'];
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ protected function request(&$data, $table, $where)
|
|
|
+ {
|
|
|
+ $body = Dever::db($table, 'connect')->select($where);
|
|
|
+ if ($body) {
|
|
|
+ foreach ($body as $k => $v) {
|
|
|
+ $value = $this->value($data, $v['key'], $v['default'], $v['type']);
|
|
|
+ if ($value || Dever::zero($value)) {
|
|
|
+ $data[$v['key']] = $value;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ 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,
|
|
|
+ );
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ $msg = '';
|
|
|
+ $status = 2;
|
|
|
+ if ($code = Dever::isset($response, $this->connect['response_code'])) {
|
|
|
+ $code = Dever::db('response_code', 'connect')->find(array('connect_id' => $this->connect['id'], 'value' => $code));
|
|
|
+ if ($code && $code['type'] == 1) {
|
|
|
+ $status = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $msg = $response[$this->connect['response_msg']] ?? : 'no';
|
|
|
+ $data = '';
|
|
|
+ if (strstr($this->connect['response_data'], ',')) {
|
|
|
+ $temp = explode(',', $this->connect['response_data']);
|
|
|
+ foreach ($temp as $k => $v) {
|
|
|
+ if (isset($response[$v])) {
|
|
|
+ $data = $response[$v];
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } elseif (strstr($this->connect['response_data'], '.')) {
|
|
|
+ $temp = explode('.', $this->connect['response_data']);
|
|
|
+ $data = isset($response[$temp[0]][$temp[1]]) ? $response[$temp[0]][$temp[1]] : (isset($response[$temp[0]]) ? $response[$temp[0]] : false);
|
|
|
+ } else {
|
|
|
+ $data = $response[$this->connect['response_data']] ?? false;
|
|
|
+ }
|
|
|
+ if ($data) {
|
|
|
+ if (method_exists($this, 'responseHandle')) {
|
|
|
+ $this->responseHandle($data);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return array
|
|
|
+ (
|
|
|
+ 'status' => $status,
|
|
|
+ 'msg' => $msg,
|
|
|
+ 'response' => $response,
|
|
|
+ 'request' => array(),
|
|
|
+ 'data' => $data,
|
|
|
+ );
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function sign(&$body)
|
|
|
+ {
|
|
|
+ $config = Dever::db('sign', 'connect')->find(array('connect_id' => $this->connect['id']));
|
|
|
+ if (!$config || ($config && $config['method'] == 1)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ $sign = array();
|
|
|
+ if ($config['col']) {
|
|
|
+ $col = explode('+', $config['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']);
|
|
|
+ foreach ($col as $k => $v) {
|
|
|
+ $sign[$v] = $this->value($body, $v, $v, 1);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($config['sort'] == 2) {
|
|
|
+ ksort($sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ $string = '';
|
|
|
+ foreach ($sign as $k => $v) {
|
|
|
+ if ($config['empty'] == 2 && !Dever::zero($v) && !$v) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if ($config['encode'] == 2 && strstr($v, 'http')) {
|
|
|
+ $v = urlencode($v);
|
|
|
+ }
|
|
|
+ if ($config['type'] == 1) {
|
|
|
+ $string .= $v;
|
|
|
+ } else {
|
|
|
+ $string .= $k . '=' . $v . '&';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $sign = rtrim($string, '&');
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if ($config['method'] == 2) {
|
|
|
+ $sign = md5($sign);
|
|
|
+ } elseif ($config['method'] == 3) {
|
|
|
+ $sign = hash("sha256", $sign);
|
|
|
+ } elseif ($config['method'] == 4) {
|
|
|
+ $sign = sha1($sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($config['after'] == 2) {
|
|
|
+ $sign = strtoupper($sign);
|
|
|
+ } elseif ($config['after'] == 2) {
|
|
|
+ $sign = strtolower($sign);
|
|
|
+ }
|
|
|
+
|
|
|
+ $body[$this->signname] = $sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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[$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();
|
|
|
+ } elseif ($value == 'api_request') {
|
|
|
+ $value = $data;
|
|
|
+ } elseif ($value == 'timestamp') {
|
|
|
+ $value = Dever::timestamp();
|
|
|
+ } elseif ($value == 'nonce') {
|
|
|
+ $value = Dever::nonce();
|
|
|
+ } elseif ($value == 'token') {
|
|
|
+ $value = $this->token(1, 'token');
|
|
|
+ } elseif ($value == 'ticket') {
|
|
|
+ $value = $this->token(2, 'ticket');
|
|
|
+ } elseif (strstr($value, 'Dever::')) {
|
|
|
+ $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) {
|
|
|
+ return Dever::input($key);
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($type == 1 || $type == 100) {
|
|
|
+ $value = (string) $value;
|
|
|
+ } elseif ($type == 2 || $type == 200) {
|
|
|
+ $value = (float) $value;
|
|
|
+ } elseif ($type == 3 && is_array($value)) {
|
|
|
+ $value = Dever::json_encode($value);
|
|
|
+ } elseif ($type == 4) {
|
|
|
+ $value = $this->encrypt($value);
|
|
|
+ } elseif ($type == 300 && is_array($value)) {
|
|
|
+ $value = Dever::json_decode($value);
|
|
|
+ } elseif ($type == 400) {
|
|
|
+ $value = $this->decrypt($value);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function service($data)
|
|
|
+ {
|
|
|
+
|
|
|
+ $convert = Dever::db('convert', 'connect')->select(array('connect_id' => $this->connect['id']));
|
|
|
+
|
|
|
+ $param = array();
|
|
|
+ if ($convert) {
|
|
|
+ if (isset($data[0])) {
|
|
|
+ foreach ($data as $k1 => $v1) {
|
|
|
+ foreach ($convert as $k => $v) {
|
|
|
+ if (isset($data[$k1][$v['before']])) {
|
|
|
+ $param[$k1][$v['after']] = $data[$k1][$v['before']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ foreach ($convert as $k => $v) {
|
|
|
+ if (isset($data[$v['before']])) {
|
|
|
+ $param[$v['after']] = $data[$v['before']];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ $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';
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ 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) {
|
|
|
+ $value = base64_encode($value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function decrypt($value)
|
|
|
+ {
|
|
|
+ if ($config['method'] && $config['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 = base64_decode($value);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return $value;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function public_encrypt()
|
|
|
+ {
|
|
|
+ $publicKey = "-----BEGIN PUBLIC KEY-----\n" .
|
|
|
+ chunk_split($this->nowpayPublickey, 64, "\n") .
|
|
|
+ "-----END PUBLIC KEY-----\n";
|
|
|
+ $output = '';
|
|
|
+ openssl_public_encrypt($this->encryptKey, $output, $publicKey);
|
|
|
+ return base64_encode($output);
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function protected_decrypt($str)
|
|
|
+ {
|
|
|
+ $priKey = "-----BEGIN RSA protected KEY-----\n" .
|
|
|
+ chunk_split($this->protectedKey, 64, "\n") .
|
|
|
+ "-----END RSA protected KEY-----\n";
|
|
|
+ $output = '';
|
|
|
+ openssl_protected_decrypt(base64_decode($str), $output, $priKey);
|
|
|
+ return $output;
|
|
|
+ }
|
|
|
+
|
|
|
+ protected function filter($data)
|
|
|
+ {
|
|
|
+ return preg_replace("/<!--[^\!\[]*?(?<!\/\/)-->/","",$data);
|
|
|
+ }
|
|
|
+}
|