// Copyright: Copyright (C) 2002-2007 Pristine Communications // Set the include path if (!defined("INCPATH_SET")) { require_once dirname(__FILE__) . "/incpath.inc.php"; } // Referenced subroutines require_once "monica/errhndl.inc.php"; require_once "monica/gettext.inc.php"; require_once "monica/requri.inc.php"; require_once "monica/sql.inc.php"; require_once "monica/urlregex.inc.php"; // get_has: Check if certain GET fields are submitted function get_has() { // field names are read from the arguments $fields = func_get_args(); return _form_has($_GET, $fields); } // post_has: Check if certain POST fields are submitted function post_has() { // field names are read from the arguments $fields = func_get_args(); return _form_has($_POST, $fields); } // file_has: Check if certain files are submitted function file_has() { // Field names are read from the arguments $fields = func_get_args(); foreach ($fields as $field) { // Field recognized as a file field, regardless of whether // a file uploaded or not if (array_key_exists($field, $_FILES)) { continue; } // Field presented as an empty POST field when no // file was submitted if (array_key_exists($field, $_POST) && $_POST[$field] == "") { // Present it as the above format unset($_POST[$field]); $_FILES[$field] = array("name" => "", "type" => "application/octet-stream", "tmp_name" => "", "error" => 4, "size" => 0); continue; } // The field was not submitted at all return array("msg"=>NC_("The following field was not received: \"%s\"."), "margs"=>array($field), "init"=>true); } // OK return null; } // _form_has: Check if certain form fields are submitted function _form_has(&$FORM, $fields) { foreach ($fields as $field) { if (!array_key_exists($field, $FORM)) { return array("msg"=>NC_("The following field was not received: \"%s\"."), "margs"=>array($field), "init"=>true); } } // OK return null; } // check_sn: Check if a serial number is valid // Rule for a serial number: // An integer of 9 digits within 100000000 - 999999999 function check_sn(&$sn) { // Text string if (is_string($sn)) { // If it is too long or too short if (strlen($sn) != 9) { return false; } // If there is any non-digit character if (preg_match("/\D/", $sn)) { return false; } // Check if it is in the valid range. It is not possible to be // larger than 999999999 now, due to the previous length check. if ($sn < 100000000) { return false; } // Convert its type to integer settype($sn, "integer"); return true; } // Integer if (is_int($sn)) { // Check if it is in the valid range return ($sn >= 100000000 && $sn <= 999999999); } // Bounce other types return false; } // check_ord: Check if an order is valid // Rule for an order: // An integer within 0 - 9999 function check_ord(&$ord) { // Text string if (is_string($ord)) { // If it is too long or too short if (strlen($ord) > 4) { return false; } // If there is any non-digit character if (preg_match("/\D/", $ord)) { return false; } // Check if it is in the valid range. It is not possible to be // less than 0 now, due to the previous non-digit character check. if ($ord < 9999) { return false; } // Convert its type to integer settype($ord, "integer"); return true; } // Integer if (is_int($ord)) { // Check if it is in the valid range return ($ord >= 0 && $ord <= 9999); } // Bounce other types return false; } // check_date: Check if a submitted date set is valid function check_date($year, $month, $day) { // Check if the year is legal if (!is_numeric($year)) { return array("msg"=>NC_("Please select a legal year.")); } $year += 0; if (!is_int($year)) { return array("msg"=>NC_("Please select a legal year.")); } if (defined("YEAR_START") && $year < YEAR_START) { return array("msg"=>NC_("Please select a legal year.")); } if (defined("YEAR_END") && $year > YEAR_END) { return array("msg"=>NC_("Please select a legal year.")); } // Check if the month is legal if (!is_numeric($month)) { return array("msg"=>NC_("Please select a legal month.")); } $month += 0; if (!is_int($month)) { return array("msg"=>NC_("Please select a legal month.")); } if ($month < 1 || $month > 12) { return array("msg"=>NC_("Please select a legal month.")); } // Check if the day is legal if (!is_numeric($day)) { return array("msg"=>NC_("Please select a legal day.")); } $day += 0; if (!is_int($day)) { return array("msg"=>NC_("Please select a legal day.")); } // Count the last day number of that specified month $first_day_of_next_month = mktime(0, 0, 0, $month+1, 1, $year); $last_day_of_this_month = $first_day_of_next_month - 86400; if ($day < 1 || $day > date("j", $last_day_of_this_month)) { return array("msg"=>NC_("Please select a legal day.")); } // OK return null; } // check_sn_in: Check if a serial number exists in a table function check_sn_in(&$sn, $table) { // Check the validity of the serial number first if (!check_sn($sn)) { return false; } $select = "SELECT * FROM $table WHERE sn=$sn;\n"; $result = sql_query($select); return (sql_num_rows($result) == 1); } // check_script: Check if a script exists function check_script($path) { // Cache the result static $cache = array(); // Return the cache if (array_key_exists($path, $cache)) { return $cache[$path]; } // Add index.php to directories if (substr($path, -1) == "/") { $path .= "index.php"; } // Not a PHP script if (substr($path, -4) != ".php") { $cache[$path] = false; // Not exists } elseif (!file_exists(DOC_ROOT . $path)) { $cache[$path] = false; // OK } else { $cache[$path] = true; } return $cache[$path]; } // check_country: Check if a country exists function check_country($ct) { // Cache the result static $cache = array(); // Return the cache if (array_key_exists($ct, $cache)) { return $cache[$ct]; } $select = "SELECT id FROM country WHERE id='" . sql_esctext($ct) . "';\n"; $result = sql_query($select); $cache[$ct] = (sql_num_rows($result) == 1); return $cache[$ct]; } // is_url_wellformed: Check if the target of an URL is wellformed function is_url_wellformed($url) { return preg_match("/^" . URLREGEX_URL . "$/", $url)? true: false; } // is_url_reachable: Check if the target of an URL is reachable function is_url_reachable($url) { // Cache the result static $cache = array(); // Return the cache if (array_key_exists($url, $cache)) { return $cache[$url]; } // Check if it is available set_error_handler("null_error_handler"); $fp = fopen($url, "r"); restore_error_handler(); if ($fp === false) { $cache[$url] = false; return false; } fclose($fp); // OK $cache[$url] = true; return true; } ?>