<?PHP

// I/O helper functions for phppunchmachine

// TOC:
// function ppformelemts()
// function pp_cleanutf8($str)
// function pp_postfield($name)
// function pp_parsedate($input, &$datestr)
// function pp_parsedatetime($input, &$datestr)
// function pp_parsemask($input, $mask) (private)
// function pp_parsedecimal($input)

function ppformelemts() {
	$rc = "<INPUT type=\"hidden\" name=\"pp_mqcheck\" value=\"".htmlspecialchars("'")."\"><INPUT type=\"hidden\" name=\"pp_utf8check\" value=\"ä\">";
	return $rc;	
}
function pp_cleanutf8($str) {
	// TODO: check for invalid chars to avoid ambiguities in string comparation
	// there are no grave critical comparations now so this is ok for now...
	return $str;
}
function pp_postfield($name) {
	global $ppioREQUEST;
	if (isset($ppioREQUEST[$name])) {
		$rc = $ppioREQUEST[$name];
	} elseif (isset($_REQUEST[$name])) {
		$rc = $_REQUEST[$name];
	} else {
		return "";
	}
	if (isset($_REQUEST["pp_mqcheck"])) {
		if ($_REQUEST["pp_mqcheck"] == "\\'") {
			// magic quotes gpc where added - strip them
			$rc = stripslashes($rc);
		}
	}
	if (isset($_REQUEST["pp_utf8check"])) {
		if ($_REQUEST["pp_utf8check"] != "ä") {
			// browser did not return utf, so probably we have ISO-8859-1
			$rc = utf8_encode($rc);
		}
	}
	return pp_cleanutf8($rc);
}

function pp_parsedate($input, &$datestr) {
	global $ppconf;
	$datestr = "";
	$arr = pp_parsemask($input, $ppconf["datefmt"]);
	if ($arr["err"]) return false;
	if ($arr["y"] < 1900) {
		return false;
	}
	if (!checkdate($arr["m"],$arr["d"],$arr["y"])) {
		return false;
	}
	$datestr = sprintf("%04d",$arr["y"])."-".sprintf("%02d",$arr["m"])."-".sprintf("%02d",$arr["d"]);
	return true;	
}
function pp_parsedatetime($input, &$datestr) {
	global $ppconf;
	$datestr = "";
	$arr = pp_parsemask($input, $ppconf["datetimefmt"]);
	if ($arr["err"]) return false;
	if ($arr["y"] < 1900) return false;
	if (!checkdate($arr["m"],$arr["d"],$arr["y"])) return false;
	if ($arr["h"] > 23 || $arr["i"] > 59 || $arr["s"] > 59) return false;
	$datestr = sprintf("%04d",$arr["y"])."-".sprintf("%02d",$arr["m"])."-".sprintf("%02d",$arr["d"])." ".sprintf("%02d",$arr["h"]).":".sprintf("%02d",$arr["i"]).":".sprintf("%02d",$arr["s"]);
	return true;	
	
}
function pp_parsemask($input, $mask) {
	
	$arr = array();
	$arr["err"] = false;
	$arr["d"] = "";
	$arr["m"] = "";
	$arr["y"] = "";
	$arr["h"] = "";
	$arr["i"] = "";
	$arr["s"] = "";
	
	$mask = trim(strtr($mask, array("d"=>"dd", "m"=>"mm", "Y"=>"yyyy", "H"=>"hh", "i"=>"ii", "s"=>"ss")));
	
	$input = trim($input);
	$input = strtr($input, array("\t"=>" "));
	for ($j = 0;$j < strlen($input);$j++) {
		$c = substr($input,$j,1);
		$r = substr($mask,$j,1);
		if ($r == "." && $c == ",") $c = ".";	// allow , instead of .
		
		if ($r == "d" || $r == "m" || $r == "y" || $r == "h" || $r == "i" || $r == "s") {
			if (ord($c) >= ord("1") && ord($c) <= ord("9")) {
				$arr[$r] .= $c;
			} elseif ($c == "0") {
				if (strlen($arr[$r]) > 0) $arr[$r] .= $c;
			} elseif ($c == "." || $c == "," || $c == ":" || $c == "") {
				// be tolarant to short notations with stripped zeros
				$mask = substr($mask, 0,$j).substr($mask,$j + 1);
				$j -= 1;
				continue;
			} else {
				$arr["err"] = true;
				return $arr;
			}
		} else {
			if ($c != $r) {
				$arr["err"] = true;
				return $arr;
			}
			if ($r == " ") {
				$ch = substr($input, $j + 1,1);
				while ($ch == " ") {
					$input = substr($input, 0,$j + 1).substr($input,$j + 2);	// strip multiple spaces
					$ch = substr($input, $j + 1,1);
				}
			}
		}	
	   
	}
	$arr["d"] = (int)$arr["d"];
	$arr["m"] = (int)$arr["m"];	
	$arr["h"] = (int)$arr["h"];
	$arr["i"] = (int)$arr["i"];
	$arr["s"] = (int)$arr["s"];	
	if (strlen($arr["y"]) >= 1) {
		$arr["y"] = (int)$arr["y"];
		if ($arr["y"] >= 0 && $arr["y"] <= 69) {
			$arr["y"] += 2000;
		} elseif ($arr["y"] > 69 && $arr["y"] <= 99) {
			$arr["y"] += 1900;
		}		
	}
	$arr["y"] = (int)$arr["y"];		
	return $arr;
}
	
function pp_parsedecimal($input) {
	$input = strtr(trim($input),array(","=>"."));
	$input = 0.0 + $input;
	return $input;
}



?>
