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

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

$module_name = basename(dirname(__FILE__));
include("modules/$module_name/admin/config.php");
get_lang($module_name);
$pagetitle = "$defis "._ALBUM_NAME."";

function underscoreTospace($module_name) {
	$module_name = str_replace("_"," ",$module_name);
	return $module_name;
}

function galleryHeader() {
	global $galleryvar, $module_name;
	include('header.php');
	OpenTable();
	echo "<center><font class=\"title\"><b>Галерея</b></font><br><br>";
	if ($galleryvar['displaySearchForm']) {
		include("modules/Album/public/search.php");
		print search_form($GLOBALS[HTTP_POST_VARS]);
	}
	echo "</center>";
	CloseTable();
}

function navigationGall() {
	global $galleryvar, $user, $admin, $prefix, $module_name, $db;

	if (is_admin($admin)) {
		$row = $db->sql_fetchrow($db->sql_query("SELECT COUNT(p.pid) AS total FROM $prefix"._album_pictures." AS p LEFT JOIN $prefix"._album_categories." AS c ON c.gallid=p.gid WHERE c.visible>=0 AND (TO_DAYS(NOW()) - TO_DAYS(p.date))<7"));
	} else {
		$row = $db->sql_fetchrow($db->sql_query("SELECT COUNT(p.pid) AS total FROM $prefix"._album_pictures." AS p LEFT JOIN $prefix"._album_categories." AS c ON c.gallid=p.gid WHERE c.visible>0 AND (TO_DAYS(NOW()) - TO_DAYS(p.date))<7"));
	}
	
	if (is_admin($admin)) {
		$row2 = $db->sql_fetchrow($db->sql_query("SELECT SUM(total) AS total FROM $prefix"._album_categories." WHERE visible>=0 AND parent=-1"));
	} else {
		$row2 = $db->sql_fetchrow($db->sql_query("SELECT SUM(total) AS total FROM $prefix"._album_categories." WHERE visible>0 AND parent=-1"));
	}
	$out = '<table border="0" cellspacing="0" cellpadding="5" align="center"><tr><td><a class="title" href="index.php?name=Album&file=index"><img src="modules/Album/images/menu-icon-home.gif" border="0"></a></td><td><a class="title" href="index.php?name=Album&file=index">'._GALHOME.'</a></td>';
        
	if ($galleryvar['displaytop']) {
		$out .= '<td><font class="title">&nbsp;&nbsp;&nbsp;</font><a class="title" href="index.php?name=Album&file=index&do=top"><img src="modules/Album/images/menu-icon-top10.gif" border="0"></a></td><td><a class="title" href="index.php?name=Album&file=index&do=top">'._GALTOP10.'</a></td>';
	}

	if ($galleryvar['allowpostpics']) {
		$out .= '<td><font class="title">&nbsp;&nbsp;&nbsp;</font><a class="title" href="index.php?name=Album&file=index&do=upload"><img src="modules/Album/images/menu-icon-submit.gif" border="0"></a></td><td><a class="title" href="index.php?name=Album&file=index&do=upload">'._GALPOSTMEDIA.'</a></td>';
	}
	$out .= '</tr></table><center>';
	
	list($files_num) = $db->sql_fetchrow($db->sql_query("SELECT Count(*) FROM ".$prefix."_album_pictures"));
	
	$out .= ''._THEREARE.': <b>'.$files_num.'</b> '._GALIMAGES.'';
	if ($row[total] && !$do) {
		$out .= ' (<b>'.$row[total].'</b> '._GALNEWIMAGES.')</center>';
	}
	$out .= '<hr>';
	return $out;
}

function recursiveBuild($gid) {
	global $prefix, $db;
	$gid = intval($gid);

	if ($gid > 0) {
		$sql = "SELECT gallname, parent from $prefix"._album_categories." WHERE gallid=$gid";
		list($gname, $parent) = $db->sql_fetchrow($db->sql_query($sql));
	} else {
		return "";
	}

	if ($parent > 0) {
		$gid = intval($gid);
		$out .= recursiveBuild($parent);
		$out .= ' &gt;&gt; <a href="index.php?name=Album&file=index&do=showgall&amp;gid='.$gid.'"><b>'.underscoreTospace($gname).'</b></a>';
		return $out;
	} else {
		$gid = intval($gid);
	}
	return ' &gt;&gt; <a href="index.php?name=Album&file=index&do=showgall&amp;gid='.$gid.'"><b>'.underscoreTospace($gname).'</b></a>';
}

function navigationTree($gid, $pid) {
	global $bgcolor2, $prefix, $db;
	
	$gid = intval($gid);
	$pid = intval($pid);
	$sql = "SELECT gallname, parent from $prefix"._album_categories." WHERE gallid=$gid";
	list($gallname, $parent) = $db->sql_fetchrow($db->sql_query($sql));
	$out .= '<table width="100%" border="0" cellspacing="0" cellpadding="0"><tr><td>';
	$out .= '<a class="title" href="index.php?name=Album&file=index">'._GALHOME.'</a>';
	$out .= recursiveBuild($parent);

	if (isset($pid) && $pid!="") {
		$gid = intval($gid);
		list($module_name) = $db->sql_fetchrow($db->sql_query("SELECT name from $prefix"._album_pictures." WHERE pid=$pid"));
		$out .= ' &gt;&gt; <a href="index.php?name=Album&file=index&do=showgall&amp;gid='.$gid.'"><b>'.underscoreTospace($gallname).'</b></a>';
		$out .= ' &gt;&gt; <b>'.underscoreTospace($module_name).'</b>';
	} else {
		$out .= ' &gt;&gt; <b>'.underscoreTospace($gallname).'</b>';
	}
	$out .= '</td></tr></table><hr>';
	return $out;
}

function postcomment($pid, $comment, $gname, $member) {
	global $prefix, $db;
	$pid = intval($pid);
	$comment = FixQuotes($comment);
	$gname = FixQuotes($gname);
	$db->sql_query("insert into $prefix"._album_comments." values (NULL, '$pid', '$comment', now(), '$gname', $member)");
	update_points(1);
}

function indent($gid) {
	global $prefix, $db;
	$tab = 0;
	$gid = intval($gid);
	list($parent) = $db->sql_fetchrow($db->sql_query("SELECT parent FROM $prefix"._album_categories." WHERE gallid=$gid"));
	
	while ($parent != -1) {
		list($parent) = $db->sql_fetchrow($db->sql_query("SELECT parent FROM $prefix"._album_categories." WHERE gallid=$parent"));
		$tab ++;
	}
	return $tab;
}

function convertorderbyin($orderby) {
    if ($orderby == "titleA")	$orderby = "name ASC";
    if ($orderby == "dateA")	$orderby = "date ASC";
    if ($orderby == "hitsA")	$orderby = "counter ASC";
    if ($orderby == "ratingA")	$orderby = "rate ASC";
    if ($orderby == "titleD")	$orderby = "name DESC";
    if ($orderby == "dateD")	$orderby = "date DESC";
    if ($orderby == "hitsD")	$orderby = "counter DESC";
    if ($orderby == "ratingD")	$orderby = "rate DESC";
    return $orderby;
}

function convertorderbyout($orderby) {
    if ($orderby == "name ASC")		$orderby = "titleA";
    if ($orderby == "date ASC")		$orderby = "dateA";
    if ($orderby == "counter ASC")	$orderby = "hitsA";
    if ($orderby == "rate ASC")		$orderby = "ratingA";
    if ($orderby == "name DESC")	$orderby = "titleD";
    if ($orderby == "date DESC")	$orderby = "dateD";
    if ($orderby == "counter DESC")	$orderby = "hitsD";
    if ($orderby == "rate DESC")	$orderby = "ratingD";
    return $orderby;
}

switch($do) {

	case "showgall":
		$gid = intval($gid);
		include("modules/Album/public/displayCategory.php");
		showgall($gid, $offset, $orderby);
	break;

	case "showpic":
		$pid = intval($pid);
		include("modules/Album/public/displayMedia.php");
		$db->sql_query("UPDATE $prefix"._album_pictures." set counter=counter+1 WHERE pid=$pid");
		showpic($pid, $orderby);
	break;

	case "top":
		include("modules/Album/public/displayTop.php");
		top();
	break;

	case "New":
		newpics();
	break;

	case "Post":
		$pid = intval($pid);
		postcomment($pid, $comment, $gname, $member);
		Header("Location: index.php?name=$module_name&file=index&do=showpic&pid=$pid");
		exit;
	break;

	case "Vote":
		$pid = intval($pid);
		include("modules/Album/public/displayMedia.php");
		rateCollector($pid, $rate);
		showpic($pid, $orderby);
	break;

	case "upload":
		include("modules/Album/public/uploadFile.php");
		include("modules/Album/admin/fileFunctions.php");
		
		if (isset($add) && $add == "Upload" && $Category != "") {
			$userfile_name = $_FILES['userfile']['name'];
			$ext = substr($userfile_name, (strrpos($userfile_name,'.') +  1));
			$result = $db->sql_query("SELECT filetype FROM ".$prefix."_album_media_types WHERE extension='$ext'");
			if ($db->sql_numrows($result) == 0) {
				galleryHeader();
				OpenTable();
				echo navigationGall();
				echo '<br><center>'._GALTYPENOTSUPPORTED.'<br><br>'._GOBACK.'</center>';
				CloseTable();
				include("footer.php");
			} else {
				$userfile = $_FILES['userfile']['tmp_name'];
				$userfile_size = $_FILES['userfile']['size'];
				$upload_return = Add($Category, $userfile_name, $Submitter, $MediaName, $Description, $userfile, $userfile_name, $userfile_size);
				if ($upload_return!="OK") {
					galleryHeader();
					OpenTable();
					echo navigationGall();
					echo '<br><center>'.$upload_return.'<br><br>'._GOBACK.'</center>';
					CloseTable();
					include("footer.php");
				} else {
					galleryHeader();
					OpenTable();
					echo navigationGall();
					echo '<p align="center"><b>'._GALMEDIARECEIVED."</b><br>";
					echo _GALCHECKFORIT.'</p>';
					CloseTable();
					include("footer.php");
				}
			}
		} else {
			if (is_user($user)) {
				$result = $db->sql_query("SELECT gallid FROM $prefix"._album_categories." WHERE visible>0");
			} else {
				$result = $db->sql_query("SELECT gallid FROM $prefix"._album_categories." WHERE visible>1");
			}
			if ($db->sql_numrows($result)!=0) {
				upload_file();
			} else {
				galleryHeader();
				OpenTable();
				echo navigationGall();
				echo "<p align=\"center\"><font class=\"title\">No Categories Found</font></p>";
				CloseTable();
				include("footer.php");
			}
		}
	break;

	case 'search':
		include("modules/Album/public/displayCategory.php");
		include "modules/Album/public/search.php";
		search_go($HTTP_POST_VARS);
	break;

	case 'deletecomment':
		$pid = intval($pid);
		include("modules/Album/public/displayMedia.php");
		$sql = "DELETE FROM $prefix"._album_comments." WHERE cid=$cid";
		$db->sql_query($sql);
		showpic($pid, $orderby);
	break;

	case "getit":
		$url = "modules/Album/album/".$url;
		if (!strstr($url, "..") && file_exists($url)) {
			Header("Content-type: application/octet-stream");
			Header("Location: $url");
		} else {
			galleryHeader();
			OpenTable();
			echo navigationGall();
			echo '<p align="center"><b>'._GALFILENOTFOUND.'</b><br><br>'._GOBACK.'</p>';
			CloseTable();
			include("footer.php");
		}
	break;
	
	default:
		galleryHeader();
		include("modules/Album/public/mainGallery.php");
		viewcats();
	break;
}
?>