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

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

?>
<script language="JavaScript">
function ViewImage(ifile,ix,iy,ititle) {
	var win;
	var sWidth;
	var sHeight;
	win = window.open("","imageviewer","width="+ix+",height="+iy+",menubar=no,toolbar=no");
	win.document.open();
	win.document.write("<html><head><title>"+ititle+"</title>");
	win.document.write("</head><body>");
	win.document.write('<div style="position:absolute; width:'+ix+'px; height:'+iy+'px; left:0px;top:0px">');
	win.document.write('<a href="#" onclick="self.close()"><img src='+ifile+' border="0"></a></div></body></html>');
	win.document.close();
}
</script>
<?php

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&amp;gid='.$gid.'&amp;pid='.$prev.'&amp;orderby='.$orderby.'"><img src="modules/Album/images/left.png" 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&amp;gid='.$gid.'&amp;pid='.$next.'&amp;orderby='.$orderby.'"><img src="modules/Album/images/right.png" 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) {
	global $bgcolor1, $bgcolor4;
	
	if ($row[description]!="") {
		return '<table border="0" cellpadding="2" cellspacing="0" width="100%"><tr><td align="center">'.$row[description].'</td></tr></table>';
	} else {
		return "К сожалению, описание нет";
	}
}

function displaySubmitterPic($row, $size) {
	global $bgcolor1, $bgcolor4, $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=Your_Account&op=userinfo&amp;username='.$row[submitter].'" target="_blank">'.$row[submitter].'</a>';
	}
	return ''._GALSUBMITTER.': <a href="index.php?name=Your_Account&op=userinfo&amp;username='.$row[submitter].'" target="_blank">'.$row[submitter].'</a> | '._GALSIZE.': '.$size[0].' x '.$size[1].'';
}

function displayDateAddedPic($row) {
	global $bgcolor1, $bgcolor4;
	
	return ''._GALADDED.': '.strftime(_GALDATEBRIEF, $row[unix_time]).'';
}

function displayHitsPic($row) {
	global $bgcolor1, $bgcolor4;

	return ''._GALHITS.': '.$row[counter].'';
}

function displayRatePic($row) {
	global $galleryvar, $bgcolor1, $bgcolor4, $postcardverz;
        
	if(!$galleryvar['allowrate']) {
		return;
	}
	return ''._GALVOTES.': '.$row[votes].' | '._GALRATING.': '.$row[rate].'';
}

function displayRatingBarPic($row, $orderby) {
	global $galleryvar, $user, $imagepath, $postcardverz;
	
	if(!$galleryvar['allowrate']) {
		return;
	}
	
	if(!$galleryvar['anonrate'] || $user) {
		return  '<table border="0" cellpadding="0" cellspacing="0">'
		.'<form action="index.php?name=Album&file=index&do=Vote" method="post">'
		.'<tr><td>'
		.'<input type="hidden" name="pid" value="'.$row[pid].'">'
		.'<input type="hidden" name="orderby" value="'.$orderby.'">'
		.'<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>'
		.'</form></table>';
        } else {
			return '<font class="tiny">'._GALMEMBERSRATE.'</font>';
        }
}

function displayCommentsPic($pid, $orderby) {
	global $adminpath, $user, $admin, $imagepath, $prefix, $bgcolor1, $bgcolor4, $postcardverz, $db, $module_name, $cookie;

	$pid = intval($pid);
	include("modules/$module_name/admin/config.php");

	$out = '<br><font class="tiny"><b>'._GALCOMMPOST.'</b></font><hr size="1">';

	$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 .= '<table border="0" cellpadding="0" cellspacing="0"><tr><td><img src="images/admin/info.gif"></td><td class="option" align="center">&nbsp;'._GALNOCOMM.'&nbsp;</td><td><img src="images/admin/info.gif"></td></tr></table><hr size="1">';
	} else {
#serg_pnz-комментарий: начало таблицы
		$out .= '<table border="0" class=bgcolor4 cellpadding="3" cellspacing="1" width="100%" align="center" class="bgcolor4">';

		while(list($cid, $cpid, $comment, $date, $name, $member) = $db->sql_fetchrow($result3)) {
			if ($member) {
				$poster = '<a href="index.php?name=Your_Account&op=userinfo&amp;username='.$name.'">'.underscoreTospace($name).'</a>';
			} else {
				$poster = ''.underscoreTospace($name).'';
			}
			$out .= '<tr class="bgcolor1">';
#serg_pnz-комментарий: настройка цвета ячеек, а вообще таблица начинается 8-ью строками выше
			$out .= '  <td width="90" align="center">'.$poster.'</td>'
			.'<td align="center" width="70">'.substr($date,0,10).'</td><td>'.$comment.'</td>';


			if (is_admin($admin)) {
#serg_pnz-комментарий: настройка цвета ячеек
				$out .= '  <td width="20" align="center"><a href="index.php?name=Album&file=index&do=deletecomment&amp;cid='.$cid.'&amp;pid='.$pid.'&amp;orderby='.$orderby.'" onClick="return confirm(\''._GALSURE2DELETECOMMENT.'\')">'
				.'<img src="'.$imagepath.'/delete.png" border="0" alt="'._DELETE.'"></a></td>';
			}
			$out .= '</tr>';
		}
		$out .= '</table><hr>';
	}
	if (is_user($user)) {
#		cookiedecode($user);
		$out .= '<table border="0" cellpadding="2" cellspacing="1" align="center" class="bgcolor4">' 
		.'<form action="index.php?name=Album&file=index" method="POST">'
		.'<input type="hidden" name="do" value="Post">'
		.'<input type="hidden" name="gname" value="'.$user[1].'">'
		.'<input type="hidden" name="member" value="1">'
		.'<tr class="bgcolor1"><td><img src="'.$imagepath.'/com.png" border="0" title="Комментарий"></td>'
		.'<td><input class="textbox" type="text" name="comment" size="80" maxlength="255">'
		.'<input class="textbox" type="hidden" name="pid" value="'.$pid.'">'
		.'</td><td><input type="submit" value='._GALPOSTMEDIA.'></td>'
		.'</td></tr>'
		.'</form></table>';
	} elseif (!$galleryvar['anoncomments']) {
		$out .= '<table border="0" cellpadding="3" cellspacing="1" align="center" class="bgcolor4">'
		.'<form action="index.php?name=Album&file=index" method="post">'
		.'<input type="hidden" name="do" value="Post">'
		.'<tr class="bgcolor1"><td align="center"><img src="'.$imagepath.'/guest.png" border="0" title="Автор"></td><td><input type="text" SIZE="20" MAXLENGTH="20" name="gname" value="Гость">'
		.'<input type="hidden" name="member" value="0"></td><td></td></tr>'
		.'<tr class="bgcolor1"><td><img src="'.$imagepath.'/com.png" border="0" title="Комментарий"></td>'
		.'<td><input class="textbox" type="text" name="comment" size="80" maxlength="255"></td>'
		.'<td><input class="textbox" type="hidden" name="pid" value="'.$pid.'">'
		.'<input type="submit" value='._GALPOSTMEDIA.'></form></td></tr></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");

	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("/modules.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')\">&nbsp;";
		}

		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 = "&nbsp;<td><a class=\"tiny\" href=\"index.php?name=Album&file=index&do=getit&amp;url=$prow[galloc]/$thefile&amp;filename=$thefile\"><img src=\"$imagepath/hit.png\" border=\"0\" align=\"middle\" alt=\"Download this Media\" ></a></td><td>&nbsp;<a class=\"content\" href=\"index.php?name=Album&file=index&do=getit&amp;url=$prow[galloc]/$thefile&amp;filename=$thefile\">"._GALDOWNLOADMEDIA."</a></td>";
		}
	
		$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");
	}
}
?>