* */ /********************************************************************* * utils.lib.php * * Definition of global constants and functions. * * Note: * This is a MODULE FILE to be imported by other php scripts. * Importing should be done using the php function 'reqire_once'. * There should be no output produced by this file. */ /* ======================================================================== CONSTANTS * ======================================================================== DEFINITIONS * */ /********************************************************************* * LOCALES */ // DATE FORMATS // format syntax see php function 'date' // used by functions human_date, humal_date_l etc. define("DATE_FORMAT_S", "j. M. Y"); // 27. Okt. 1987 define("DATE_FORMAT_M", "D, j. M. Y"); // Sa, 27. Okt. 1987 define("DATE_FORMAT_L", "l, j. F Y"); // Samstag, 27. Oktober 1987 // TIME FORMATS // format syntax see php function 'date' // used by functions human_time, humal_time_l etc. define("TIME_FORMAT_S", "G:i"); // 9:30 define("TIME_FORMAT_M", "H:i"); // 09:30 define("TIME_FORMAT_L", "H:i:s"); // 09:30:56 define("NOW", time()); // locale setlocale (LC_ALL, 'de_DE.ISO8859-15', 'de_DE@euro', 'de_DE', 'de_DE.ISO8859-1', 'de', 'ge'); define("GERMAN_DATE_MAPPING", TRUE); // ======================================================================== HTTP/HTML // ======================================================================== FUNCTIONS /********************************************************************* * htmlize * * a wrapper around php's function htmlentities with takes * a default value to be used if the given string is empty. */ function htmlize ($string, $default = "") { $string = trim($string); if($string === "") return $default; return htmlentities($string); } /********************************************************************* * href * a simplified way to produce an html hyperlink */ function href ($link, $string = "") { if($string === "") $string = htmlize($link); return "$string"; } /********************************************************************* * hyper_mailto * * returns given string with all email-addresses turned into * mailto: hyperlinks */ function hyper_mailto ($string) { $expr = "/([a-z0-9\-\.]+\@[a-z0-9\-]+\.[a-z0-9\-\.]+)/i"; $subst = "\\1"; return preg_replace($expr, $sub, $string); } /********************************************************************* * hyper_url * * returns given string with all URLs turned into hyperlinks */ function hyper_url ($string) { $expr = '/(\S+:\/\/[A-Za-z0-9\/_.%?&+=-]+(:\d+)?)/'; $subst = '\1'; return preg_replace($expr, $subst, $string); } /********************************************************************* * get_req_var * Reads a vaiable from a "superglobal" scope and * returns its content. */ function get_req_var ($varname, $scope, $default = '') { $value = $default; $scope = strtoupper(trim($scope)); $n = strlen($scope); for($i = 0; $i < $n; $i++) { $f = substr ($scope, $i, 1); if($f == 'G') { if(isset($_GET[$varname])) { $value = $_GET[$varname]; break; } } elseif($f == 'P') { if(isset($_POST[$varname])) { $value = $_POST[$varname]; break; } } elseif($f == 'C') { if(isset($_COOKIE[$varname])) { $value = $_COOKIE[$varname]; break; } } else trigger_error("get_req_var: invalid scope identifier: '$f'", E_USER_WARNING); } return $value; } /* ======================================================================== DATE/TIME * ======================================================================== FUNCTIONS * */ /********************************************************************* * human_date * Returns the date of a given UNIX timestamp * in a human readable format. * * Note: The trim() is necessary, since %e prepends a space * to the number of the day when it has only one digit. */ function human_date ($timestamp = NOW) { if(!$timestamp) return '-'; return trim(german_date(DATE_FORMAT_M, $timestamp)); } function human_date_s ($timestamp = NOW) { if(!$timestamp) return '-'; return trim(german_date(DATE_FORMAT_S, $timestamp)); } function human_date_l ($timestamp = NOW) { if(!$timestamp) return '-'; return trim(german_date(DATE_FORMAT_L, $timestamp)); } function german_date ($format, $timestamp = NOW) { $res = date($format, $timestamp); if(! defined("GERMAN_DATE_MAPPING") || !GERMAN_DATE_MAPPING) return $res; $pattern = array( '/Monday/', '/Tuesday/', '/Wednesday/', '/Thursday/', '/Friday/', '/Saturday/', '/Sunday/', '/Mon\b/', '/(Tue|Die)\b/', '/(Wed|Mit)\b/', '/(Thu|Don)\b/', '/Fr(i|e)\b/', '/Sa(t|m)\b/', '/S(u|o)n\b/', '/January/', '/February/', '/March/', '/June/', '/July/', '/October/', '/December/', '/M(a|ä)r\b\.?/', '/Apr\b\.?/', '/May\b\.?/', '/Jun\b\.?/', '/Jul\b\.?/', '/Oct/', '/Dec/' ); $subst = array( "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag", "Sonntag", "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So", "Januar", "Februar", "März", "Juni", "Juli", "Oktober", "December", "März", "April", "Mai", "Juni", "Juli", "Okt", "Dez" ); return preg_replace($pattern, $subst, $res); } /********************************************************************* * human_time * Returns the time of a given UNIX timestamp * in a human readable format. */ function human_time ($timestamp = NOW) { if(!$timestamp) return '-'; return date(TIME_FORMAT_M, $timestamp); } function human_time_s ($timestamp = NOW) { if(!$timestamp) return '-'; return date(TIME_FORMAT_S, $timestamp); } function human_time_l ($timestamp = NOW) { if(!$timestamp) return '-'; return date(TIME_FORMAT_L, $timestamp); } ?>