1: <?php
2: /*
3: * Simpletools Framework.
4: * Copyright (c) 2009, Marcin Rosinski. (https://www.getsimpletools.com/)
5: * All rights reserved.
6: *
7: * LICENCE
8: *
9: * Redistribution and use in source and binary forms, with or without modification,
10: * are permitted provided that the following conditions are met:
11: *
12: * - Redistributions of source code must retain the above copyright notice,
13: * this list of conditions and the following disclaimer.
14: *
15: * - Redistributions in binary form must reproduce the above copyright notice,
16: * this list of conditions and the following disclaimer in the documentation and/or other
17: * materials provided with the distribution.
18: *
19: * - Neither the name of the Simpletools nor the names of its contributors may be used to
20: * endorse or promote products derived from this software without specific prior written permission.
21: *
22: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
23: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
28: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29: * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30: *
31: * @framework Simpletools
32: * @description MVC framework
33: * @copyright Copyright (c) 2009 Marcin Rosinski. (https://www.getsimpletools.com/)
34: * @license (BSD)
35: *
36: */
37:
38: namespace Simpletools\Mvc;
39:
40: /**
41: * MVC Common Methods
42: */
43: class Common
44: {
45: /**
46: * Action name normaliser
47: *
48: * @param string $action Action name to normalise
49: * @return string Normalised action name
50: */
51: public static function getCorrectActionName($action)
52: {
53: $action = strtolower($action);
54: $action = str_replace(array('-','.'),' ',$action);
55: $action = str_replace(' ','',ucwords($action));
56:
57: //$action = (string)(strtolower(substr($action,0,1)).substr($action,1));
58: return lcfirst($action);
59: }
60:
61: /**
62: * Controller name normaliser
63: *
64: * @param string $controller Controller name to normalise
65: * @return string Normalised controller name
66: */
67: public static function getCorrectControllerName($controller)
68: {
69: $controller = strtolower($controller);
70: $controller = str_replace(array('-','.'),' ',$controller);
71: $controller = ucwords($controller);
72: $controller = str_replace(' ','',$controller);
73:
74: return $controller;
75: }
76:
77: /**
78: * HTTP POST checker
79: *
80: * @param string $id POST key to check
81: * @param int $filter Filter type - http://php.net/manual/en/filter.filters.sanitize.php
82: * @param array $filterOptions Filter options
83: * @return mixed boolean if no filter is set
84: */
85: public function isPost($id=false,$filter=null,$filterOptions=array())
86: {
87: if(!$id)
88: {
89: return (isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD']) == 'POST') ? true : false;
90: }
91: else
92: {
93: if(isset($_POST[$id]))
94: {
95: return $filter ? filter_var($_POST[$id],$filter,$filterOptions) : true;
96: }
97: else
98: {
99: return false;
100: }
101: }
102: }
103:
104: /**
105: * HTTP GET checker
106: *
107: * @param string $id GET key to check
108: * @param int $filter Filter type - http://php.net/manual/en/filter.filters.sanitize.php
109: * @param array $filterOptions Filter options
110: * @return mixed boolean if no filter is set
111: */
112: public function isQuery($id=false,$filter=null,$filterOptions=array())
113: {
114: if(!$id)
115: {
116: return (isset($_SERVER['REQUEST_METHOD']) && strtoupper($_SERVER['REQUEST_METHOD']) == 'GET') ? true : false;
117: }
118: else
119: {
120: if(isset($_GET[$id]))
121: {
122: return $filter ? filter_var($_GET[$id],$filter,$filterOptions) : true;
123: }
124: else
125: {
126: return false;
127: }
128: }
129: }
130:
131: /**
132: * HTTP REQUEST checker
133: *
134: * @param string $id REQUEST key to check
135: * @param int $filter Filter type - http://php.net/manual/en/filter.filters.sanitize.php
136: * @param array $filterOptions Filter options
137: * @return mixed boolean if no filter is set
138: */
139: public function isRequest($id=false,$filter=null,$filterOptions=array())
140: {
141: if(!$id)
142: {
143: return (isset($_SERVER['REQUEST_METHOD']) && (strtoupper($_SERVER['REQUEST_METHOD']) == 'GET' OR strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')) ? true : false;
144: }
145: else
146: {
147: if(isset($_REQUEST[$id]))
148: {
149: return $filter ? filter_var($_REQUEST[$id],$filter,$filterOptions) : true;
150: }
151: else
152: {
153: return false;
154: }
155: }
156: }
157:
158: /**
159: * HTTP GET getter
160: *
161: * @param string $id GET key to return
162: * @param int $sanitizeFilter Sanitize Filter type - http://php.net/manual/en/filter.filters.sanitize.php, defaults to FILTER_SANITIZE_STRING
163: * @param array $sanitizeFilterOptions Sanitize Filter options
164: * @return mixed GET value or values if $id is not provided
165: */
166: public function getQuery($id=null,$sanitizeFilter=FILTER_SANITIZE_STRING,$sanitizeFilterOptions=array('flags'=>FILTER_FLAG_STRIP_HIGH))
167: {
168: if($id==null) return $_GET;
169:
170: if(isset($_GET[$id]))
171: {
172: if(is_array($_GET[$id]))
173: {
174: $RETURN = array();
175: foreach($_GET[$id] as $index => $value)
176: {
177: $RETURN[$index] = filter_var($value,$sanitizeFilter,$sanitizeFilterOptions);
178: }
179:
180: return $RETURN;
181: }
182: else
183: {
184: return filter_var($_GET[$id],$sanitizeFilter,$sanitizeFilterOptions);
185: }
186: }
187: else
188: {
189: return null;
190: }
191: }
192:
193: /**
194: * HTTP POST getter
195: *
196: * @param string $id POST key to return
197: * @param int $sanitizeFilter Sanitize Filter type - http://php.net/manual/en/filter.filters.sanitize.php, defaults to FILTER_SANITIZE_STRING
198: * @param array $sanitizeFilterOptions Sanitize Filter options
199: * @return mixed POST value or values if $id is not provided
200: */
201: public function getPost($id=null,$sanitizeFilter=FILTER_SANITIZE_STRING,$sanitizeFilterOptions=array('flags'=>FILTER_FLAG_STRIP_HIGH))
202: {
203: if($id==null) return $_POST;
204:
205: if(isset($_POST[$id]))
206: {
207: if(is_array($_POST[$id]))
208: {
209: $RETURN = array();
210: foreach($_POST[$id] as $index => $value)
211: {
212: $RETURN[$index] = filter_var($value,$sanitizeFilter,$sanitizeFilterOptions);
213: }
214:
215: return $RETURN;
216: }
217: else
218: {
219: return filter_var($_POST[$id],$sanitizeFilter,$sanitizeFilterOptions);
220: }
221: }
222: else
223: {
224: return null;
225: }
226: }
227:
228: /**
229: * HTTP REQUEST getter
230: *
231: * @param string $id REQUEST key to return
232: * @param int $sanitizeFilter Sanitize Filter type - http://php.net/manual/en/filter.filters.sanitize.php, defaults to FILTER_SANITIZE_STRING
233: * @param array $sanitizeFilterOptions Sanitize Filter options
234: * @return mixed REQUEST value or values if $id is not provided
235: */
236: public function getRequest($id=null,$sanitizeFilter=FILTER_SANITIZE_STRING,$sanitizeFilterOptions=array('flags'=>FILTER_FLAG_STRIP_HIGH))
237: {
238: if($id==null) return $_REQUEST;
239:
240: if(isset($_REQUEST[$id]))
241: {
242: if(is_array($_REQUEST[$id]))
243: {
244: $RETURN = array();
245: foreach($_REQUEST[$id] as $index => $value)
246: {
247: $RETURN[$index] = filter_var($value,$sanitizeFilter,$sanitizeFilterOptions);
248: }
249:
250: return $RETURN;
251: }
252: else
253: {
254: return filter_var($_REQUEST[$id],$sanitizeFilter,$sanitizeFilterOptions);
255: }
256: }
257: else
258: {
259: return null;
260: }
261: }
262:
263: /**
264: * Returns all params by type
265: *
266: * @param string $type Type of the params to return, accepted values - associative or number
267: * @return array Returns all params a per specified type
268: */
269: public function returnParams($type)
270: {
271: return isset($this->_params[$type]) ? $this->_params[$type] : array();
272: }
273:
274: public function setParams(&$params,&$shifted_params)
275: {
276: $this->_params = &$params;
277: $this->_shifted_params = &$shifted_params;
278: }
279:
280: /**
281: * Is URL Param by key name
282: *
283: * @param string $key Name of the requested param
284: * @return boolean Returns true or false
285: *
286: */
287: public function isParam($key)
288: {
289: return isset($this->_params['associative'][$key]) ? true : false;
290: }
291:
292: /**
293: * Get URL Param by key name
294: *
295: * @param string $key Name of the requested param
296: * @return mixed Returns requested param value or all params if key is not specified or null if key doesn't exist
297: *
298: */
299: public function getParam($key='')
300: {
301: if(!$key) return $this->_params['associative'];
302:
303: return isset($this->_params['associative'][$key]) ? (string) $this->_params['associative'][$key] : null;
304: }
305:
306: /**
307: * Get URL Param by index
308: *
309: * @param int $index Position of the param starting from 0
310: * @return string Returns requested param value
311: *
312: * @deprecated 2.0.15 Use getParamByIndex() instead
313: */
314: public function getParam_($index)
315: {
316: return $this->getParamByIndex($index);
317: }
318:
319: /**
320: * Get URL Param by index
321: *
322: * @param int $index Position of the param starting from 0
323: * @return string Returns requested param value
324: *
325: */
326: public function getParamByIndex($index)
327: {
328: return isset($this->_params['number'][$index]) ? (string) $this->_params['number'][$index] : null;
329: }
330:
331: /**
332: * Is Param by index
333: *
334: * @param int $index Position of the param starting from 0
335: * @return boolean Returns true or false
336: *
337: * @deprecated 2.0.15 Use isParamByIndex() instead
338: */
339: public function isParam_($index)
340: {
341: return $this->isParamByIndex($index);
342: }
343:
344: /**
345: * Is Param by index
346: *
347: * @param int $index Position of the param starting from 0
348: * @return boolean Returns true or false
349: *
350: */
351: public function isParamByIndex($index)
352: {
353: return isset($this->_params['number'][$index]) ? true : false;
354: }
355:
356: public function location($location,$type=302)
357: {
358: $this->redirect($location,false,false,$type);
359: }
360:
361: public function redirect($params, $addslash=false, $protocol=false, $type=301, $HTTP_GET='')
362: {
363: $url = null;
364: $https = isset($_SERVER['HTTPS']) ? true : false;
365:
366: if(is_array($params))
367: {
368: if($protocol)
369: {
370: $url = $protocol.'://'.$_SERVER['SERVER_NAME'];
371: }
372: else
373: {
374: if(!$https)
375: $url = 'http://'.$_SERVER['SERVER_NAME'];
376: else
377: $url = 'https://'.$_SERVER['SERVER_NAME'];
378: }
379:
380: $counter = 0;
381:
382: if(count($this->_shifts_params) > 0)
383: {
384: foreach($this->_shifts_params as $p)
385: {
386: $url .= '/'.$p;
387: }
388: }
389:
390: if(isset($params['controller']))
391: {
392: foreach($params as $key => $value)
393: {
394: if($key == 'controller' || $key == 'action')
395: {
396: $url .= '/'.$value;
397: }
398: else
399: {
400: $url .= '/'.$key.'/'.$value;
401: }
402: }
403: }
404: else
405: {
406: foreach($params as $value)
407: {
408: $url .= '/'.$value;
409: }
410: }
411:
412: $url = rtrim($url,'/');
413: if($addslash) $url .= '/';
414:
415: if($HTTP_GET)
416: $url .= '?'.$HTTP_GET;
417:
418: header('Location: '.$url,true,$type);
419: exit;
420: }
421: else
422: {
423: if(stripos($params,'://') === false)
424: {
425: if(substr($params,0,1) == '/')
426: {
427: if(!$https && !$protocol)
428: $url = 'http://'.$_SERVER['SERVER_NAME'].$params;
429: else if($https && !$protocol)
430: $url = 'https://'.$_SERVER['SERVER_NAME'].$params;
431: else
432: $url = $protocol.'://'.$_SERVER['SERVER_NAME'].$params;
433: }
434: else
435: {
436: $uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
437: $uri .= '/';
438:
439: if(!$https && !$protocol)
440: $url = 'http://'.$_SERVER['SERVER_NAME'].$uri.$params;
441: else if($https && !$protocol)
442: $url = 'https://'.$_SERVER['SERVER_NAME'].$uri.$params;
443: else
444: $url = $protocol.'://'.$_SERVER['SERVER_NAME'].$uri.$params;
445: }
446: }
447: else
448: {
449: $url = $params;
450: }
451:
452: if($addslash) $url .= '/';
453:
454: if($HTTP_GET)
455: $url .= '?'.$HTTP_GET;
456:
457: header('Location: '.$url,true,$type);
458: exit;
459: }
460: }
461: }
462:
463: ?>