PluginProbe
ShiftController Employee Shift Scheduling / 4.9.97
ShiftController Employee Shift Scheduling v4.9.97
4.9.97 4.9.96 4.9.95 4.9.74 4.9.75 4.9.76 4.9.77 4.9.78 4.9.84 4.9.85 4.9.87 4.9.91 4.9.92 trunk 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 3.2.4 All 38 releases
shiftcontroller / hc3 / uri.php

uri.php in ShiftController Employee Shift Scheduling 4.9.97, at hc3/uri.php

337 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if (! defined('ABSPATH')) exit; // Exit if accessed directly
2 interface HC3_IUri
3 {
4 public function setAssetsPath( $path );
5 public function assetsPath( $src );
6 public function getSlug();
7 public function getParams();
8 public function makeUrl( $slug = NULL, $proto = NULL );
9 public function baseUrl();
10 public function fromUrl( $url );
11 public function isFullUrl( $url );
12 public function currentUrl();
13 }
14
15 if( ! defined('HC3_URI_NO_PORT') ) define( 'HC3_URI_NO_PORT', true );
16
17 class HC3_Uri implements HC3_IUri
18 {
19 protected $_hca = 'hca';
20 protected $_hcj = 'hcj';
21 // protected $_hcr = 'hcr';
22 protected $_hcr = '';
23 protected $_hcrValue = NULL;
24
25 protected $_separatorToParams = ':';
26 // protected $_separatorToParams = '*';
27 protected $_suffixForMulti = '_';
28 protected $_separatorForMulti = '|';
29
30 protected $baseUrl = NULL;
31 protected $baseParams = array();
32 protected $slug = NULL;
33 protected $rawParams = array();
34 protected $params = array();
35
36 protected $assetsPath = NULL;
37
38 public function __construct()
39 {
40 $this->fromUrl( $this->currentUrl() );
41 if( strlen($this->_hcr) ){
42 $this->_hcrValue = mt_rand(1000, 9999);
43 }
44 }
45
46 public function setAssetsPath( $path )
47 {
48 $this->assetsPath = $path;
49 return $this;
50 }
51
52 public function assetsPath( $src )
53 {
54 $return = $this->assetsPath ? $this->assetsPath : $this->baseUrl();
55 return $return;
56 }
57
58 public function setSlug( $slug )
59 {
60 $this->slug = $slug;
61 return $this;
62 }
63
64 public function getSlug()
65 {
66 return $this->slug;
67 }
68
69 public function getParams()
70 {
71 return $this->params;
72 }
73
74 public function makeUrl( $slug = NULL, $proto = NULL )
75 {
76 $params = array();
77 if( is_array($slug) ){
78 list( $slug, $params ) = $slug;
79 }
80
81 if( $this->isFullUrl($slug) ){
82 return $slug;
83 }
84
85 if( $slug == '-referrer-' ){
86 if( isset($_SERVER['HTTP_REFERER']) && strlen($_SERVER['HTTP_REFERER']) ){
87 $this->fromUrl( $_SERVER['HTTP_REFERER'] );
88 $slug = $this->getSlug();
89 // $return = $_SERVER['HTTP_REFERER'];
90 // return $return;
91 }
92 }
93
94 $href_params = $this->baseParams;
95
96 $hca_param = NULL;
97 if( $slug ){
98 $hca_param = $slug;
99
100 // pesist params within the same slug
101 if( $slug == $this->slug && ($params !== NULL) ){
102 $params = array_merge( $this->params, $params );
103 }
104 }
105
106 // pesist params within the same slug
107 if( (NULL == $slug) && (! $this->slug) ){
108 $params = array_merge( $this->params, $params );
109 }
110
111 if( $params ){
112 $params_string = $this->_buildHcaParams( $params );
113 if( strlen($params_string) ){
114 $hca_param .= $this->_separatorToParams . $params_string;
115 }
116 }
117
118 if( $hca_param ){
119 // add random to avoid unnecessary caching
120 $href_params[$this->_hca] = $hca_param;
121 if( $this->_hcrValue ){
122 $href_params[$this->_hcr] = $this->_hcrValue;
123 }
124 }
125
126 $return = $this->baseUrl;
127 if( $href_params ){
128 $href_params = http_build_query($href_params);
129 $href_params = urldecode( $href_params );
130
131 $glue = (strpos($return, '?') === FALSE) ? '?' : '&';
132 $return .= $glue . $href_params;
133 }
134
135 if( NULL !== $proto ){
136 // echo "PROTO = '$proto'<br>";
137 $starts = array( 'https://', 'http://', '//' );
138 foreach( $starts as $prefix ){
139 if( substr($return, 0, strlen($prefix)) == $prefix ){
140 $return = $proto . substr($return, strlen($prefix));
141 break;
142 }
143 }
144 }
145
146 return $return;
147 }
148
149 public function baseUrl()
150 {
151 $return = $this->baseUrl;
152 return $return;
153 }
154
155 public function fromUrl( $url )
156 {
157 $purl = parse_url( $url );
158
159 $baseUrl = $purl['scheme'] . '://'. $purl['host'];
160 if( isset($purl['port']) && (80 != $purl['port']) ){
161 if( FALSE === strpos($purl['host'], ':') ){
162 $baseUrl .= ':' . $purl['port'];
163 }
164 }
165 $baseUrl .= $purl['path'];
166
167 $this->baseUrl = $baseUrl;
168 $this->baseParams = array();
169 // $this->slug = NULL;
170
171 if( isset($purl['query']) && $purl['query']){
172 parse_str( $purl['query'], $base_params );
173
174 /* grab our hca */
175 if( isset($base_params[$this->_hca]) ){
176 // $this->slug = NULL;
177 $hca = $base_params[$this->_hca];
178
179 // trim slashes
180 $hca = trim($hca, '/');
181
182 $slug = $hca;
183 $params = array();
184
185 if( strpos($slug, $this->_separatorToParams) !== FALSE ){
186 list( $slug, $params ) = explode($this->_separatorToParams, $slug, 2);
187 $params = explode('/', $params);
188 }
189
190 $this->slug = $slug;
191 $this->rawParams = $params;
192 $this->params = $this->_parseHcaParams( $params );
193 }
194
195 /* base params */
196 unset( $base_params[$this->_hca] );
197 $this->baseParams = $base_params;
198 }
199 else {
200 $this->slug = NULL;
201 }
202
203 unset( $this->baseParams[$this->_hcj] );
204
205 return $this;
206 }
207
208 public function isFullUrl( $url )
209 {
210 $ret = false;
211
212 if( is_array($url) ){
213 $url = array_shift($url);
214 }
215
216 if( null === $url ){
217 $url = '';
218 }
219
220 $prfx = array( 'http://', 'https://', '//', 'webcal://' );
221 reset( $prfx );
222 foreach( $prfx as $prf ){
223 if( substr($url, 0, strlen($prf)) == $prf ){
224 $ret = true;
225 return $ret;
226 }
227 }
228
229 if( strpos($url, '.php') !== false ){
230 $ret = true;
231 return $ret;
232 }
233
234 return $ret;
235 }
236
237 public function currentUrl()
238 {
239 $noPort = defined( 'HC3_URI_NO_PORT' ) && HC3_URI_NO_PORT ? true : false;
240
241 $return = 'http';
242 if( isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on') ){
243 $return .= 's';
244 }
245
246 $return .= "://";
247
248 $host = '';
249 if( isset($_SERVER['HTTP_HOST']) && $_SERVER['SERVER_PORT'] != '80'){
250 if( FALSE === strpos($_SERVER['HTTP_HOST'], ':') ){
251 $host .= $_SERVER['HTTP_HOST'] . ':' . $_SERVER['SERVER_PORT'];
252 }
253 else {
254 $host .= $_SERVER['HTTP_HOST'];
255 }
256 }
257 else {
258 $host = $_SERVER['HTTP_HOST'];
259 }
260
261 if( $noPort ){
262 if (is_string($host)) {
263 $pos = strpos( $host, ':' );
264 if( false !== $pos ){
265 $host = substr( $host, 0, $pos );
266 }
267 }
268 }
269
270 $return .= $host;
271
272 if ( ! empty($_SERVER['REQUEST_URI']) ){
273 $return .= $_SERVER['REQUEST_URI'];
274 }
275 else {
276 $return .= $_SERVER['SCRIPT_NAME'];
277 }
278
279 $return = urldecode( $return );
280 return $return;
281 }
282
283 protected function _buildHcaParams( $params )
284 {
285 $return = array();
286
287 foreach( $params as $k => $v ){
288 if( is_array($v) ){
289 if( ! $v ){
290 continue;
291 }
292 $k = $k . $this->_suffixForMulti;
293 $v = HC3_Functions::glueArray( $v );
294 // $v = join($this->_separatorForMulti, $v);
295 }
296 else {
297 if( $v === NULL ){
298 continue;
299 }
300 }
301 $return[] = $k . '/' . $v;
302 }
303
304 $return = join('/', $return);
305 return $return;
306 }
307
308 protected function _parseHcaParams( $rawParams )
309 {
310 $return = array();
311 for( $ii = 0; $ii < count($rawParams); $ii = $ii + 2 ){
312 if( ! isset($rawParams[$ii + 1]) ){
313 break;
314 }
315
316 $key = $rawParams[$ii];
317 $value = $rawParams[$ii + 1];
318
319 $needArray = (substr($key, -strlen($this->_suffixForMulti)) == $this->_suffixForMulti) ? TRUE : FALSE;
320 if( $needArray ){
321 $key = substr($key, 0, -strlen($this->_suffixForMulti));
322 $value = HC3_Functions::unglueArray( $value );
323 // $value = explode($this->_separatorForMulti, $value);
324 }
325
326 if( array_key_exists($key, $return) && $needArray ){
327 $return[$key] = array_merge( $return[$key], $value );
328 }
329 else {
330 $return[$key] = $value;
331 }
332 }
333
334 return $return;
335 }
336 }
337