<?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('DISPLAYIMAGE_PHP', true);

switch($func) {
default:

include_once('modules/Coppermine/include/constants.php');

if (defined('ADMIN_ALLOW')) {
	$coppg_retention = CACHE_ACOPPERMINE;
	$coppg_home      = "CPGDisplayAdmin$pos$cat$slideshow$album$pid";
	$coppg_extension = "php";
} elseif ( defined('USER_ALLOW') ) {
	$coppg_retention = CACHE_UCOPPERMINE;
	$coppg_home      = "CPGDisplayUser$pos$cat$slideshow$album$pid";
	$coppg_extension = "html";
} else {
	$coppg_retention = CACHE_VCOPPERMINE;
	$coppg_home      = "CPGDisplay$pos$cat$slideshow$album$pid";
	$coppg_extension = "html";
}

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


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

if($CONFIG['enable_smilies']) include("modules/Coppermine/include/smilies.inc.php");

if($CONFIG['read_exif_data'] && function_exists('exif_read_data')){
 	include("modules/Coppermine/include/exif_php.inc.php");
 } elseif($CONFIG['read_exif_data']){
     cpg_die(CRITICAL_ERROR, 'PHP running on your server does not support reading EXIF data in JPEG files, please turn this off on the config page', __FILE__, __LINE__);
 }

# Sanitize the data - to fix the XSS vulnerability - Aditya
function sanitize_data(&$value, $key)
{
	if (is_array($value)) {
		array_walk($value, 'sanitize_data');
	} else {
		# sanitize against sql/html injection; trim any nongraphical non-ASCII character:
		$value = trim(htmlentities(strip_tags(trim($value,"\x7f..\xff\x0..\x1f")),ENT_QUOTES));
	}
}

/**************************************************************************
 * Local functions definition
 **************************************************************************/

function html_picture_menu($id)
{
	global $lang_display_image_php;
	$id = $GLOBALS['db']->XMFnum( $id );
	return <<<EOT
<div align="center">
<a href="modules.php?name=Coppermine&amp;op=editOnePic&amp;id=$id&amp;what=picture"  class="adm_menu" onclick="return confirm('{$lang_display_image_php['confirm_mod']}');">{$lang_display_image_php['mod_pic']}</a> -
<a href="modules.php?name=Coppermine&amp;op=delete&amp;id=$id&amp;what=picture"  class="adm_menu" onclick="return confirm('{$lang_display_image_php['confirm_del']}');">{$lang_display_image_php['del_pic']}</a></div>

EOT;
}

// Prints the image-navigation menu
function html_img_nav_menu()
{
	global $CONFIG, $CURRENT_PIC_DATA, $PHP_SELF;
	global $album, $cat, $pos, $pic_count, $lang_img_nav_bar, $template_img_navbar;

	$cat_link= is_numeric($album) ? '' : '&amp;cat='.$cat;

	$human_pos = $pos + 1;
	$page = ceil(($pos+1) / ($CONFIG['thumbrows'] * $CONFIG['thumbcols']));
	$pid = $CURRENT_PIC_DATA['pid'];

	if ($pos > 0) {
		$prev = $pos - 1;
		$prev_tgt = "modules.php?name=Coppermine&amp;op=displayimage&amp;album=$album$cat_link&amp;pos=$prev";
		$prev_title = $lang_img_nav_bar['prev_title'];
	} else {
		$prev_tgt = "javascript:;";
		$prev_title = "";
	}
	if ($pos < ($pic_count -1)) {
		$next = $pos + 1;
		$next_tgt = "modules.php?name=Coppermine&amp;op=displayimage&amp;album=$album$cat_link&amp;pos=$next";
		$next_title = $lang_img_nav_bar['next_title'];
	} else {
		$next_tgt = "javascript:;";
		$next_title = "";
	}

	if (USER_CAN_SEND_ECARDS){
		$ecard_tgt = "modules.php?name=Coppermine&amp;op=ecard&amp;album=$album$cat_link&amp;pid=$pid&amp;pos=$pos";
		$ecard_title = $lang_img_nav_bar['ecard_title'];
	} else {
		$ecard_tgt = "javascript:alert('".addslashes($lang_img_nav_bar['ecard_disabled_msg'])."');";
		$ecard_title = $lang_img_nav_bar['ecard_disabled'];
	}

	$thumb_tgt = "modules.php?name=Coppermine&amp;op=thumbnails&amp;album=$album$cat_link&amp;page=$page";

	$slideshow_tgt = "modules.php?name=Coppermine&amp;op=displayimage&amp;album=$album$cat_link&amp;pid=$pid&amp;slideshow=5000";

	$pic_pos = sprintf($lang_img_nav_bar['pic_pos'], $human_pos, $pic_count);

	$params = array(
		'{THUMB_TGT}' => $thumb_tgt,
		'{THUMB_TITLE}' => $lang_img_nav_bar['thumb_title'],
		'{PIC_INFO_TITLE}' => $lang_img_nav_bar['pic_info_title'],
		'{SLIDESHOW_TGT}' => $slideshow_tgt,
		'{SLIDESHOW_TITLE}' => $lang_img_nav_bar['slideshow_title'],
		'{PIC_POS}' => $pic_pos,
		'{ECARD_TGT}' => $ecard_tgt,
		'{ECARD_TITLE}' => $ecard_title,
		'{PREV_TGT}' => $prev_tgt,
		'{PREV_TITLE}' => $prev_title,
		'{NEXT_TGT}' => $next_tgt,
		'{NEXT_TITLE}' => $next_title,
	);

    return template_eval($template_img_navbar, $params);
}

// Displays a picture
function html_picture()
{
	global $CONFIG, $CURRENT_PIC_DATA, $CURRENT_ALBUM_DATA, $USER, $HTTP_COOKIE_VARS;
	global $album, $comment_date_fmt, $template_display_picture;
	global $lang_display_image_php;

	$pid = $CURRENT_PIC_DATA['pid'];

	// Add 1 to hit counter unless the user reloaded the page
	if (!isset($USER['liv']) || !is_array($USER['liv'])) {
 	    $USER['liv'] = array();
 	}

 	// Add 1 to hit counter
 	if ($album != "lasthits" && !in_array($pid, $USER['liv']) && isset($HTTP_COOKIE_VARS[$CONFIG['cookie_name'].'_data'])){
  		add_hit($pid);
 		if (count($USER['liv']) > 4 ) array_shift($USER['liv']);
 		array_push($USER['liv'], $pid);

	}

	if ($CONFIG['make_intermediate'] && max($CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight']) > $CONFIG['picture_width'])  {
	    $picture_url = get_pic_url($CURRENT_PIC_DATA, 'normal');
	} else {
	    $picture_url = get_pic_url($CURRENT_PIC_DATA, 'fullsize');
	}

	$picture_menu =	((USER_ADMIN_MODE && $CURRENT_ALBUM_DATA['category'] == FIRST_USER_CAT + USER_ID) || GALLERY_ADMIN_MODE) ? html_picture_menu($pid) : '';

	$image_size = compute_img_size($CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight'], $CONFIG['picture_width']);

	if (isset($image_size['reduced'])) {
		$winsizeX = $CURRENT_PIC_DATA['pwidth'] + 16;
		$winsizeY = $CURRENT_PIC_DATA['pheight'] + 16;
		$pic_html = "<a href=\"javascript:;\" onClick=\"MM_openBrWindow('modules.php?name=Coppermine&op=displayimagepopup&pid=$pid&fullsize=1','".uniqid(rand())."','toolbar=yes,status=yes,resizable=yes,width=$winsizeX,height=$winsizeY')\">";
		$pic_html .= "<img src=\"".$picture_url."\" {$image_size['geom']} class=\"image\" border=\"0\" alt=\"{$lang_display_image_php['view_fs']}\" /><br />";
		$pic_html .= "</a>\n";
	} else {
		$pic_html = "<img src=\"".$picture_url."\" {$image_size['geom']} class=\"image\" border=\"0\" alt='' /><br />\n";
	}

	if (!$CURRENT_PIC_DATA['title'] && !$CURRENT_PIC_DATA['caption']) {
		template_extract_block($template_display_picture, 'img_desc');
	} else {
		if (!$CURRENT_PIC_DATA['title']) {
			template_extract_block($template_display_picture, 'title');
		}
		if (!$CURRENT_PIC_DATA['caption']) {
			template_extract_block($template_display_picture, 'caption');
		}
	}

	$params = array(
		'{CELL_HEIGHT}' => '100',
		'{IMAGE}' => $pic_html,
		'{ADMIN_MENU}' => $picture_menu,
		'{TITLE}' => $CURRENT_PIC_DATA['title'],
		'{CAPTION}' => bb_decode($CURRENT_PIC_DATA['caption']),
	);

	return template_eval($template_display_picture, $params);
}

function html_rating_box()
{
	global $CONFIG, $CURRENT_PIC_DATA, $CURRENT_ALBUM_DATA;
	global $template_image_rating, $lang_rate_pic;

	if (!(USER_CAN_RATE_PICTURES && $CURRENT_ALBUM_DATA['votes'] == 'YES')) return '';

	$votes = $CURRENT_PIC_DATA['votes'] ? sprintf($lang_rate_pic['rating'], round($CURRENT_PIC_DATA['pic_rating']/2000, 1), $CURRENT_PIC_DATA['votes']) : $lang_rate_pic['no_votes'];
	$pid = $CURRENT_PIC_DATA['pid'];

	$params = array(
		'{TITLE}' => $lang_rate_pic['rate_this_pic'],
		'{VOTES}' => $votes,
		'{RATE0}' => "modules.php?name=Coppermine&amp;op=ratepic&amp;pic=$pid&amp;rate=0",
		'{RATE1}' => "modules.php?name=Coppermine&amp;op=ratepic&amp;pic=$pid&amp;rate=1",
		'{RATE2}' => "modules.php?name=Coppermine&amp;op=ratepic&amp;pic=$pid&amp;rate=2",
		'{RATE3}' => "modules.php?name=Coppermine&amp;op=ratepic&amp;pic=$pid&amp;rate=3",
		'{RATE4}' => "modules.php?name=Coppermine&amp;op=ratepic&amp;pic=$pid&amp;rate=4",
		'{RATE5}' => "modules.php?name=Coppermine&amp;op=ratepic&amp;pic=$pid&amp;rate=5",
		'{RUBBISH}' => $lang_rate_pic['rubbish'],
		'{POOR}' => $lang_rate_pic['poor'],
		'{FAIR}' => $lang_rate_pic['fair'],
		'{GOOD}' => $lang_rate_pic['good'],
		'{EXCELLENT}' => $lang_rate_pic['excellent'],
		'{GREAT}' => $lang_rate_pic['great'],
	);

	return template_eval($template_image_rating, $params);
}

// Display picture information
function html_picinfo()
{
	global $CONFIG, $CURRENT_PIC_DATA, $CURRENT_ALBUM_DATA;
	global $album, $lang_picinfo, $lang_display_image_php, $lang_byte_units;

	$info[$lang_picinfo['Filename']]   = htmlspecialchars($CURRENT_PIC_DATA['filename']);
	$info[$lang_picinfo['Album name']] = '<span class="alblink"><a href="modules.php?name=Coppermine&amp;op=thumbnails&amp;album=' . $CURRENT_PIC_DATA['aid'] . '">' . $CURRENT_ALBUM_DATA['title'].'</a></span>';

	if ($CURRENT_PIC_DATA['votes'] > 0) {
	    $info[sprintf($lang_picinfo['Rating'], $CURRENT_PIC_DATA['votes'])] = '<img src="modules/Coppermine/images/rating'.round($CURRENT_PIC_DATA['pic_rating']/2000).'.gif" align="absmiddle"/>';
	}

	$info[$lang_picinfo['Keywords']]   = '<span class="alblink">'.preg_replace("/(\S+)/","<a href=\"modules.php?name=Coppermine&amp;op=thumbnails&amp;album=search&amp;search=\\1\">\\1</a>" , $CURRENT_PIC_DATA['keywords']).'</span>';

	for ($i =1; $i<= 4; $i++){
		if ($CONFIG['user_field'.$i.'_name']) {
		    $info[$CONFIG['user_field'.$i.'_name']] = make_clickable($CURRENT_PIC_DATA['user'.$i]);
		}
	}

	$info[$lang_picinfo['File Size']]  = ($CURRENT_PIC_DATA['filesize'] > 10240 ? ($CURRENT_PIC_DATA['filesize'] >> 10).' '.$lang_byte_units[1] : $CURRENT_PIC_DATA['filesize'].' '.$lang_byte_units[0]);
	$info[$lang_picinfo['Dimensions']] = sprintf($lang_display_image_php['size'], $CURRENT_PIC_DATA['pwidth'], $CURRENT_PIC_DATA['pheight']);
	$info[$lang_picinfo['Displayed']]  = sprintf($lang_display_image_php['views'], $CURRENT_PIC_DATA['hits']);

	$path_to_pic = $CONFIG['fullpath'].$CURRENT_PIC_DATA['filepath'].$CURRENT_PIC_DATA['filename'];

	if ($CONFIG['read_exif_data']) $exif = exif_parse_file($path_to_pic);

        if (isset($exif) && is_array($exif)) {
		array_walk($exif, 'sanitize_data');
                $info = array_merge($info,$exif);
        }

	if (isset($exif) && is_array($exif)){
		if (isset($exif['Camera'])) $info[$lang_picinfo['Camera']] = $exif['Camera'];
		if (isset($exif['DateTaken'])) $info[$lang_picinfo['Date taken']] = $exif['DateTaken'];
		if (isset($exif['Aperture'])) $info[$lang_picinfo['Aperture']] = $exif['Aperture'];
		if (isset($exif['ExposureTime'])) $info[$lang_picinfo['Exposure time']] = $exif['ExposureTime'];
		if (isset($exif['FocalLength']))  $info[$lang_picinfo['Focal length']] = $exif['FocalLength'];
		if (isset($exif['Comment'])) $info[$lang_picinfo['Comment']] = $exif['Comment'];
	}

	return theme_html_picinfo($info);
}


// Displays comments for a specific picture
function html_comments($pid)
{
	global $CONFIG, $USER, $CURRENT_ALBUM_DATA, $comment_date_fmt, $HTML_SUBST;
	global $template_image_comments, $template_add_your_comment, $lang_display_comments;

	$html = '';

	if (!$CONFIG['enable_smilies']) {
		$tmpl_comment_edit_box = template_extract_block($template_image_comments, 'edit_box_no_smilies','{EDIT}');
		template_extract_block($template_image_comments, 'edit_box_smilies');
		template_extract_block($template_add_your_comment, 'input_box_smilies');
	} else {
		$tmpl_comment_edit_box = template_extract_block($template_image_comments, 'edit_box_smilies', '{EDIT}');
		template_extract_block($template_image_comments, 'edit_box_no_smilies');
		template_extract_block($template_add_your_comment, 'input_box_no_smilies');
	}

	$tmpl_comments_buttons = template_extract_block($template_image_comments, 'buttons', '{BUTTONS}');

	$result = $GLOBALS['db']->sql_query("SELECT `msg_id`, `msg_author`, `msg_body`, UNIX_TIMESTAMP(msg_date) AS msg_date, `author_id`, `author_md5_id` FROM `".TABLE_CPG_COMMENTS."` WHERE `pid`='$pid' ORDER BY `msg_id` ASC");

	while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
		$user_can_edit = (GALLERY_ADMIN_MODE) || (USER_ID && USER_ID == $row['author_id'] && USER_CAN_POST_COMMENTS) || (!USER_ID && USER_CAN_POST_COMMENTS && ($USER['ID'] == $row['author_md5_id']));
  		$comment_buttons = $user_can_edit ? $tmpl_comments_buttons : '';
		$comment_edit_box = $user_can_edit ? $tmpl_comment_edit_box : '';

		if ($CONFIG['enable_smilies']) {
			$comment_body = process_smilies(make_clickable($row['msg_body']));
			$smilies = generate_smilies("f{$row['msg_id']}", 'msg_body');
		} else {
			$comment_body = make_clickable($row['msg_body']);
			$smilies = '';
		}

		$params = array(
			'{EDIT}' => &$comment_edit_box,
			'{BUTTONS}' => &$comment_buttons,
		);

		$template = template_eval($template_image_comments, $params);

		$params = array(
			'{MSG_AUTHOR}' => $row['msg_author'],
			'{MSG_ID}' => $row['msg_id'],
			'{EDIT_TITLE}' => &$lang_display_comments['edit_title'],
			'{CONFIRM_DELETE}' => &$lang_display_comments['confirm_delete'],
			'{MSG_DATE}' => localised_date($row['msg_date'], $comment_date_fmt),
			'{MSG_BODY}' => &$comment_body,
			'{MSG_BODY_RAW}' => $row['msg_body'],
			'{OK}' => &$lang_display_comments['OK'],
			'{SMILIES}'=> $smilies,
		);

		$html .= template_eval($template, $params);
	}

	if (USER_CAN_POST_COMMENTS && $CURRENT_ALBUM_DATA['comments'] == 'YES'){
		if (USER_ID) {
			$username_input = '<input type="hidden" name="msg_author" value="'.username.'" />';
			template_extract_block($template_add_your_comment, 'username_input', $username_input);
			$username = '';
		} else {
			$username = isset($USER['name']) ? '"'.strtr($USER['name'], $HTML_SUBST).'"' : '"'.$lang_display_comments['your_name'].'" onClick="javascript:this.value=\'\';"';
		}

		$params = array(
			'{ADD_YOUR_COMMENT}' => $lang_display_comments['add_your_comment'],
			'{PIC_ID}' => $pid,
			'{username}' => $username,
			'{MAX_COM_LENGTH}' => $CONFIG['max_com_size'],
			'{OK}' => $lang_display_comments['OK'],
			'{SMILIES}' => '',
		);

		if ($CONFIG['enable_smilies']) $params['{SMILIES}'] = generate_smilies();

		$html .= template_eval($template_add_your_comment, $params);
	}

	return $html;
}

// Display the full size image
function display_fullsize_pic()
{
	global $CONFIG, $HTTP_GET_VARS, $THEME_DIR, $ALBUM_SET;
 	global $lang_errors;

	if (function_exists('theme_display_fullsize_pic')) {
	    theme_display_fullsize_pic();
		return;
	}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title><?php echo $CONFIG['gallery_name'] ?></title>
<meta http-equiv="Content-Type" content="text/html; charset=<?php echo $CONFIG['charset'] ?>" />
<link rel="stylesheet" href="<?php echo $THEME_DIR ?>style.css" />
<script type="text/javascript" src="modules/Coppermine/scripts.js"></script>
</head>
<body scroll="auto">
<script language="JavaScript" type="text/JavaScript">
adjust_popup();
</script>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="2">
 <td align="center" valign="middle">
  <table cellspacing="2" cellpadding="0" style="border: 1px solid #000000; background-color: #FFFFFF;">
   <td>
<?php
	if (isset($HTTP_GET_VARS['picfile'])){

		if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);


		$picfile = $HTTP_GET_VARS['picfile'];
		$picname = $CONFIG['fullpath'].$picfile;
		$imagesize = @getimagesize($picname);
		echo "<img src=\"".path2url($picname)."\" $imagesize[3] class=\"image\" border=\"0\" alt=\"$picfile\"/><br />\n";
	} elseif (isset($HTTP_GET_VARS['pid'])) {
		$sql =  "SELECT * ".
		"FROM `".TABLE_CPG_PICTURES."` ".
		"WHERE `pid`='{$HTTP_GET_VARS['pid']}' $ALBUM_SET";
		$result = $GLOBALS['db']->sql_query($sql);
		if (!$GLOBALS['db']->sql_numrows($result)) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);

		$row = $GLOBALS['db']->sql_fetchrow($result);
		$pic_url = get_pic_url($row, 'fullsize');
		$geom = 'width="' . $row['pwidth'] . '" height="' . $row['pheight'] . '"';
		echo "<img src=\"".$pic_url."\" $geom class=\"image\" border=\"0\" alt=\"". htmlspecialchars($row['filename']). "\" /><br />\n";
	}
?>
   </td>
  </table>
 </td>
</table>
</body>
</html>
<?php
}

function slideshow()
{
	global $CONFIG, $HTTP_GET_VARS, $lang_display_image_php, $template_display_picture;

	if (function_exists('theme_slideshow')) {
	    theme_slideshow();
		return;
	}

	pageheader($lang_display_image_php['slideshow']);

	include "modules/Coppermine/include/slideshow.inc.php";

	$start_slideshow = '<script type="text/JavaScript">runSlideShow()</script>';
	template_extract_block($template_display_picture, 'img_desc', $start_slideshow);

	$params = array(
		'{CELL_HEIGHT}' => $CONFIG['picture_width'] + 100,
		'{IMAGE}' => '<img src="'.$start_img.'" name="SlideShow" class="image" /><br />',
		'{ADMIN_MENU}' => '',
	);

	starttable();
	echo template_eval($template_display_picture, $params);
	endtable();
	starttable();
	echo <<<EOT
	<tr>
		<td align="center" class="navmenu" style="white-space: nowrap;">
			<a href="javascript:endSlideShow()" class="navmenu">{$lang_display_image_php['stop_slideshow']}</a>
		</td>
	</tr>

EOT;
	endtable();
	pagefooter();
}

function get_subcat_data($parent, $level)
{
    global $CONFIG, $ALBUM_SET;
	$parent = $GLOBALS['db']->XMFnum( $parent );
	$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[] = secure( $row['aid'], 'strip', '1' );
			} // while
		}
	}
}

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

$pos = isset($_GET['pos']) ? (int)$_GET['pos'] : 0;
/**
 * Hack added by tarique to prevent incorrect picture being seen on last view or last uploaded
 */

$pid = isset($_GET['pid']) ? (int)$_GET['pid'] : 0;
$cat = isset($_GET['cat']) ? (int)$_GET['cat'] : 0;
$album = isset($_GET['album']) ? $_GET['album'] : '';

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

// Build the album set if required
if (!is_numeric($album) && $cat) { // Meta albums, we need to restrict the albums to the current category
	if ($cat < 0) {
	    $ALBUM_SET .= 'AND `aid` IN ('.(- $cat).') ';
	} 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
		get_subcat_data($cat, $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) ";
		} else {
			$ALBUM_SET = '';
		}
	}
}

// Retrieve data for the current picture
if ($pos < 0) {
    $pid = -$pos;
	$result = $GLOBALS['db']->sql_query("SELECT `aid` from `".TABLE_CPG_PICTURES."` WHERE `pid`='$pid' $ALBUM_SET LIMIT 1");
	if ($GLOBALS['db']->sql_numrows($result) == 0) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
	$row = $GLOBALS['db']->sql_fetchrow($result);
	$album = stripslashes($row['aid']);
	$pic_data = get_pic_data($album, $pic_count, $album_name, -1, -1, false);
	for($pos=0; $pic_data[$pos]['pid'] != $pid && $pos < $pic_count; $pos++);
	$pic_data = get_pic_data($album, $pic_count, $album_name, $pos, 1, false);
	$CURRENT_PIC_DATA = $pic_data[0];
} elseif (isset($_GET['pos'])) {
	$pic_data = get_pic_data($album, $pic_count, $album_name, $pos, 1, false);
	if ($pic_count == 0) {
	    cpg_die(INFORMATION, $lang_errors['no_img_to_display'], __FILE__, __LINE__);
	} elseif (count($pic_data) == 0 && $pos >= $pic_count) {
	    $pos = $pic_count - 1;
		$human_pos = $pos + 1;
		$pic_data = get_pic_data($album, $pic_count, $album_name, $pos, 1, false);
	}
	$CURRENT_PIC_DATA = $pic_data[0];
}

// Retrieve data for the current album
if (isset($CURRENT_PIC_DATA)) {
	$result = $GLOBALS['db']->sql_query("SELECT `title`, `comments`, `votes`, `category` FROM `".TABLE_CPG_ALBUMS."` WHERE `aid`='{$CURRENT_PIC_DATA['aid']}' LIMIT 1");
	if (!$GLOBALS['db']->sql_numrows($result)) cpg_die(CRITICAL_ERROR, sprintf($lang_errors['pic_in_invalid_album'],$CURRENT_PIC_DATA['aid']), __FILE__, __LINE__);
	$CURRENT_ALBUM_DATA = $GLOBALS['db']->sql_fetchrow($result);

    if (is_numeric($album)) {
		$cat = - $album;
		$actual_cat = stripslashes($CURRENT_ALBUM_DATA['category']);
	} else {
		$actual_cat = stripslashes($CURRENT_ALBUM_DATA['category']);
	}
}


if (isset($_GET['fullsize'])){
	display_fullsize_pic();
	//ob_end_flush();
} elseif (isset($_GET['slideshow'])) {
	slideshow();
	//ob_end_flush();
} else {
	if (!isset($_GET['pos'])) cpg_die(ERROR, $lang_errors['non_exist_ap'], __FILE__, __LINE__);
	$picture_title = $CURRENT_PIC_DATA['title'] ? $CURRENT_PIC_DATA['title'] : strtr(preg_replace("/(.+)\..*?\Z/", "\\1", htmlspecialchars($CURRENT_PIC_DATA['filename'])), "_", " ");


	$nav_menu = html_img_nav_menu();
	$picture  = html_picture();
	$votes    = html_rating_box();
	$pic_info = html_picinfo();
	$comments = html_comments($CURRENT_PIC_DATA['pid']);
        if ($CURRENT_PIC_DATA['keywords']) {
		$meta_keywords = "<meta name=\"keywords\" content=\"".$CURRENT_PIC_DATA['keywords']."\"/>";
	} else {
		$meta_keywords = '';
	}
	( !empty( $meta_nav ) ) ?  $meta_keywords .= $meta_nav : '';
	pageheader($album_name.'/'.$picture_title, $meta_keywords, false);
	theme_display_image($nav_menu, $picture, $votes, $pic_info, $comments);
	pagefooter();

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