field = $field; $this->config = $config; if (isset($this->config['sort']) && $this->config['sort'] == 3) { ksort($this->field->body); } } public function get() { if ($this->config['method'] == -1) { return false; } $this->create(); if ($this->config['sort'] == 2) { ksort($this->info); } $this->split(); $this->toString(); $this->encrypt(); $this->after(); $this->field->setSign($this->info); return $this->info; } public function check($sign) { if ($this->config['method'] == -1) { return false; } if ($this->config['verify_type'] == 2 && $this->config['method'] > 0) { $this->create(); if ($this->config['sort'] == 2) { ksort($this->info); } $this->split(); $this->toString(); $check = $this->field->ssl->decrypt($this->config['method'], $sign, $this->info); } else { $check = $sign == $this->get(); } if (!$check) { Dever::error('签名验证失败'); } } protected function create() { $this->info = array(); if ($this->config['col']) { $col = explode('+', $this->config['col']); foreach ($col as $k => $v) { if ($v == 'body') { $this->info = array_merge($this->field->body, $this->info); } else { $k = $v; if (strstr($v, '=')) { $t = explode('=', $v); $v = $t[1]; $k = $t[0]; } $this->info[$k] = $this->field->value($v); } } } else { $this->info = $this->field->body; } } protected function split() { if (strstr($this->config['split'], '\\')) { $this->config['split'] = preg_replace_callback( '/\\\\([nrtf])/', // 匹配 \n, \r, \t, \f 等特殊字符 function ($matches) { $map = [ 'n' => "\n", 'r' => "\r", 't' => "\t", 'f' => "\f" ]; return $map[$matches[1]]; // 直接从映射中获取替换值 }, $this->config['split'] ); } } protected function toString() { $string = ''; foreach ($this->info as $k => $v) { if ($this->config['empty'] == 2 && !$v) { continue; } if ($this->config['encode'] == 2 && strstr($v, 'http')) { $v = urlencode($v); if (isset($this->field->body[$k])) { $this->field->body[$k] = $v; } } if ($this->config['type'] == 1) { $string .= $v; } elseif ($this->config['type'] == 2) { $string .= $k . '=' . $v; } elseif ($this->config['type'] == 3) { $string .= $k . $v; } $string .= "{$this->config['split']}"; } if ($this->config['split_type'] == 1) { $this->info = rtrim($string, $this->config['split']); } else { $this->info = $string; } } protected function encrypt() { if ($this->config['method'] == -2) { $this->info = md5($this->info); } elseif ($this->config['method'] == -3) { $this->info = hash("sha256", $this->info); } elseif ($this->config['method'] == -4) { $this->info = sha1($this->info); } else { $this->info = $this->field->ssl->encrypt($this->config['method'], $this->info); } } protected function after() { if ($this->config['after'] == 2) { $this->info = strtoupper($this->info); } elseif ($this->config['after'] == 2) { $this->info = strtolower($this->info); } } }