From fb2af1669d7e6086c596c0daa2c68ccaa76c8101 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=86var=20Arnfj=C3=B6r=C3=B0=20Bjarmason?= Date: Tue, 3 Jan 2006 02:59:05 +0000 Subject: [PATCH] * ctype_* compatability functions --- includes/GlobalFunctions.php | 4 + includes/compatability/ctype.php | 222 +++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 includes/compatability/ctype.php diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 319f1127c7..a19c121cd9 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -107,6 +107,10 @@ if ( !function_exists( 'array_diff_key' ) ) { } } +// If it doesn't exist no ctype_* stuff will +if ( ! function_exists( 'ctype_alnum' ) ) + require_once 'compatability/ctype.php'; + /** * Where as we got a random seed diff --git a/includes/compatability/ctype.php b/includes/compatability/ctype.php new file mode 100644 index 0000000000..fc5e0411b4 --- /dev/null +++ b/includes/compatability/ctype.php @@ -0,0 +1,222 @@ + + * @copyright Copyright © 2006, Ævar Arnfjörð Bjarmason + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License + * + * @link http://perldoc.perl.org/perlre.html perldoc perlre + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +/**#@+ + * Takes the same input and returns the same values as the equvalent PHP + * function + * + * @param mixed $in + * @return bool + */ +function ctype_alnum() { + $fname = 'ctype_alnum'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:alnum:]]+$~i', $in ); +} + +function ctype_alpha() { + $fname = 'ctype_alpha'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:alpha:]]+$~i', $in ); +} + +function ctype_cntrl() { + $fname = 'ctype_cntrl'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:cntrl:]]+$~', $in ); +} + +function ctype_digit() { + $fname = 'ctype_digit'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:digit:]]+$~', $in ); +} + +function ctype_graph() { + $fname = 'ctype_graph'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:graph:]]+$~', $in ); +} + +function ctype_lower() { + $fname = 'ctype_lower'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:lower:]]+$~', $in ); +} + +function ctype_print() { + $fname = 'ctype_print'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:print:]]+$~', $in ); +} + +function ctype_punct() { + $fname = 'ctype_punct'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:punct:]]+$~', $in ); +} + +function ctype_space() { + $fname = 'ctype_space'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:space:]]+$~', $in ); + +} + + +function ctype_upper() { + $fname = 'ctype_upper'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:upper:]]+$~', $in ); + +} + +function ctype_xdigit() { + $fname = 'ctype_xdigit'; + + $args = func_get_args(); + list( $in, $ret ) = _ctype_parse_args( $fname, $args ); + + if ( ! is_null( $ret ) ) + return $ret; + else + return (bool)preg_match( '~^[[:xdigit:]]+$~i', $in ); +} + + + + +function _ctype_parse_args( $fname, $args ) { + $ret = null; + + $cnt = count( $args ); + + if ( $cnt !== 1 ) + // PHP only throws E_ERROR on this, but fuck it;) + wfDebugDieBacktrace( "Error: $fname() expects exactly 1 parameter $cnt given" ); + + $in = array_pop( $args ); + + if ( is_int( $in ) ) { + if ( $in >= 256 ) + // >= 256 returns false, except in the case of these functions + return array( + null, + $fname === 'ctype_alnum' || + $fname === 'ctype_digit' || + $fname === 'ctype_graph' || + $fname === 'ctype_print' || + $fname === 'ctype_xdigit' + ); + else if ( $in >= 0 ) + $in = chr( $in ); + else if ( $in >= -128 ) + $in = ord( $in + 256 ); + else if ( $in < -128 ) + // <-128 values return false, except in the case of these functions + return + array( + null, + $fname === 'ctype_graph' || $fname === 'ctype_print' + ); + } + + if ( is_string( $in ) ) + if ( $in === '' ) + return array( null, true ); + else + return array( $in, null ); + else + // Like PHP + return array( null, false ); +} -- 2.20.1