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

if (isset($_GET['basepath']) || isset($_GET['adminpath'])) {
	Header("Location: index.php");
	die();
}

function navigationPic($name, $gid, $pid, $orderby) {
	global $prefix, $postcardverz, $db;
	$orderbyA = convertorderbyin($orderby);
	$pid = intval($pid);
	$gid = intval($gid);
	$sql = "SELECT pid, name FROM $prefix"._album_pictures." where gid=$gid ORDER BY $orderbyA";
	$res = $db->sql_query($sql);
	$max = $db->sql_numrows($res);
	$ind = getPos($res, $pid);
	
	if ($ind > 0) {
		$prev = mysql_result($res, $ind-1, 0);
		$pname = mysql_result($res, $ind-1, 1);
	}
	if ($ind<$max-1) {
		$next = mysql_result($res, $ind+1, 0);
		$nname = mysql_result($res, $ind+1, 1);
	}

	$out = '<table border="0" cellspacing="10" cellpadding="0" align="center"><tr>';
	$gid = intval($gid);

	if ($prev) {
		$out .= '<td><a href="index.php?name=Album&file=index&do=showpic&gid='.$gid.'&pid='.$prev.'&orderby='.$orderby.'"><img src="modules/Album/images/left.gif" alt="'._GALPREV.'" border="0" align="middle "></a></td>';
	}
	
	$out .= '<td><font class="option"> '.$name.' </font></td>';

	if ($next) {
		$out .= '<td><a href="index.php?name=Album&file=index&do=showpic&gid='.$gid.'&pid='.$next.'&orderby='.$orderby.'"><img src="modules/Album/images/right.gif" alt="'._GALNEXT.'" border="0" align="middle "></a></td>';
	}
	$out .= '</tr></table>';
	return $out;
}


function displayPicturePic($row, $size) {
	global $gallerypath, $postcardverz;
        $template = $row[displaytag];
        $template = str_replace('<:FILENAME:>', "$gallerypath/$row[galloc]/$row[img]", $template);
		if ($size[0] > 600 || ($row[filetype] != 1 && $size[0] == 320)) {
			$size[1] = 600 * ($size[1]/$size[0]); $size[0] = 600;
		}
        $template = str_replace('<:WIDTH:>', $size[0], $template);
        $template = str_replace('<:HEIGHT:>', $size[1], $template);
        $template = str_replace('<:DESCRIPTION:>', $row[description], $template);
        return $template;
}

function displayDescriptionPic($row) {
	if ($row[description]!="") {
		return '<table border="0" cellpadding="2" cellspacing="0" width="50%" align="right">'
		.'<tr><td align="right" valign="top" width="100"><b>'._GALDESCRIPTION.':</b></td>'
		.'<td align="left" valign="top">'.$row[description].'</td></tr></table>';
	} else {
		return "";
	}
}

function displaySubmitterPic($row, $size) {
	global $prefix, $postcardverz, $db;
	$sql2 = "SELECT * FROM $prefix"._users." where uname='".$row[submitter]."'";
    $result2 = $db->sql_query($sql2);
    $row2 = $db->sql_fetchrow($result2);
	if ($result2>0) {
		$row[submitter] = '<a href="index.php?name=Account&op=userinfo&username='.$row[submitter].'" target="_blank">'.$row[submitter].'</a>';
	}
	return '<table border="0" class="bgcolor4" cellpadding="2" cellspacing="1" width="160">'
	.'<tr>'
	.'<td width="50" align="left" class="bgcolor1"><font class="tiny">'._GALSUBMITTER.'</font></td>'
	.'<td width="110" align="left" class="bgcolor1"><font class="tiny"><a href="index.php?name=Account&op=userinfo&username='.$row[submitter].'" target="_blank">'.$row[submitter].'</a></font></td></tr>'
	.'<tr><td width="50" align="left" class="bgcolor1"><font class="tiny">'._GALSIZE.'</font></td>'
	.'<td width="110" align="left" class="bgcolor1">'.$size[0].' x '.$size[1].'</td></tr>';
}

function displayDateAddedPic($row) {
	return '<tr><td width="50" align="left" class="bgcolor1"><font class="tiny">'._GALADDED.'</font></td>'
	.'<td width="110" align="left" class="bgcolor1"><font class="tiny">'.strftime(_GALDATEBRIEF, $row[unix_time]).'</font></td></tr>';
}

function displayHitsPic($row) {
	return '<tr><td width="50" align="left" class="bgcolor1"><font class="tiny">'._GALHITS.'</font></td>'
	.'<td width="110" align="left" class="bgcolor1"><font class="tiny">'.$row[counter].'</font></td></tr>';
}

function displayRatePic($row) {
	global $galleryvar, $postcardverz;
        
	if(!$galleryvar['allowrate']) {
		return;
	}
	return '<tr>'
	.'<td width="50" align="left" class="bgcolor1"><font class="tiny">'._GALVOTES.'</font></td>'
	.'<td width="110" align="left" class="bgcolor1" ><font class="tiny">'.$row[votes].'</font></td>'
	.'</tr>'
	.'<tr><td width="50" align="left" class="bgcolor1"><font class="tiny">'._GALRATING.'</font></td>'
	.'<td width="110" align="left" class="bgcolor1"><font class="tiny">'.$row[rate].'</font></td>'
	.'</tr></table>';
}

function displayRatingBarPic($row, $orderby) {
	global $galleryvar, $user, $imagepath, $postcardverz;
	
	if(!$galleryvar['allowrate']) {
		return;
	}
	
	if(!$galleryvar['anonrate'] || $user) {
		return  '<form action="index.php?name=Album&file=index&do=Vote" method="post">'
		.'<input type="hidden" name="pid" value="'.$row[pid].'">'
		.'<input type="hidden" name="orderby" value="'.$orderby.'">'
		.'<table width="160" border="0" cellpadding="2" cellspacing="1" align="center">'
		.'<tr><td align="center">'
		.'<select name="rate">'
		.'<option value="1">1</option>'
		.'<option value="2">2</option>'
		.'<option value="3">3</option>'
		.'<option value="4">4</option>'
		.'<option value="5">5</option>'
		.'<option value="6">6</option>'
		.'<option value="7">7</option>'
		.'<option value="8">8</option>'
		.'<option value="9">9</option>'
		.'<option value="10" selected>10</option>'
		.'</select>'
		.' <input type="submit" value='._RATE.'>'
		.'</td></tr></table></form>';
        } else {
			return '<font class="tiny">'._GALMEMBERSRATE.'</font>';
        }
}

function displayCommentsPic($pid, $orderby) {
	global $adminpath, $user, $admin, $imagepath, $prefix, $postcardverz, $db, $module_name;
	$pid = intval($pid);
	include("modules/$module_name/admin/config.php");
	$out = '<br><font class="option">'._GALCOMMPOST.'</font><hr>';
	$result3 = $db->sql_query("select cid, pid, comment, date, name, member from $prefix"._album_comments." where pid=$pid order by date ASC");
	if ($db->sql_numrows($result3) == 0) {
		$out .= '<center><font class="content">'._GALNOCOMM.'</font><hr size="1">';
	} else {
		$out .= '<table border="0" class="bgcolor4" cellpadding="2" cellspacing="1" width="100%" align="center">';

		while(list($cid, $cpid, $comment, $date, $name, $member) = $db->sql_fetchrow($result3)) {
			if ($member) {
				$poster = '<a href="index.php?name=Account&op=userinfo&username='.$name.'">'.underscoreTospace($name).'</a>';
			} else {
				$poster = ''.underscoreTospace($name).'';
			}
			$out .= '<tr>';
			$out .= '  <td align="left" class="bgcolor1" vAlign="top" width="100">'.$poster.'</td>'
			.'<td class="bgcolor1" align="center" width="70">'.substr($date,0,10).'</td><td class="bgcolor1">'.$comment.'</td>';

			if (is_admin($admin)) {
				$out .= '  <td align="center" class="bgcolor1" width="30"><a href="index.php?name=Album&file=index&do=deletecomment&cid='.$cid.'&pid='.$pid.'&orderby='.$orderby.'" onClick="return confirm(\''._GALSURE2DELETECOMMENT.'\')">'
				.'<img src="'.$imagepath.'/delete.gif" border="0" alt="'._DELETE.'"></a></td>';
			}
			$out .= '</tr>';
		}
		$out .= '</table><hr>';
	}
	if (is_user($user)) {
		$uname = explode(":", addslashes(base64_decode($user)));
		$uname = htmlspecialchars(substr($uname[1], 0, 25));
		$out .= '<form action="index.php?name=Album&file=index" method="POST"><input type="hidden" name="do" value="Post"><input type="hidden" name="gname" value="'.$uname.'"><input type="hidden" name="member" value="1">'
		.'<table border="0" cellpadding="3" cellspacing="0" align="center">'
		.'<tr><td>'._GALNAME.': </td><td>'.$uname.'</td></tr>'
		.'<tr><td>'._GALCOMMENT.': </td><td><input class="textbox" type="text" name="comment" size="80" maxlength="80"></td></tr>'
		.'<tr><td colspan="2" align="center"><input class="textbox" type="hidden" name="pid" value="'.$pid.'">'
		.'<input type="submit" value='._GALPOSTMEDIA.'></td></tr></form></table>';
	} elseif (!$galleryvar['anoncomments']) {
		$out .= '<form action="index.php?name=Album&file=index" method="POST"><input type="hidden" name="do" value="Post">'
		.'<table border="0" cellpadding="3" cellspacing="0" align="center">'
		.'<tr><td>'._GALNAME.': </td><td><input type="text" size="20" maxlength="20" name="gname"><input type="hidden" name="member" value="0"></td></tr>'
		.'<tr><td>'._GALCOMMENT.': </td><td><input class="textbox" type="text" name="comment" size="80" maxlength="80"></td></tr>'
		.'<tr><td colspan="2" align="center"><input class="textbox" type="hidden" name="pid" value="'.$pid.'">'
		.'<input type="submit" value='._GALPOSTMEDIA.'></td></tr></form></table>';
	} else {
		$out .= '<p align="center">'._GALREGISTER."</p>";
	}
	return $out;
}

function getPos($res, $pid) {
	global $db;
	$pid = intval($pid);
	$i=0;
	while ($row=$db->sql_fetchrow($res)) {
		if ($row[pid] == $pid) return $i;
		$i++;
	}
}

function rateCollector($pid, $rate) {
	global $cookiePrefix, $HTTP_COOKIE_VARS, $adminpath, $prefix, $postcardverz, $db, $module_name;
	$pid = intval($pid);
	include("modules/$module_name/admin/config.php");
	$ip = getenv("REMOTE_ADDR");
    $past = time()-1800;
    $db->sql_query("DELETE FROM $prefix"._album_rate_check." WHERE time < $past");
    $result = $db->sql_query("SELECT ip, pid FROM $prefix"._album_rate_check." WHERE pid=$pid");
    list($ips, $pids) = $db->sql_fetchrow($result);
    $ctime = time();
    if (($ip == $ips) && ($pid == $pids)) {
        $voteValid = 0;
    } else {
        $db->sql_query("INSERT INTO $prefix"._album_rate_check." (ip, time, pid) VALUES ('$ip', '$ctime', '$pid')");
        $voteValid = 1;
    }
	
	if ($setRateCookies > 0) {
		list($time) = $db->sql_fetchrow($db->sql_query("SELECT date FROM $prefix"._album_pictures." WHERE pid=$pid"));
		$timeStamp = formatTimestamp($time);
		$cookieName = $cookiePrefix.$timeStamp;
		if($HTTP_COOKIE_VARS["$cookieName"] == "1") {
			echo $warn = "You already voted today!";
			$voteValid = 0;
		} else {
			$cvalue = 1;
			setcookie("$cookieName",$cvalue,time()+86400);
		}
    }
    if ($voteValid > 0) {
		$db->sql_query("update $prefix"._album_pictures." set votes=votes+1, rate=(rate*(votes-1) + $rate)/votes where pid=$pid");
	}
    print '<html><head></head><body></body></html>';
}

function showpic($pid, $orderby) {
	global $adminpath, $prefix, $gallerypath, $user, $admin, $galleryvar, $PHP_SELF, $postcardverz, $db, $module_name, $sitename;
	$pid = intval($pid);
	include("modules/$module_name/admin/config.php");
	include("modules/$module_name/admin/postcardverz.php");
	echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"javascripts/ViewImage.js\"></script>";
	if (!isset($orderby) || $orderby=="") $orderby = $galleryvar['defaultsortmedia'];
	$prow = $db->sql_fetchrow($db->sql_query("SELECT p.*, UNIX_TIMESTAMP(p.date) AS unix_time, c.galloc, c.visible, t.templatePictures, t.templateCSS, f.displaytag, f.filetype, f.description AS description_media FROM $prefix"._album_pictures." AS p LEFT JOIN $prefix"._album_categories." AS c ON c.gallid=p.gid LEFT JOIN $prefix"._album_template_types." AS t ON t.id=c.template LEFT JOIN $prefix"._album_media_types." AS f ON f.extension=p.extension WHERE pid=$pid"));
	$ok=0;
	
	switch ($prow[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($prow[filetype] == 1) {
			$size = @getimagesize("$gallerypath/$prow[galloc]/$prow[img]");
		} else {
			$size[0] = $prow[width];
			$size[1] = $prow[height];
			$size[2] = $prow[description_media];
			$size[3] = "width=\"size[0]\" height=\"size[1]\"";
		}
		$navtree = navigationTree($prow[gid], $prow[pid]);
		$navpic = navigationPic($prow[name], $prow[gid], $prow[pid], $orderby);
		$navgall = navigationGall();
		$img = displayPicturePic($prow, $size);
		$date = displayDateAddedPic($prow);
		$submitter = displaySubmitterPic($prow, $size);
		$hits = displayHitsPic($prow);
		$rate = displayRatePic($prow);
		$ratingbar = displayRatingBarPic($prow, $orderby);
		$description = displayDescriptionPic($prow);

		if ($galleryvar['allowcomments']) $comments = displayCommentsPic($prow[pid], $orderby);
		if ($galleryvar['allowpostcard']) {
			$path = substr($PHP_SELF, 0, strlen($PHP_SELF) - strlen("/index.php"));
			if($prow[filetype] == 1) {
				$postcard = "<a href=\"#\"  onMouseOver=\"window.status='eCard';return true\" onMouseOut=\"window.status='';return true\" onClick=\"window.open('$postcardverz/index.php?picture=$path/$gallerypath/$prow[galloc]/$prow[img]','$target','width=$postcardwidth,height=$postcardheight,directories=no,location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,left=10,top=10,screenx=50,Screeny=50');return false\"><img src=\"$imagepath/mailcard.gif\" border=\"0\" align=\"left\" alt=\""._GALPOSTCARD."\">"._GALPOSTCARD."</a>";
			}
		}
		if ($galleryvar['allowprint'] && $prow[filetype] == 1)  {
			$dir = "../album/$prow[galloc]/";
			$pic = "$prow[img]";
			$width = "$prow[width]";
			$height = "$prow[height]";
			$target++;
			$print .= "<input type=\"button\" value=\""._GALPRINTMEDIA."\" onClick=\"ViewImage('$gallerypath/$prow[galloc]/$prow[img]', '$width', '$height', '$sitename - "._ALBUM_NAME." - $pic', '$description_media')\">";
		}

		if ($galleryvar['allowdownload']) {
			$thefile = substr($prow[img], 0, strrpos($prow[img],'.'));
			$zipfile = $thefile.".zip";
			$gzfile = $prow[img].".gz";
			if ($galleryvar['downloadmode']=="zip") {
				$thefile = $zipfile;
			} elseif ($galleryvar['downloadmode']=="gz") {
				$thefile = $gzfile;
			}
			$download = "<a class=\"tiny\" href=\"index.php?name=Album&file=index&do=getit&url=$prow[galloc]/$thefile&filename=$thefile\"><img src=\"$imagepath/hit.gif\" border=\"0\" align=\"middle\" alt=\"Download this Media\" ></a> <a class=\"content\" href=\"index.php?name=Album&file=index&do=getit&url=$prow[galloc]/$thefile&filename=$thefile\"> "._GALDOWNLOADMEDIA."</a>";
		}
	
		$template = $prow[templatePictures];
		$template = str_replace('<:IMAGE:>', $img, $template);
		$template = str_replace('<:DESCRIPTION:>', $description, $template);
		$template = str_replace('<:RATE:>', $rate, $template);
		$template = str_replace('<:RATINGBAR:>', $ratingbar, $template);
		$template = str_replace('<:SUBMITTER:>', $submitter, $template);
		$template = str_replace('<:NAMESIZE:>', $iname, $template);
		$template = str_replace('<:COMMENTS:>', $comments, $template);
		$template = str_replace('<:HITS:>', $hits, $template);
		$template = str_replace('<:DATE:>', $date, $template);
		$template = str_replace('<:RAWIMAGE:>', $prow[img], $template);
		$template = str_replace('<:RAWIMAGESIZE:>', $size[3], $template);
		$template = str_replace('<:RAWIMAGEWIDTH:>', $size[0], $template);
		$template = str_replace('<:RAWIMAGEHEIGHT:>', $size[1], $template);
		$template = str_replace('<:RAWDESCRIPTION:>', $prow[description], $template);
		$template = str_replace('<:RAWRATE:>', $prow[rate], $template);
		$template = str_replace('<:RAWVOTES:>', $prow[votes], $template);
		$template = str_replace('<:RAWSUBMITTER:>', $prow[submitter], $template);
		$template = str_replace('<:RAWNAME:>', $prow[name], $template);
		$template = str_replace('<:RAWHITS:>', $prow[hist], $template);
		$template = str_replace('<:RAWDATEBRIEF:>', strftime(_GALBRIEFDATE, $prow[unix_time]), $template);
		$template = str_replace('<:POSTCARD:>', $postcard, $template);
		$template = str_replace('<:PRINT:>', $print, $template);
		$template = str_replace('<:DOWNLOAD:>', $download, $template);
		print "$navgall $navtree";
		print "$navpic $template";
		CloseTable();
		include("footer.php");
	}
}
?>