<?php
// ======================================================================== //
//                                                                          //
//   KooBI- Webportalsystem                                                 //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   ====================================================================   //
//                                                                          //   
//   dream4 | webservices - Am Michelsgrund 7 - 69469 Weinheim - Germany    //    
//   tel. +49(0) 6201 - 980442 / fax. +49(0) 6201 - 64809                   //
//                                                                          //
// ======================================================================== //
if(!checkactive('gallery')) include(dirname(__FILE__)."/lib/notactive.php" );
// ====================================================
// <<-- Wenn keine Berechtigung -->>
// ====================================================
$sname = simpletitle($lang['picfullsize']);

if (!permission("showgaleriepicsbig")) {
	echo "<pre>" . $lang['nopermission_t'];
	exit;
} 


if($_REQUEST['dlf']=="1"){
	
	if(count($_POST['dl']) > $MAXPICDOWNLOAD){
		echo "<pre>" . str_replace("__MAXPICDOWNLOAD__", $MAXPICDOWNLOAD, $lang['maxpicnopermission']);
		exit;
	}
	include_once('class/zip/pclzip.lib.php');
	
	$fs = date("dmYHis") . $UID;
	$file = "uploads/galerie/koobigallery_".$fs.".zip";
	$archive = new PclZip($file);
	
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_gallery_items WHERE gal_id='$_REQUEST[galid]' ");
	
	
	foreach ($_POST['dl'] as $id => $pid) {
		if($id){
			$sql = new dbc("SELECT path FROM ".PREFIX."_gallery_items WHERE id=$id ");
			$row_gal = $sql->fetchrow();
			$img[] = "uploads/galerie/".$row_gal->path;
		}
	}
	$files = implode(",", $img);
	$v_list = $archive->create($files);
	
	header("Cache-control: private");
	header("Content-type: application/octet-stream"); 
	header("Content-disposition:attachment; filename=koobigallery_".$fs.".zip");
	header("Content-Length:" . filesize($file));
	
	if(@readfile($file)){ @unlink($file); } exit;
	
}

else {
	// ====================================================
	// <<-- Ansonsten Bild(er) anzeigen... -->>
	// ====================================================
	$gallerypics = array();
	$sql = new dbc("SELECT * FROM ".PREFIX."_gallery_items WHERE gal_id=$_REQUEST[galid] order by id $_REQUEST[ascdesc]  ");
	while($row_gal = $sql->fetchrow()){
		array_push($gallerypics,$row_gal);
	}
	
	$tmpl->assign('gallerypics', $gallerypics);
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_gallery_items WHERE id=$_REQUEST[img_id] ");
	$row = $sql->fetchrow();
	
	if(file_exists( BASEDIR . "/uploads/galerie/" . $row->path)){
		$size = @GetImageSize ( BASEDIR . "/uploads/galerie/" . $row->path );
		$tmpl->assign('img', $row->path);
		$tmpl->assign('width', $size[0]);
		$tmpl->assign('height', $size[1]);
		$thename = $row->name;
	}
	
	
	$sql = new dbc("SELECT id,name FROM ".PREFIX."_gallery_items WHERE gal_id=$_REQUEST[galid] order by id $_REQUEST[ascdesc] ");
	$all = $sql->numrows();
	
	$gallery = array();
	$i = 1;
	
	$curr_pic = '0';
	$next_pic = '0';
	$prev_pic = '0';
	
	while($rowall = $sql->fetchrow()){
		$n = $i++;
		$rowall->number = $n;
		
		if ($rowall->id == $row->id) {
			$curr_pic = $rowall;
		}
		
		array_push($gallery,$rowall);
	}
	
	
	$pic_index = karray_search($curr_pic,$gallery);
	
	// um indexüberschreitung zu vermeiden
	if ($pic_index < (count($gallery) - 1))
	$next_pic = $gallery[$pic_index + 1];
	// um indexunterschreitung zu vermeiden
	if ($pic_index > 0)
	$prev_pic = $gallery[$pic_index - 1];
	
	$sql_w = new dbc("SELECT galwidth FROM " . PREFIX . "_settings");
	$row_w = $sql_w->fetchrow();
	
	$tmpl->assign("next_pic", $next_pic);
	$tmpl->assign("prev_pic", $prev_pic);
	$tmpl->assign('gallery', $gallery);
	$tmpl->assign('all', $all);
	$tmpl->assign('lang', $lang);
	$tmpl->assign('item', $row);
	$tmpl->assign('divwidth', $row_w->galwidth/1.3);
	if(permission("picdownload")) $tmpl->assign('picdownload', 1);
	
	$sname = simpletitle($lang['gallery'] . $lang['f_navi_separator'] . getNavigationptitle($_REQUEST['galid'], 'gallery', '', 'id', 'id', 'title').$lang['f_navi_separator'] . $thename );
	
	$plustitle = ($thename!='') ? SEPARATOR . $thename : '' ;
	$headernav = '<a href="index.php?p=gallery&amp;area='.$area.'">' . $lang['gallery'] . '</a>' . SEPARATOR . getNavigationPath($_REQUEST['galid'], 'gallery', 'gallery&amp;action=showimages', 'galid', 'id', 'title') . $plustitle;
	
	

	
	// ===============================================================
	// <<-- KOMMENTARE  -->>
	// ===============================================================
	
	// <<-- IST ES ERLAUBT, GALERIEBILDER HIER ZU KOMMENTIEREN? -->>
	$cperm = -1;
	$q = "SELECT cancomment FROM ".PREFIX."_gallery WHERE id='".(int)$_REQUEST['galid']."'";
	$sql = new dbc($q);
	$cperm = $sql->fetchrow();
	
	// <<-- WENN KOMMENTIERT WERDEN DARF -->>
	if($cperm->cancomment == 1) {
		
		// <<-- NEUER KOMMENTAR -->>
		if($_REQUEST['post']==1) {
		
			// <<-- PRÜFEN OB SPAMSCHUTZ AKTIV IST UND WIE LANGE ER GILT... -->>
			$sql = new dbc("SELECT spamprotect,spamprotect_time,mailbycomment FROM " . PREFIX . "_settings");
			$row_spam = $sql->fetchrow();
			$error = false;
			
				if($row_spam->spamprotect==1){
				$q = "SELECT ip,ctime FROM ".PREFIX."_gallerycomments  WHERE ip='".$_SERVER['REMOTE_ADDR']."' AND imgid='".(int)$_REQUEST['img_id']."' ORDER BY id DESC limit 1";
				$sql = new dbc($q);
				$row = $sql->fetchrow();
					
					if($row->ip!=''){
						if( ($row->ctime) + (60*$row_spam->spamprotect_time) > time()){
							
							$EOUT = msg("spamprotect","spamprotect_text", str_replace("__URL__", "index.php?p=gallerypic&amp;img_id=".(int)$_REQUEST['img_id']."&amp;galid=".(int)$_REQUEST['galid']."&amp;area=".(int)$_REQUEST['area']."&amp;ascdesc=".$_REQUEST['ascdesc']."", $lang['redirect']), "index.php?p=gallerypic&amp;img_id=".(int)$_REQUEST['img_id']."&amp;galid=".(int)$_REQUEST['galid']."&amp;area=".(int)$_REQUEST['area']."&amp;ascdesc=".$_REQUEST['ascdesc']."");
							$NOOUT = 1;
							$error = true;
						}
					}
				}
			
			// <<-- EINTRAG -->>
			if(!$error){
			$text = formtext($_POST['text'],$MAXCOMMLENGTH);
			$sql = new dbc("
			INSERT INTO ".PREFIX."_gallerycomments
			(
			id,
			imgid,
			ctime,
			autor,
			title,
			comment,
			ip
			) VALUES (
			'',
			'".(int)$_REQUEST['img_id']."',
			'".time()."',
			'".$UID."',
			'".iform($_POST['title'],"255","")."',
			'".$text."',
			'".$_SERVER['REMOTE_ADDR']."'
			) 
			");
			
			$EOUT = msg("thankyou","pollcomments_thanks", str_replace("__URL__", "index.php?p=gallerypic&amp;img_id=".(int)$_REQUEST['img_id']."&amp;galid=".(int)$_REQUEST['galid']."&amp;area=".(int)$_REQUEST['area']."&amp;ascdesc=".$_REQUEST['ascdesc']."", $lang['redirect']), "index.php?p=gallerypic&amp;img_id=".(int)$_REQUEST['img_id']."&amp;galid=".(int)$_REQUEST['galid']."&amp;area=".(int)$_REQUEST['area']."&amp;ascdesc=".$_REQUEST['ascdesc']."");
			$NOOUT = 1;
			}
		}
	
	$limit = limit();
	if( !isset($_REQUEST['sort']) && $_REQUEST['sort']!="desc" && $_REQUEST['sort']!="asc" ){
		$sorts = "desc";
		$_REQUEST['sort'] = "desc";
	} else {
		$sorts = $_REQUEST['sort'];
		$_REQUEST['sort'] = $_REQUEST['sort'];
	}
	
	$sql = new dbc("SELECT imgid FROM ".PREFIX."_gallerycomments  WHERE imgid='".escs($_REQUEST['img_id'])."'");
	$num = $sql->numrows();
	
	$seiten = ceil($num / $limit);
	$a = prepage() * $limit - $limit;
	
	$sql = new dbc("SELECT * FROM ".PREFIX."_gallerycomments  WHERE imgid='".escs($_REQUEST['img_id'])."' ORDER BY id $sorts limit $a,$limit");
	$erg = $sql->numrows();
			

	$path = dirname(__FILE__);
	include($path ."/lib/pagesobjects.php" );
	include($path ."/lib/commentobjects.php" );
	
	
	
	// ===============================================================
	// <<-- SEITEN- NAVIGATION -->>
	// ===============================================================
	if($num > $limit){
		$nav = pagenav($seiten, $lang['aktpos'], " <a class=\"page_navigation\" href=\"index.php?p=gallerypic&amp;img_id=".(int)$_REQUEST['img_id']."&amp;galid=".(int)$_REQUEST['galid']."&amp;area=".(int)$_REQUEST['area']."&amp;pp=$limit&amp;sort=$_REQUEST[sort]&amp;page={s}\">{t}</a> ");
	}
	
	$tmpl->assign('pages', $nav);
	$tmpl->assign('guest', $lang['guest']);
	$tmpl->assign('comments_array', $comments_array);
	
	if($erg > 0) {
		$tmpl->assign('comments', parsetrue("container/".container('comments'), $lang['img_comments'], $tmpl->fetch('comments/comments.tpl')));
		}
	
	// <<-- KOMMENTAFORM AUSGEBEN -->>
	if(permission('commentimg')){
		$tmpl->assign('formlink', "index.php?p=gallerypic&amp;img_id=".(int)$_REQUEST['img_id']."&amp;galid=".(int)$_REQUEST['galid']."&amp;area=".(int)$_REQUEST['area']."&amp;post=1");
		$tmpl->assign('commentform', parsetrue("container/".container("comments"), $lang['postcomment'], $tmpl->fetch("comments/commentform.tpl")));
	} else {
		$tmpl->assign('commentform', $tmpl->fetch('comments/commentform_false.tpl'));
	}
	
  } // <<-- KANN KOMMENTIERT WERDEN; ENDE -->>
  
	// ===============================================================
	// <<-- KOMMENTARE ENDE -->>
	// ===============================================================
	
	// ===============================================================
	// <<-- SEITE AUSGEBEN / GANZES BILD ODER ANSICHT?  -->>
	// ===============================================================
	if($_REQUEST['size']=='full'){
		$_REQUEST['p'] = "misc";
		$tmpl->assign('content', $tmpl->fetch("gallery/gallerypic.tpl"));
	} else if ($_REQUEST['type']=="popup"){
		$_REQUEST['p'] = "misc";
		$tmpl->assign('content', $tmpl->fetch("gallery/gallerypic.tpl"));
	} else {
		$cookie_id = "galimg_" . $_REQUEST['img_id'] . "_" . $area . "_readed";
		if($_COOKIE["galimg_" . $_REQUEST['img_id'] . "_" . $area] !=  $cookie_id){
		$sql = new dbc("UPDATE ".PREFIX."_gallery_items SET hits=hits+1 WHERE id='".escs($_REQUEST['img_id'])."'");
		@setcookie ("galimg_" . $_REQUEST['img_id'] . "_" . $area, $cookie_id); 
		}
		#$_REQUEST['p'] = "misc";
		#$tmpl->assign('content', $tmpl->fetch("gallery/gallerypic.tpl"));
		$tmpl->assign('content', parsetrue("container/".container('gallery'), $headernav, $tmpl->fetch('gallery/gallerypic.tpl')));
	}
	
}
?>