| 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 were just like the stupidest |
| 35 |
// thing ever. (<http://php.net/manual/en/security.magicquotes.php>) |
| 36 |
// Happily they were DEPRECATED as of PHP 5.3.0, |
| 37 |
// and REMOVED as of PHP 5.4.0, so we don't need |
| 38 |
// to worry about them anymore. |
| 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 |
/** |
| 84 |
* MyPHP::request: Makes sure that we strip all slashes and double slashes |
| 85 |
* we happen to encounter on our strings. |
| 86 |
* |
| 87 |
* The function is recursive and gets as deep as it must. |
| 88 |
* |
| 89 |
* @param array|object|string $value Item to de-slash. |
| 90 |
* |
| 91 |
* @return array|object|string De-slashed item. |
| 92 |
*/ |
| 93 |
static function stripslashes_deep( $value ) { |
| 94 |
if ( is_array( $value ) ) { |
| 95 |
$value = array_map( array( __CLASS__, 'stripslashes_deep' ), $value ); |
| 96 |
} elseif ( is_object( $value ) ) { |
| 97 |
$vars = get_object_vars( $value ); |
| 98 |
foreach ( $vars as $key => $data ) { |
| 99 |
$value->{$key} = stripslashes_deep( $data ); |
| 100 |
} |
| 101 |
} else { |
| 102 |
$value = stripslashes( $value ); |
| 103 |
} |
| 104 |
return $value; |
| 105 |
} /* MyPHP::stripslashes_deep () */ |
| 106 |
|
| 107 |
/** |
| 108 |
* Constructs an URL with the list of passed parameters. |
| 109 |
* |
| 110 |
* @param string $url Base URL. |
| 111 |
* @param Array $params Variable parameters. |
| 112 |
* |
| 113 |
* @return string |
| 114 |
*/ |
| 115 |
static function url( $url, $params = array() ) { |
| 116 |
$sep = '?'; |
| 117 |
if ( strpos( $url, '?' ) !== false ) : |
| 118 |
$sep = '&'; |
| 119 |
endif; |
| 120 |
$url .= $sep . self::to_http_post( $params ); |
| 121 |
|
| 122 |
return $url; |
| 123 |
} /* MyPHP::url () */ |
| 124 |
|
| 125 |
/** |
| 126 |
* MyPHP::to_http_post () -- convert a hash table or object into |
| 127 |
* an application/x-www-form-urlencoded query string |
| 128 |
* |
| 129 |
* @param mixed $params Hash table/object to convert. |
| 130 |
* |
| 131 |
* @return string The application/x-www-form-urlencoded query string. |
| 132 |
*/ |
| 133 |
static function to_http_post( $params ) { |
| 134 |
$getQuery = array(); |
| 135 |
foreach ( $params as $key => $value ) : |
| 136 |
if ( is_scalar( $value ) ) : |
| 137 |
$getQuery[] = urlencode( $key ) . '=' . urlencode( $value ); |
| 138 |
else : |
| 139 |
// Make some attempt to deal with array and |
| 140 |
// object members. Really we should descend |
| 141 |
// recursively to get the whole thing.... |
| 142 |
foreach ((array) $value as $k => $v ) : |
| 143 |
$getQuery[] = urlencode( $key . '[' . $k . ']' ) . '=' . urlencode( $v ); |
| 144 |
endforeach; |
| 145 |
endif; |
| 146 |
endforeach; |
| 147 |
return implode( "&", $getQuery ); |
| 148 |
} /* MyPHP::to_http_post () */ |
| 149 |
|
| 150 |
/** |
| 151 |
* MyPHP::val(): Captures the output of var_dump() to a string, |
| 152 |
* with some optional filtering removing newlines and replacing |
| 153 |
* them with spaces) applied. |
| 154 |
* |
| 155 |
* @note As far as _I_ understand regexes, this replaces _multiple_ |
| 156 |
* consecutive whitespace elements with a single space — which is |
| 157 |
* not quite what the description says! However, since the result of |
| 158 |
* var_dump() has a reasonably predictable output, so this _may_ work |
| 159 |
* as intended...(gwyneth 20230916) |
| 160 |
* |
| 161 |
* @param mixed $v Value to dump to a string representation. |
| 162 |
* @param bool $no_newlines Whether to filter out newline chars. |
| 163 |
* |
| 164 |
* @return string Captured & cleaned-up output of var_dump(). |
| 165 |
*/ |
| 166 |
static function val( $v, $no_newlines = false ) { |
| 167 |
ob_start(); |
| 168 |
var_dump( $v ); |
| 169 |
$out = ob_get_contents(); ob_end_clean(); |
| 170 |
if ( $no_newlines ) : |
| 171 |
$out = preg_replace( '/\s+/', " ", $out ); |
| 172 |
endif; |
| 173 |
return $out; |
| 174 |
} /* MyPHP::val () */ |
| 175 |
} /* class MyPHP */ |
| 176 |
endif; |
| 177 |
|