summaryrefslogtreecommitdiffstats
path: root/lib/connector/sabre/client.php
blob: 87f9d59b3aed1ae0785df2bd47f2a855234d7c14 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php

/**
 * ownCloud
 *
 * @author Bjoern Schiessle
 * @copyright 2012 Bjoern Schiessle <schiessle@owncloud.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU Affero General Public
 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

class OC_Connector_Sabre_Client extends Sabre_DAV_Client {
	
	protected $trustedCertificates;

	/**
	 * Add trusted root certificates to the webdav client.
	 *
	 * The parameter certificates should be a absulute path to a file which contains
	 * all trusted certificates
	 *
	 * @param string $certificates
	 */
	public function addTrustedCertificates($certificates) {
		$this->trustedCertificates = $certificates;
	}
	
	/**
	 * Copied from SabreDAV with some modification to use user defined curlSettings
	 * Performs an actual HTTP request, and returns the result.
	 *
	 * If the specified url is relative, it will be expanded based on the base
	 * url.
	 *
	 * The returned array contains 3 keys:
	 *   * body - the response body
	 *   * httpCode - a HTTP code (200, 404, etc)
	 *   * headers - a list of response http headers. The header names have
	 *     been lowercased.
	 *
	 * @param string $method
	 * @param string $url
	 * @param string $body
	 * @param array $headers
	 * @return array
	 */
	public function request($method, $url = '', $body = null, $headers = array()) {
	
		$url = $this->getAbsoluteUrl($url);
	
		$curlSettings = array(
				CURLOPT_RETURNTRANSFER => true,
				// Return headers as part of the response
				CURLOPT_HEADER => true,
				CURLOPT_POSTFIELDS => $body,
				// Automatically follow redirects
				CURLOPT_FOLLOWLOCATION => true,
				CURLOPT_MAXREDIRS => 5,
				CURLOPT_SSL_VERIFYPEER => true,
				//CURLOPT_SSL_VERIFYPEER	=> false,
		);
		
		if($this->trustedCertificates) {
			$curlSettings[CURLOPT_CAINFO] = $this->trustedCertificates;
		}
	
		switch ($method) {
			case 'PUT':
				$curlSettings[CURLOPT_PUT] = true;
				break;
			case 'HEAD' :
	
				// do not read body with HEAD requests (this is neccessary because cURL does not ignore the body with HEAD
				// requests when the Content-Length header is given - which in turn is perfectly valid according to HTTP
				// specs...) cURL does unfortunately return an error in this case ("transfer closed transfer closed with
				// ... bytes remaining to read") this can be circumvented by explicitly telling cURL to ignore the
				// response body
				$curlSettings[CURLOPT_NOBODY] = true;
				$curlSettings[CURLOPT_CUSTOMREQUEST] = 'HEAD';
				break;
	
			default:
				$curlSettings[CURLOPT_CUSTOMREQUEST] = $method;
				break;
	
		}
	
		// Adding HTTP headers
		$nHeaders = array();
		foreach($headers as $key=>$value) {
	
			$nHeaders[] = $key . ': ' . $value;
	
		}
		$curlSettings[CURLOPT_HTTPHEADER] = $nHeaders;
	
		if ($this->proxy) {
			$curlSettings[CURLOPT_PROXY] = $this->proxy;
		}
	
		if ($this->userName) {
			$curlSettings[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC | CURLAUTH_DIGEST;
			$curlSettings[CURLOPT_USERPWD] = $this->userName . ':' . $this->password;
		}
	
		list(
				$response,
				$curlInfo,
				$curlErrNo,
				$curlError
		) = $this->curlRequest($url, $curlSettings);
	
		$headerBlob = substr($response, 0, $curlInfo['header_size']);
		$response = substr($response, $curlInfo['header_size']);
	
		// In the case of 100 Continue, or redirects we'll have multiple lists
		// of headers for each separate HTTP response. We can easily split this
		// because they are separated by \r\n\r\n
		$headerBlob = explode("\r\n\r\n", trim($headerBlob, "\r\n"));
	
		// We only care about the last set of headers
		$headerBlob = $headerBlob[count($headerBlob)-1];
	
		// Splitting headers
		$headerBlob = explode("\r\n", $headerBlob);
	
		$headers = array();
		foreach($headerBlob as $header) {
			$parts = explode(':', $header, 2);
			if (count($parts)==2) {
				$headers[strtolower(trim($parts[0]))] = trim($parts[1]);
			}
		}
	
		$response = array(
				'body' => $response,
				'statusCode' => $curlInfo['http_code'],
				'headers' => $headers
		);
	
		if ($curlErrNo) {
			throw new Sabre_DAV_Exception('[CURL] Error while making request: ' . $curlError . ' (error code: ' . $curlErrNo . ')');
		}
	
		if ($response['statusCode']>=400) {
			switch ($response['statusCode']) {
				case 404:
					throw new Sabre_DAV_Exception_NotFound('Resource ' . $url . ' not found.');
					break;
	
				default:
					throw new Sabre_DAV_Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')');
			}
		}
	
		return $response;
	
	}
}