|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Helper for dealing with VFS paths returned from the API |
|
5
|
|
|
* |
|
6
|
|
|
* @author Sam Stenvall <[email protected]> |
|
7
|
|
|
* @copyright Copyright © Sam Stenvall 2013- |
|
8
|
|
|
* @license https://www.gnu.org/licenses/gpl.html The GNU General Public License v3.0 |
|
9
|
|
|
*/ |
|
10
|
|
|
class VFSHelper |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var Backend the current backend |
|
15
|
|
|
*/ |
|
16
|
|
|
private $_backend; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class constructor |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
|
|
$this->_backend = Yii::app()->backendManager->getCurrent(); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Returns the absolute URL to the specified API path |
|
28
|
|
|
* @param string $path a path returned from an API call |
|
29
|
|
|
* @param boolean $omitCredentials whether to omit credentials from the URL |
|
30
|
|
|
* @return string |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getUrl($path, $omitCredentials = false) |
|
33
|
|
|
{ |
|
34
|
|
|
if (!empty($this->_backend->proxyLocation) && substr($path, 0, 3) === 'vfs') |
|
35
|
|
|
return $this->getProxiedUrl($path); |
|
36
|
|
|
else |
|
37
|
|
|
return $this->getNonProxiedUrl($path, $omitCredentials); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the absolute URL to the specified path using the proxy location |
|
42
|
|
|
* specified |
|
43
|
|
|
* @param string $path a path returned from an API call |
|
44
|
|
|
* @return string the URL |
|
45
|
|
|
*/ |
|
46
|
|
|
private function getProxiedUrl($path) |
|
47
|
|
|
{ |
|
48
|
|
|
// Only use HTTPS if user has explicitly enabled it |
|
49
|
|
|
$scheme = 'http://'; |
|
50
|
|
|
if (Setting::getBoolean('useHttpsForVfsUrls') && Yii::app()->request->isSecureConnection) |
|
51
|
|
|
$scheme = 'https://'; |
|
52
|
|
|
|
|
53
|
|
|
// Remove the beginning "vfs/" from the path |
|
54
|
|
|
$path = substr($path, 4); |
|
55
|
|
|
|
|
56
|
|
|
return $scheme.$_SERVER['HTTP_HOST'].$this->_backend->proxyLocation.'/'.$path; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Returns the absolute URL to the specified path |
|
61
|
|
|
* @param string $path a path returned from an API call |
|
62
|
|
|
* @param boolean $omitCredentials whether to omit credentials from the URL |
|
63
|
|
|
* @return string the URL |
|
64
|
|
|
*/ |
|
65
|
|
|
private function getNonProxiedUrl($path, $omitCredentials) |
|
66
|
|
|
{ |
|
67
|
|
|
$hostname = Backend::normalizeAddress($this->_backend->hostname); |
|
68
|
|
|
$port = $this->_backend->port; |
|
69
|
|
|
$url = 'http://{credentials}'.$hostname.':'.$port.'/'.$path; |
|
70
|
|
|
|
|
71
|
|
|
if ($omitCredentials) |
|
72
|
|
|
$url = str_replace('{credentials}', '', $url); |
|
73
|
|
|
else |
|
74
|
|
|
{ |
|
75
|
|
|
$url = str_replace('{credentials}', $this->_backend->username.':'. |
|
76
|
|
|
$this->_backend->password.'@', $url); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $url; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|