123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- <?php namespace Api\Lib\Platform;
- use Dever;
- class Field
- {
- private $data = array();
- public $ssl;
- public function __construct($data)
- {
- $this->ssl = new Ssl($this);
- $this->set('time', time());
- $this->set('timestamp', \Dever\Helper\Secure::timestamp());
- $this->set('nonce', \Dever\Helper\Secure::nonce());
- $this->data = $data;
- }
- public function add($index, $value, $key = 'body')
- {
- $this->data[$key][$index] = $value;
- }
- public function set($key, $value)
- {
- $this->data[$key] = $value;
- }
- public function __set($key, $value)
- {
- $this->data[$key] = $value;
- }
- public function __get($key)
- {
- if (array_key_exists($key, $this->data)) {
- return $this->data[$key];
- } else {
- return null;
- }
- }
- public function createRequestId()
- {
- $value = Dever::db('api_log', 'api')->insert([]);
- $this->set('request_id', $value);
- }
- public function setPlatformId($value)
- {
- $this->set('platform_id', $value);
- }
- public function setApid($value)
- {
- $this->set('api_id', $value);
- }
- public function setHost($value)
- {
- $this->set('body', $value);
- }
- public function setUri($value)
- {
- $this->set('uri', $value);
- }
- public function setPath($value)
- {
- $this->set('path', $value);
- }
- public function setQuery($value)
- {
- $this->set('query', $value);
- }
- public function setUrl($value)
- {
- $this->set('url', $value);
- }
- public function setNotify($value)
- {
- $this->set('notify', $value);
- }
- public function setMethod($value)
- {
- $this->set('method', $value);
- }
- public function setBody($value)
- {
- $this->set('body', $value);
- }
- public function setBodyJson($value)
- {
- $this->set('body_json', $value);
- }
- public function setHeader($value)
- {
- $this->set('header', $value);
- }
- public function setHeaderJson($value)
- {
- $this->set('header_json', $value);
- }
- public function setNumber($value)
- {
- $this->set('number', $value);
- }
- public function value($source, $type = '', $state = true)
- {
- $value = trim($source);
- if ($this->data && isset($this->data[$value])) {
- $value = $this->data[$value];
- } elseif (strstr($value, 'key=') && strstr($value, '&')) {
- $value = $this->parse($value);
- } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
- $value = $this->eval($value);
- }
- if (!$type && $source == $value) {
- /*
- $field = Dever::db('platform_field', 'api')->find(array('platform_id' => $this->data['platform_id'], 'key' => $value));
- if ($field) {
- $value = $field['value'];
- $type = $field['type'];
- } else {
- $sign = Dever::db('platform_sign', 'api')->find(array('platform_id' => $this->data['platform_id'], 'name' => $value));
- if ($sign) {
- $value = '';
- $type = '3,' . $sign['id'];
- }
- }*/
- $sign = Dever::db('platform_sign', 'api')->find(array('platform_id' => $this->data['platform_id'], 'name' => $value));
- if ($sign) {
- $value = '';
- $type = '3,' . $sign['id'];
- }
- }
- return $this->handle($type, $value, $state);
- }
- protected function parse($value)
- {
- parse_str($value, $temp);
- $k = $temp['key'];
- unset($temp['key']);
- if (isset($this->data[$k]) && isset($temp[$this->data[$k]])) {
- $value = $temp[$this->data[$k]];
- }
- return $value;
- }
- protected function eval($value)
- {
- $func = function ($r) {
- return $this->value($r[1]);
- };
- $value = preg_replace_callback('/{(.*?)}/', $func, $value);
- $value = '$value = '.$value.';';
- eval($value);
- return $value;
- }
- protected function handle($type, $value, $state)
- {
- if (strpos($type, ',')) {
- list($type, $type_id) = explode(',', $type);
- if ($type == 1) {
- $info = Dever::db('format', 'api')->find($type_id);
- if ($info) {
- $value = \Dever\Helper\Str::val($info['method'], array('value' => $value));
- }
- } elseif ($type == 2) {
- # state == true 是编码 == false 是解码
- if ($state) {
- $value = $this->ssl->encrypt($type_id, $value);
- } else {
- $value = $this->ssl->decrypt($type_id, $value);
- }
- } elseif ($type == 3) {
- $info = Dever::db('platform_sign', 'api')->find($type_id);
- if ($info) {
- if ($value) {
- $info['arg'] = $value;
- }
- $value = Sign::load($this, $info)->get();
- }
- }
- }
- return $value;
- }
- }
|