<?php

// Cache
require_once('dis_cache.php');

// Get FOTO
$FOTO = @unserialize(@file_get_contents('../foto/'.substr($_GET['foto_id'], -2).'/'.substr($_GET['foto_id'], -4, 2).'/'.$_GET['foto_id'].'/foto.dat'));
if(!$FOTO){
	$ERROR404 = 'foto is missing';
	require_once('dis_404.php');
}

// Get all else
$strecke_id = $FOTO['strecke_id'];
$template_name = $_REQUEST['werbung'] ? 'fotow' : 'foto';
require_once('dis_common.php');

// cache some not!
/*if(
//	$STRECKE['strecke_id'] == 1215313 || // vor
//	$STRECKE['strecke_id'] == 1215314 || // sta
	$STRECKE['strecke_id'] == 1215315 || // oerl
//	$STRECKE['strecke_id'] == 1215316 || // laemm
//	$STRECKE['strecke_id'] == 1215317 || // ziel
//	$STRECKE['strecke_id'] == 1215318 || // mini
	false
){
	$GLOBALS['cache']['is_running'] = false;
}*/

// werbung
extract_werbung($STRECKE['match']);

// check url
$canonical_url = $STRECKE['url_base'].$FOTO['foto_id'].($_REQUEST['werbung'] ? 'w' : '').($_REQUEST['paypal'] ? 'p' : '').'.html';
if($_SERVER['REQUEST_URI'] != $canonical_url){
	header('HTTP/1.1 301 Moved Permanently');
	header('Location: '.$canonical_url);
	die();
}

if($_REQUEST['werbung']){
	header('X-Pictools-Log: '.$MANDANT[$_SERVER['SERVER_NAME']][0].';foto_werbung;'.$FOTO['foto_id'].';'.$STRECKE['strecke_id']);
}else{
	header('X-Pictools-Log: '.$MANDANT[$_SERVER['SERVER_NAME']][0].';foto;'.$FOTO['foto_id'].';'.$STRECKE['strecke_id']);
}

// fill in the template
$REPLACE['%%URL%%'] = 'http://'.$_SERVER['SERVER_NAME'].$canonical_url;
$REPLACE['%%URL-SHORT%%'] = 'http://'.$_SERVER['SERVER_NAME'].'/!'.$FOTO['foto_id'];

// styles
if(!$_REQUEST['werbung']){
$REPLACE['%%STYLE%%'] .=
'.btn { display: none }'.
'#holder:hover .btn { display: block }'
;
}

// Pic data
$this_pic = array_search($FOTO['foto_id'], $STRECKE['foto_ids']);

// next/prev
$num_pics = count($STRECKE['foto_ids']);
if($_REQUEST['werbung']){
	$prev = $this_pic;
}else if($this_pic == 0){
	$prev = $num_pics-1;
}else{
	$prev = $this_pic-1;
}
$next = $this_pic+1;
if($next == $num_pics){
	$next = 0;
}
$prevW = $nextW = '';
if($WERBUNG['foto'] != '' && $num_pics > 1 && !$_REQUEST['werbung']){
	if($num_pics <= 7){
		$wc = 1;
	}else if($STRECKE['index']){
		$wc = round(($num_pics+1) / 10);
	}else{
		$wc = round(($num_pics+1) / 5);
	}
	$xb = $wc * ($this_pic ? $this_pic : $num_pics);
	if(($xb >= $num_pics) && ($xb % $num_pics < $wc)){
		$prevW = 'w';
	}
	$xb = $wc * ($next ? $next : $num_pics);
	if(($xb >= $num_pics) && ($xb % $num_pics < $wc)){
		$nextW = 'w';
		$next = $this_pic;
	}
}
$prev = $STRECKE['foto_ids'][$prev].$prevW.'.html';
$next = $STRECKE['foto_ids'][$next].$nextW.'.html';

$REPLACE['%%LINK-NEXT%%'] = $next;
$REPLACE['%%LINK-PREV%%'] = $prev;
$REPLACE['%%PIC-NUM%%'] = $this_pic+1;
$REPLACE['%%PIC-COUNT%%'] = $num_pics;

// index?
if($STRECKE['index']){
	$REPLACE['%%INDEX%%'] = $TEMPLATE['INDEX_ON'];
	$REPLACE['%%LINK-INDEX%%'] = $STRECKE['strecke_id'].'s'.(1+floor($this_pic / $PicsPerPage)).'.html';
}else{
	$REPLACE['%%INDEX%%'] = $TEMPLATE['INDEX_OFF'];
}

// Image
$out = '';
$min_height = 400;
if($_REQUEST['werbung']){
	$wwidth = 300;
	$wheight = 250+16;
	$height = max($min_height, $wheight);
	$out .= '<div id="holder" style="width: 750px; height: '.$height.'px; position: relative">';
	$out .= '<div style="position: absolute; top: '.floor(($height-$wheight) / 2).'px; left: '.floor((750 - $wwidth) / 2).'px; width: '.$wwidth.'px; height: '.$wheight.'px; border: 0px">';
	$out .= '<img src="'.$TEMPLATE['TBASE'].'rectangle.png" width="300" height="16" border="0"><br/>'.$WERBUNG['foto'];
	$out .= '</div>';
	$out .= '<a href="'.$prev.'"><div class="btn"><img src="'.$TEMPLATE['TBASE'].'left.png" width="150" height="'.$min_height.'" border="0" style="position: absolute; left: 0px; top: '.floor(($height-$min_height) / 2).'px; opacity: 0.8"></div></a>';
	$out .= '<a href="'.$next.'"><div class="btn"><img src="'.$TEMPLATE['TBASE'].'right.png" width="150" height="'.$min_height.'" border="0" style="position: absolute; left: '.(750-150).'px; top: '.floor(($height-$min_height) / 2).'px; opacity: 0.8"></div></a>';
	$out .= '</div>';
}else if($_REQUEST['paypal'] && isset($STRECKE['buy'][$MANDANT[$_SERVER['SERVER_NAME']][1]]['paypal'])){

	// never cache this page!
	$cache['is_running'] = false;

	// don't show kaufen/download links here
	$FOTO['can_download'] = false;

	// start session
	session_start();

	// set/check session variables
	if(!is_array($_SESSION['WARENKORB']) || (isset($_SESSION['PARAMS']) && is_array($_SESSION['PARAMS']) && !array_alloc_equal($_SESSION['PARAMS'], $STRECKE['buy'][$MANDANT[$_SERVER['SERVER_NAME']][1]]['paypal']))){
		$_SESSION['WARENKORB'] = array();
	}
	if(!is_bool($_SESSION['OPTIN'])){
		$_SESSION['OPTIN'] = false;
	}
	$_SESSION['AGB'] = false;
	$_SESSION['CLIENT'] = $MANDANT[$_SERVER['SERVER_NAME']][1];
	$_SESSION['PARAMS'] = $STRECKE['buy'][$MANDANT[$_SERVER['SERVER_NAME']][1]]['paypal'];

	// add picture
	if(false === array_search($FOTO['foto_id'], $_SESSION['WARENKORB'])){
		$_SESSION['WARENKORB'][] = $FOTO['foto_id'];
		$added = true;
	}else{
		$added = false;
	}

	$out = '<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" align="center" width="400">';
	
	$out .= '<img src="'.$FOTO['foto_id'].'s.jpeg" width="'.round($FOTO['size_screen_w'] * 400 / 750).'" height="'.round($FOTO['size_screen_h'] * 400 / 750).'"><br/>';

	$out .= '<div style="float: right">';
	if($added){
		$out .= 'Das Bild wurde ihrem Warenkorb hinzugefügt.<br/>';
	}else{
		$out .= 'Das Bild befindet sich bereits in ihrem Warenkorb.<br/>';
	}
	$out .= '</div>';

	$out .= '</td><td width="20">';

	$out .= '</td><td width="330">';
	
/*	$out .= '<b>Sicher online bestellen und zahlen</b><br/>
<br/>
Wir bieten Ihnen mit PayPal ein SSL-gesichertes Zahlungsumfeld. 
Sie können mit einem  PayPal-Konto per Lastschrift oder Kreditkarte bezahlen. 
Falls Sie noch kein PayPal Konto besitzen, wird bei dieser Bestellung ein Konto für Sie angelegt.
<br/>
<br/>
';*/

	$has_karte = isset($_SESSION['PARAMS']['karte']);
	$has_normal = !($has_karte && (isset($_SESSION['PARAMS']['karte']['only']) && $_SESSION['PARAMS']['karte']['only']));

	if($has_karte){

		$out .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td>';

		if($has_normal){
			$out .= '<b>Preise mit NW-Karte</b><br/>';
		}else{
			$out .= '<b>Preise</b><br/>';
		}

		if(isset($_SESSION['PARAMS']['karte']['preis'])){
			$out .= 'Jedes Bild kostet '.number_format($_SESSION['PARAMS']['karte']['preis'], 2, ',', '.').' €.<br/>';
		}else if(isset($_SESSION['PARAMS']['karte']['staffelung'])){
			$prices = $_SESSION['PARAMS']['karte']['staffelung'];
			$any = array_pop($prices);
			foreach($prices as $k => $v){
				$out .= ($k == 0 ? 'Das' : 'das').' '.($k+1).'. Bild kostet '.number_format($v, 2, ',', '.').' €,<br/>';
			}
			$out .= 'jedes weitere Bild kostet '.number_format($any, 2, ',', '.').' €.<br/>';
		}
	
		if(isset($_SESSION['PARAMS']['karte']['preis']) && $_SESSION['PARAMS']['karte']['preis'] == 0){
//			$out .= '<a href="/paypal/?karte=true" target="_blank">zum download</a><br/>';
			$out .= '<form action="/paypal/" method="get" target="_blank"><input type="hidden" name="karte" value="true">';
			$out .= '<input type="submit" value="zum Download"><br/>';
			$out .= '</form>';
		}else{
//			$out .= '<a href="/paypal/?karte=true" target="_blank">zur kasse</a><br/>';
			$out .= '<form action="/paypal/" method="get" target="_blank"><input type="hidden" name="karte" value="false">';
			$out .= '<input type="submit" value="zur Kasse"><br/>';
			$out .= '</form>';
		}

		$out .= '</td><td width="30"></td><td><img src="/paypal/nwkarte_logo.png" width="122"></td></tr></table>';
		
		$out .= '<br/>';
	}
	
	if($has_normal){

		if($has_karte){
			$out .= '<b>Preise ohne NW-Karte</b><br/>';
		}else{
			$out .= '<b>Preise</b><br/>';
		}

		if(isset($_SESSION['PARAMS']['preis'])){
			$out .= 'Jedes Bild kostet '.number_format($_SESSION['PARAMS']['preis'], 2, ',', '.').' €.<br/>';
		}else if(isset($_SESSION['PARAMS']['staffelung'])){
			$prices = $_SESSION['PARAMS']['staffelung'];
			$any = array_pop($prices);
			foreach($prices as $k => $v){
				$out .= ($k == 0 ? 'Das' : 'das').' '.($k+1).'. Bild kostet '.number_format($v, 2, ',', '.').' €,<br/>';
			}
			$out .= 'jedes weitere Bild kostet '.number_format($any, 2, ',', '.').' €.<br/>';
		}
	
//		$out .= '<a href="/paypal/?karte=false" target="_blank">zur kasse</a><br/>';
		$out .= '<form action="/paypal/" method="get" target="_blank"><input type="hidden" name="karte" value="false">';
		$out .= '<input type="submit" value="zur Kasse"><br/>';
		$out .= '</form>';
		$out .= '<br/>';
	}

	$out .= '<b>Warenkorb: '.count($_SESSION['WARENKORB']).'</b> Bild/er';

	$out .= '</td></tr></table>';

//	$out .= var_export($_SESSION,1);
	
	$out .= display_timeline(4, $this_pic/($num_pics-1));
}else{
/*	if($FOTO['size_screen_h'] == 0 || $FOTO['size_screen_h'] > 600){
		if($FOTO['size_arc_w']/$FOTO['size_arc_h'] > 750/600){
			$FOTO['size_screen_h'] = (int) round(750*$FOTO['size_arc_h']/$FOTO['size_arc_w']);
			$FOTO['size_screen_w'] = 750;
		}else{
			$FOTO['size_screen_w'] = (int) round(600*$FOTO['size_arc_w']/$FOTO['size_arc_h']);
			$FOTO['size_screen_h'] = 600;
		}
	}*/
	if($FOTO['size_screen_h'] < $min_height){
		$height = $min_height;
	}else{
		$height = $FOTO['size_screen_h'];
	}
	$out .= '<div id="holder" style="width: 750px; height: '.$height.'px; position: relative">';
	$out .= '<img src="'.                                                     $FOTO['foto_id'].'s.jpeg" width="'.$FOTO['size_screen_w'].'" height="'.$FOTO['size_screen_h'].'" style="position: absolute; top: '.floor(($height-$FOTO['size_screen_h']) / 2).'px; left: '.floor((750 - $FOTO['size_screen_w']) / 2).'px; border: 0px" />';
//	$out .= '<img src="http://bilder.nw-news.de/'.dirname($canonical_url).'/'.$FOTO['foto_id'].'s.jpeg" width="'.$FOTO['size_screen_w'].'" height="'.$FOTO['size_screen_h'].'" style="position: absolute; top: '.floor(($height-$FOTO['size_screen_h']) / 2).'px; left: '.floor((750 - $FOTO['size_screen_w']) / 2).'px; border: 0px" />';
	$REPLACE['%%FOTO-URL%%'] = dirname($REPLACE['%%URL%%']).'/'.$FOTO['foto_id'].'s.jpeg';
	$out .= '<a href="'.$prev.'"><div class="btn"><img src="'.$TEMPLATE['TBASE'].'left.png" width="150" height="'.$min_height.'" border="0" style="position: absolute; left: 0px; top: '.floor(($height-$min_height) / 2).'px; opacity: 0.8"></div></a>';
	$out .= '<a href="'.$next.'"><div class="btn"><img src="'.$TEMPLATE['TBASE'].'right.png" width="150" height="'.$min_height.'" border="0" style="position: absolute; left: '.(750-150).'px; top: '.floor(($height-$min_height) / 2).'px; opacity: 0.8"></div></a>';
	$out .= '</div>';
	
	$out .= display_timeline(4, $this_pic/($num_pics-1));
}


$REPLACE['%%FOTO%%'] = $out;

if(!$_REQUEST['werbung'] && !$_REQUEST['paypal']){
	// copy & bildzeile
	$copys = array();
	if(count($FOTO['copy_ids']) > 0){
		$t = count($FOTO['copy_ids']) == 1 ? 0 : (count($FOTO['copy_ids']) == 2 ? 1 : 2);
		foreach($FOTO['copy_ids'] AS $id){
			$c = @json_decode(@file_get_contents('../data/copyrights/'.substr($id, -2).'/'.$id.'.dat'));
			if($c){
				$copys[] = $c[$t];
			}
		}
	}
	
	if(count($copys) == 0){
		$REPLACE['%%COPY%%'] = '';
	}else{
		$REPLACE['%%COPY%%'] = 'Foto: '.implode(', ', $copys);
	}
	if(!isset($STRECKE['vote']) || !$STRECKE['vote']){
		// if set to false (or for compatibility, if not set at all) -> dont show box
		$FOTO['bildzeile'] = preg_replace('!<br/><iframe src="/like/[0-9]+\.html" width="103" height="21" scrolling="no" frameborder="0"></iframe>!', '', $FOTO['bildzeile']);
	}
	$REPLACE['%%BILDZEILE%%'] = $FOTO['bildzeile'];
}else{
	$REPLACE['%%COPY%%'] = '';
	$REPLACE['%%BILDZEILE%%'] = '';
}

$REPLACE['%%TAGS%%'] = implode(', ', $tags_out);

$kaufen = array();
if($FOTO['can_buy']){
	if(isset($STRECKE['buy'][$MANDANT[$_SERVER['SERVER_NAME']][1]]['bildpartner'])){
		$kaufen[] = '<a href="#" onclick="fotowin=window.open(\'http://www.bildpartner.de/upload/picup.php?vcode='.$STRECKE['buy']['bildpartner'].'&pic='.$FOTO['foto_id'].'.jpeg&artnr=&referenz=\',\'fotowin\',\'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=300,height=250\'); fotowin.focus(); return false;">Printabzug bestellen <img src="'.$TEMPLATE['TBASE'].'/pfeil_rechts.png"></a>';
	}
	if(isset($STRECKE['buy'][$MANDANT[$_SERVER['SERVER_NAME']][1]]['paypal'])){
		if($_REQUEST['paypal']){
			$kaufen[] = '<a href="'.$FOTO['foto_id'].'.html">Weitere Bilder aussuchen <img src="'.$TEMPLATE['TBASE'].'/pfeil_rechts.png"></a>';
		}else{
			$kaufen[] = '<a href="'.$FOTO['foto_id'].'p.html">Download bestellen <img src="'.$TEMPLATE['TBASE'].'/pfeil_rechts.png"></a>';
		}
	}
}
if($FOTO['can_download']){
	$kaufen[] = '<label title="Bild-auflösung: '.$FOTO['size_arc_w'].'×'.$FOTO['size_arc_h'].', '.(($FOTO['size_dl'] < 1024*1024) ? round($FOTO['size_dl']/1024).' kB' : strtr(sprintf('%.1f', $FOTO['size_dl']/(1024*1024)), '.', ',').' MB').'"><a href="'.$FOTO['foto_id'].'.jpeg">Downloaden <img src="'.$TEMPLATE['TBASE'].'/pfeil_rechts.png"></a></label>';
}
$REPLACE['%%KAUFEN%%'] = implode('<br/>', $kaufen);

output_page();

// debug
//echo '<pre>'.htmlspecialchars(var_export(array($MANDANT[$_SERVER['SERVER_NAME']],$WERBUNG,$FOTO,$STRECKE,$_SERVER['SERVER_NAME']),true));

	function array_alloc_equal($a1, $a2){
		// eleminate all those who are equal from both
		foreach($a1 AS $k => $v){
			if(!isset($a2[$k])){
				return false;
			}else{
				if(is_array($v) && is_array($a2[$k])){
					// recursive call
					if(!array_alloc_equal($v, $a2[$k])){
						return false;
					}
				}else if($v !== $a2[$k]){
					// mismatch
					return false;
				}
				// remove "seen" keys
				unset($a2[$k]);
			}
		}

		return count($a2) == 0;
	}

?>