Sign.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. }
  52. $this->join();
  53. $this->toString();
  54. }
  55. protected function create()
  56. {
  57. $this->info = array();
  58. if ($this->config['arg']) {
  59. $col = explode("\n", $this->config['arg']);
  60. foreach ($col as $k => $v) {
  61. if ($v == 'body') {
  62. $this->info = array_merge($this->field->body, $this->info);
  63. } elseif ($v == 'header') {
  64. $this->info = array_merge($this->field->header, $this->info);
  65. } elseif ($v == 'query') {
  66. $this->info = array_merge($this->field->query, $this->info);
  67. } else {
  68. $k = $v;
  69. if (strstr($v, '=')) {
  70. $t = explode('=', $v);
  71. $v = $t[1];
  72. $k = $t[0];
  73. }
  74. $this->info[$k] = $this->field->value($v);
  75. }
  76. }
  77. } else {
  78. $this->info = $this->field->body;
  79. }
  80. }
  81. protected function join()
  82. {
  83. if (strstr($this->config['kv_join'], '\\')) {
  84. $this->config['kv_join'] = preg_replace_callback(
  85. '/\\\\([nrtf])/', // 匹配 \n, \r, \t, \f 等特殊字符
  86. function ($matches) {
  87. $map = [
  88. 'n' => "\n",
  89. 'r' => "\r",
  90. 't' => "\t",
  91. 'f' => "\f"
  92. ];
  93. return $map[$matches[1]]; // 直接从映射中获取替换值
  94. },
  95. $this->config['kv_join']
  96. );
  97. }
  98. }
  99. protected function toString()
  100. {
  101. $string = '';
  102. foreach ($this->info as $k => $v) {
  103. if ($this->config['kv_value_empty'] == 2 && null === $v) {
  104. continue;
  105. }
  106. if (is_array($v)) {
  107. $v = Dever::json_encode($v);
  108. }
  109. if ($this->config['kv_key_handle']) {
  110. $k = Dever::load('util', 'api')->format($this->config['kv_key_handle'], $k);
  111. }
  112. if ($this->config['kv_value_handle']) {
  113. $v = Dever::load('util', 'api')->format($this->config['kv_value_handle'], $v);
  114. }
  115. if ($this->config['kv_type'] == 1) {
  116. $string .= $v;
  117. } elseif ($this->config['kv_type'] == 2) {
  118. $string .= $k;
  119. } elseif ($this->config['kv_type'] == 3) {
  120. $string .= $k . '=' . $v;
  121. } elseif ($this->config['kv_type'] == 4) {
  122. $string .= $k . $v;
  123. } elseif ($this->config['kv_type'] == 5) {
  124. $string .= $k . ':' . $v;
  125. }
  126. $string .= "{$this->config['kv_join']}";
  127. }
  128. if ($this->config['kv_join_handle'] == 1) {
  129. $this->info = rtrim($string, $this->config['kv_join']);
  130. } else {
  131. $this->info = $string;
  132. }
  133. }
  134. protected function encrypt()
  135. {
  136. $log = array();
  137. $log['request_id'] = $this->field->request_id;
  138. $log['name'] = $this->config['name'];
  139. $log['string'] = $this->info;
  140. if ($this->config['encrypt'] == -2) {
  141. $this->info = md5($this->info);
  142. } elseif ($this->config['encrypt'] == -3) {
  143. $this->info = hash("sha256", $this->info);
  144. } elseif ($this->config['encrypt'] == -4) {
  145. $this->info = sha1($this->info);
  146. } else {
  147. $this->info = $this->field->ssl->encrypt($this->config['encrypt'], $this->info);
  148. }
  149. $log['encode'] = $this->info;
  150. if ($this->field->log) {
  151. Dever::log($log, 'api_sign');
  152. Dever::debug($log, 'api_sign');
  153. }
  154. }
  155. protected function after()
  156. {
  157. if ($this->config['after']) {
  158. $this->info = Dever::load('util', 'api')->format($this->config['after'], $this->info);
  159. }
  160. }
  161. }