| 1 |
<?php |
| 2 |
if (!class_exists('MyPHP')) : |
| 3 |
class MyPHP { |
| 4 |
// For dealing with HTTP GET/POST parameters sent to us as input |
| 5 |
static public function param ($key, $default = NULL, $type = 'REQUEST') { |
| 6 |
// PHP 5.4 introduces "just-in-time" initialization of |
| 7 |
// $GLOBALS. Which seems to me largely to defeat the |
| 8 |
// purpose of the $GLOBALS array. But whatever. Force |
| 9 |
// $_GET, $_POST, and $_REQUEST into the array. |
| 10 |
global $_GET, $_POST, $_REQUEST; |
| 11 |
|
| 12 |
$where = '_'.strtoupper($type); |
| 13 |
$ret = $default; |
| 14 |
if (isset($GLOBALS[$where]) and is_array($GLOBALS[$where])) : |
| 15 |
if (isset($GLOBALS[$where][$key])) : |
| 16 |
$ret = $GLOBALS[$where][$key]; |
| 17 |
|
| 18 |
// Magic quotes are like the stupidest |
| 19 |
// thing ever. |
| 20 |
if (get_magic_quotes_gpc()) : |
| 21 |
$ret = self::stripslashes_deep($ret); |
| 22 |
endif; |
| 23 |
endif; |
| 24 |
endif; |
| 25 |
|
| 26 |
return $ret; |
| 27 |
} /* MyPHP::param () */ |
| 28 |
|
| 29 |
static function post ($key, $default = NULL) { |
| 30 |
return self::param($key, $default, 'POST'); |
| 31 |
} /*MyPHP::post () */ |
| 32 |
|
| 33 |
static function get ($key, $default = NULL) { |
| 34 |
return self::param($key, $default, "GET"); |
| 35 |
} /* MyPHP::get () */ |
| 36 |
|
| 37 |
static function request ($key, $default = NULL) { |
| 38 |
return self::param($key, $default, "REQUEST"); |
| 39 |
} /* MyPHP::request () */ |
| 40 |
|
| 41 |
static function stripslashes_deep ($value) { |
| 42 |
if ( is_array($value) ) { |
| 43 |
$value = array_map(array(__CLASS__, 'stripslashes_deep'), $value); |
| 44 |
} elseif ( is_object($value) ) { |
| 45 |
$vars = get_object_vars( $value ); |
| 46 |
foreach ($vars as $key=>$data) { |
| 47 |
$value->{$key} = stripslashes_deep( $data ); |
| 48 |
} |
| 49 |
} else { |
| 50 |
$value = stripslashes($value); |
| 51 |
} |
| 52 |
return $value; |
| 53 |
} /* MyPHP::stripslashes_deep () */ |
| 54 |
|
| 55 |
static function url ($url, $params = array()) { |
| 56 |
$sep = '?'; |
| 57 |
if (strpos($url, '?') !== false) : |
| 58 |
$sep = '&'; |
| 59 |
endif; |
| 60 |
$url .= $sep . self::to_http_post($params); |
| 61 |
|
| 62 |
return $url; |
| 63 |
} /* MyPHP::url () */ |
| 64 |
|
| 65 |
/** |
| 66 |
* MyPHP::to_http_post () -- convert a hash table or object into |
| 67 |
* an application/x-www-form-urlencoded query string |
| 68 |
*/ |
| 69 |
static function to_http_post ($params) { |
| 70 |
$getQuery = array(); |
| 71 |
foreach ($params as $key => $value) : |
| 72 |
if (is_scalar($value)) : |
| 73 |
$getQuery[] = urlencode($key) . '=' . urlencode($value); |
| 74 |
else : |
| 75 |
// Make some attempt to deal with array and |
| 76 |
// object members. Really we should descend |
| 77 |
// recursively to get the whole thing.... |
| 78 |
foreach ((array) $value as $k => $v) : |
| 79 |
$getQuery[] = urlencode($key.'['.$k.']') . '=' . urlencode($v); |
| 80 |
endforeach; |
| 81 |
endif; |
| 82 |
endforeach; |
| 83 |
return implode("&", $getQuery); |
| 84 |
} /* MyPHP::to_http_post () */ |
| 85 |
} |
| 86 |
endif; |
| 87 |
|
| 88 |
|
| 89 |
|