diff --git a/README.md b/README.md index dd16a1422..6ad47e161 100644 --- a/README.md +++ b/README.md @@ -41,10 +41,10 @@ object: ```php use \jamesiarmes\PhpEws\Client; -$ews = new Client($server, $username, $password, $version); +$ews = new Client($server, $username, $password, $version, $client_certificate); ``` -The `Client` class takes four parameters for its constructor: +The `Client` class takes five parameters for its constructor: * `$server`: The url to the exchange server you wish to connect to, without the protocol. Example: mail.example.com. If you have trouble determining the @@ -56,6 +56,8 @@ The `Client` class takes four parameters for its constructor: * `$version` (optional): The version of the Exchange sever to connect to. Valid values can be found at `\jamesiarmes\PhpEws\Client::VERSION_*`. Defaults to Exchange 2007. +* `$client_certicaticate` (optional): The path to the client certificate to use + for authentication. When specified, `$username` and `$password` are ignored. Once you have your `\jamesiarmes\PhpEws\Client` object, you need to build your request object. The type of object depends on the operation you are calling. If diff --git a/src/Client.php b/src/Client.php index c552d1009..af6c6e83b 100644 --- a/src/Client.php +++ b/src/Client.php @@ -152,6 +152,11 @@ class Client */ protected $version; + /** + * @var ?string Path to client certificate + */ + protected $client_certificate; + /** * Constructor for the ExchangeWebServices class * @@ -165,7 +170,8 @@ public function __construct( $server = null, $username = null, $password = null, - $version = self::VERSION_2013 + $version = self::VERSION_2013, + $client_certificate = null ) { // Set the object properties. $this->setServer($server); @@ -189,6 +195,16 @@ public function getClient() return $this->soap; } + /** + * Gets the client certificate path (or null if not set) + * + * @return string|null + */ + public function getClientCertificate() + { + return $this->client_certificate; + } + /** * Sets the cURL options that will be set on the SOAP client. * @@ -280,6 +296,22 @@ public function setVersion($version) $this->headers = array(); } + /** + * Sets the client certificate. Should be a readable path to a certificate file + * + * @param string $client_certificate + * + * @throws \InvalidArgumentException If the certificate is not readable + */ + public function setClientCertificate($client_certificate) + { + if (!is_readable($client_certificate)) { + throw new \InvalidArgumentException('Client certificate not readable'); + } + + $this->client_certificate = $client_certificate; + } + /** * Adds one or more delegates to a principal's mailbox and sets specific * access permissions. @@ -1604,16 +1636,25 @@ public function UploadItems($request) */ protected function initializeSoapClient() { + $options = array( + 'location' => 'https://' . $this->server . '/EWS/Exchange.asmx', + 'classmap' => $this->classMap(), + 'curlopts' => $this->curl_options, + 'features' => SOAP_SINGLE_ELEMENT_ARRAYS + ); + + //If client certificate is set, add it to the options for usage + //Else, default to login/password + if ($this->client_certificate !== null) { + $options['local_cert'] = $this->client_certificate; + } else { + $options['user'] = $this->username; + $options['password'] = $this->password; + } + $this->soap = new SoapClient( dirname(__FILE__) . '/assets/services.wsdl', - array( - 'user' => $this->username, - 'password' => $this->password, - 'location' => 'https://' . $this->server . '/EWS/Exchange.asmx', - 'classmap' => $this->classMap(), - 'curlopts' => $this->curl_options, - 'features' => SOAP_SINGLE_ELEMENT_ARRAYS, - ) + $options ); return $this->soap;