. */ /** * Convert SQL-style dates into European date style * example: 2002-02-01 -> 01.02.2002 * * @param string $date date in SQL style * @return string date in European date style */ function getDisplayDate($date) { if( $date == "0000-00-00 00:00:00" OR $date == "0000-00-00" ) return ""; elseif( strlen($date)==10 ) return date("d.m.Y", strtotime($date)); elseif( strlen($date)==16 ) return date("d.m.Y H:i T", strtotime($date)); elseif( strlen($date)==19 ) return date("d.m.Y H:i:s T", strtotime($date)); else return $date; } /** * Convert European date style into SQL-style * example: 01.02.2002 -> 2002-02-01 * * @param string $date date in European date style * @return string date in SQL style */ function getSqlDate($date) { $Y = substr($date, 6, 4); $m = substr($date, 3, 2); $d = substr($date, 0, 2); return "$Y-$m-$d"; } /** * Read a variable that was submitted by the browser * * @param string $varname name of the variable to be returned * @return string content of the variable, or 'NULL' if it was not set */ function getVar($varname) { if( isset($_GET[$varname]) ) return stripslashes($_GET[$varname]); elseif( isset($_POST[$varname]) ) return stripslashes($_POST[$varname]); else return NULL; } /** * Read a uid and return a username * * @param string $varname name of the variable to be returned * @return string content of the variable */ function LdapUidToUsername($varname) { if (strstr($varname, "uid=")){ return (substr($varname, (strpos($varname, '=')+1), (strpos($varname, ',ou=')-4))); }else{ return $varname; } } /** * Read a username and return a uid * * @param string $varname name of the variable to be returned * @return string content of the variable */ function LdapUsernameToUid($varname) { if (strstr($varname, "uid=")){ return $varname; }else{ return "uid=".$varname.",ou=people,o=AEGEE,c=EU,ou=Zeus-devel,o=AEGEE,c=FR"; } } /** * Read an integer variable that was submitted by the browser * * @param string $varname name of the variable to be returned * @return int content of the variable, or 0 if it is not set or not an integer */ function getInt($varname) { if( preg_match("/^-?[0-9]+$/", getVar($varname)) ) { return getVar($varname); }else { return 0; } } /** * Encode a header string to be used in mails. With non-ascii characters the header is encoded * into UTF-8, otherwise it is returned as is (or quoted if requested). * Example output: =?UTF-8?B?SWYgeW91IGNhbiByZWFkIHRoaXMgeW8=?= * * @param string $header header to be encoded * @param boolean $quoteifascii (default true) in case the header is ascii only, quote the returned value? * @return string the encoded header */ function encode($header, $quotesifascii=true) { for( $i=0; $i127 ) { // Header contains non-ascii characters return "=?UTF-8?B?".base64_encode($header)."=?="; } } if( $quotesifascii ) return "\"".$header."\""; else return $header; } /** * Undo htmlentities * * @param string $string text that is translated using htmlentities. * @return string text without characters encoded into html entities */ function unhtmlentities($string) { $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($string, $trans_tbl); } /** * Open an URL for reading (comparable to urlfopen, but not blocked by safe mode) * * @todo: add HTTPS support * * @param string $host hostname part of the URL to be opened * @param string $path path part of the URL to be opened * @return filepointer reference to the opened socket */ function urlopen($host, $path) { if( $fp = fsockopen($host, 80) ) { fputs($fp, "GET $path HTTP/1.1\r\n"); fputs($fp, "Host: $host\r\n"); fputs($fp, "Connection: close\r\n\r\n"); $buf="a"; while( !feof($fp) AND $buf!="\r\n" ) { $buf = fgets($fp, 1024); } return $fp; }else { return NULL; } } /** * Read the data at the given URL * * @todo: add HTTPS support * * @param string $url URL to be read * @return string data at the given URL */ function urlread($url) { if( preg_match("/^https?:\/\/((\w|-)+(\.(\w|-)+)*\.[a-zA-Z]{2,4})(\/.*)?$/", $url, $matches) ) { $host = $matches[1]; $path = $matches[5]; $body = ""; if( $fp = fsockopen($host, 80) ) { fputs($fp, "GET ".$path." HTTP/1.1\r\n"); fputs($fp, "Host: ".$host."\r\n"); fputs($fp, "Connection: close\r\n\r\n"); // Read headers $buf = "dummy"; while( !feof($fp) AND $buf!="\r\n" ) { $buf = fgets($fp, 1024); } // Read body $len = 1; while( !feof($fp) AND $len>0 ) { $len = hexdec(fgets($fp, 1024)); $buf = ""; while( strlen($buf)<$len ) $buf .= fread($fp, $len-strlen($buf)); $body .= $buf; fread($fp, 2); // Skip CRLF after last line } } fclose($fp); return $body; }else { return NULL; } } ?>