|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @author Threema GmbH |
|
4
|
|
|
* @copyright Copyright (c) 2015-2016 Threema GmbH |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
namespace Threema\Core; |
|
9
|
|
|
|
|
10
|
|
|
class Url { |
|
11
|
|
|
/** |
|
12
|
|
|
* @var string[] |
|
13
|
|
|
*/ |
|
14
|
|
|
private $values = array(); |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
private $path; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $host; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $path |
|
28
|
|
|
* @param string $host |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct($path, $host = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->path = $path; |
|
33
|
|
|
$this->host = $host; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param string $key |
|
38
|
|
|
* @param string $value |
|
39
|
|
|
* @return $this |
|
40
|
|
|
*/ |
|
41
|
|
|
public function setValue($key, $value){ |
|
42
|
|
|
$this->values[$key] = $value; |
|
43
|
|
|
return $this; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Add a path to the current url |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $path |
|
50
|
|
|
* @return $this |
|
51
|
|
|
*/ |
|
52
|
|
|
public function addPath($path) { |
|
53
|
|
|
while(substr($this->path, strlen($this->path)-1) == '/') { |
|
54
|
|
|
$this->path = substr($this->path, 0, strlen($this->path)-1); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$realPath = ''; |
|
58
|
|
|
foreach(explode('/', $path) as $c => $pathPiece) { |
|
59
|
|
|
if($c > 0) { |
|
60
|
|
|
$realPath .= '/'; |
|
61
|
|
|
} |
|
62
|
|
|
$realPath .= urlencode($pathPiece); |
|
63
|
|
|
} |
|
64
|
|
|
while(substr($path, 0, 1) == '/') { |
|
65
|
|
|
$path = substr($path, 1); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->path .= '/'.$path; |
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getPath() { |
|
76
|
|
|
$p = $this->path; |
|
77
|
|
|
if(count($this->values) > 0) { |
|
78
|
|
|
$s = http_build_query($this->values); |
|
79
|
|
|
if(strlen($s) > 0) { |
|
80
|
|
|
$p .= '?'.$s; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $p; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
public function __toString() { |
|
88
|
|
|
return $this->getPath(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return string |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getFullPath() { |
|
95
|
|
|
return $this->host.(substr($this->getPath(), 0, 1) == '/' ? '' : '/').$this->getPath(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
public static function parametersToArray($urlParameter) |
|
99
|
|
|
{ |
|
100
|
|
|
$result = array(); |
|
101
|
|
|
|
|
102
|
|
|
while(strlen($urlParameter) > 0) { |
|
103
|
|
|
// name |
|
104
|
|
|
$keyPosition= strpos($urlParameter,'='); |
|
105
|
|
|
$keyValue = substr($urlParameter,0,$keyPosition); |
|
106
|
|
|
// value |
|
107
|
|
|
$valuePosition = strpos($urlParameter,'&') ? strpos($urlParameter,'&'): strlen($urlParameter); |
|
108
|
|
|
$valueValue = substr($urlParameter,$keyPosition+1,$valuePosition-$keyPosition-1); |
|
109
|
|
|
|
|
110
|
|
|
// decoding the response |
|
111
|
|
|
$result[$keyValue] = urldecode($valueValue); |
|
112
|
|
|
$urlParameter = substr($urlParameter,$valuePosition+1,strlen($urlParameter)); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
return $result; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|