Completed
Push — master ( 067925...6361e3 )
by Dmitry
02:41
created

General::check_and_parse_request_node()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 5
rs 9.4285
1
<?php
2
3
/**
4
 *      General class for all request types
5
 *
6
 *      @package php_EasyPay
7
 *      @version 1.1
8
 *      @author Dmitry Shovchko <[email protected]>
9
 *
10
 */
11
12
namespace EasyPay\Provider31\Request;
13
14
use EasyPay\Log as Log;
15
use EasyPay\Exception;
16
use EasyPay\Key as Key;
17
use EasyPay\OpenSSL as OpenSSL;
18
19
class General
20
{
21
    /**
22
     *      @var string raw request
23
     */
24
    protected $raw_request;
25
26
    /**
27
     *      @var string 'DateTime' node
28
     */
29
    protected $DateTime;
30
31
    /**
32
     *      @var string 'Sign' node
33
     */
34
    protected $Sign;
35
36
    /**
37
     *      @var string 'Operation' type
38
     */
39
    protected $Operation;
40
41
    /**
42
     *      @var string 'ServiceId' node
43
     */
44
    protected $ServiceId;
45
46
    /**
47
     *      @var array list of possible operations
48
     */
49
    protected $operations = array('Check','Payment','Confirm','Cancel');
50
51
    /**
52
     *      General constructor
53
     *
54
     *      @param string $raw Raw request data
55
     */
56
    public function __construct($raw)
57
    {
58
        $this->raw_request = strval($raw);
59
60
        $this->parse_request_data();
61
    }
62
63
    /**
64
     *      Get DateTime
65
     *
66
     *      @return string
67
     */
68
    public function DateTime()
69
    {
70
        return $this->DateTime;
71
    }
72
73
    /**
74
     *      Get Sign
75
     *
76
     *      @return string
77
     */
78
    public function Sign()
79
    {
80
        return $this->Sign;
81
    }
82
83
    /**
84
     *      Get Operation type
85
     *
86
     *      @return string
87
     */
88
    public function Operation()
89
    {
90
        return $this->Operation;
91
    }
92
93
    /**
94
     *      Get ServiceId
95
     *
96
     *      @return string
97
     */
98
    public function ServiceId()
99
    {
100
        return $this->ServiceId;
101
    }
102
103
    /**
104
     *      Parse xml-request, which was previously "extracted" from the body of the http request
105
     *
106
     *      @throws Exception\Structure
107
     */
108
    protected function parse_request_data()
109
    {
110
        if (empty($this->raw_request))
111
        {
112
            throw new Exception\Structure('An empty xml request', -50);
113
        }
114
115
        libxml_use_internal_errors(true);
116
        $doc = new \DOMDocument();
117
        if ( ! $doc->loadXML($this->raw_request))
118
        {
119
            foreach(libxml_get_errors() as $e){
120
                Log::instance()->error($e->message);
121
            }
122
            throw new Exception\Structure('The wrong XML is received', -51);
123
        }
124
125
        // process <Request> group
126
        $r = $this->getNodes($doc, 'Request');
127
128
        if (count($r) < 1)
129
        {
130
            throw new Exception\Structure('The xml-query does not contain any element Request!', -52);
131
        }
132
133
        foreach ($r[0]->childNodes as $child)
134
        {
135
            $this->check_and_parse_request_node($child, 'DateTime');
136
            $this->check_and_parse_request_node($child, 'Sign');
137
138
            if (in_array($child->nodeName, $this->operations))
139
            {
140
                if ( ! isset($this->Operation))
141
                {
142
                    $this->Operation = $child->nodeName;
143
                }
144
                else
145
                {
146
                    throw new Exception\Structure('There is more than one Operation type element in the xml-query!', -53);
147
                }
148
            }
149
        }
150
151
        if ( ! isset($this->Operation))
152
        {
153
            throw new Exception\Structure('There is no Operation type element in the xml request!', -55);
154
        }
155
156
        // process <Operation> group
157
        $r = $this->getNodes($doc, $this->Operation);
158
159
        foreach ($r[0]->childNodes as $child)
160
        {
161
            $this->check_and_parse_request_node($child, 'ServiceId');
162
        }
163
    }
164
165
    /**
166
     *      Check if present node of request and then parse it
167
     *
168
     *      @param \DOMNode $n
169
     *      @param string $name
170
     */
171
    protected function check_and_parse_request_node($n, $name)
172
    {
173
        if ($n->nodeName == $name)
174
        {
175
            $this->parse_request_node($n, $name);
176
        }
177
    }
178
179
    /**
180
     *      Parse node of request
181
     *
182
     *      @param \DOMNode $n
183
     *      @param string $name
184
     *
185
     *      @throws Exception\Structure
186
     */
187
    protected function parse_request_node($n, $name)
188
    {
189
        if ( ! isset($this->$name))
190
        {
191
            $this->$name = $n->nodeValue;
192
        }
193
        else
194
        {
195
            throw new Exception\Structure('There is more than one '.$name.' element in the xml-query!', -56);
196
        }
197
    }
198
199
    /**
200
     *      "Rough" validation of the received xml request
201
     *
202
     *      @param array $options
203
     *      @throws Exception\Data
204
     *      @throws Exception\Structure
205
     */
206
    public function validate_request($options)
207
    {
208
        $this->validate_element('DateTime');
209
        $this->validate_element('Sign');
210
        $this->validate_element('ServiceId');
211
212
        // compare received value ServiceId with option ServiceId
213
        if (intval($options['ServiceId']) != intval($this->ServiceId))
214
        {
215
            throw new Exception\Data('This request is not for our ServiceId!', -58);
216
        }
217
    }
218
219
    /**
220
     *      Validation of xml-element
221
     *
222
     *      @param string $name
223
     */
224
    public function validate_element($name)
225
    {
226
        if ( ! isset($this->$name))
227
        {
228
            throw new Exception\Structure('There is no '.$name.' element in the xml request!', -57);
229
        }
230
    }
231
232
    /**
233
     *      Verify signature of request
234
     *
235
     *      @param array $options
236
     */
237
    public function verify_sign($options)
238
    {
239
        if (isset($options['UseSign']) && ($options['UseSign'] === true))
240
        {
241
            $this->check_verify_sign_result(
242
                $result = (new OpenSSL())->verify(
243
                    str_replace($this->Sign, '', $this->raw_request),
244
                    pack("H*", $this->Sign),
245
                    (new OpenSSL())->get_pub_key($this->get_pub_key($options))
246
                )
247
            );
248
        }
249
    }
250
251
    /**
252
     *      load file with easysoft public key
253
     *
254
     *      @param array $options
255
     *      @throws Exception\Runtime
256
     *      @return string
257
     */
258
    protected function get_pub_key($options)
259
    {
260
        if ( ! isset($options['EasySoftPKey']))
261
        {
262
            throw new Exception\Runtime('The parameter EasySoftPKey is not set!', -94);
263
        }
264
265
        return (new Key())->get($options['EasySoftPKey'], 'public');
266
    }
267
268
    /**
269
     *      check result of openssl verify signature
270
     *
271
     *      @param integer $result
272
     *      @throws Exception\Sign
273
     */
274
    protected function check_verify_sign_result($result)
275
    {
276
        if ($result == -1)
277
        {
278
            throw new Exception\Sign('Error verify signature of request!', -96);
279
        }
280
        elseif ($result == 0)
281
        {
282
            throw new Exception\Sign('Signature of request is incorrect!', -95);
283
        }
284
    }
285
286
    /**
287
     *      Selects nodes by name
288
     *
289
     *      @param \DOMDocument $dom
290
     *      @param string $name
291
     *      @param array $ret
292
     *
293
     *      @return array nodes with the name
294
     */
295
    protected function getNodes($dom, $name, $ret=array())
296
    {
297
        foreach($dom->childNodes as $child)
298
        {
299
            if ($child->nodeName == $name)
300
            {
301
                array_push($ret, $child);
302
            }
303
            else
304
            {
305
                if (count($child->childNodes) > 0)
306
                {
307
                    $ret = $this->getNodes($child, $name, $ret);
308
                }
309
            }
310
        }
311
312
        return $ret;
313
    }
314
315
}
316