123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php namespace Api\Api;
- use Dever;
- use Api\Lib\Api;
- use Api\Lib\Platform\Sign;
- use Api\Lib\Platform\Value;
- class Notify extends Api
- {
- private $notify;
- public function common()
- {
- $input = Dever::input();
- $file = file_get_contents("php://input");
- if ($file) {
- $file = Dever::json_decode($file);
- if ($file) {
- $input = array_merge($file, $input);
- }
- }
- Dever::log($input, 'api_notify');
- if (!isset($input['s'])) {
- $this->error('error');
- }
- $s = \Dever\Helper\Str::decode($input['s']);
- if ($s) {
- $data = explode('|', $s);
- $api_id = $data[0];
- if (!$api_id) {
- $this->error('error');
- }
- if (isset($data[1])) {
- $method = $data[1];
- unset($data[0]);
- unset($data[1]);
- $param = array_values($data);
- }
- } else {
- $this->error('error');
- }
- unset($input['s']);
- unset($input['l']);
- $state = $this->setting($api_id);
- if (!$state) {
- $this->error('error');
- }
- if (!$input) {
- $this->error('error');
- }
- if ($this->info['notify'] == 2) {
- $this->error('error');
- }
- $this->notify = Dever::db('api/api_notify')->find(['api_id' => $api_id]);
- if (!$this->notify) {
- $this->error('error');
- }
- $body = $this->body($input);
- $this->header();
- $this->verify();
- # 判断是否成功
- $status = $this->status($body);
- if ($status < 3 && isset($method)) {
- $param[] = $status;
- $param[] = $body;
- $msg = Dever::call($method, $param);
- if ($msg) {
- $this->error($msg);
- }
- }
- # 返回给上游信息
- if ($status == 1) {
- echo $this->notify['success'];die;
- } elseif ($status == 2) {
- $this->error('error');
- }
- }
- protected function body($body)
- {
- $config = Dever::db('api/api_notify_body')->select(['api_id' => $this->info['id']]);
- $result = Value::load($this->field)->get($config, $body);
- if ($result) {
- foreach ($config as $k => $v) {
- if (isset($body[$v['value']])) {
- $value = $this->field->value($body[$v['value']], $v['type'], false);
- $this->field->set($v['key'], $value);
- }
- }
- }
- return $result;
- }
- protected function header()
- {
- $header = getallheaders();
- $config = Dever::db('api/platform_response_header')->select(['platform_id' => $this->platform['id']]);
- if ($config) {
- foreach ($config as $k => $v) {
- if (isset($header[$v['value']])) {
- $value = $this->field->value($header[$v['value']], $v['type'], false);
- $this->field->set($v['key'], $value);
- }
- }
- }
- $config = Dever::db('api/api_response_header')->select(['api_id' => $this->info['id']]);
- if ($config) {
- foreach ($config as $k => $v) {
- if (isset($header[$v['value']])) {
- $value = $this->field->value($header[$v['value']], $v['type'], false);
- $this->field->set($v['key'], $value);
- }
- }
- }
- }
- protected function verify()
- {
- if (!$this->notify['sign_id']) {
- Dever::error('签名验证失败');
- }
- $info = Dever::db('api/platform_sign')->find($this->notify['sign_id']);
- Sign::load($this->field, $info)->check($this->notify['sign_arg']);
- }
- protected function status($body)
- {
- # 1成功 2失败 3不做任何操作
- $status = 3;
- $config = Dever::db('api/api_notify_code')->select(['api_id' => $this->info['id']]);
- if ($config) {
- foreach ($config as $k => $v) {
- if (isset($body[$v['key']]) && $body[$v['key']] == $v['value']) {
- $status = $v['type'];
- }
- }
- }
- return $status;
- }
- protected function error($msg)
- {
- if ($this->notify && $this->notify['error']) {
- $temp = explode("\n", $this->notify['error']);
- if (!isset($temp[1])) {
- $temp[1] = 500;
- }
- $this->code($temp[1]);
- echo $this->notify['error'];die;
- }
- echo $msg;die;
- }
- protected function code($code)
- {
- if ($code == 500) {
- header("HTTP/1.1 500 Internal Server Error");
- header("Status: 500 Internal Server Error");
- }
- }
- }
|