| 1 |
<?php |
| 2 |
/** |
| 3 |
* MyPHP: handy general utility functions, for things that PHP should do, but |
| 4 |
* doesn't do (yet). |
| 5 |
* |
| 6 |
* @package MyPHP |
| 7 |
* @version 2014.0805 |
| 8 |
*/ |
| 9 |
if (!class_exists('MyPHP')) : |
| 10 |
class MyPHP { |
| 11 |
/** |
| 12 |
* MyPHP::param: For dealing with HTTP GET/POST parameters sent |
| 13 |
* to us as input. |
| 14 |
* |
| 15 |
* @param string $key The name of the GET/POST parameter |
| 16 |
* @param mixed $default Default to return if parameter is unset |
| 17 |
* @param string $type 'GET', 'POST' or 'REQUEST' (=both GET and POST) |
| 18 |
* @return mixed The value of the named parameter, or the fallback |
| 19 |
* in $default if there is no value set for that param name. |
| 20 |
*/ |
| 21 |
static public function param ($key, $default = NULL, $type = 'REQUEST') { |
| 22 |
// PHP 5.4 introduces "just-in-time" initialization of |
| 23 |
// $GLOBALS. Which seems to me largely to defeat the |
| 24 |
// purpose of the $GLOBALS array. But whatever. Force |
| 25 |
// $_GET, $_POST, and $_REQUEST into the array. |
| 26 |
global $_GET, $_POST, $_REQUEST; |
| 27 |
|
| 28 |
$where = '_'.strtoupper($type); |
| 29 |
$ret = $default; |
| 30 |
if (isset($GLOBALS[$where]) and is_array($GLOBALS[$where])) : |
| 31 |
if (isset($GLOBALS[$where][$key])) : |
| 32 |
$ret = $GLOBALS[$where][$key]; |
| 33 |
|
| 34 |
// Magic quotes are like the stupidest |
| 35 |
// thing ever. |
| 36 |
if (get_magic_quotes_gpc()) : |
| 37 |
$ret = self::stripslashes_deep($ret); |
| 38 |
endif; |
| 39 |
endif; |
| 40 |
endif; |
| 41 |
|
| 42 |
return $ret; |
| 43 |
} /* MyPHP::param () */ |
| 44 |
|
| 45 |
/** |
| 46 |
* MyPHP::post: For dealing with HTTP POST parameters sent as input |
| 47 |
* |
| 48 |
* @param string $key The name of the POST parameter |
| 49 |
* @param mixed $default Default to return if parameter is unset |
| 50 |
* @return mixed The value of the named parameter, or the fallback |
| 51 |
* in $default if there is no value set for that param name. |
| 52 |
*/ |
| 53 |
static function post ($key, $default = NULL) { |
| 54 |
return self::param($key, $default, 'POST'); |
| 55 |
} /*MyPHP::post () */ |
| 56 |
|
| 57 |
/** |
| 58 |
* MyPHP::post: For dealing with HTTP GET parameters sent as input |
| 59 |
* |
| 60 |
* @param string $key The name of the GET parameter |
| 61 |
* @param mixed $default Default to return if parameter is unset |
| 62 |
* @return mixed The value of the named parameter, or the fallback |
| 63 |
* in $default if there is no value set for that param name. |
| 64 |
*/ |
| 65 |
static function get ($key, $default = NULL) { |
| 66 |
return self::param($key, $default, "GET"); |
| 67 |
} /* MyPHP::get () */ |
| 68 |
|
| 69 |
/** |
| 70 |
* MyPHP::request: For dealing with HTTP GET/POST parameters |
| 71 |
* sent as input. This method checks both GET parameters and POST |
| 72 |
* parameters and will return values from either. |
| 73 |
* |
| 74 |
* @param string $key The name of the GET/POST parameter |
| 75 |
* @param mixed $default Default to return if parameter is unset |
| 76 |
* @return mixed The value of the named parameter, or the fallback |
| 77 |
* in $default if there is no value set for that param name. |
| 78 |
*/ |
| 79 |
static function request ($key, $default = NULL) { |
| 80 |
return self::param($key, $default, "REQUEST"); |
| 81 |
} /* MyPHP::request () */ |
| 82 |
|
| 83 |
static function stripslashes_deep ($value) { |
| 84 |
if ( is_array($value) ) { |
| 85 |
$value = array_map(array(__CLASS__, 'stripslashes_deep'), $value); |
| 86 |
} elseif ( is_object($value) ) { |
| 87 |
$vars = get_object_vars( $value ); |
| 88 |
foreach ($vars as $key=>$data) { |
| 89 |
$value->{$key} = stripslashes_deep( $data ); |
| 90 |
} |
| 91 |
} else { |
| 92 |
$value = stripslashes($value); |
| 93 |
} |
| 94 |
return $value; |
| 95 |
} /* MyPHP::stripslashes_deep () */ |
| 96 |
|
| 97 |
static function url ($url, $params = array()) { |
| 98 |
$sep = '?'; |
| 99 |
if (strpos($url, '?') !== false) : |
| 100 |
$sep = '&'; |
| 101 |
endif; |
| 102 |
$url .= $sep . self::to_http_post($params); |
| 103 |
|
| 104 |
return $url; |
| 105 |
} /* MyPHP::url () */ |
| 106 |
|
| 107 |
/** |
| 108 |
* MyPHP::to_http_post () -- convert a hash table or object into |
| 109 |
* an application/x-www-form-urlencoded query string |
| 110 |
*/ |
| 111 |
static function to_http_post ($params) { |
| 112 |
$getQuery = array(); |
| 113 |
foreach ($params as $key => $value) : |
| 114 |
if (is_scalar($value)) : |
| 115 |
$getQuery[] = urlencode($key) . '=' . urlencode($value); |
| 116 |
else : |
| 117 |
// Make some attempt to deal with array and |
| 118 |
// object members. Really we should descend |
| 119 |
// recursively to get the whole thing.... |
| 120 |
foreach ((array) $value as $k => $v) : |
| 121 |
$getQuery[] = urlencode($key.'['.$k.']') . '=' . urlencode($v); |
| 122 |
endforeach; |
| 123 |
endif; |
| 124 |
endforeach; |
| 125 |
return implode("&", $getQuery); |
| 126 |
} /* MyPHP::to_http_post () */ |
| 127 |
|
| 128 |
/** |
| 129 |
* MyPHP::val(): Captures the output of var_dump() to a string, |
| 130 |
* with some optional filtering removing newlines and replacing |
| 131 |
* them with spaces) applied. |
| 132 |
* |
| 133 |
* @param mixed $v Value to dump to a string representation |
| 134 |
* @param bool $no_newlines Whether to filter out newline chars |
| 135 |
* |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
static function val ($v, $no_newlines = false) { |
| 139 |
ob_start(); |
| 140 |
var_dump($v); |
| 141 |
$out = ob_get_contents(); ob_end_clean(); |
| 142 |
|
| 143 |
if ($no_newlines) : |
| 144 |
$out = preg_replace('/\s+/', " ", $out); |
| 145 |
endif; |
| 146 |
return $out; |
| 147 |
} /* MyPHP::val () */ |
| 148 |
} /* class MyPHP */ |
| 149 |
endif; |
| 150 |
|