Test Failed
Branch master (340a00)
by Mike
09:57
created

FilesWebDavClient   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 57
c 1
b 1
f 0
dl 0
loc 114
rs 10
wmc 22
1
<?php
2
/**
3
 * File plugin webdav client.
4
 * Overrides the request function to add support for downloading really large files with less memory.
5
 */
6
7
namespace Files\Backend\Webdav\sabredav;
8
9
include __DIR__ . "/vendor/autoload.php";
10
11
class FilesWebDavClient extends \Sabre\DAV\Client {
12
	public function __construct(array $settings) {
13
		if (isset($settings['userName'])) {
14
			$this->userName = $settings['userName'];
15
		}
16
		if (isset($settings['password'])) {
17
			$this->password = $settings['password'];
18
		}
19
		parent::__construct($settings);
20
	}
21
22
	/**
23
	 * Performs an actual HTTP request, and returns the result.
24
	 *
25
	 * If the specified url is relative, it will be expanded based on the base
26
	 * url.
27
	 *
28
	 * The returned array contains 3 keys:
29
	 *   * body - the response body
30
	 *   * httpCode - a HTTP code (200, 404, etc)
31
	 *   * headers - a list of response http headers. The header names have
32
	 *     been lowercased.
33
	 *
34
	 * @param string $url
35
	 * @param string $dstpath
36
	 * @param array  $headers
37
	 *
38
	 * @return array
39
	 */
40
	public function getFile($url, $dstpath, $headers = []) {
41
		if (empty($url)) {
42
			throw new \Sabre\DAV\Exception('Empty path');
43
		}
44
		$url = $this->getAbsoluteUrl($url);
45
		$file_handle = fopen($dstpath, "w");
46
47
		if (!$file_handle) {
48
			throw new \Sabre\DAV\Exception('[CURL] Error writing to temporary file! (' . $dstpath . ')');
49
		}
50
51
		// straight up curl instead of sabredav here, sabredav put's the entire get result in memory
52
		$curl = curl_init($url);
53
54
		if ($this->verifyPeer !== null) {
55
			curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, $this->verifyPeer);
56
		}
57
		if ($this->trustedCertificates) {
58
			curl_setopt($curl, CURLOPT_CAINFO, $this->trustedCertificates);
59
		}
60
61
		curl_setopt($curl, CURLOPT_USERPWD, $this->userName . ":" . $this->password);
62
		curl_setopt($curl, CURLOPT_FILE, $file_handle);
63
		curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
64
		curl_setopt($curl, CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
65
		curl_setopt($curl, CURLOPT_REDIR_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
66
67
		curl_exec($curl);
68
69
		$statusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
70
71
		curl_close($curl);
72
73
		$response = [
74
			'statusCode' => $statusCode,
75
		];
76
77
		if ($response['statusCode'] >= 400) {
78
			switch ($response['statusCode']) {
79
				case 400 :
80
					throw new \Sabre\DAV\Exception\BadRequest('Bad request');
81
82
				case 401 :
83
					throw new \Sabre\DAV\Exception\NotAuthenticated('Not authenticated');
84
85
				case 402 :
86
					throw new \Sabre\DAV\Exception\PaymentRequired('Payment required');
87
88
				case 403 :
89
					throw new \Sabre\DAV\Exception\Forbidden('Forbidden');
90
91
				case 404:
92
					throw new \Sabre\DAV\Exception\NotFound('Resource not found.');
93
94
				case 405 :
95
					throw new \Sabre\DAV\Exception\MethodNotAllowed('Method not allowed');
96
97
				case 409 :
98
					throw new \Sabre\DAV\Exception\Conflict('Conflict');
99
100
				case 412 :
101
					throw new \Sabre\DAV\Exception\PreconditionFailed('Precondition failed');
102
103
				case 416 :
104
					throw new \Sabre\DAV\Exception\RequestedRangeNotSatisfiable('Requested Range Not Satisfiable');
105
106
				case 500 :
107
					throw new \Sabre\DAV\Exception('Internal server error');
108
109
				case 501 :
110
					throw new \Sabre\DAV\Exception\NotImplemented('Not Implemented');
111
112
				case 507 :
113
					throw new \Sabre\DAV\Exception\InsufficientStorage('Insufficient storage');
114
115
				default:
116
					throw new \Sabre\DAV\Exception('HTTP error response. (errorcode ' . $response['statusCode'] . ')');
117
			}
118
		}
119
120
		return $response;
121
	}
122
123
	public function uploadChunkedFile($destination, $resource) {
124
		return $this->request("PUT", $destination, $resource);
125
	}
126
}
127