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