@@ -4,76 +4,76 @@ |
||
4 | 4 | |
5 | 5 | class OpenSSL |
6 | 6 | { |
7 | - public function __construct() {} |
|
7 | + public function __construct() {} |
|
8 | 8 | |
9 | - /** |
|
10 | - * verify signature of xml |
|
11 | - * |
|
12 | - * @param string $xml |
|
13 | - * @param string $bin_sign |
|
14 | - * @param resource $pub_key |
|
15 | - * |
|
16 | - * @return integer result of checking |
|
17 | - */ |
|
18 | - public function verify($xml, $bin_sign, $pub_key) |
|
19 | - { |
|
9 | + /** |
|
10 | + * verify signature of xml |
|
11 | + * |
|
12 | + * @param string $xml |
|
13 | + * @param string $bin_sign |
|
14 | + * @param resource $pub_key |
|
15 | + * |
|
16 | + * @return integer result of checking |
|
17 | + */ |
|
18 | + public function verify($xml, $bin_sign, $pub_key) |
|
19 | + { |
|
20 | 20 | return @openssl_verify($xml, $bin_sign, $pub_key); |
21 | - } |
|
21 | + } |
|
22 | 22 | |
23 | - /** |
|
24 | - * generate signature of xml |
|
25 | - * |
|
26 | - * @param string $xml |
|
27 | - * @param string $sign |
|
28 | - * @param resource $priv_key |
|
29 | - * |
|
30 | - * @return bool result of signing |
|
31 | - */ |
|
32 | - public function sign($xml, &$sign, $priv_key) |
|
33 | - { |
|
23 | + /** |
|
24 | + * generate signature of xml |
|
25 | + * |
|
26 | + * @param string $xml |
|
27 | + * @param string $sign |
|
28 | + * @param resource $priv_key |
|
29 | + * |
|
30 | + * @return bool result of signing |
|
31 | + */ |
|
32 | + public function sign($xml, &$sign, $priv_key) |
|
33 | + { |
|
34 | 34 | return @openssl_sign($xml, $sign, $priv_key); |
35 | - } |
|
35 | + } |
|
36 | 36 | |
37 | - /** |
|
38 | - * get public key |
|
39 | - * |
|
40 | - * @param mixed $certificate |
|
41 | - * @return resource |
|
42 | - */ |
|
43 | - public function get_pub_key($certificate) |
|
44 | - { |
|
37 | + /** |
|
38 | + * get public key |
|
39 | + * |
|
40 | + * @param mixed $certificate |
|
41 | + * @return resource |
|
42 | + */ |
|
43 | + public function get_pub_key($certificate) |
|
44 | + { |
|
45 | 45 | $pub_key = @openssl_pkey_get_public($certificate); |
46 | 46 | $this->is_key($pub_key); |
47 | 47 | |
48 | 48 | return $pub_key; |
49 | - } |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * get private key |
|
53 | - * |
|
54 | - * @param mixed $certificate |
|
55 | - * @return resource |
|
56 | - */ |
|
57 | - public function get_priv_key($certificate) |
|
58 | - { |
|
51 | + /** |
|
52 | + * get private key |
|
53 | + * |
|
54 | + * @param mixed $certificate |
|
55 | + * @return resource |
|
56 | + */ |
|
57 | + public function get_priv_key($certificate) |
|
58 | + { |
|
59 | 59 | $priv_key = @openssl_pkey_get_private($certificate); |
60 | 60 | $this->is_key($priv_key); |
61 | 61 | |
62 | 62 | return $priv_key; |
63 | - } |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * check key |
|
67 | - * |
|
68 | - * @param resource $key |
|
69 | - * @throws Exception\Runtime |
|
70 | - */ |
|
71 | - public function is_key($key) |
|
72 | - { |
|
65 | + /** |
|
66 | + * check key |
|
67 | + * |
|
68 | + * @param resource $key |
|
69 | + * @throws Exception\Runtime |
|
70 | + */ |
|
71 | + public function is_key($key) |
|
72 | + { |
|
73 | 73 | if ($key === FALSE) |
74 | 74 | { |
75 | - throw new Exception\Runtime('Can not extract key from certificate!', -97); |
|
75 | + throw new Exception\Runtime('Can not extract key from certificate!', -97); |
|
76 | + } |
|
76 | 77 | } |
77 | - } |
|
78 | 78 | |
79 | 79 | } |
@@ -17,93 +17,93 @@ discard block |
||
17 | 17 | |
18 | 18 | abstract class Response extends \DomDocument |
19 | 19 | { |
20 | - /** |
|
21 | - * @var string |
|
22 | - * |
|
23 | - */ |
|
24 | - const TEMPLATE = '<Response><StatusCode></StatusCode><StatusDetail></StatusDetail><DateTime></DateTime></Response>'; |
|
25 | - |
|
26 | - /** |
|
27 | - * @var \DOMNode |
|
28 | - */ |
|
29 | - protected $Response; |
|
30 | - |
|
31 | - /** |
|
32 | - * @var \DOMElement |
|
33 | - */ |
|
34 | - protected $Sign; |
|
35 | - |
|
36 | - /** |
|
37 | - * Response constructor |
|
38 | - * |
|
39 | - */ |
|
40 | - public function __construct() |
|
41 | - { |
|
20 | + /** |
|
21 | + * @var string |
|
22 | + * |
|
23 | + */ |
|
24 | + const TEMPLATE = '<Response><StatusCode></StatusCode><StatusDetail></StatusDetail><DateTime></DateTime></Response>'; |
|
25 | + |
|
26 | + /** |
|
27 | + * @var \DOMNode |
|
28 | + */ |
|
29 | + protected $Response; |
|
30 | + |
|
31 | + /** |
|
32 | + * @var \DOMElement |
|
33 | + */ |
|
34 | + protected $Sign; |
|
35 | + |
|
36 | + /** |
|
37 | + * Response constructor |
|
38 | + * |
|
39 | + */ |
|
40 | + public function __construct() |
|
41 | + { |
|
42 | 42 | parent::__construct('1.0', 'UTF-8'); |
43 | 43 | |
44 | 44 | $this->loadXML(self::TEMPLATE); |
45 | 45 | |
46 | 46 | $this->Response = $this->firstChild; |
47 | 47 | $this->set_DateTime(); |
48 | - } |
|
48 | + } |
|
49 | 49 | |
50 | - /** |
|
51 | - * Set DateTime element value by current time |
|
52 | - */ |
|
53 | - public function set_DateTime() |
|
54 | - { |
|
50 | + /** |
|
51 | + * Set DateTime element value by current time |
|
52 | + */ |
|
53 | + public function set_DateTime() |
|
54 | + { |
|
55 | 55 | $this->setElementValue('DateTime', date('Y-m-d\TH:i:s', time())); |
56 | - } |
|
57 | - |
|
58 | - /** |
|
59 | - * Create new element node |
|
60 | - * |
|
61 | - * @param string $name |
|
62 | - * @param string $value (optional) |
|
63 | - */ |
|
64 | - public function createElement($name, $value=NULL) |
|
65 | - { |
|
56 | + } |
|
57 | + |
|
58 | + /** |
|
59 | + * Create new element node |
|
60 | + * |
|
61 | + * @param string $name |
|
62 | + * @param string $value (optional) |
|
63 | + */ |
|
64 | + public function createElement($name, $value=NULL) |
|
65 | + { |
|
66 | 66 | return parent::createElement($name, $value); |
67 | - } |
|
68 | - |
|
69 | - /** |
|
70 | - * Set node value |
|
71 | - * |
|
72 | - * @param string $name |
|
73 | - * @param string $value |
|
74 | - */ |
|
75 | - public function setElementValue($name, $value) |
|
76 | - { |
|
67 | + } |
|
68 | + |
|
69 | + /** |
|
70 | + * Set node value |
|
71 | + * |
|
72 | + * @param string $name |
|
73 | + * @param string $value |
|
74 | + */ |
|
75 | + public function setElementValue($name, $value) |
|
76 | + { |
|
77 | 77 | foreach ($this->Response->childNodes as $child) |
78 | 78 | { |
79 | - if ($child->nodeName == $name) |
|
80 | - { |
|
79 | + if ($child->nodeName == $name) |
|
80 | + { |
|
81 | 81 | $child->nodeValue = $value; |
82 | - } |
|
82 | + } |
|
83 | + } |
|
83 | 84 | } |
84 | - } |
|
85 | 85 | |
86 | - /** |
|
87 | - * Dumps response into a string |
|
88 | - * |
|
89 | - * @return string XML |
|
90 | - */ |
|
91 | - public function friendly() |
|
92 | - { |
|
86 | + /** |
|
87 | + * Dumps response into a string |
|
88 | + * |
|
89 | + * @return string XML |
|
90 | + */ |
|
91 | + public function friendly() |
|
92 | + { |
|
93 | 93 | $this->encoding = 'UTF-8'; |
94 | 94 | $this->formatOutput = true; |
95 | 95 | //$this->save('/tmp/test1.xml'); |
96 | 96 | |
97 | 97 | return $this->saveXML(NULL, LIBXML_NOEMPTYTAG); |
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Send response |
|
102 | - * |
|
103 | - * @param array $options |
|
104 | - */ |
|
105 | - public function out($options) |
|
106 | - { |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Send response |
|
102 | + * |
|
103 | + * @param array $options |
|
104 | + */ |
|
105 | + public function out($options) |
|
106 | + { |
|
107 | 107 | $this->sign($options); |
108 | 108 | |
109 | 109 | Log::instance()->debug('response sends: '); |
@@ -113,83 +113,83 @@ discard block |
||
113 | 113 | header("Content-Type: text/xml; charset=utf-8"); |
114 | 114 | echo $this->friendly(); |
115 | 115 | exit; |
116 | - } |
|
117 | - |
|
118 | - /** |
|
119 | - * Add Sign (if hasn't yet done) |
|
120 | - * |
|
121 | - * @param array $options |
|
122 | - */ |
|
123 | - protected function sign($options) |
|
124 | - { |
|
116 | + } |
|
117 | + |
|
118 | + /** |
|
119 | + * Add Sign (if hasn't yet done) |
|
120 | + * |
|
121 | + * @param array $options |
|
122 | + */ |
|
123 | + protected function sign($options) |
|
124 | + { |
|
125 | 125 | if (isset($this->Sign)) return; |
126 | 126 | |
127 | 127 | if (isset($options['UseSign']) && ($options['UseSign'] === true)) |
128 | 128 | { |
129 | - $this->Sign = $this->createElement('Sign'); |
|
130 | - $this->Response->appendChild($this->Sign); |
|
129 | + $this->Sign = $this->createElement('Sign'); |
|
130 | + $this->Response->appendChild($this->Sign); |
|
131 | 131 | |
132 | - $sign = $this->generate_sign($options); |
|
132 | + $sign = $this->generate_sign($options); |
|
133 | 133 | |
134 | - $this->Sign->nodeValue = $sign; |
|
134 | + $this->Sign->nodeValue = $sign; |
|
135 | + } |
|
135 | 136 | } |
136 | - } |
|
137 | 137 | |
138 | - /** |
|
139 | - * Generate signature of response |
|
140 | - * |
|
141 | - * @param array $options |
|
142 | - * @return string |
|
143 | - */ |
|
144 | - public function generate_sign($options) |
|
145 | - { |
|
138 | + /** |
|
139 | + * Generate signature of response |
|
140 | + * |
|
141 | + * @param array $options |
|
142 | + * @return string |
|
143 | + */ |
|
144 | + public function generate_sign($options) |
|
145 | + { |
|
146 | 146 | try |
147 | 147 | { |
148 | - $sign = ''; |
|
149 | - $this->check_sign_result( |
|
148 | + $sign = ''; |
|
149 | + $this->check_sign_result( |
|
150 | 150 | $result = (new OpenSSL())->sign( |
151 | - $this->friendly(), |
|
152 | - $sign, |
|
153 | - (new OpenSSL())->get_priv_key($this->get_priv_key($options)) |
|
151 | + $this->friendly(), |
|
152 | + $sign, |
|
153 | + (new OpenSSL())->get_priv_key($this->get_priv_key($options)) |
|
154 | 154 | ) |
155 | - ); |
|
155 | + ); |
|
156 | 156 | |
157 | - return strtoupper(bin2hex($sign)); |
|
157 | + return strtoupper(bin2hex($sign)); |
|
158 | 158 | } |
159 | 159 | catch (\Exception $e) |
160 | 160 | { |
161 | - return null; |
|
161 | + return null; |
|
162 | + } |
|
162 | 163 | } |
163 | - } |
|
164 | 164 | |
165 | - /** |
|
166 | - * load file with provider private key |
|
167 | - * |
|
168 | - * @param array $options |
|
169 | - * @throws Exception\Runtime |
|
170 | - * @return string |
|
171 | - */ |
|
172 | - protected function get_priv_key($options) |
|
173 | - { |
|
165 | + /** |
|
166 | + * load file with provider private key |
|
167 | + * |
|
168 | + * @param array $options |
|
169 | + * @throws Exception\Runtime |
|
170 | + * @return string |
|
171 | + */ |
|
172 | + protected function get_priv_key($options) |
|
173 | + { |
|
174 | 174 | if ( ! isset($options['ProviderPKey'])) |
175 | 175 | { |
176 | - throw new \EasyPay\Exception\Runtime('The parameter ProviderPKey is not set!', -94); |
|
176 | + throw new \EasyPay\Exception\Runtime('The parameter ProviderPKey is not set!', -94); |
|
177 | 177 | } |
178 | 178 | |
179 | 179 | return (new Key())->get($options['ProviderPKey'], 'private'); |
180 | - } |
|
181 | - |
|
182 | - /** |
|
183 | - * check result of openssl sign |
|
184 | - * |
|
185 | - * @param bool $result |
|
186 | - * @throws Exception\Sign |
|
187 | - */ |
|
188 | - protected function check_sign_result($result) |
|
189 | - { |
|
180 | + } |
|
181 | + |
|
182 | + /** |
|
183 | + * check result of openssl sign |
|
184 | + * |
|
185 | + * @param bool $result |
|
186 | + * @throws Exception\Sign |
|
187 | + */ |
|
188 | + protected function check_sign_result($result) |
|
189 | + { |
|
190 | 190 | if ($result === FALSE) |
191 | 191 | { |
192 | - throw new \EasyPay\Exception\Sign('Can not generate signature!', -96); |
|
192 | + throw new \EasyPay\Exception\Sign('Can not generate signature!', -96); |
|
193 | + } |
|
193 | 194 | } |
194 | - } |
|
195 | 195 | } |
@@ -122,7 +122,9 @@ discard block |
||
122 | 122 | */ |
123 | 123 | protected function sign($options) |
124 | 124 | { |
125 | - if (isset($this->Sign)) return; |
|
125 | + if (isset($this->Sign)) { |
|
126 | + return; |
|
127 | + } |
|
126 | 128 | |
127 | 129 | if (isset($options['UseSign']) && ($options['UseSign'] === true)) |
128 | 130 | { |
@@ -155,8 +157,7 @@ discard block |
||
155 | 157 | ); |
156 | 158 | |
157 | 159 | return strtoupper(bin2hex($sign)); |
158 | - } |
|
159 | - catch (\Exception $e) |
|
160 | + } catch (\Exception $e) |
|
160 | 161 | { |
161 | 162 | return null; |
162 | 163 | } |