|
From: <mxb...@li...> - 2005-03-17 00:07:55
|
Update of /cvsroot/mxbb/core/includes In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv31490 Modified Files: mx_functions.php Log Message: Created class mx_request_vars in mx_functions.php Index: mx_functions.php =================================================================== RCS file: /cvsroot/mxbb/core/includes/mx_functions.php,v retrieving revision 1.26 retrieving revision 1.27 diff -C2 -d -r1.26 -r1.27 *** mx_functions.php 16 Mar 2005 00:16:27 -0000 1.26 --- mx_functions.php 17 Mar 2005 00:07:44 -0000 1.27 *************** *** 46,49 **** --- 46,54 ---- */ + /** + * Included classes in this file: + * - mx_request_vars + */ + // ****************************************************************** // ****************************************************************** *************** *** 1290,1292 **** --- 1295,1453 ---- } + + // -------------------------------------------------------------------------------- + // Class: mx_request_vars + // Encapsulate several functions related to GET/POST variables. + // + + // + // Following flags are options for the $type parameter in method _read() + // + define('MX_TYPE_INT' , 1); + define('MX_TYPE_FLOAT' , 2); + define('MX_TYPE_NO_HTML' , 4); + define('MX_TYPE_NO_TAGS' , 8); + define('MX_TYPE_NO_STRIP' , 16); + define('MX_TYPE_NO_SQL' , 32); + define('MX_TYPE_POST_VARS' , 64); + define('MX_TYPE_GET_VARS' , 128); + + // + // This class must be instatiated in common.php ;-) + // + // Usage examples: + // + // $mx_request_vars = new mx_request_vars(); + // $mode = $mx_request_vars->post('mode', MX_TYPE_NO_TAGS, ''); + // $page_id = $mx_request_vars->post('page', MX_TYPE_INT, 1); + // + class mx_request_vars + { + // + // Properties + // + + // + // Contructor + // + //function mx_request_vars() + //{ + // Not implemented + //} + + // + // Private Methods (convention: first char in name is underscore) + // + function _read($var, $type = 0, $dflt = '') + { + global $HTTP_POST_VARS, $HTTP_GET_VARS; + + if( ($type & (MX_TYPE_POST_VARS|MX_TYPE_GET_VARS)) == 0 ) + { + $type |= (MX_TYPE_POST_VARS|MX_TYPE_GET_VARS); + } + if( ($type & MX_TYPE_POST_VARS) && isset($HTTP_POST_VARS[$var]) || + ($type & MX_TYPE_GET_VARS) && isset($HTTP_GET_VARS[$var]) ) + { + $val = ( ($type & MX_TYPE_POST_VARS) && isset($HTTP_POST_VARS[$var]) ? $HTTP_POST_VARS[$var] : $HTTP_GET_VARS[$var] ); + if( !($type & MX_TYPE_NO_STRIP) ) + { + if( is_array($val) ) + { + foreach( $val as $k => $v ) + { + $val[$k] = trim(stripslashes($v)); + } + } + else + { + $val = trim(stripslashes($val)); + } + } + } + else + { + $val = $dflt; + } + if( $type & MX_TYPE_INT ) // integer + { + return intval($val); + } + if( $type & MX_TYPE_FLOAT ) // float + { + return floatval($val); + } + if( $type & MX_TYPE_NO_HTML ) // no slashes nor html + { + if( is_array($val) ) + { + foreach( $val as $k => $v ) + { + $val[$k] = htmlspecialchars(ltrim(rtrim($v, " \t\n\r\0\x0B\\"))); + } + } + else + { + $val = htmlspecialchars(ltrim(rtrim($val, " \t\n\r\0\x0B\\"))); + } + } + elseif( $type & MX_TYPE_NO_TAGS ) // ie username + { + if( is_array($val) ) + { + foreach( $val as $k => $v ) + { + $val[$k] = htmlspecialchars(strip_tags(ltrim(rtrim($v, " \t\n\r\0\x0B\\")))); + } + } + else + { + $val = htmlspecialchars(strip_tags(ltrim(rtrim($val, " \t\n\r\0\x0B\\")))); + } + } + if( !($type & MX_TYPE_NO_SQL) ) + { + if( is_array($val) ) + { + foreach( $val as $k => $v ) + { + $val[$k] = str_replace(($type & MX_TYPE_NO_STRIP ? "\'" : "'"), "''", $v); + } + } + else + { + $val = str_replace(($type & MX_TYPE_NO_STRIP ? "\'" : "'"), "''", $val); + } + } + return $val; + } + + // + // Public Methods + // + function post($var, $type = 0, $dflt = '') + { + return _read($var, ($type | MX_TYPE_POST_VARS), $dflt); + } + function get($var, $type = 0, $dflt = '') + { + return _read($var, ($type | MX_TYPE_GET_VARS), $dflt); + } + function is_post($var) + { + global $HTTP_POST_VARS; + return ( isset($HTTP_POST_VARS[$var]) || ( isset($HTTP_POST_VARS[$var.'_x']) && isset($HTTP_POST_VARS[$var.'_y']) ) ); + } + function is_get($var) + { + global $HTTP_GET_VARS; + return ( isset($HTTP_GET_VARS[$var]) ); + } + function is_request($var) + { + return ( is_get($var) || is_post($var) ); + } + + } // class mx_request_vars + ?> \ No newline at end of file |