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