.
*/
function dolog($category, $message, $level="info") {
echoln("
".strtoupper($level).": $category - $message
\n", true);
}
function ssl_redirect() {
if(headers_sent()) {
echo " ssl_redirect: Headers already sent.";
return;
}
if( !(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") && !(isset($_SERVER['HTTP_HTTPS']) && $_SERVER['HTTP_HTTPS'] != "off") ) {
if(!isset($_SERVER['REQUEST_URI']) OR !isset($_SERVER['HTTP_HOST'])) {
include("./include/header.php");
echo " Your browser doesn't support HTTP 1.1 requests.\n";
echo " Please use https:// instead of http://\n";
include("./include/footer.php");
return;
}
echo "\n \n";
echo " \n";
echo " \n";
exit;
}
}
function get_date($date) {
// convert from 2002-02-01 to 01.02.2002
if( strlen($date)==10 ) return date("d.m.Y", strtotime($date));
elseif( strlen($date)==16 ) return date("d.m.Y H\hi", strtotime($date));
else return date("d.m.Y H\hi:s", strtotime($date));
}
function get_sql_date($date) {
// convert from 01.02.2002 to 2002-02-01
$Y = substr($date, 6, 4);
$m = substr($date, 3, 2);
$d = substr($date, 0, 2);
return "$Y-$m-$d";
}
function get_var($var) {
if( isset($_GET[$var]) )
return stripslashes($_GET[$var]);
elseif( isset($_POST[$var]) )
return stripslashes($_POST[$var]);
else
return null;
}
function get_int($var) {
if( preg_match("/^-?[0-9]+$/", get_var($var)) ) {
return get_var($var);
}else {
return 0;
}
}
function get_bytesize($size) {
// Return the size, converted to B, KB, MB, GB or TB
// Input is in bytes
$SIZE_LABEL=array(0=>"B", 1=>"KB", 2=>"MB", 3=>"GB", 4=>"TB");
$size_loop=0;
while( $size>950 ) {
$size_loop++;
$size=$size/1024;
}
if( $size>99.5 )
return number_format($size, 0, ",", ".")." ".$SIZE_LABEL[$size_loop];
else
return number_format($size, 1, ",", ".")." ".$SIZE_LABEL[$size_loop];
}
function get_bytes($size) {
// Return the size in bytes
// Input is in B, KB, MB, GB or TB
$SIZE_LABEL=array("B"=>1, "KB"=>1024, "K"=>1024, "MB"=>pow(1024,2), "M"=>pow(1024,2), "GB"=>pow(1024,3), "G"=>pow(1024,3), "TB"=>pow(1024,4), "T"=>pow(1024,4));
if( preg_match("/^([0-9]+)\w*([BKMGT]+)$/", $size, $matches) ) {
// Number and label as input
if( array_key_exists($matches[2], $SIZE_LABEL) ) {
return $matches[1]*$SIZE_LABEL[ $matches[2] ];
}else {
// Unknown label
return false;
}
}elseif( preg_match("/^([0-9]+$/", $size) ) {
// Only numbers, return input
return $size;
}else {
// Unknown
return false;
}
}
function check_email($Email) {
// Check if the supplied e-mail address is a valid e-mail address. If yes, return true, else return false
$re="/(^(\w|\.|-)+@(\w|-)+(\.(\w|-)+)*\.[a-zA-Z]{2,4}$)/";
if( preg_match($re,$Email) ) {
//Regex matches, now check MX
if( getmxrr(substr($Email,strpos($Email,"@")+1), $mxhosts) ) {
return true;
}else {
return false;
}
}else {
return false;
}
}
function photo_updatecount($book) {
if( $res=doquery("SELECT `id` FROM `photo` WHERE `book_id`=".addslashes($book)) ) {
$total=mysql_num_rows($res);
if( doquery("UPDATE `book` SET `photocount`=".addslashes($total)." WHERE `id`=".addslashes($book)) ) {
return $total;
}else {
return false;
}
}else {
return false;
}
}
function photo_getnextnum($book) {
if( $res=doquery("SELECT MAX(`nr`) as `nr` FROM `photo` WHERE `book_id`=".addslashes($book)." GROUP BY `book_id`") ) {
$row=mysql_fetch_array($res);
return $row['nr']+1;
}else {
return false;
}
}
function photo_resize($srcIm, $orig_x, $orig_y, $maxsize) {
// Return a resized image. Return is array with 'width', 'height' and 'data'
global $FULL_PATH;
if( $orig_x>$orig_y ) {
$return['width']=min($maxsize, $orig_x);
$return['height']=round(( min($maxsize, $orig_x) /$orig_x)*$orig_y);
}else {
$return['width']=round(( min($maxsize, $orig_y) /$orig_y)*$orig_x);
$return['height']=min($maxsize, $orig_y);
}
//debug("Resizing from ".$orig_x."x".$orig_y." to ".$return['width']."x".$return['height']);
if( !($dstIm=imagecreatetruecolor($return['width'], $return['height'])) ) return false;
if( !imagecopyresampled($dstIm, $srcIm, 0, 0, 0, 0, $return['width'], $return['height'], $orig_x, $orig_y) ) return false;
if( !($tempfile=tempnam($FULL_PATH."/tmp", "photo")) ) return false;
if( !imagejpeg($dstIm, $tempfile) ) return false;
imagedestroy($dstIm);
if( !($fp=fopen($tempfile, "rb")) ) return false;
if( !($return['data']=fread($fp, filesize($tempfile))) ) return false;
fclose($fp);
unlink($tempfile);
return $return;
}
function photo_rotate($src, $rotate) {
// Return a rotated image. Rotate should be in degrees.
global $FULL_PATH;
if( !($srcIm=imagecreatefromstring($src)) ) return false;
if( !($dstIm=imagerotate($srcIm, $rotate, 0)) ) return false;
if( !($tempfile=tempnam($FULL_PATH."/tmp", "photo")) ) return false;
if( !imagejpeg($dstIm, $tempfile) ) return false;
imagedestroy($dstIm);
if( !($fp=fopen($tempfile, "rb")) ) return false;
if( !($return=fread($fp, filesize($tempfile))) ) return false;
fclose($fp);
unlink($tempfile);
return $return;
}
function doflush() {
#echo "\n";
ob_flush();
flush();
}
function echoln($t, $debugmsg=false, $layout=false) {
global $DEBUG;
if( $DEBUG ) {
echo $t;
if( !$layout ) echo "\n";
}elseif( !$debugmsg ) {
echo trim($t)." ";
}
}
function debug($msg) {
global $DEBUG;
if( $DEBUG ) {
echo "\n$msg
\n";
}
}
function page_header() {
if( isset($_GET['logout']) ) {
//Do logout
$_SESSION['sess_uid']=NULL;
$_SESSION['sess_name']=NULL;
$_SESSION['sess_access']=NULL;
if( substr($_SERVER['PHP_SELF'], -11)=="addbook.php" ) header("location: event.php");
if( substr($_SERVER['PHP_SELF'], -12)=="addphoto.php" ) header("location: event.php");
}
if( isset($_POST['login']) ) {
// Check login
include("./include/logincheck.php");
if( check_login($_POST['username'], $_POST['password']) ) {
// Ok, logincheck.php has done the session setup
}else {
layout("login");
exit();
}
}
if( isset($_GET['login']) && (!isset($_SESSION['sess_uid']) || !($_SESSION['sess_uid']>0)) ) {
//Show login page and exit
ssl_redirect();
layout("login");
exit();
}
layout("top");
layout_menu("Home");
layout_menu("Events by date");
layout_menu("Events by title");
#layout_menu("Photos by user");
#layout_menu("Add photos");
#layout_menu("My photos");
layout_menu("About");
layout("account-top");
if( isset($_SESSION['sess_uid']) && $_SESSION['sess_uid']>0 ) {
layout("account-loggedin");
}else {
layout("account-anonymous");
}
layout("account-bottom");
layout("page-top");
}
function page_footer() {
layout("page-bottom");
layout("bottom");
}
function layout($part) {
$parts=array();
$parts[]="login";
$parts[]="top";
$parts[]="menu-top";
$parts[]="menu-bottom";
$parts[]="account-top";
$parts[]="account-anonymous";
$parts[]="account-loggedin";
$parts[]="account-bottom";
$parts[]="page-top";
$parts[]="page-bottom";
$parts[]="bottom";
$parts[]="all";
if( in_array($part, $parts) ) {
if( $part=="top" ) header("Content-Type: text/html; charset=UTF-8");
if( $fd=@fopen($FULL_PATH."layout/".$_SESSION['layout']."/".$part.".html", "r") ) {
$search=array(); $replace=array();
$search[]="%PHP_SELF%"; $replace[]=$_SERVER['PHP_SELF'];
$search[]="%USERNAME%"; $replace[]=(isset($_SESSION['sess_name']) ? $_SESSION['sess_name'] : "");
$search[]="%RAWENCODEUSERNAME%"; $replace[]=rawurlencode(isset($_SESSION['sess_name']) ? $_SESSION['sess_name'] : "");
$search[]="%LAYOUTDIR%"; $replace[]="./layout/".$_SESSION['layout']."/";
$search[]="%QUERY_STRING%"; $replace[]=$_SERVER['QUERY_STRING'];
while( !feof($fd) ) {
echoln( str_replace($search, $replace, fgets($fd, 4096)), false, true );
}
fclose ($fd);
}
}else {
// Not valid
}
}
function layout_img($img) {
$imgs=array();
$imgs[]="previous.gif";
$imgs[]="next.gif";
$imgs[]="list.gif";
if( in_array($img, $imgs) ) {
return "./layout/".$_SESSION['layout']."/image/".$img;
}else {
// Not valid
return "";
}
}
function layout_menu($item) {
layout("menu-top");
echoln($item."\n");
layout("menu-bottom");
}
function get_eventlink($eventcode, $documentid) {
if( strtolower(substr($eventcode, 0, 2))=="ev" ) {
return NULL; // doesn't work anymore without Lotus Notes
//return "";
}elseif( strtolower(substr($eventcode, 0, 2))=="su" ) {
return NULL;
}else {
return NULL;
}
}
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;
}
}
?>