NtlmSoapClient::__doRequest()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 37
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 37
rs 8.8571
cc 2
eloc 24
nc 2
nop 5
1
<?php
2
3
namespace PhpEws\Ntlm;
4
5
use PhpEws\Exception\EwsException;
6
7
/**
8
 * Soap Client using Microsoft's NTLM Authentication.
9
 *
10
 * @link http://rabaix.net/en/articles/2008/03/13/using-soap-php-with-ntlm-authentication
11
 * @author Thomas Rabaix
12
 */
13
class NtlmSoapClient extends \SoapClient
14
{
15
    /**
16
     * cURL resource used to make the SOAP request
17
     *
18
     * @var resource
19
     */
20
    protected $ch;
21
22
    /**
23
     * Whether or not to validate ssl certificates
24
     *
25
     * @var boolean
26
     */
27
    protected $validate = false;
28
29
    /**
30
     * Performs a SOAP request
31
     *
32
     * @link http://php.net/manual/en/function.soap-soapclient-dorequest.php
33
     *
34
     * @param string $request the xml soap request
35
     * @param string $location the url to request
36
     * @param string $action the soap action.
37
     * @param integer $version the soap version
38
     * @param integer $one_way
39
     *
40
     * @return string the xml soap response.
41
     *
42
     * @throws EwsException If the response if false than there was an error.
43
     */
44
    public function __doRequest($request, $location, $action, $version, $one_way = 0)
45
    {
46
        $headers = array(
47
            'Method: POST',
48
            'Connection: Keep-Alive',
49
            'User-Agent: PHP-SOAP-CURL',
50
            'Content-Type: text/xml; charset=utf-8',
51
            'SOAPAction: "'.$action.'"',
52
        );
53
54
        $this->__last_request_headers = $headers;
0 ignored issues
show
Bug introduced by
The property __last_request_headers does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
55
        $this->ch = curl_init($location);
56
57
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, $this->validate);
58
        curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, $this->validate);
59
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
60
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
61
        curl_setopt($this->ch, CURLOPT_POST, true );
62
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request);
63
        curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
64
        curl_setopt($this->ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
65
        curl_setopt($this->ch, CURLOPT_USERPWD, $this->user.':'.$this->password);
0 ignored issues
show
Bug introduced by
The property user does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Bug introduced by
The property password does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
67
        $response = curl_exec($this->ch);
68
69
        // TODO: Add some real error handling.
70
        // If the response if false than there was an error and we should throw
71
        // an exception.
72
        if ($response === false) {
73
            throw new EwsException(
74
              'Curl error: ' . curl_error($this->ch),
75
              curl_errno($this->ch)
76
            );
77
        }
78
79
        return $response;
80
    }
81
82
    /**
83
     * Returns last SOAP request headers
84
     *
85
     * @link http://php.net/manual/en/function.soap-soapclient-getlastrequestheaders.php
86
     *
87
     * @return string the last soap request headers
88
     */
89
    public function __getLastRequestHeaders()
90
    {
91
        return implode('n', $this->__last_request_headers) . "\n";
92
    }
93
94
    /**
95
     * Sets whether or not to validate ssl certificates
96
     *
97
     * @param boolean $validate
98
     *
99
     * @return true
100
     */
101
    public function validateCertificate($validate = true)
102
    {
103
        $this->validate = $validate;
104
105
        return true;
106
    }
107
}
108