Sign.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php namespace Api\Lib\Platform;
  2. use Dever;
  3. class Sign
  4. {
  5. private $field;
  6. private $config;
  7. public function init($field, $config)
  8. {
  9. $this->field = $field;
  10. $this->config = $config;
  11. return $this;
  12. }
  13. public function get()
  14. {
  15. $this->load();
  16. $this->encrypt();
  17. $this->after();
  18. $this->field->set($this->config['name'], $this->info);
  19. return $this->info;
  20. }
  21. public function check($arg = '')
  22. {
  23. if ($arg) {
  24. $this->config['arg'] = $arg;
  25. }
  26. $sign = $this->field->{$this->config['name']};
  27. if (!$sign) {
  28. Dever::error('签名验证失败');
  29. }
  30. if ($this->config['encrypt'] > 0) {
  31. $this->load();
  32. $check = $this->field->ssl->decrypt($this->config['encrypt'], $sign, $this->info);
  33. } else {
  34. $check = $sign == $this->get();
  35. }
  36. if (!$check) {
  37. Dever::error('签名验证失败');
  38. }
  39. }
  40. protected function load()
  41. {
  42. $this->create();
  43. if ($this->config['kv_sort'] == 2) {
  44. ksort($this->info);
  45. } elseif ($this->config['kv_sort'] == 3) {
  46. $this->info = array_values($this->info);
  47. sort($this->info, SORT_STRING);
  48. }
  49. $this->join();
  50. $this->toString();
  51. }
  52. protected function create()
  53. {
  54. $this->info = [];
  55. if ($this->config['arg']) {
  56. $col = explode("\n", $this->config['arg']);
  57. foreach ($col as $k => $v) {
  58. if ($v == 'body') {
  59. $this->info = array_merge($this->field->body, $this->info);
  60. } elseif ($v == 'header') {
  61. $this->info = array_merge($this->field->header, $this->info);
  62. } elseif ($v == 'query') {
  63. $this->info = array_merge($this->field->query, $this->info);
  64. } else {
  65. $k = $v;
  66. if (strstr($v, '=')) {
  67. $t = explode('=', $v);
  68. $v = $t[1];
  69. $k = $t[0];
  70. }
  71. $this->info[$k] = $this->field->value($v);
  72. }
  73. }
  74. } else {
  75. $this->info = $this->field->body;
  76. }
  77. }
  78. protected function join()
  79. {
  80. if (strstr($this->config['kv_join'], '\\')) {
  81. $this->config['kv_join'] = preg_replace_callback(
  82. '/\\\\([nrtf])/', // 匹配 \n, \r, \t, \f 等特殊字符
  83. function ($matches) {
  84. $map = [
  85. 'n' => "\n",
  86. 'r' => "\r",
  87. 't' => "\t",
  88. 'f' => "\f"
  89. ];
  90. return $map[$matches[1]]; // 直接从映射中获取替换值
  91. },
  92. $this->config['kv_join']
  93. );
  94. }
  95. }
  96. protected function toString()
  97. {
  98. $string = '';
  99. foreach ($this->info as $k => $v) {
  100. if ($this->config['kv_value_empty'] == 2 && null === $v) {
  101. continue;
  102. }
  103. if (is_array($v)) {
  104. $v = Dever::json_encode($v);
  105. }
  106. if ($this->config['kv_key_handle']) {
  107. $k = Dever::load(\Api\Lib\Util::class)->format($this->config['kv_key_handle'], $k);
  108. }
  109. if ($this->config['kv_value_handle']) {
  110. $v = Dever::load(\Api\Lib\Util::class)->format($this->config['kv_value_handle'], $v);
  111. }
  112. if ($this->config['kv_type'] == 1) {
  113. $string .= $v;
  114. } elseif ($this->config['kv_type'] == 2) {
  115. $string .= $k;
  116. } elseif ($this->config['kv_type'] == 3) {
  117. $string .= $k . '=' . $v;
  118. } elseif ($this->config['kv_type'] == 4) {
  119. $string .= $k . $v;
  120. } elseif ($this->config['kv_type'] == 5) {
  121. $string .= $k . ':' . $v;
  122. }
  123. $string .= "{$this->config['kv_join']}";
  124. }
  125. if ($this->config['kv_join_handle'] == 1) {
  126. $this->info = rtrim($string, $this->config['kv_join']);
  127. } else {
  128. $this->info = $string;
  129. }
  130. }
  131. protected function encrypt()
  132. {
  133. $log = [];
  134. $log['request_id'] = $this->field->request_id;
  135. $log['name'] = $this->config['name'];
  136. $log['string'] = $this->info;
  137. if ($this->config['encrypt'] == -2) {
  138. $this->info = md5($this->info);
  139. } elseif ($this->config['encrypt'] == -3) {
  140. $this->info = hash("sha256", $this->info);
  141. } elseif ($this->config['encrypt'] == -4) {
  142. $this->info = sha1($this->info);
  143. } else {
  144. $this->info = $this->field->ssl->encrypt($this->config['encrypt'], $this->info);
  145. }
  146. $log['encode'] = $this->info;
  147. if ($this->field->log) {
  148. Dever::log($log, 'api_sign');
  149. Dever::debug($log, 'api_sign');
  150. }
  151. }
  152. protected function after()
  153. {
  154. if ($this->config['after']) {
  155. $this->info = Dever::load(\Api\Lib\Util::class)->format($this->config['after'], $this->info);
  156. }
  157. }
  158. }