<?php
/*
script: gallery.php
purpose: displays the specified gallery.
copyright: copyright 2004 Php Image Host. All Rights Reserved. You may modify this file for use on your
					site running a licenced copy of the Php Image Host scripts.
					You must not distribute this file or derivations of it.
Nullified By CrAcKlOvE

*/

require_once(dirname(__FILE__).'/includes/config.inc.php');
require_once(dirname(__FILE__).'/classes/images.class.php');
$images =& new images($ace);

$un = $ace->getstr('u', $_REQUEST);
$gn = $ace->getstr('g', $_REQUEST);
$iname = $ace->getstr('i', $_REQUEST);

$gallery = $images->getgallerybyusernameimage($un, $gn, $iname);

if( $gallery ){

	$gurl = $ace->config->siteurl.'gallery.php?g='.$gallery->gallery_name.'&u='.$gallery->username;

	if( $gallery->image != 0 ){	// viewing the image in the gallery

		$tpl = join("", file(dirname(__FILE__).'/includes/gallery-image.htm'));
		// display the image
		// if next, display next, if prev, display prev

		$gwidth = $gallery->image->width;
		$gheight = $gallery->image->height;
		$mw = $ace->config->gallery_max_image_width;
		$mh = $ace->config->gallery_max_image_height;

		if( $gwidth > $mw || $gheight > $mh ){
			$dx = (double)$gwidth / (double)$mw;
			$dy = (double)$gheight / (double)$mh;
			$d = $dx > $dy ? $dx : $dy;
			$gwidth = (int)((double)$gwidth / $d);
			$gheight = (int)((double)$gheight / $d);
		}

		$s = array('{nextimagename}','{nextimageurl}', '{nextimagethumb}', '{previmagename}', '{previmageurl}',
							'{previmagethumb}', '{image_name}', '{image_url}', '{image_width}', '{image_height}',
							'{image_size}', '{image_thumb}', '{gallery_width}', '{gallery_height}');

		$r = array('', '', '', '', '', '', $gallery->image->name.'.'.$gallery->image->type,
								$gallery->image->image_url,
								$gallery->image->width, $gallery->image->height, 
								$gallery->image->filesize, $gallery->image->thumb_url,
								$gwidth, $gheight);

		if( $gallery->nextimage == -1 ){
			$tpl = preg_replace('/<nextimage>.*?<\/nextimage>/is', '', $tpl);
		}else{
			$tpl = preg_replace('/<nonextimage>.*?<\/nonextimage>/is', '', $tpl);
			$img = &$gallery->images[$gallery->nextimage];
			$r[0] = $img->name.'.'.$img->type;
			$r[1] = $gurl.'&i='.$r[0];
			$r[2] = $img->thumb_url;
		}

		if( $gallery->previmage == -1 ){
			$tpl = preg_replace('/<previmage>.*?<\/previmage>/is', '', $tpl);
		}else{
			$tpl = preg_replace('/<noprevimage>.*?<\/noprevimage>/is', '', $tpl);
			$img = &$gallery->images[$gallery->previmage];
			$r[3] = $img->name.'.'.$img->type;
			$r[4] = $gurl.'&i='.$r[3];
			$r[5] = $img->thumb_url;
		}

		$tpl = preg_replace('/<[\/]{0,1}(next|prev)image>/is', '', $tpl);
		$tpl = str_replace($s, $r, $tpl);

	}else{	// display the gallery

		$tpl = join("", file(dirname(__FILE__).'/includes/gallery.htm'));

		// find the <images> bit of the template & add all images

		if( count($gallery->images) == 0 ){	// no images?
			$tpl = preg_replace('/<images>.*?<\/images>/is', '', $tpl);
		}else{
			$tpl = preg_replace('/<noimages>.*?<\/noimages>/is', '', $tpl);
			if( preg_match('/<images>(.*)<\/images>/is', $tpl, $match ) ){
				$itpl = '';
				$ss = array('{thumb_url}', '{image_url}', '{image_name}', '{image_width}', '{image_height}');
				foreach( $gallery->images as $i ){
					$rr = array($i->thumb_url, $gurl.'&i='.$i->name.'.'.$i->type, $i->name.'.'.$i->type, $i->width, $i->height);
					$itpl.= str_replace($ss, $rr, $match[1]);
				}
				$tpl = preg_replace('/<images>.*<\/images>/is', $itpl, $tpl);
			}
		}

		$tpl = preg_replace('/<[\/]{0,1}(noimages|images)>/is', '', $tpl);

	}

	// general template stuff
	//	1) do ads
	// 	2) do username, gallery name, gallery intro, number of images, site name, site url

	$s = array('{username}', '{gallery_name}', '{gallery_intro}', '{images}', '{sitename}', '{siteurl}',
							'{gallery_url}');
	$r = array($gallery->username, $gallery->gallery_name, nl2br(htmlspecialchars($gallery->gallery_intro)),
							count($gallery->images), htmlspecialchars($ace->config->sitename), $ace->config->siteurl,
							$gurl);

	$adcnt = substr_count($tpl, '{bannerad}');
	if( $adcnt > 0 ){
		require_once(dirname(__FILE__).'/classes/adrotator.class.php');
		$ads = new adrotator($ace);
		$ads->preload($adcnt);
		for( $i = 1; $i <= $adcnt; $i++){
			$tpl = preg_replace('/\{bannerad\}/i', $ads->display(true), $tpl, 1);
		}
	}

	echo str_replace($s, $r, $tpl);

}else{
	require_once(dirname(__FILE__).'/header.php');
	$pagecontent->display('Gallery Not Found');
	require_once(dirname(__FILE__).'/footer.php');
}

?>
