PageRenderTime 38ms CodeModel.GetById 32ms app.highlight 4ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/core/functions/_params.funcs.php

https://github.com/balupton/balphp
PHP | 312 lines | 150 code | 48 blank | 114 comment | 31 complexity | 7c127094511cfcfa534022b624cb285a MD5 | raw file
  1<?php
  2/**
  3 * Balupton's Resource Library (balPHP)
  4 * Copyright (C) 2008 Benjamin Arthur Lupton
  5 * http://www.balupton.com/
  6 *
  7 * This file is part of Balupton's Resource Library (balPHP).
  8 *
  9 * You should have received a copy of the GNU Affero General Public License
 10 * along with Balupton's Resource Library (balPHP).  If not, see <http://www.gnu.org/licenses/>.
 11 *
 12 * @package balphp
 13 * @subpackage core
 14 * @version 0.1.1-final, November 11, 2009
 15 * @since 0.1.0-final, April 21, 2008
 16 * @author Benjamin "balupton" Lupton <contact@balupton.com> - {@link http://www.balupton.com/}
 17 * @copyright Copyright (c) 2008, Benjamin Arthur Lupton - {@link http://www.balupton.com/}
 18 * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License
 19 */
 20
 21require_once (dirname(__FILE__).DIRECTORY_SEPARATOR.'_general.funcs.php');
 22require_once (dirname(__FILE__).DIRECTORY_SEPARATOR.'_strings.funcs.php');
 23
 24if ( function_compare('explode_querystring', 1, true, __FILE__, __LINE__) ) {
 25
 26	/**
 27	 * Do something
 28	 *
 29	 * @version 1
 30	 *
 31	 * @todo figure out what the hell this does
 32	 *
 33	 */
 34	function explode_querystring ( $query_string, $amp = '&amp;' ) {
 35		$query_string = explode($amp, $query_string);
 36		$params = array();
 37		for($i = 0, $n = sizeof($query_string); $i < $n; ++$i) {
 38			$param = explode('=', $query_string[$i]);
 39			if ( sizeof($param) === 2 ) {
 40				$key = $param[0];
 41				$value = $param[1];
 42				$params[$key] = $value;
 43			}
 44		}
 45		return $params;
 46	}
 47}
 48
 49if ( function_compare('implode_querystring', 1, true, __FILE__, __LINE__) ) {
 50
 51	/**
 52	 * Do something
 53	 *
 54	 * @version 1.1, June 24, 2010 - added urlencode
 55	 * @since 1
 56	 *
 57	 * @todo figure out what the hell this does
 58	 *
 59	 */
 60	function implode_querystring ( $query_string, $amp = '&amp;' ) {
 61		$params = '';
 62		foreach ( $query_string as $key => $value ) {
 63			$params .= urlencode($key) . '=' . urlencode($value) . $amp;
 64		}
 65		return $params;
 66	}
 67}
 68
 69if ( function_compare('regenerate_params', 2, true, __FILE__, __LINE__) ) {
 70
 71	/**
 72	 * Do something
 73	 *
 74	 * @version 1
 75	 *
 76	 * @todo figure out what the hell this does
 77	 *
 78	 */
 79	function regenerate_params ( $display = 'form', $params = NULL ) {
 80		if ( $params === NULL )
 81			$params = array_merge($_GET, $_POST);
 82		elseif ( gettype($params) === 'string' ) {
 83			$params = explode_querystring($params);
 84		}
 85		
 86		$result = '';
 87		
 88		switch ( $display ) {
 89			case 'form' :
 90				foreach ( $params as $key => $value ) {
 91					$result .= '<input type="hidden" name="' . $key . '" value="' . $value . '"  />';
 92				}
 93				break;
 94			
 95			default :
 96				die('Unknown regenerate params display: ' . $display);
 97				break;
 98		}
 99		
100		return $result;
101	}
102}
103
104if ( function_compare('hydrate_request_init', 1, true, __FILE__, __LINE__) ) {
105
106	/**
107	 * Rebuild and hydrate $_REQUEST
108	 * @version 1, January 06, 2010
109	 */
110	function hydrate_request_init ( $once = true ) {
111		# Init
112		if ( defined('REQUEST_HYDRATED') ) {
113			if ( $once ) return;
114		} else
115			define('REQUEST_HYDRATED', 1);
116	
117		# Prepare
118		$stripslashes = ini_get('magic_quotes_gpc') ? true : false;
119		
120		# Prepare
121		$_POST_HYDRATED		= $_POST;
122		$_GET_HYDRATED		= $_GET;
123		$_FILES_HYDRATED	= array();
124		
125		# Clean
126		array_clean_form($_POST_HYDRATED);
127		array_clean_form($_GET_HYDRATED);
128		array_clean_form($_FILES_HYDRATED);
129		
130		# Hydrate
131		hydrate_value($_POST_HYDRATED,	array('stripslashes'=>$stripslashes));
132		hydrate_value($_GET_HYDRATED,	array('stripslashes'=>$stripslashes));
133		hydrate_value($_FILES_HYDRATED);
134		
135		# Liberate
136		liberate_files($_FILES_HYDRATED);
137		unset_empty_files($_FILES_HYDRATED);
138		
139		# Merge
140		$_REQUEST = array_merge_recursive_keys(false, $_FILES_HYDRATED, $_GET_HYDRATED, $_POST_HYDRATED);
141		
142		# Done
143		return true;
144	}
145}
146
147if ( function_compare('get_param', 2, true, __FILE__, __LINE__) ) {
148
149	/**
150	 * Get a unhydrated param
151	 * @version 3, May 05, 2010
152	 * @since 2
153	 * @param string $name
154	 * @param array $options [optional]
155	 */
156	function get_param ( $name, $default = null, $stripslashes = null) {
157		# Prepare
158		if ( $stripslashes === null )
159			$stripslashes = get_magic_quotes_gpc() ? true : false;
160		
161		# Fetch
162		$value = delve($_POST,$name,delve($_GET,$name,delve($_FILES,$name,$default)));
163		
164		# Stripslashes
165		if ( $stripslashes && is_string($value) ) {
166			$value = stripslashes($value);
167		}
168		
169		# Return value
170		return $value;
171	}
172}
173
174
175if ( function_compare('fetch_param', 1, true, __FILE__, __LINE__) ) {
176
177	/**
178	 * Get a hydrated param
179	 * @version 1, January 06, 2010
180	 */
181	function fetch_param ( $name, $default = null ) {
182		# Prepare
183		hydrate_request_init();
184		
185		# Fetch
186		$value = delve($_REQUEST, $name, $default);
187		
188		# Return value
189		return $value;
190	}
191}
192
193if ( function_compare('has_param', 1, true, __FILE__, __LINE__) ) {
194
195	/**
196	 * Check to see if the param exists
197	 * @version 1, January 06, 2010
198	 */
199	function has_param ( $name ) {
200		# Prepare
201		hydrate_request_init();
202		
203		# Fetch
204		$exists = delve($_REQUEST, $name) !== null;
205		
206		# Return exists
207		return $exists;
208	}
209}
210
211
212if ( function_compare('liberate_subfiles', 1, true, __FILE__, __LINE__) ) {
213
214	/**
215	 * Liberate subfiles
216	 * @version 1, January 06, 2010
217	 */
218	function liberate_subfiles ( &$where, $prefix, $suffix, $subvalue ) {
219		# Prepare
220		$prefix = trim($prefix, '.');
221		$suffix = trim($suffix, '.');
222	
223		# Handle
224		if ( !is_array($subvalue) ) {
225			# We have reached the bottom
226			$name = $prefix.'.'.$suffix;
227			array_apply($where, $name, $subvalue, true); // when setting to false, PHP memory reference error occurs...
228		}
229		else {
230			# More sub files
231			foreach ( $subvalue as $key => $value ) {
232				liberate_subfiles($where, $prefix.'.'.$key, $suffix, $value);
233			}
234		}
235	
236		# Done
237		return true;
238	}
239}
240
241
242if ( function_compare('liberate_files', 1, true, __FILE__, __LINE__) ) {
243
244	/**
245	 * Liberate files
246	 * The purpose of this is when using $_FILE with param arrays, we want to be able to do this $_FILE['user']['avatar']['tmpname'] instead of $_FILE['user']['tmpname']['avatar']
247	 * @version 1, January 06, 2010
248	 */
249	function liberate_files ( &$where ) {
250		# Handle
251		foreach ( $_FILES as $fileKey => $fileValue ) {
252			foreach ( $fileValue as $filePartKey => $filePartValue ) {
253				if ( is_array($filePartValue) ) {
254					# We have a multiple file
255					liberate_subfiles($where, $fileKey, $filePartKey, $filePartValue);
256				}
257			}
258		}
259	}
260}
261
262if ( function_compare('unset_empty_files', 1, true, __FILE__, __LINE__) ) {
263
264	/**
265	 * Unset empty files
266	 * The purpose of this function is to unset empty files (error==4)
267	 * @version 1, February 22, 2010
268	 */
269	function unset_empty_files ( &$files, $current = null, $prefix = '', $last = '' ) {
270		# Prepare
271		$prefix = trim($prefix, '.');
272		if ( $prefix === '' && $current === null ) $current = $files;
273		
274		# Handle
275		if ( is_array($current) ) {
276			# Deeper
277			foreach ( $current as $key => $value ) {
278				if ( is_array($value) ) {
279					unset_empty_files($files, $value, $prefix.'.'.$key, $key);
280				}
281				elseif ( ($key === 'error' && $value === 4) || ($key === 'size' && !$value) ) {
282					array_unapply($files, $prefix);
283				}
284			}
285		}
286		
287		# Done
288		return true;
289	}
290}
291
292if ( function_compare('fix_magic_quotes', 1, true, __FILE__, __LINE__) ) {
293
294	/**
295	 * Fix magic quotes
296	 * @version 2, August 22, 2009
297	 * @since 1, Unknown
298	 * @package BalPHP {@link http://www.balupton/projects/balphp}
299	 * @author Benjamin "balupton" Lupton <contact@balupton.com> - {@link http://www.balupton.com/}
300	 * @copyright Copyright (c) 2008-2010, Benjamin Arthur Lupton - {@link http://www.balupton.com/}
301	 * @license http://www.gnu.org/licenses/agpl.html GNU Affero General Public License
302	 */
303	function fix_magic_quotes ( ) {
304		if ( ini_get('magic_quotes_gpc') ) {
305			$_POST = array_map('stripslashes_deep', $_POST);
306			$_GET = array_map('stripslashes_deep', $_GET);
307			$_COOKIE = array_map('stripslashes_deep', $_COOKIE);
308			$_REQUEST = array_map('stripslashes_deep', $_REQUEST);
309			ini_set('magic_quotes_gpc', 0);
310		}
311	}
312}