<!-- This IS working... -->
<?php
#
#
#
require_once "HTML/IT.php";
require_once "inc/db_wrap_mysql.php";

if ($_GET['date'] != "") {
	$date = $_GET['date'];

$tok = strtok ($date,"-");
if ($tok) {
$year = $tok;
$tok = strtok ("-");
}
if ($tok) {
$month = $tok;
}
$tok = strtok ("-");
if ($tok) {
$day = $tok;
}

$stamp = mktime(0,0,0,$month,$day,$year);

} else {

$stamp = time();


}
$da = getdate($stamp);
$yesterday = date("Y-m-d",mktime(0,0,0,$da['mon'],$da['mday']-1,$da['year']));
$tomorrow =date("Y-m-d",mktime(0,0,0,$da['mon'],$da['mday']+1,$da['year']));
$today = date('d-m-Y',$stamp);

# let's build the url's

$datepart1=date('Ymd',$stamp);
$datepart2=date('y-m-d',$stamp);
$datepart3=date('m-d-y',$stamp);

$monthpart=date('Ym',$stamp);
$daypart=date('d',$stamp);

if ($comics = return_query('SELECT * FROM comics ORDER BY gewicht')) {
	$tpl = new IntegratedTemplate("inc");
	$tpl->loadTemplatefile("template.html", true, true);
	
	$tpl->SetVariable("today",$today);
	$tpl->SetVariable("tomorrow",$tomorrow);
	$tpl->SetVariable("yesterday",$yesterday);
		
	
	foreach($comics as $comic) {
		if ($comic['an'] == 'j') {
			$picurl = ereg_replace("{Ymd}",$datepart1,$comic['url']);
	  	$picurl = ereg_replace("{y-m-d}",$datepart2,$picurl);
		  $picurl = ereg_replace("{m-d-y}",$datepart3,$picurl);
  
			if ($comic['methode'] == 'picture') {
   		 $tpl->SetCurrentBlock("pic");	
			  $tpl->SetVariable("picurl",$picurl);
			  $tpl->SetVariable("homepage",$comic['homepage']);
			  $tpl->SetVariable("name",$comic['name']);
			  $tpl->SetVariable("beschreibung",$comic['beschreibung']);			
		   $tpl->ParseCurrentBlock("pic");
		 	} else {
				if ($realpic = get_picture($picurl,$comic['expression'])) {
					$realpic = $comic['host'].$realpic;
   		 $tpl->SetCurrentBlock("pic");	
			  $tpl->SetVariable("picurl",$realpic);
			  $tpl->SetVariable("homepage",$comic['homepage']);
			  $tpl->SetVariable("name",$comic['name']);
			  $tpl->SetVariable("beschreibung",$comic['beschreibung']);			
		   $tpl->ParseCurrentBlock("pic");
				
				} else {
			
			
			 $tpl->SetCurrentBlock("html");
				$tpl->SetVariable("url",$picurl);
				$tpl->SetVariable("name",$comic['name']);
				$tpl->SetVariable("beschreibung",$comic['beschreibung']);
			 $tpl->ParseCurrentBlock("html");	
			 }
			}
		} else {
	   $tpl->SetCurrentBlock("off");
			$tpl->SetVariable("homepage",$comic['homepage']);
			$tpl->SetVariable("name",$comic['name']);
			$tpl->SetVariable("beschreibung",$comic['beschreibung']);			
		 $tpl->ParseCurrentBlock("off");
		}
	}
	
	$tpl->parse();
	$tpl->show();


} else {
	die("no database connection");
}

function get_picture($html_url, $expression) {

	$html_text = join ('', file ($html_url));


	if (preg_match($expression,$html_text,$matches)) {
		return $matches[1];
	} else {
		return false;
	}

}

?>