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