Sign.php 5.3 KB

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