Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
yandexcert.php
1<?php
3
8
9Loc::loadMessages(__FILE__);
10
12{
13 static public $pkey = null;
14 static public $csr = null;
15 static public $sign = null;
16 static public $cn = '';
17 static public $errors = array();
18
23 static public function generate($shopId, $companyName)
24 {
25 $yandexCsr = self::loadFromOptions($shopId);
26 if ($yandexCsr === '')
27 {
28 self::$cn = "/business/".$companyName;
29
30 $config = array(
31 "digest_alg" => "sha1",
32 "private_key_bits" => 2048,
33 "private_key_type" => OPENSSL_KEYTYPE_RSA,
34 );
35
36 $dnFull = array(
37 "countryName" => "RU",
38 "stateOrProvinceName" => "Russia",
39 "localityName" => "Moscow",
40 "commonName" => self::$cn,
41 );
42
43 $res = openssl_pkey_new($config);
44 $csr_origin = openssl_csr_new($dnFull, $res);
45 if ($csr_origin === false)
46 return;
47 $csr_full = "";
48 openssl_pkey_export($res, self::$pkey);
49 openssl_csr_export($csr_origin, self::$csr);
50
51 openssl_csr_export($csr_origin, $csr_full, false);
52 preg_match('"Signature Algorithm\: (.*)-----BEGIN"ims', $csr_full, $sign);
53 $sign = str_replace("\t", "", $sign);
54 if ($sign)
55 {
56 $sign = $sign[1];
57 $a = explode("\n", $sign);
58 unset($a[0]);
59 $sign = str_replace(" ", "", trim(join("\n", $a)));
60 }
61 self::$sign = $sign;
62
63 $dbRes = YandexSettingsTable::getById($shopId);
64 if ($dbRes->fetch())
65 YandexSettingsTable::update($shopId, array('SIGN' => self::$sign, 'CSR' => self::$csr, 'PKEY' => self::$pkey, 'CERT' => ''));
66 else
67 YandexSettingsTable::add(array('SHOP_ID' => $shopId, 'SIGN' => self::$sign, 'CSR' => self::$csr, 'PKEY' => self::$pkey));
68 }
69 }
70
76 static public function clear($shopId, $all = false)
77 {
78 if ($all)
79 $settings = array('CERT' => '', 'SIGN' => '', 'CSR' => '', 'PKEY' => '');
80 else
81 $settings = array('CERT' => '');
82
83 YandexSettingsTable::update($shopId, $settings);
84 }
85
90 static public function getCn($shopId)
91 {
92 $yandexCsr = self::getValue('CSR', $shopId);
93
94 $subjects = openssl_csr_get_subject($yandexCsr);
95 if (!isset($subjects['CN']) || empty($subjects['CN']))
96 return '';
97
98 return $subjects['CN'];
99 }
100
105 static private function loadFromOptions($shopId)
106 {
107 $dbRes = PersonTypeTable::getList(array('select' => array('ID', 'PT_SITE_ID' => 'PERSON_TYPE_SITE.SITE_ID')));
108 while ($data = $dbRes->fetch())
109 {
110 $csr = Option::get('yandexmoney.ycms', 'KASSA_MWS_CSR', '', $data['PT_SITE_ID']);
111 if ($csr === '')
112 continue;
113
114 $csr = Option::get('yandexmoney.ycms', 'KASSA_MWS_CSR', '', $data['PT_SITE_ID']);
115 $pkey = Option::get('yandexmoney.ycms', 'KASSA_MWS_PKEY', '', $data['PT_SITE_ID']);
116 $sign = Option::get('yandexmoney.ycms', 'KASSA_MWS_SIGN', '', $data['PT_SITE_ID']);
117 $cert = Option::get('yandexmoney.ycms', 'KASSA_MWS_CERT', '', $data['PT_SITE_ID']);
118
119 $dbRes = YandexSettingsTable::getById($shopId);
120 if (!$dbRes->fetch())
121 {
122 YandexSettingsTable::add(array('SHOP_ID' => $shopId, 'CSR' => $csr, 'PKEY' => $pkey, 'SIGN' => $sign, 'CERT' => $cert));
123 return $csr;
124 }
125 }
126
127 return '';
128 }
129
135 static public function isLoaded($shopId)
136 {
137 $cert = self::getValue('CERT', $shopId);
138 return !empty($cert);
139 }
140
145 static public function setCert($file, $shopId)
146 {
147 if (!empty($file['name']))
148 {
149 if (mb_substr($file['name'], -4) != '.cer')
150 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_EXT');
151 elseif ($file['error'] != UPLOAD_ERR_OK)
152 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_LOAD');
153 elseif (filesize($file['tmp_name']) > 2048)
154 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_SIZE');
155 }
156 else
157 {
158 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_LOAD');
159 }
160
161 if (empty(self::$errors))
162 {
163 $cert = file_get_contents($file['tmp_name']);
164 $cert_info = openssl_x509_parse($cert);
165 if (isset($cert_info['subject']['CN']))
166 {
167 if ($cert_info['subject']['CN'] != self::getCn($shopId))
168 {
169 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_CN');
170 }
171 else
172 {
173 $pkey = static::getValue('PKEY', $shopId);
174 if (openssl_x509_check_private_key($cert, $pkey))
175 {
176 YandexSettingsTable::update($shopId, array('CERT' => $cert));
177 }
178 else
179 {
180 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_ACCORDING_PKEY_TO_CERT');
181 }
182 }
183 }
184 else
185 {
186 self::$errors[] = Loc::getMessage('YANDEX_CERT_ERR_NULL');
187 }
188 }
189 }
190
195 static public function getSign($shopId)
196 {
197 return self::getValue('SIGN', $shopId);
198 }
199
204 static public function getCert($shopId)
205 {
206 return self::getValue('CERT', $shopId);
207 }
208
213 static public function getCsr($shopId)
214 {
215 global $APPLICATION;
216 $APPLICATION->RestartBuffer();
217
218 header('Content-Description: File Transfer');
219 header('Content-Type: application/octet-stream');
220 header('Content-Disposition: attachment; filename=csr_for_yamoney.csr');
221 header('Content-Transfer-Encoding: binary');
222 header('Expires: 0');
223 header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
224 header('Pragma: public');
225 echo self::getValue('CSR', $shopId);
226 die();
227 }
228
234 static public function getValue($field, $shopId)
235 {
236 $dbRes = YandexSettingsTable::getList(array('filter' => array('SHOP_ID' => $shopId)));
237 if ($data = $dbRes->fetch())
238 return $data[$field];
239
240 return '';
241 }
242}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())
static update($primary, array $data)
static setCert($file, $shopId)
static generate($shopId, $companyName)
static clear($shopId, $all=false)
static getValue($field, $shopId)