merchant.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. class merchantControl extends vbaseControl
  3. {
  4. private $mMchid;
  5. private $mAdminid;
  6. private $mUseKey;
  7. public function __construct()
  8. {
  9. parent::__construct();
  10. $mchid = $_POST['mchid'];
  11. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  12. if(empty($mchinfo)) {
  13. throw new Exception("合作方ID:{$mchid}不存在");
  14. }
  15. else {
  16. $this->mAdminid = intval($mchinfo['admin_id']);
  17. }
  18. if ($mchinfo['merchant_state'] != 1) {
  19. throw new Exception("机构已被关闭。");
  20. }
  21. $ips = unserialize($mchinfo['ip_white_list']);
  22. if(!empty($ips)) {
  23. $addr = $_SERVER['REMOTE_ADDR'];
  24. Log::record("request ip:{$addr}",Log::DEBUG);
  25. if(!in_array($addr,$ips)) {
  26. throw new Exception("请求地址不在白名单中");
  27. }
  28. }
  29. $this->mUseKey = intval($mchinfo['use_key']);
  30. if($this->mUseKey && !$this->verify_md5($mchinfo['secure_key'])) {
  31. throw new UnSignException();
  32. }
  33. $this->mMchid = intval($mchid);
  34. }
  35. public function mchid() : int
  36. {
  37. return $this->mMchid;
  38. }
  39. public function adminid() : int {
  40. return $this->mAdminid;
  41. }
  42. private function verify_md5($key)
  43. {
  44. $input = $_GET;
  45. $sign = $input['sign'];
  46. $input['sign'] = null;
  47. $input['from'] = null;
  48. $body = $this->sign_body($input);
  49. if($this->mUseKey) {
  50. $body .= "&key={$key}";
  51. }
  52. return ($sign == md5($body));
  53. }
  54. protected function check_empty($value)
  55. {
  56. if (!isset($value))
  57. return true;
  58. if ($value === null)
  59. return true;
  60. if (trim($value) === "")
  61. return true;
  62. return false;
  63. }
  64. private function sign_body($params)
  65. {
  66. ksort($params);
  67. $content = "";
  68. $i = 0;
  69. foreach ($params as $k => $v)
  70. {
  71. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  72. {
  73. if ($i == 0) {
  74. $content .= "{$k}" . "=" . urlencode($v);
  75. } else {
  76. $content .= "&" . "{$k}" . "=" . urlencode($v);
  77. }
  78. $i++;
  79. }
  80. }
  81. return $content;
  82. }
  83. }