Sign.php 5.4 KB

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