<?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");

if ( !defined('ADMIN_ALLOW') ) {
	redirect_url('modules.php?name=Coppermine');
}

$pagetitle = "- Coppermine";


define('IN_COPPERMINE', true);
define('SEARCHNEW_PHP', true);

switch($func) {
default:
incoh();

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

if (!GALLERY_ADMIN_MODE) cpg_die(ERROR, $lang_errors['access_denied'], __FILE__, __LINE__);
new_cache();
/**************************************************************************
 * Local functions definition
 **************************************************************************/

/**
 * albumselect()
 *
 * return the HTML code for a listbox with name $id that contains the list
 * of all albums
 *
 * @param string $id the name of the listbox
 * @return the HTML code
 **/
function albumselect($id="album")
{
	global $CONFIG;
	static $select = "";

	if ($select == ""){
		$sql = "SELECT aid, IF(username IS NOT NULL, CONCAT('(', username, ') ', title), CONCAT(' - ', title)) AS title ".
			   "FROM `".TABLE_CPG_ALBUMS."` AS a ".
			   "LEFT JOIN `".TABLE_USERS."` AS u ON category = (".FIRST_USER_CAT." + user_id) ".
			   "ORDER BY title";
		$result = $GLOBALS['db']->sql_query($sql);

		while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
			$select .= "<option value=\"" . $row["aid"] . "\">" . $row["title"] . "</option>\n";
		}
		$GLOBALS['db']->sql_free($result);
	}

	return "\n<select name=\"$id\" class=\"listbox\">\n$select</select>\n";
}

/**
 * dirheader()
 *
 * return the HTML code for the row to be displayed when we start a new
 * directory
 *
 * @param $dir the directory
 * @param $dirid the name of the listbox that will list the albums
 * @return the HTML code
 **/
function dirheader($dir, $dirid)
{
	global $CONFIG, $lang_search_new_php;
	$warning = '';

	if (!is_writable($CONFIG['fullpath'].$dir))
		$warning ="<tr><td class=\"tableh2\" valign=\"middle\" colspan=\"3\">\n".
			"<b>{$lang_search_new_php['warning']}</b>: {$lang_search_new_php['change_perm']}</td></tr>\n";
	return "<tr><td class=\"tableh2\" valign=\"middle\" colspan=\"3\">\n".
			sprintf($lang_search_new_php['target_album'], $dir, albumselect($dirid))."</td></tr>\n".$warning;
}

/**
 * picrow()
 *
 * return the HTML code for a row to be displayed for an image
 * the row contains a checkbox, the image name, a thumbnail
 *
 * @param $picfile the full path of the file that contains the picture
 * @param $picid the name of the check box
 * @return the HTML code
 **/
function picrow($picfile, $picid, $albid)
{
	global $CONFIG, $expic_array;

	$encoded_picfile = base64_encode($picfile);
	$picname = $CONFIG['fullpath'].$picfile;
	$pic_url = urlencode($picfile);
	$pic_fname = basename($picfile);
$sql = "SELECT * FROM `".TABLE_CPG_PICTURES."` WHERE `filename` ='$pic_fname'";
$result = $GLOBALS['db']->sql_query($sql);

$exists = $GLOBALS['db']->sql_numrows($result);

while ($exists <= 0 )
{

	$thumb_file = dirname($picname).'/'.$CONFIG['thumb_pfx'].$pic_fname;
	if(file_exists($thumb_file) ){
		$thumb_info = getimagesize($picname);
		$thumb_size = compute_img_size($thumb_info[0], $thumb_info[1], 48);
		$img ='<img src="'.path2url($picname).'" '.$thumb_size['geom'].' class="thumbnail" border="0">';
	} else {
		$img ='<img src="modules.php?name=Coppermine&amp;op=showthumbbatch&amp;picfile='.$pic_url.'&amp;size=48" class="thumbnail" border="0">';
	}

	if (filesize($picname) && is_readable($picname)) {
		$fullimagesize = getimagesize($picname);
		$winsizeX = ($fullimagesize[0] + 16);
		$winsizeY = ($fullimagesize[1] + 16);

		$checked = isset($expic_array[$picfile]) || !$fullimagesize ? '' : 'checked';

		return <<<EOT
	<tr>
		<td class="tableb" valign="middle">
			<input name="pics[]" type="checkbox" value="$picid" $checked>
			<input name="album_lb_id_$picid" type="hidden" value="$albid">
			<input name="picfile_$picid" type="hidden" value="$encoded_picfile">
		</td>
		<td class="tableb" valign="middle" width="100%">
			<a href="javascript:;" onClick= "MM_openBrWindow('modules.php?name=Coppermine&op=displayimagepopup&fullsize=1&picfile=$pic_url', 'ImageViewer', 'toolbar=yes, status=yes, resizable=yes, width=$winsizeX, height=$winsizeY')">$pic_fname</a>
		</td>
		<td class="tableb" valign="middle" align="center">
			<a href="javascript:;" onClick= "MM_openBrWindow('modules.php?name=Coppermine&op=displayimagepopup&fullsize=1&picfile=$pic_url', 'ImageViewer', 'toolbar=yes, status=yes, resizable=yes, width=$winsizeX, height=$winsizeY')">$img<br /></a>
		</td>
	</tr>
EOT;
	} else {
		$winsizeX = (300);
		$winsizeY = (300);
		return <<<EOT
	<tr>
		<td class="tableb" valign="middle">
			&nbsp;
		</td>
		<td class="tableb" valign="middle" width="100%">
			<i>$pic_fname</i>
		</td>
		<td class="tableb" valign="middle" align="center">
			<a href="javascript:;" onClick= "MM_openBrWindow('modules.php?name=Coppermine&op=displayimagepopup&fullsize=1&picfile=$pic_url', 'ImageViewer', 'toolbar=yes, status=yes, resizable=yes, width=$winsizeX, height=$winsizeY')"><img src="modules.php?name=Coppermine&op=showthumbbatch&picfile=$pic_url&size=48" class="thumbnail" border="0"><br /></a>
		</td>
	</tr>
EOT;
	}
}

/**
 * getfoldercontent()
 *
 * return the files and directories of a folder in two arrays
 *
 * @param $folder the folder to read
 * @param $dir_array the array that will contain name of sub-dir
 * @param $pic_array the array that will contain name of picture
 * @param $expic_array  an array that contains pictures already in db
 * @return
 **/
 }
function getfoldercontent($folder, &$dir_array, &$pic_array, &$expic_array)
{
	global $CONFIG;

	$dir = opendir($CONFIG['fullpath'].$folder);
	while($file = readdir($dir)){
		if(is_dir($CONFIG['fullpath'].$folder.$file)) {
			if ($file != "." && $file != "..")
				$dir_array[] = $file;
		}
		if(is_file($CONFIG['fullpath'].$folder.$file)) {
			if(strncmp($file, $CONFIG['thumb_pfx'], strlen($CONFIG['thumb_pfx'])) != 0
				&&  strncmp($file, $CONFIG['normal_pfx'], strlen($CONFIG['normal_pfx'])) != 0)
				$pic_array[] = $file;
		}
	}
	closedir($dir);

	natcasesort($dir_array);
	natcasesort($pic_array);
}

function display_dir_tree($folder, $ident)
{
	global $CONFIG, $PHP_SELF, $lang_search_new_php;

	$dir_path = $CONFIG['fullpath'].$folder;

	if (!is_readable($dir_path)) return;

	$dir = opendir($dir_path);
	while($file = readdir($dir)){
		if(is_dir($CONFIG['fullpath'].$folder.$file) && $file != "." && $file != "..") {
			$start_target = $folder.$file;
			$dir_path = $CONFIG['fullpath'].$folder.$file;

			$warnings = '';
			if (!is_writable($dir_path)) $warnings .= $lang_search_new_php['dir_ro'];
			if (!is_readable($dir_path)) $warnings .= $lang_search_new_php['dir_cant_read'];

			if ($warnings) $warnings = '&nbsp;&nbsp;&nbsp;<b>'.$warnings.'<b>';

			echo <<<EOT
			<tr>
				<td class="tableb">
					$ident<img src="modules/Coppermine/images/folder.gif" alt="">&nbsp;<a href= "modules.php?name=Coppermine&op=searchnew&startdir=$start_target">$file</a>$warnings
				</td>
			</tr>
EOT;
			display_dir_tree($folder.$file.'/', $ident.'&nbsp;&nbsp;&nbsp;&nbsp;');
		}
	}
	closedir($dir);
}

/**
 * getallpicindb()
 *
 * Fill an array where keys are the full path of all images in the picture table
 *
 * @param $pic_array the array to be filled
 * @return
 **/
function getallpicindb(&$pic_array, $startdir)
{
	global $CONFIG;

	$sql = "SELECT `filepath`, `filename` ".
		   "FROM `".TABLE_CPG_PICTURES."` ".
		   "WHERE `filepath` LIKE '$startdir%'";
	$result = $GLOBALS['db']->sql_query($sql);
	while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
		$pic_file = $row['filepath'].$row['filename'];
		$pic_array[$pic_file]=1;
	}
	$GLOBALS['db']->sql_free($result);
}

/**
 * getallalbumsindb()
 *
 * Fill an array with all albums where keys are aid of albums and values are
 * album title
 *
 * @param $album_array the array to be filled
 * @return
 **/
function getallalbumsindb(&$album_array)
{
	global $CONFIG;

	$sql = "SELECT `aid`, `title` ".
		   "FROM `".TABLE_CPG_ALBUMS."` ".
		   "WHERE 1";
	$result = $GLOBALS['db']->sql_query($sql);

	while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
		$album_array[$row['aid']]= secure( $row['title'], 'strip', 1 );
	}
	$GLOBALS['db']->sql_free($result);
}


/**
 * scandir()
 *
 * recursive function that scan a directory, create the HTML code for each
 * picture and add new pictures in an array
 *
 * @param $dir the directory to be scanned
 * @param $expic_array the array that contains pictures already in DB
 * @param $newpic_array the array that contains new pictures found
 * @return
 **/
function scandir2($dir, &$expic_array)
{
	static $dir_id = 0;
	static $count =0;
	static $pic_id=0;

	$pic_array = array();
	$dir_array = array();

	getfoldercontent($dir, $dir_array, $pic_array, $expic_array );

	if (count($pic_array) > 0){
		$dir_id_str=sprintf("d%04d", $dir_id++);
		echo dirheader($dir, $dir_id_str);
		foreach ($pic_array as $picture) {
			$count++;
			$pic_id_str=sprintf("i%04d", $pic_id++);
			echo picrow($dir.$picture, $pic_id_str, $dir_id_str );
		}
	}
	if (count($dir_array) > 0){
		foreach ($dir_array as $directory) {
			scandir2($dir.$directory.'/', $expic_array);
		}
	}
	return $count;
}

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

$album_array = array();
getallalbumsindb($album_array);

// We need at least one album
if (!count($album_array)) cpg_die(ERROR, $lang_search_new_php['need_one_album'], __FILE__, __LINE__);

if (isset($_POST['insert'])){

	if(!isset($_POST['pics'])) cpg_die(ERROR, $lang_search_new_php['no_pic_to_add'], __FILE__, __LINE__);

	pageheader($lang_search_new_php['page_title']);
	ob_end_flush();
	starttable("100%");
	echo <<<EOT
	<tr>
		<td colspan="4" class="tableh1"><h2>{$lang_search_new_php['insert']}</h2></td>
	</tr>
	<tr>
		<td class="tableh2" valign="middle" align="center"><b>{$lang_search_new_php['folder']}</b></td>
		<td class="tableh2" valign="middle" align="center"><b>{$lang_search_new_php['image']}</b></td>
		<td class="tableh2" valign="middle" align="center"><b>{$lang_search_new_php['album']}</b></td>
		<td class="tableh2" valign="middle" align="center"><b>{$lang_search_new_php['result']}</b></td>
	</tr>
EOT;

	$count=0;
	foreach ($_POST['pics'] as $pic_id){
		$album_lb_id = $_POST['album_lb_id_'.$pic_id];
		$album_id    = $_POST[$album_lb_id];
		$album_name  = $album_array[$album_id];
		$pic_file    = base64_decode($_POST['picfile_'.$pic_id]);
		$dir_name    = dirname($pic_file)."/";
		$file_name   = basename($pic_file);

		// To avoid problems with PHP scripts max execution time limit, each picture is
		// added individually using a separate script that returns an image
		$status = "<a href=\"modules.php?name=Coppermine&op=addpic&aid=$album_id&pic_file=".($_POST['picfile_'.$pic_id])."&reload=".uniqid('')."\"><img src=\"modules.php?name=Coppermine&op=addpic&aid=$album_id&pic_file=".($_POST['picfile_'.$pic_id])."&reload=".uniqid('')."\" class=\"thumbnail\" border=\"0\" width=\"24\" height=\"24\" /><br /></a>";

		echo "<tr>\n";
		echo "<td class=\"tableb\" valign=\"middle\" align=\"left\">$dir_name</td>\n";
		echo "<td class=\"tableb\" valign=\"middle\" align=\"left\">$file_name</td>\n";
		echo "<td class=\"tableb\" valign=\"middle\" align=\"left\">$album_name</td>\n";
		echo "<td class=\"tableb\" valign=\"middle\" align=\"center\">$status</td>\n";
		echo "</tr>\n";
		$count++;
	}
	echo <<<EOT
	<tr>
		<td class="tableh2" colspan="4">
			<b>{$lang_search_new_php['be_patient']}</b>
		</td>
	</tr>
	<tr>
		<td class="tableb" colspan="4">
			{$lang_search_new_php['notes']}
		</td>
	</tr>

EOT;
	endtable();
	pagefooter();
} elseif(isset($_GET['startdir'])){
	pageheader($lang_search_new_php['page_title']);
	ob_end_flush();
	starttable("100%");
	echo <<<EOT
	<form method="post" action="modules.php?name=Coppermine&op=searchnew&insert=1">
	<tr>
		<td colspan="3" class="tableh1"><h2>{$lang_search_new_php['list_new_pic']}</h2></td>
	</tr>

EOT;
	$expic_array = array();
	getallpicindb($expic_array, $_GET['startdir']);
	if(scandir2($_GET['startdir'].'/', $expic_array)){

	echo <<<EOT
	<tr>
		<td colspan="3" align="center" class="tablef">
			<input type="submit" class="button" name="insert" value="{$lang_search_new_php['insert_selected']}">
		</td>
	</tr>
	</form>

EOT;
	} else {
	echo <<<EOT
	<tr>
		<td colspan="3" align="center" class="tableb">
			<br /><br />
			<b>{$lang_search_new_php['no_pic_found']}</b>
			<br /><br /><br />
		</td>
	</tr>
	</form>

EOT;
	}
	endtable();
	pagefooter();
} else {
	pageheader($lang_search_new_php['page_title']);
	starttable(-1,$lang_search_new_php['select_dir']);
	display_dir_tree('','');
	echo <<<EOT
	<tr>
		<td class="tablef">
			<b>{$lang_search_new_php['select_dir_msg']}</b>
		</td>
	</tr>

EOT;
	endtable();
	pagefooter();
	ob_end_flush();
}
incof();
break;
}