DeployerGuide/Others/eDokumentyApi: EDokApiClient.inc

Plik EDokApiClient.inc, 4.5 KB (dodany przez ogembalski, 8 years temu)
xx

eDokApiClient.inc

Line 
1<?php
2require_once('EDokApiConf.inc');
3
4/**
5 * Klasa rozszerzająca wbudowaną bibliotekę SOAP o
6 * uwierzytelnianie WSSSecurity
7 * @author Marcin Król
8 * (c) BetaSoft Sp. z o.o.
9 * @version 1.0
10 */
11class EDokApiClient extends SoapClient {
12
13    protected $username = NULL;
14    protected $password = NULL;
15    protected $timestamp = NULL;
16    protected $ns_env = array(
17        SOAP_1_1 => 'http://schemas.xmlsoap.org/soap/envelope',
18        SOAP_1_2 => 'http://www.w3.org/2003/05/soap-envelope',
19    );
20    protected $envelopeNS = NULL;
21
22
23    /**
24     *
25     * @param $request
26     * @param $location
27     * @param $action
28     * @param $version
29     */
30    public function __doRequest($request, $location, $action, $version) {
31   
32        $this->envelopeNS = isset($this->ns_env[$version]) ? $this->ns_env[$version] : $this->ns_env[SOAP_1_1];
33       
34        $dom = new DOMDocument('1.0');
35        try {
36            $dom->loadXML($request, LIBXML_PARSEHUGE);
37        } catch (DOMException $e) {
38            die('Dokument nie jest prawidłowy:' . $e->code);
39        }
40
41        $this->addWSSEHeader($dom);
42        return parent::__doRequest($dom->saveXML(), $location, $action, $version);
43
44    }
45
46    /**
47     *
48     * @param $username
49     */
50    public function setUser($username) {
51        $this->username = $username;
52    }
53
54    /**
55     *
56     * @param $password
57     */
58    public function setPass($password) {
59        $this->password = md5($password.EDokApiConf::$PASS_TOKEN);
60    }
61
62
63
64    public function setTimeStamp($timestamp) {
65
66        $this->timestamp = $timestamp;
67
68    }
69
70
71
72    /**
73     *
74     * @param $domDocument
75     */
76    private function addWSSEHeader($domDocument) {
77
78        $soapEnvelope = $domDocument->documentElement;
79        $nodes = $domDocument->getElementsByTagName('Header');
80        if ($nodes->length == 0) {
81            $header = $domDocument->createElementNS($this->envelopeNS, 'Header');
82            $body = $domDocument->getElementsByTagName('Body');
83            $body = $body->item(0);
84            $header = $soapEnvelope->insertBefore($header, $body);
85        } else {
86            $header = $nodes->item(0);
87        }
88
89        $ns_wsu = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd';
90
91        $security = $domDocument->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'wsse:Security');
92        $usernameToken = $domDocument->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd', 'UsernameToken');
93        $username = $domDocument->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','Username');
94        $usernameText = $domDocument->createTextNode($this->username);
95        $password = $domDocument->createElementNS('http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd','Password');
96        $passwordText = $domDocument->createTextNode($this->password);
97        $type = $domDocument->createAttribute('Type');
98        $type->value = 'http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText';
99        $username->appendChild($usernameText);
100        $password->appendChild($type);
101        $password->appendChild($passwordText);
102        $usernameToken->appendChild($username);
103        $usernameToken->appendChild($password);
104        $security->appendChild($usernameToken);
105        $header->appendChild($security);
106
107        if (is_numeric($this->timestamp) AND $this->timestamp) {
108            // Creating date using yyyy-mm-ddThh:mm:ssZ format
109            $tm_created = gmdate('Y-m-d\TH:i:s\Z');
110            $tm_expires = gmdate('Y-m-d\TH:i:s\Z', gmdate('U') + $this->timestamp);
111
112            $timestamp = $domDocument->createElementNS($ns_wsu, 'wsu:Timestamp');
113            $timestamp = $security->appendChild($timestamp);
114
115            $wsuTimestampId = $domDocument->createAttribute('wsu:Id');
116            $wsuTimestampId->value = 'TS-6';
117            $timestamp->appendChild($wsuTimestampId);
118
119            $createdNode = $domDocument->createElementNS($ns_wsu, 'wsu:Created');
120            $createdNodeText = $domDocument->createTextNode($tm_created);
121            $createdNode->appendChild($createdNodeText);
122
123            $expireNode = $domDocument->createElementNS($ns_wsu, 'wsu:Expires');
124            $expireNodeText = $domDocument->createTextNode($tm_expires);
125            $expireNode->appendChild($expireNodeText);
126
127            $timestamp->appendChild($createdNode);
128            $timestamp->appendChild($expireNode);
129        }
130
131    }
132
133}
134
135?>