<?php
if (!defined('MODULE_FILE')) {
	Header("Location: ../../../index.php");
	exit;
}

global $module_name;
include("modules/$module_name/public/imageFunctions.php");
include("modules/$module_name/admin/config.php");

function getThumbnail($img, $galloc) {
	global $gallerypath, $imagepath, $prefix, $db;
	$ext = substr($img, (strrpos($img,'.') +  1));
	$name_gif = str_replace(".$ext", ".gif", $img);
	$name_jpg = str_replace(".$ext", ".jpg", $img);
	$name_png = str_replace(".$ext", ".png", $img);
	if (file_exists("$gallerypath/$galloc/thumb/$name_gif")) {
		$thumb = "$gallerypath/$galloc/thumb/$name_gif";
	} elseif (file_exists("$gallerypath/$galloc/thumb/$name_jpg")) {
		$thumb = "$gallerypath/$galloc/thumb/$name_jpg";
	} elseif (file_exists("$gallerypath/$galloc/thumb/$name_png")) { 
		$thumb = "$gallerypath/$galloc/thumb/$name_png";
	} else {
		$row = $db->sql_fetchrow($db->sql_query("SELECT thumbnail FROM ".$prefix."_magazin_media_types WHERE extension='$ext'"));
		$thumbnail = $row[thumbnail];
		$thumb = "$imagepath/$thumbnail";
	}
	return $thumb;
}

function displayPictureGall($filetype, $gid, $pid, $thumbwidth, $galloc, $name, $img, $orderby) {
	global $gallerypath, $admin_file, $adminpath, $imagepath, $galleryvar, $admin;
	$gid = intval($gid);
	$pid = intval($pid);
	$image = "$gallerypath/$galloc/$img";
	$thumb_dir = "$gallerypath/$galloc/thumb";
	$thumb = "$thumb_dir/$img";
	if (!file_exists($thumb)) {
		if (!file_exists($thumb_dir) && $galleryvar['imageSoftware'] != "browser") {
			mkDir("$thumb_dir");
		}
		if ($filetype==1) {
			$thumb = RatioResizeImg("$gallerypath/$galloc/$img", $thumb, $gid);
		}
		if ($thumb=="browser") $thumb = $image; else $thumb = getThumbnail($img, $galloc);
	}
	if (is_admin($admin)) {
		$gid = intval($gid);
		$out = "<center><a href=\"".$admin_file.".php?op=magazin&do=editmedia&type=edit&category=$gid&pid=$pid&gid=$gid\" title=\""._EDIT."\"><img src=\"images/edit.gif\" border=\"0\" alt=\""._EDIT."\"></a></center>";
	}
	if ($filetype == 1) {
		return "<a href=\"index.php?name=magazin&file=index&do=showpic&pid=$pid&orderby=$orderby\">"
		."<img src=\"$thumb\" width=\"$thumbwidth\" alt=\"".underscoreTospace($name)."\" border=\"0\" align=\"MIDDLE\">"
		.'</a>&nbsp;&nbsp;'.$out;
	} else {
		return "<a href=\"index.php?name=magazin&file=index&do=showpic&pid=$pid&orderby=$orderby\">"
		."<img src=\"$thumb\" alt=\"".underscoreTospace($name)."\" border=\"0\" align=\"MIDDLE\">"
		.'</a>&nbsp;&nbsp;'.$out;
	}
}

function displayDescriptionGall($row, $crow) {
	$m = 90 / $crow[numcol];
	if (strlen($row[description]) > $m) {
		$row[description] = trim(substr($row[description],0,$m));
		$p = strrpos($row[description], ' ');
		$row[description] = trim(substr($row[description],0,$p)).'...';
	}
	return "<font class=\"tiny\">$row[description]</font>\n";
}

function displayNewGall($row) {
	global $imagepath;
	if ($row[new_day] == 0) {
		return "<img src=\"$imagepath/newred.gif\" border=\"0\" alt=\""._GALNEWMEDIA1.'">';
	} elseif($row[new_day] < 3) {
		return "<img src=\"$imagepath/newgreen.gif\" border=\"0\" width=\"34\" height=\"14\" alt=\"".sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
	} elseif($row[new_day] < 7) {
		return "<img src=\"$imagepath/newblue.gif\" border=\"0\" width=\"34\" height=\"14\" alt=\"".sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
	}
}

function displaySizeGall($row, $crow) {
	if ($row[imginfo][2]) {
		$size = $row[imginfo];
		return "<font class=\"tiny\"><b>"._GALSIZE.": </b>$size[0]x$size[1]</font>";
	} else {
		return "<font class=\"tiny\"><b>"._GALSIZE.": </b>"._GALUNKNOWN."</font>";
	}
}

function displayFormatGall($row) {
	$ext = $row[imginfo][2];
	switch($ext) {
		case 1: $type = 'GIF'; break;
		case 2: $type = 'JPEG'; break;
		case 3: $type = 'PNG'; break;
		case 4: $type = 'WBMP'; break;
		default: $type = $ext; break;
	}
	return "<font class=\"tiny\"><b>"._GALFORMAT.": </b>$type";
}

function displayNumCommentsGall($row) {
	global $galleryvar, $prefix, $db;
	$numcomments = $db->sql_fetchrow($db->sql_query("SELECT COUNT(cid) AS total FROM ".$prefix."_magazin_comments WHERE pid=$row[pid]"));
	if ($galleryvar['allowcomments']) {
		return "<font class=\"tiny\"><b>"._GALNBCOMMENTS.": </b> $numcomments[total]</font>";
	}
}

function displayHitsGall($row) {
	return "<b>"._GALHITS.": </b>$row[counter]";
}

function displayRateGall($row) {
	global $galleryvar;
	if ($allowrate) {
		return "<b>"._GALRATING.": </b>$row[rate]";
	}
}

function displayDateAddedGall($row) {
	return "<b>"._GALADDED.': </b><font class="tiny">'.strftime(_GALDATEBRIEF,$row[unix_date]).'</font>';
}

function displayName($row) {
	return "<b>$row[name]</b>";
}

function navigationCat($gid, $offset, $orderby) {
	global $galleryvar, $prefix, $db;
	$limit = $galleryvar['maxNumberMedia'];
	$orderby = convertorderbyout($orderby);
	$gid = intval($gid);
	$numresults = $db->sql_query("SELECT * FROM $prefix"._magazin_pictures." WHERE gid=$gid");
	$numrows = $db->sql_numrows($numresults);
	$nav = '';
	$pages = intval($numrows/$limit);
	if ($numrows > 0 & $pages>=1) {
		if ($offset != 0) {
			$prevoffset = $offset-$limit;
			$gid = intval($gid);
			$nav = "<a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$prevoffset&orderby=$orderby\">[&lt;&lt;]</a>&nbsp;[&nbsp;";
		}
		if ($numrows%$limit) {
			$pages++;
		}
		for ($i=1;$i<=$pages; $i++) {
			$newoffset=$limit*($i-1);
			if ($newoffset==$offset) {
				if ($i==1) {
					if ($pages==1) {
						$nav .= "[ ".$i." ]";
					} else {
						$nav .= "[ ".$i."&nbsp;|&nbsp;";
					}
				} elseif ($i==$pages) {
					$nav .= $i."&nbsp;]";
				} else {
					$nav .= $i."&nbsp;|&nbsp;";
				}
			} else {
				$gid = intval($gid);
				if ($i < $pages) {
					$nav .= "<a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=$orderby\"><b>$i</b></a>&nbsp;|&nbsp;";
				} else {
					$nav .= "<a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=$orderby\"><b>$i</b></a>&nbsp;]&nbsp;";
				}
			}
		}
		if (!(($offset/$limit)==($pages-1)) && $pages!=1) {
			$gid = intval($gid);
			$newoffset=$offset+$limit;
			$nav .= "<a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=$orderby\">[&gt;&gt;]</a>";
		}
	}
	return "$nav<br><br>";
}

function navigationSubCat($gid) {
	global $admin_file, $imagepath, $admin, $user, $prefix, $gallerypath, $galleryvar, $db;
	$gid = intval($gid);
	if (is_admin($admin)) {
		$result = $db->sql_query("SELECT gallid, gallname, galloc, gallimg, description, visible, total, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._magazin_categories." WHERE parent=$gid AND visible>=0 ORDER by gallname");
	} else {
		$result = $db->sql_query("SELECT gallid, gallname, galloc, gallimg, description, visible, total, (TO_DAYS(NOW()) - TO_DAYS(lastadd)) AS new_day FROM $prefix"._magazin_categories." WHERE parent=$gid AND visible>0 ORDER by gallname");
	}
	$nb = $db->sql_numrows($result);
	if(!$nb) {
		return false;
	}
	$c = 0;
	$out .= '<table border="0" width="90%" cellspacing="10" cellpadding="0" align="center"><tr>';
	$pc = ceil(100/3);
	$isuser = is_user($user);
	$isadmin = is_admin($admin);
	while($row = $db->sql_fetchrow($result)) {
		$vis='';
		if ($row[visible]==1 && !$isuser) $vis = '(*)';
		if ($isadmin) {
			switch ($row[visible]) {
				case 0:
				$vis .= "&nbsp;<a href=\"".$admin_file.".php?op=magazin&do=editcategory&type=status&visible=1&category=".$row[gallid]."\" title=\""._GALVISIBLEADMIN."\"><img src=\"$imagepath/red_dot.gif\" border=\"0\" alt=\""._GALVISIBLEADMIN.'"></a>&nbsp;';
				break;
				case 1:
				$vis .= "&nbsp;<a href=\"".$admin_file.".php?op=magazin&do=editcategory&type=status&visible=2&category=".$row[gallid]."\" title=\""._GALVISIBLEMEMBER."\"><img src=\"$imagepath/yellow_dot.gif\" border=\"0\" alt=\""._GALVISIBLEMEMBER.'"></a>&nbsp;';
				break;
				default:
				$vis .= "&nbsp;<a href=\"".$admin_file.".php?op=magazin&do=editcategory&type=status&visible=0&category=".$row[gallid]."\" title=\""._GALVISIBLEPUBLIC."\"><img src=\"$imagepath/green_dot.gif\" border=\"0\" alt=\""._GALVISIBLEPUBLIC.'"></a>&nbsp;';
				break;
			}
		}
		if ($galleryvar['displaySubCatIcon']) {
			if (isset($galleryvar['SubCatIconwidth']) && $galleryvar['SubCatIconwidth']!="") {
				$icon = "<img src=\"$gallerypath/".$row[galloc]."/".$row[gallimg]."\" width=\"".$galleryvar['SubCatIconwidth']."\" border=\"0\" alt=\"$row[description]\">";
			} else {
				$icon = "<img src=\"$gallerypath/".$row[galloc]."/".$row[gallimg]."\" border=\"0\" alt=\"$row[description]\"></a>";
			}
		}
		$out .= "<td height=\"31\" width=\"31\"><a class=\"tiny\" href=\"index.php?name=magazin&file=index&do=showgall&gid=$row[gallid]\"><img src=\"$imagepath/folder.gif\" border=\"0\"></a></td><td>";

		if ($row[visible]==1 && !$isuser && !$isadmin) {
			$out .= underscoreTospace($row[gallname])." ($row[total]) $vis";
		} else {
			$out .= "<a href=\"index.php?name=magazin&file=index&do=showgall&gid=$row[gallid]\"><b>".underscoreTospace($row[gallname])."</b></a> ($row[total])  $vis";
			$icon = "<a href=\"index.php?name=magazin&file=index&do=showgall&gid=$row[gallid]\">".$icon."</a>";
		}

		if($row[new_day] == 0) {
			$out .= "<img src=\"$imagepath/newred.gif\" border=\"0\" alt=\""._GALNEWMEDIA1.'">';
		} elseif($row[new_day] < 3) {
			$out .= "<img src=\"$imagepath/newgreen.gif\" border=\"0\" alt=\"".sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
		} elseif($row[new_day] < 7) {
			$out .= "<img src=\"$imagepath/newblue.gif\" border=\"0\" alt=\"".sprintf(_GALNEWMEDIA2, $row[new_day]).'">';
		}
		$out .= "".$icon."";
		$out .= "</td>";
		$c++;
		if ($c == 3 && $nb != 3) {
			$out .= '</tr><tr>';
			$c = 0;
		}
	}
	$out .= '</tr></table><hr>';
	return $out;
}

function convertorderbytrans($orderby) {
	if ($orderby == "counter ASC")		$orderbyTrans = ""._GALPOPULARITY1."";
    if ($orderby == "counter DESC")		$orderbyTrans = ""._GALPOPULARITY2."";
    if ($orderby == "name ASC")			$orderbyTrans = ""._GALNAMEAZ."";
    if ($orderby == "name DESC")		$orderbyTrans = ""._GALNAMEZA."";
    if ($orderby == "date ASC")			$orderbyTrans = ""._GALDATE1."";
    if ($orderby == "date DESC")			$orderbyTrans = ""._GALDATE2."";
    if ($orderby == "rate ASC")			$orderbyTrans = ""._GALRATING1."";
    if ($orderby == "rate DESC")			$orderbyTrans = ""._GALRATING2."";
    return $orderbyTrans;
}

function showgall($gid, $offset, $orderby) {
	global $galleryvar, $adminpath, $prefix, $user, $admin, $db, $module_name;
	$gid = intval($gid);
	include("modules/$module_name/admin/config.php");
	$res = $db->sql_fetchrow($db->sql_query("SELECT count(*) as total FROM $prefix"._magazin_categories." WHERE parent=$gid AND visible=1"));
	$crow = $db->sql_fetchrow($db->sql_query("SELECT c.galloc, c.numcol, c.description, c.visible, c.thumbwidth, t.templateCategory, t.templateCSS FROM $prefix"._magazin_categories." AS c LEFT JOIN $prefix"._magazin_template_types." AS t ON t.id=c.template WHERE gallid=$gid"));

	$ok=0;
	switch ($crow[visible]) {
	case 0:
		if (is_admin($admin)) $ok=1;
	break;
	case 1:
		if (is_user($user) || is_admin($admin)) $ok=1;
	break;
	default:
		$ok=1;
	break;
	}

	if ($ok) {
		galleryHeader();
		OpenTable();
		if(isset($orderby)) {
			$orderby = convertorderbyin($orderby);
		} else {
			$orderby = convertorderbyin($galleryvar['defaultsortmedia']);
		}
		$gid = intval($gid);
		if (!$gid) {
			header("Location: index.php?name=magazin&file=index");
			exit;
		}
		$limit = $galleryvar['maxNumberMedia'];
		if (empty($offset)) {
			$offset = 0;
		}
		$gid = intval($gid);
		$orderbyTrans = convertorderbytrans($orderby);
		$navtree = navigationTree($gid, false);
		$nav = navigationCat($gid, $offset, $orderby);
		$subcats = navigationSubCat($gid);
		$navgall = navigationGall();
		$offset = intval($offset);
		$limit = intval($limit);
		$result = $db->sql_query("SELECT m.*, UNIX_TIMESTAMP(m.date) AS unix_date, (TO_DAYS(NOW()) - TO_DAYS(m.date)) AS new_day, f.displaytag, f.thumbnail, f.filetype, f.description AS description_media FROM $prefix"._magazin_pictures." AS m LEFT JOIN $prefix"._magazin_media_types." AS f ON f.extension=m.extension WHERE gid=$gid ORDER BY $orderby LIMIT $offset,$limit");
		$nb = $db->sql_numrows($result);
		$pc = ceil(100/$crow[numcol]);
		$c = 0;
		print '<p align="center">'.$navgall.'</p>';
		print '<p align="center">'.$navtree.'</p>';
		if($subcats != -1) {
			print "<center>$subcats</center>";
		}
		print "<center>$crow[description]</center>";
		if($galleryvar['displaysortbar']) {
			$gid = intval($gid);
			print "<center><b>"._GALSORTMEDIABY.": "._NAME." <a class=\"tiny\" href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=titleA\"><img src=\"modules/magazin/images/asc.gif\" border=\"0\"></a><a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=titleD\"><img src=\"modules/magazin/images/desc.gif\" border=\"0\"></a> "
			.""._DATE." <a class=\"tiny\" href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=dateA\"><img src=\"modules/magazin/images/asc.gif\" border=\"0\"></a><a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=dateD\"><img src=\"modules/magazin/images/desc.gif\" border=\"0\"></a> "
			.""._RATING." <a class=\"tiny\" href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=ratingA\"><img src=\"modules/magazin/images/asc.gif\" border=\"0\"></a><a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=ratingD\"><img src=\"modules/magazin/images/desc.gif\" border=\"0\"></a> "
			.""._POPULARITY." <a class=\"tiny\" href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=hitsA\"><img src=\"modules/magazin/images/asc.gif\" border=\"0\"></a><a href=\"index.php?name=magazin&file=index&do=showgall&gid=$gid&offset=$newoffset&orderby=hitsD\"><img src=\"modules/magazin/images/desc.gif\" border=\"0\"></a>"
			."<br>"._GALMEDIASORTED.": $orderbyTrans</font></center></b>";
		}
		print '<center>'.$nav.'</center>';
		print '<table border="0" width="100%" align="center" cellpadding="2"><tr>';
		while($row = $db->sql_fetchrow($result)) {
			$template = $crow[templateCategory];
			$template = str_replace('<:IMAGE:>', displayPictureGall($row[filetype], $row[gid], $row[pid], $crow[thumbwidth], $crow[galloc], $row[name], $row[img], convertorderbyout($orderby)), $template);
			if($row[filetype] == 1) {
				$row[imginfo] = @getimagesize("$gallerypath/$crow[galloc]/$row[img]");
			} else {
				$row[imginfo] = false;
				$row[imginfo][0] = $row[width];
				$row[imginfo][1] = $row[height];
				$row[imginfo][2] = $row[description_media];
				$row[imginfo][3] = "width=\"$row[width]\" height=\"$row[height]\"";
			}
			$val_description = displayDescriptionGall($row, $crow);
			$val_rate = displayRateGall($row);
			$val_format = displayFormatGall($row);
			$val_size = displaySizeGall($row, $crow);
			$val_nmbComments = displayNumCommentsGall($row);
			$val_new = displayNewGall($row);
			$val_hits = displayHitsGall($row);
			$val_date = displayDateAddedGall($row);
			$val_name = displayName($row);
			$template = str_replace('<:DESCRIPTION:>', $val_description, $template);
			$template = str_replace('<:RATE:>', $val_rate, $template);
			$template = str_replace('<:FORMAT:>', $val_format, $template);
			$template = str_replace('<:SIZE:>', $val_size, $template);
			$template = str_replace('<:NBCOMMENTS:>', $val_nmbComments, $template);
			$template = str_replace('<:NEW:>', $val_new, $template);
			$template = str_replace('<:HITS:>', $val_hits, $template);
			$template = str_replace('<:DATE:>', $val_date, $template);
			$template = str_replace('<:NAME:>', $val_name, $template);
			print '<td width="'.$pc.'%">'.$template.'</td>';
			$c++;
			if ($c == $crow[numcol] && $nb != $crow[numcol]) {
				print '</tr><tr>';
				$c = 0;
			}
		}
		echo "</tr></table><center><br>$nav</center>";
		if ($res[total]>0 && !is_user($user)) {
			print '<br><center><font class="tiny"><i>'._GALCATMEMBERS.'</i></font></center>';
		}
		CloseTable();
		include("footer.php");
	} else {
		echo "Access Denied";
	}
}
?>