Completed
Push — master ( f4b338...f199c6 )
by Thomas
08:27
created

WebDavClientService::newClient()   B

Complexity

Conditions 6
Paths 18

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 15
nc 18
nop 1
dl 0
loc 24
rs 8.5125
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Vincent Petry <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2017, ownCloud GmbH
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OC\Http\Client;
23
24
use OCP\Http\Client\IWebDavClientService;
25
use OCP\IConfig;
26
use OCP\ICertificateManager;
27
use Sabre\DAV\Client;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, OC\Http\Client\Client.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
28
29
/**
30
 * Class WebDavClientService
31
 *
32
 * @package OC\Http
33
 */
34
class WebDavClientService implements IWebDavClientService {
35
	/** @var IConfig */
36
	private $config;
37
	/** @var ICertificateManager */
38
	private $certificateManager;
39
40
	/**
41
	 * @param IConfig $config
42
	 * @param ICertificateManager $certificateManager
43
	 */
44
	public function __construct(IConfig $config,
45
								ICertificateManager $certificateManager) {
46
		$this->config = $config;
47
		$this->certificateManager = $certificateManager;
48
	}
49
50
	/**
51
	 * Instantiate new Sabre client
52
	 *
53
	 * Settings are provided through the 'settings' argument. The following
54
	 * settings are supported:
55
	 *
56
	 *   * baseUri
57
	 *   * userName (optional)
58
	 *   * password (optional)
59
	 *   * proxy (optional)
60
	 *   * authType (optional)
61
	 *   * encoding (optional)
62
	 *
63
	 *  authType must be a bitmap, using self::AUTH_BASIC, self::AUTH_DIGEST
64
	 *  and self::AUTH_NTLM. If you know which authentication method will be
65
	 *  used, it's recommended to set it, as it will save a great deal of
66
	 *  requests to 'discover' this information.
67
	 *
68
	 *  Encoding is a bitmap with one of the ENCODING constants.
69
	 *
70
	 * @param array $settings Sabre client settings
71
	 * @return Client
72
	 */
73
	public function newClient($settings) {
74
		if (!isset($settings['proxy'])) {
75
			$proxy = $this->config->getSystemValue('proxy', '');
76
			if($proxy !== '') {
77
				$settings['proxy'] = $proxy;
78
			}
79
		}
80
81
		$certPath = null;
82
		if (strpos($settings['baseUri'], 'https') === 0) {
83
			$certPath = $this->certificateManager->getAbsoluteBundlePath();
84
			if (!file_exists($certPath)) {
85
				$certPath = null;
86
			}
87
		}
88
89
		$client = new Client($settings);
90
		$client->setThrowExceptions(true);
91
92
		if ($certPath !== null) {
93
			$client->addCurlSetting(CURLOPT_CAINFO, $certPath);
94
		}
95
		return $client;
96
	}
97
}
98