-
Notifications
You must be signed in to change notification settings - Fork 9
/
RequestURI.class.php
160 lines (137 loc) · 4.8 KB
/
RequestURI.class.php
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
<?php
/**
* This class will provide you a tool to ask for URI parameters
*
* @package The-DataTank
* @copyright (C) 2011 by iRail vzw/asbl
* @license AGPLv3
* @author Pieter Colpaert
*/
class RequestURI{
private static $instance;
private $protocol, $host, $port, $package,$resource, $filters, $format, $GETParameters;
private function __construct(){
$this->protocol = 'http';
if (!empty($_SERVER['HTTPS'])) {
if($_SERVER['HTTPS'] == 'on') {
$this->protocol .= "s";
}
}
$this->host = $_SERVER['SERVER_NAME'] . "/";
$this->port = $_SERVER["SERVER_PORT"];
$requestURI = $_SERVER["REQUEST_URI"];
//if a SUBDIR has been set in the config, remove this from here
if(Config::$SUBDIR != ""){
$subdir = str_replace("/", "\/", Config::$SUBDIR);
$requestURI = preg_replace("/".$subdir."/si","",$requestURI,1);
}
//Now for the hard part: parse the REQUEST_URI
//This can look like this: /package/resource/identi/fiers.json
$path = explode("/",$requestURI);
array_shift($path);
$i = 0;
//shift the path chunks as long as they exist and add them to the right variable
while(sizeof($path) > 0){
if($i == 0){
$this->package = $path[0];
}elseif($i == 1){
$this->resource = $path[0];
//if this is the last element in the array
//we might get the format out of it
$resourceformat = explode(".",$this->resource);
if(sizeof($path) == 1 && sizeof($resourceformat)>1){
$this->format = array_pop($resourceformat);
$this->resource = implode(".",$resourceformat);
}
}elseif($i > 1){
//if this is the last element in the array
//we might get the format out of it
$arrayformat = explode(".",$path[0]);
if(sizeof($path) == 1 && sizeof($arrayformat) > 1){
$this->format = array_pop($arrayformat);
$this->filters[] = implode(".",$arrayformat);
}else{
$this->filters[] = $path[0];
}
}
array_shift($path);
$i++;
}
//we need to sort all the GET parameters, otherwise we won't have a unique identifier for for instance caching purposes
if (is_null($_GET)){
$this->GETParameters = $_GET;
asort($GETParameters);
}
}
public static function getInstance(){
if(!isset(self::$instance)){
self::$instance = new RequestURI();
}
return self::$instance;
}
public function getProtocol(){
return $this->protocol;
}
public function getHostName(){
return $this->host;
}
public function getSubDir(){
return Config::$SUBDIR;
}
public function getPackage(){
return $this->package;
}
public function getResource(){
return $this->resource;
}
public function getFilters(){
if(!is_null($this->filters)){
return $this->filters;
}
return array();
}
public function getGET(){
if(!is_null($this->GETParameters)){
return $this->GETParameters;
}
return array();
}
public function getGivenFormat(){
return $this->format;
}
public function getRealWorldObjectURI(){
$URI = $this->protocol . "://" . $this->host . $this->getSubDir() . $this->package . "/";
$URI .= $this->getResourcePath();
return $URI;
}
public function getResourcePath(){
$URI = $this->resource;
if(isset($this->filters) && !is_null($this->filters)){
$URI .= "/";
$URI .= implode("/", $this->filters);
}
//Remove trailing slash
if (strripos($URI, '/') == strlen($URI) - 1)
$URI = substr($URI, 0, strlen($URI) - 1);
return $URI;
}
public function getURI(){
$URI = $this->protocol . "://" . $this->host . $this->getSubDir() . $this->package . "/" . $this->resource;
if(isset($this->filters) && !is_null($this->filters)){
$URI .= "/";
$URI .= implode("/", $this->filters);
}
if($this->format != ""){
$URI .= "." . $this->format;
}
if(sizeof($this->GETParameters) > 0){
$URI .= "?";
foreach($this->GETParameters as $key => $value){
$URI .= $key . "=" . $value . "&";
}
$URI = rtrim($URI,"&");
}
return $URI;
}
}
?>