Sign.php 5.6 KB

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