merchant.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. class merchantControl
  3. {
  4. private $mMchid;
  5. private $mAdminid;
  6. private $mUseKey;
  7. public function __construct()
  8. {
  9. $mchid = $_POST['mchid'];
  10. $mchinfo = Model('merchant')->getMerchantInfo(['mchid' => $mchid]);
  11. if(empty($mchinfo)) {
  12. throw new Exception("合作方ID:{$mchid}不存在");
  13. }
  14. else {
  15. $this->mAdminid = intval($mchinfo['admin_id']);
  16. }
  17. if ($mchinfo['merchant_state'] != 1) {
  18. throw new Exception("机构已被关闭。");
  19. }
  20. // $ips = unserialize($mchinfo['ip_white_list']);
  21. //
  22. // if(!empty($ips)) {
  23. // $addr = $_SERVER['REMOTE_ADDR'];
  24. // Log::record("request ip:{$addr}",Log::DEBUG);
  25. //
  26. // if(!in_array($addr,$ips)) {
  27. // throw new Exception("请求地址不在白名单中");
  28. // }
  29. // }
  30. $this->mUseKey = intval($mchinfo['use_key']);
  31. if($this->mUseKey && !$this->verify_md5($mchinfo['secure_key'])) {
  32. throw new UnSignException();
  33. }
  34. $this->mMchid = intval($mchid);
  35. }
  36. public function mchid() : int
  37. {
  38. return $this->mMchid;
  39. }
  40. public function adminid() : int {
  41. return $this->mAdminid;
  42. }
  43. private function pubKey($mchid)
  44. {
  45. static $pubs = [];
  46. if(array_key_exists($mchid,$pubs)) {
  47. return $pubs[$mchid];
  48. }
  49. else {
  50. $pub_key = BASE_DATA_PATH . "/api/merchant/key/{$mchid}_pub.pem";
  51. $key = file_get_contents($pub_key);
  52. $pub = openssl_get_publickey($key);
  53. $pubs[$mchid] = $pub;
  54. return $pub;
  55. }
  56. }
  57. private function verify_md5($key)
  58. {
  59. $input = $_GET;
  60. $sign = $input['sign'];
  61. $input['sign'] = null;
  62. $input['from'] = null;
  63. $body = $this->sign_body($input);
  64. if($this->mUseKey) {
  65. $body .= "&key={$key}";
  66. }
  67. return ($sign == md5($body));
  68. }
  69. private function verify_rsa($mchid)
  70. {
  71. $pub = $this->pubKey($mchid);
  72. if(empty($pub)) {
  73. return false;
  74. }
  75. $input = $_GET;
  76. $sign = $input['sign'];
  77. $input['sign'] = null;
  78. $input['from'] = null;
  79. $data = $this->sign_body($input);
  80. $res = openssl_verify($data,base64_decode($sign),$pub);
  81. Log::record("openssl_verify res={$res}",Log::DEBUG);
  82. return ($res == 1);
  83. }
  84. protected function check_empty($value)
  85. {
  86. if (!isset($value))
  87. return true;
  88. if ($value === null)
  89. return true;
  90. if (trim($value) === "")
  91. return true;
  92. return false;
  93. }
  94. private function sign_body($params)
  95. {
  96. ksort($params);
  97. $content = "";
  98. $i = 0;
  99. foreach ($params as $k => $v)
  100. {
  101. if (false === $this->check_empty($v) && "@" != substr($v, 0, 1))
  102. {
  103. if ($i == 0) {
  104. $content .= "{$k}" . "=" . urlencode($v);
  105. } else {
  106. $content .= "&" . "{$k}" . "=" . urlencode($v);
  107. }
  108. $i++;
  109. }
  110. }
  111. return $content;
  112. }
  113. public static function outsuccess($data)
  114. {
  115. joutput_data($data);
  116. return true;
  117. }
  118. public static function outerr($code, $msg = '')
  119. {
  120. joutput_error($code, $msg);
  121. return true;
  122. }
  123. }