<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2007                                            */
/* http://www.php-maximus.org                                           */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* This program is free software. You can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
/* Portage Coppermine Photo Gallery                                     */
/************************************************************************/
defined('MAXIMUS') or require_once("mainfile.php");

$pagetitle = "- Coppermine";


define('IN_COPPERMINE', true);
define('THUMBNAILS_PHP', true);
define('INDEX_PHP', true);

switch($func) {
default:
include_once('modules/Coppermine/include/constants.php');

if (defined('ADMIN_ALLOW')) {
	$coppg_retention = CACHE_ACOPPERMINE;
	$coppg_home      = "CPGThumbnailsAdmin$album$cat";
	$coppg_extension = "php";
} elseif ( defined('USER_ALLOW') ) {
	$coppg_retention = CACHE_UCOPPERMINE;
	$coppg_home      = "CPGThumbnailsUser$album$cat";
	$coppg_extension = "html";
} else {
	$coppg_retention = CACHE_VCOPPERMINE;
	$coppg_home      = "CPGThumbnails$album$cat";
	$coppg_extension = "html";
}

incoh("".$coppg_home."", "".$coppg_extension."", "".$coppg_retention."");


require('modules/Coppermine/include/init.inc.php');

function get_subcat_data($parent, &$album_set_array, $level) {

	$parent = secure( $parent, 'num', 1, 'sec' );
	$result = $GLOBALS['db']->sql_query("SELECT `cid`, `namee`, `description` FROM `".TABLE_CPG_CATEGORIES."` WHERE `parent` = '$parent'");
	if ($GLOBALS['db']->sql_numrows($result) > 0){
		$rowset = $GLOBALS['db']->sql_fetch_rowset($result);
		foreach ($rowset as $subcat){
			$result=$GLOBALS['db']->sql_query("SELECT `aid` FROM `".TABLE_CPG_ALBUMS."` WHERE `category` = {$subcat['cid']}");
			$album_count = $GLOBALS['db']->sql_numrows($result);
			while( $row = $GLOBALS['db']->sql_fetchrow($result)){
				$album_set_array[] = secure( $row['aid'], 'strip', 1 );
			} // while
		}
	}
}

/**************************************************************************
 * Main code
 **************************************************************************/

if (isset($_GET['sort']))	$USER['sort'] = $_GET['sort'];
if (isset($_GET['cat'])) $cat = secure( $_GET['cat'], 'num', 1, 'sec' );
if (isset($_GET['search'])){
 	$USER['search'] = secure( $_GET['search'], '', 1, 'sec' );
 	if (isset($_GET['type']) && $_GET['type'] == 'full') {
 	    $USER['search'] = '###'.$USER['search'];
 	}
 }

$album = secure( $_GET['album'], '', 1, 'sec' );
( !empty( $cat ) ) ? $cat = secure( $cat, 'num', 1, 'sec' ): $cat = '';
if (isset($_GET['page'])){
	$page = max((int)$_GET['page'], 1);
} else {
	$page = 1;
}

$breadcrumb = '';
$breadcrumb_text = '';
$cat_data = array();

// Build the private album set
if (!GALLERY_ADMIN_MODE && $CONFIG['allow_private_albums']) get_private_album_set();

if (is_numeric($album)){
	$result = $GLOBALS['db']->sql_query("SELECT `category`, `title` FROM `".TABLE_CPG_ALBUMS."` WHERE `aid`='$album'");
	if ($GLOBALS['db']->sql_numrows($result)>0) {
		$CURRENT_ALBUM_DATA = $GLOBALS['db']->sql_fetchrow($result);
		$actual_cat = stripslashes($CURRENT_ALBUM_DATA['category']);
		breadcrumb($actual_cat, $breadcrumb, $breadcrumb_text);
		$cat = -$album;
	}

} elseif (isset($cat) && $cat) { // Meta albums, we need to restrict the albums to the current category
	if ($cat < 0) {
		$result = $GLOBALS['db']->sql_query("SELECT `category`, `title` FROM `".TABLE_CPG_ALBUMS."` WHERE `aid`='".(-$cat)."'");
		if ($GLOBALS['db']->sql_numrows($result)>0) {
			$CURRENT_ALBUM_DATA = $GLOBALS['db']->sql_fetchrow($result);
			$actual_cat       = secure( $CURRENT_ALBUM_DATA['category'], 'strip', 1 );
			$CURRENT_CAT_NAME = secure( $CURRENT_ALBUM_DATA['title'], 'strip', 1 );
		}
		$ALBUM_SET .= 'AND `aid` IN ('.(-$cat).') ';
		breadcrumb($actual_cat, $breadcrumb, $breadcrumb_text);

	} else {
		$album_set_array = array();
		if ($cat == USER_GAL_CAT)
		    $where = '`category` > '.FIRST_USER_CAT;
		else
			$where = "`category` = '$cat'";

		$result= $GLOBALS['db']->sql_query("SELECT `aid` FROM `".TABLE_CPG_ALBUMS."` WHERE $where");
		while($row = $GLOBALS['db']->sql_fetchrow($result)){
			$album_set_array[] = secure( $row['aid'], 'strip', 1 );
		} // while
		if ($cat >= FIRST_USER_CAT) {
		    $result = $GLOBALS['db']->sql_query("SELECT `username` FROM `".TABLE_USERS."` WHERE `user_id` = '".($cat - FIRST_USER_CAT)."'");
			if ($GLOBALS['db']->sql_numrows($result) == 0) cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_cat'], __FILE__, __LINE__);
			$row = $GLOBALS['db']->sql_fetchrow($result);
			$username = secure( $row['username'], 'strip', 1 );
			$CURRENT_CAT_NAME = sprintf($lang_list_categories['xx_s_gallery'], $username);
		} else {
		    $result = $GLOBALS['db']->sql_query("SELECT `namee` FROM `".TABLE_CPG_CATEGORIES."` WHERE `cid` = '$cat'");
			if ($GLOBALS['db']->sql_numrows($result) == 0) cpg_die(CRITICAL_ERROR, $lang_errors['non_exist_cat'], __FILE__, __LINE__);
			$row = $GLOBALS['db']->sql_fetchrow($result);
			$CURRENT_CAT_NAME = secure( $row['namee'], 'strip', 1 );
		}
		get_subcat_data($cat, $album_set_array, $CONFIG['subcat_level']);

		// Treat the album set
		if (count($album_set_array)) {
			$set ='';
		    foreach ($album_set_array as $album_id) $set .= ($set == '') ? $album_id : ','.$album_id;
			$ALBUM_SET .= "AND `aid` IN ($set) ";
		}
		breadcrumb($cat, $breadcrumb, $breadcrumb_text);
	}
}

pageheader(isset($CURRENT_ALBUM_DATA) ? $CURRENT_ALBUM_DATA['title'] : $lang_meta_album_names[$album]);
if ($breadcrumb) theme_display_cat_list($breadcrumb, $cat_data, '');
display_thumbnails($album, (isset($cat) ? $cat : 0), $page, $CONFIG['thumbcols'], $CONFIG['thumbrows'], true);
pagefooter();
$GLOBALS['db']->sql_free($result);

incof("".$coppg_home."", "".$coppg_extension."", "".$coppg_retention."");
break;
}