", "/", "?"); $lastdot=strrpos($string, "."); $clean = trim(str_replace($strip, "_", strip_tags($string))); $clean = preg_replace('/\s+/', "-", $clean); // remove the leading dot if any, this prevents the creation of hidden files on unix platforms $clean = preg_replace('/^\./', '', $clean); $clean = ($alphanumeric) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ; if ($lastdot !== false) { $clean= substr_replace ( $clean , '.' , $lastdot , 1 ); } return ($force_lowercase) ? (function_exists('mb_strtolower')) ? mb_strtolower($clean, 'UTF-8') : strtolower($clean) : $clean; } /** * Function: sanitize_dirname * sanitizes a string that will be used as a directory name * * Parameters: * $string - The string to sanitize. * $force_lowercase - Force the string to lowercase? * $alphanumeric - If set to *true*, will remove all non-alphanumeric characters. */ function sanitize_dirname($string, $force_lowercase = true, $alphanumeric = false) { $string = str_replace(".", "", $string); return sanitize_filename($string, $force_lowercase, $alphanumeric); } // paranoid sanitization -- only let the alphanumeric set through function sanitize_paranoid_string($string, $min='', $max='') { if (isset($string)) { $string = preg_replace("/[^_.a-zA-Z0-9]/", "", $string); $len = strlen($string); if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max))) return FALSE; return $string; } } function sanitize_cquestions($string, $min='', $max='') { if (isset($string)) { $string = preg_replace("/[^_.a-zA-Z0-9+#]/", "", $string); $len = strlen($string); if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max))) return FALSE; return $string; } } // sanitize a string in prep for passing a single argument to system() (or similar) function sanitize_system_string($string, $min='', $max='') { if (isset($string)) { $pattern = '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'; // no piping, passing possible environment variables ($), // separate commands, nested execution, file redirection, // background processing, special commands (backspace, etc.), quotes // newlines, or some other special characters $string = preg_replace($pattern, '', $string); $string = '"'.preg_replace('/\$/', '\\\$', $string).'"'; //make sure this is only interpretted as ONE argument $len = strlen($string); if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max))) return FALSE; return $string; } } function sanitize_xss_string($string) { if (isset($string)) { $bad = array ('*','^','&',';','\"','(',')','%','$','?'); return str_replace($bad, '',$string); } } // sanitize a string for SQL input (simple slash out quotes and slashes) function sanitize_sql_db_tablename($string) { $bad = array ('*','^','&','\'','-',';','\"','(',')','%','$','?'); return str_replace($bad, "",$string); } // sanitize a string for SQL input (simple slash out quotes and slashes) function sanitize_ldap_string($string, $min='', $max='') { $pattern = '/(\)|\(|\||&)/'; $len = strlen($string); if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max))) return FALSE; return preg_replace($pattern, '', $string); } // sanitize a string for HTML (make sure nothing gets interpretted!) function sanitize_html_string($string) { $pattern[0] = '/\&/'; $pattern[1] = '//"; $pattern[3] = '/\n/'; $pattern[4] = '/"/'; $pattern[5] = "/'/"; $pattern[6] = "/%/"; $pattern[7] = '/\(/'; $pattern[8] = '/\)/'; $pattern[9] = '/\+/'; $pattern[10] = '/-/'; $replacement[0] = '&'; $replacement[1] = '<'; $replacement[2] = '>'; $replacement[3] = '
'; $replacement[4] = '"'; $replacement[5] = '''; $replacement[6] = '%'; $replacement[7] = '('; $replacement[8] = ')'; $replacement[9] = '+'; $replacement[10] = '-'; return preg_replace($pattern, $replacement, $string); } // make int int! function sanitize_int($integer, $min='', $max='') { $int = preg_replace("#[^0-9]#", "", $integer); if((($min != '') && ($int < $min)) || (($max != '') && ($int > $max))) { return FALSE; } if ($int=='') { return null; } return $int; } // sanitize a username // TODO: define the exact format of the username // allow for instance 0-9a-zA-Z@_-. function sanitize_user($string) { $username_length=64; $string=mb_substr($string,0,$username_length); return $string; } // sanitize a username // TODO: define the exact format of the username // allow for instance 0-9a-zA-Z@_-. function sanitize_userfullname($string) { $username_length=50; $string=mb_substr($string,0,$username_length); return $string; } function sanitize_labelname($string) { $labelname_length=100; $string=mb_substr($string,0,$labelname_length); return $string; } // make float float! function sanitize_float($float, $min='', $max='') { $float = str_replace(',','.',$float); // GMP library allows for high precision and high value numbers if (function_exists('gmp_init') && defined('GMP_VERSION') && version_compare(GMP_VERSION,'4.3.2')==1) { $gNumber = gmp_init($float); if(($min != '' && gmp_cmp($gNumber,$min)<0) || ($max != '' && gmp_cmp($gNumber,$max)>0)) { return FALSE; } else { return gmp_strval($gNumber); } } else { $fNumber = str_replace(',','.',$float); $fNumber = floatval($fNumber); if((($min != '') && ($fNumber < $min)) || (($max != '') && ($fNumber > $max))) return FALSE; return $fNumber; } } // glue together all the other functions function sanitize($input, $flags, $min='', $max='') { if($flags & PARANOID) $input = sanitize_paranoid_string($input, $min, $max); if($flags & INT) $input = sanitize_int($input, $min, $max); if($flags & FLOAT) $input = sanitize_float($input, $min, $max); if($flags & HTML) $input = sanitize_html_string($input, $min, $max); if($flags & LDAP) $input = sanitize_ldap_string($input, $min, $max); if($flags & SYSTEM) $input = sanitize_system_string($input, $min, $max); return $input; } function check_paranoid_string($input, $min='', $max='') { if($input != sanitize_paranoid_string($input, $min, $max)) return FALSE; return TRUE; } function check_int($input, $min='', $max='') { if($input != sanitize_int($input, $min, $max)) return FALSE; return TRUE; } function check_float($input, $min='', $max='') { if($input != sanitize_float($input, $min, $max)) return FALSE; return TRUE; } function check_html_string($input, $min='', $max='') { if($input != sanitize_html_string($input, $min, $max)) return FALSE; return TRUE; } function check_ldap_string($input, $min='', $max='') { if($input != sanitize_string($input, $min, $max)) return FALSE; return TRUE; } function check_system_string($input, $min='', $max='') { if($input != sanitize_system_string($input, $min, $max, TRUE)) return FALSE; return TRUE; } // glue together all the other functions function check($input, $flags, $min='', $max='') { $oldput = $input; if($flags & UTF8) $input = my_utf8_decode($input); if($flags & PARANOID) $input = sanitize_paranoid_string($input, $min, $max); if($flags & INT) $input = sanitize_int($input, $min, $max); if($flags & FLOAT) $input = sanitize_float($input, $min, $max); if($flags & HTML) $input = sanitize_html_string($input, $min, $max); if($flags & LDAP) $input = sanitize_ldap_string($input, $min, $max); if($flags & SYSTEM) $input = sanitize_system_string($input, $min, $max, TRUE); if($input != $oldput) return FALSE; return TRUE; } function sanitize_languagecode($codetosanitize) { return preg_replace('/[^a-z0-9-]/i', '', $codetosanitize); } function sanitize_languagecodeS($codestringtosanitize) { $codearray=explode(" ",trim($codestringtosanitize)); $codearray=array_map("sanitize_languagecode",$codearray); return implode(" ",$codearray); } function sanitize_token($codetosanitize) { return preg_replace('/[^_a-z0-9]/i', '', $codetosanitize); } function sanitize_signedint($integer, $min='', $max='') { $int = (int) $integer; if((($min != '') && ($int < $min)) || (($max != '') && ($int > $max))) { return FALSE; // Oops! Outside limits. } return $int; };