Field.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Field
  4. {
  5. private $data = array();
  6. public $ssl;
  7. public function __construct($data)
  8. {
  9. $this->ssl = new Ssl($this);
  10. $this->set('time', time());
  11. $this->set('timestamp', \Dever\Helper\Secure::timestamp());
  12. $this->set('nonce', \Dever\Helper\Secure::nonce());
  13. $this->data = $data;
  14. }
  15. public function add($index, $value, $key = 'body')
  16. {
  17. $this->data[$key][$index] = $value;
  18. }
  19. public function set($key, $value)
  20. {
  21. $this->data[$key] = $value;
  22. }
  23. public function __set($key, $value)
  24. {
  25. $this->data[$key] = $value;
  26. }
  27. public function __get($key)
  28. {
  29. if (array_key_exists($key, $this->data)) {
  30. return $this->data[$key];
  31. } else {
  32. return null;
  33. }
  34. }
  35. public function createRequestId()
  36. {
  37. $value = Dever::db('api_log', 'api')->insert([]);
  38. $this->set('request_id', $value);
  39. }
  40. public function setPlatformId($value)
  41. {
  42. $this->set('platform_id', $value);
  43. }
  44. public function setApid($value)
  45. {
  46. $this->set('api_id', $value);
  47. }
  48. public function setHost($value)
  49. {
  50. $this->set('body', $value);
  51. }
  52. public function setUri($value)
  53. {
  54. $this->set('uri', $value);
  55. }
  56. public function setPath($value)
  57. {
  58. $this->set('path', $value);
  59. }
  60. public function setQuery($value)
  61. {
  62. $this->set('query', $value);
  63. }
  64. public function setUrl($value)
  65. {
  66. $this->set('url', $value);
  67. }
  68. public function setNotify($value)
  69. {
  70. $this->set('notify', $value);
  71. }
  72. public function setMethod($value)
  73. {
  74. $this->set('method', $value);
  75. }
  76. public function setBody($value)
  77. {
  78. $this->set('body', $value);
  79. }
  80. public function setBodyJson($value)
  81. {
  82. $this->set('body_json', $value);
  83. }
  84. public function setHeader($value)
  85. {
  86. $this->set('header', $value);
  87. }
  88. public function setHeaderJson($value)
  89. {
  90. $this->set('header_json', $value);
  91. }
  92. public function setNumber($value)
  93. {
  94. $this->set('number', $value);
  95. }
  96. public function value($source, $type = '', $state = true)
  97. {
  98. $value = trim($source);
  99. if ($this->data && isset($this->data[$value])) {
  100. $value = $this->data[$value];
  101. } elseif (strstr($value, 'key=') && strstr($value, '&')) {
  102. $value = $this->parse($value);
  103. } elseif ($a = strstr($value, '{') || strstr($value, '(')) {
  104. $value = $this->eval($value);
  105. }
  106. if (!$type && $source == $value) {
  107. /*
  108. $field = Dever::db('platform_field', 'api')->find(array('platform_id' => $this->data['platform_id'], 'key' => $value));
  109. if ($field) {
  110. $value = $field['value'];
  111. $type = $field['type'];
  112. } else {
  113. $sign = Dever::db('platform_sign', 'api')->find(array('platform_id' => $this->data['platform_id'], 'name' => $value));
  114. if ($sign) {
  115. $value = '';
  116. $type = '3,' . $sign['id'];
  117. }
  118. }*/
  119. $sign = Dever::db('platform_sign', 'api')->find(array('platform_id' => $this->data['platform_id'], 'name' => $value));
  120. if ($sign) {
  121. $value = '';
  122. $type = '3,' . $sign['id'];
  123. }
  124. }
  125. return $this->handle($type, $value, $state);
  126. }
  127. protected function parse($value)
  128. {
  129. parse_str($value, $temp);
  130. $k = $temp['key'];
  131. unset($temp['key']);
  132. if (isset($this->data[$k]) && isset($temp[$this->data[$k]])) {
  133. $value = $temp[$this->data[$k]];
  134. }
  135. return $value;
  136. }
  137. protected function eval($value)
  138. {
  139. $func = function ($r) {
  140. return $this->value($r[1]);
  141. };
  142. $value = preg_replace_callback('/{(.*?)}/', $func, $value);
  143. $value = '$value = '.$value.';';
  144. eval($value);
  145. return $value;
  146. }
  147. protected function handle($type, $value, $state)
  148. {
  149. if (strpos($type, ',')) {
  150. list($type, $type_id) = explode(',', $type);
  151. if ($type == 1) {
  152. $info = Dever::db('format', 'api')->find($type_id);
  153. if ($info) {
  154. $value = \Dever\Helper\Str::val($info['method'], array('value' => $value));
  155. }
  156. } elseif ($type == 2) {
  157. # state == true 是编码 == false 是解码
  158. if ($state) {
  159. $value = $this->ssl->encrypt($type_id, $value);
  160. } else {
  161. $value = $this->ssl->decrypt($type_id, $value);
  162. }
  163. } elseif ($type == 3) {
  164. $info = Dever::db('platform_sign', 'api')->find($type_id);
  165. if ($info) {
  166. if ($value) {
  167. $info['arg'] = $value;
  168. }
  169. $value = Sign::load($this, $info)->get();
  170. }
  171. }
  172. }
  173. return $value;
  174. }
  175. }