PluginProbe
ShiftController Employee Shift Scheduling / 4.9.26
ShiftController Employee Shift Scheduling v4.9.26
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.26, at hc3/uri.php

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