<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.9 www.exoops.info 2006/07  1.08.10
//v1.8 www.exoops.info 2006/03 
// $Id: image_manager.php,v 1.7 2003/03/19 06:58:15 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Half-Dead
// Author Website : http://www.e-xoops.com
// License Type   : Proprietary: See /manual/LICENSES/E-Xoops.txt
// ------------------------------------------------------------------------- //
global $_SERVER;
if ( preg_match("/image_manager\.php/i", $_SERVER['PHP_SELF']) ) {
	exit();
}
$admin = 0;
if ($xoopsUser && $xoopsUser->isAdmin()) {
	$admin = 1;
}
if (!$admin && $bcoosConfig['allow_library'] == 0) {
	exit();
}
$path = $_GET['path'];
if ( empty($path) || preg_match("'\.'", $path) || !preg_match("'^library/'", $path) ) {
	$path = "library/";
	} elseif ( (substr($path, -1) != '/') ) {
		$path .= "/";
	}
//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function getImages($path="library/") {
global $_GET, $admin,$bcoosConfig;
$dir = XOOPS_ROOT_PATH . "/images/" . $path;
$url = XOOPS_URL . "/images/" . $path;
$values = array();
$handle = opendir($dir);
while (false !== ($file = readdir($handle))) {
	if ( ($file != ".") && ($file != "..") && (preg_match("/\.(gif|jpg|png)$/i", $file) || @is_dir($dir.$file)) ) {
		if ( @is_dir($dir.$file) ) {
			array_push($values, $file);
			} else {
				array_unshift($values, $file);
			}
	}
}
closedir($handle);
while (list($key, $filename) = @each($values)) {
	if ( @is_dir($dir.$filename) ) {
		if ($_GET['showall'] == 1) {
			echo "<tr><td colspan='3'><img src='".XOOPS_URL."/images/icons/".$bcoosConfig['iconset']."menu_tree_open.gif' alt=''> <a href='"._REQUEST_URI."&path=".$path.$filename."'>".$path.$filename."/</a></td></tr>";
			getImages($path.$filename."/");
			} else {
				echo "<tr><td colspan='3'><img src='".XOOPS_URL."/images/icons/".$bcoosConfig['iconset']."menu_tree_close.gif' alt=''> <a href='"._REQUEST_URI."&path=".$path.$filename."'>".$path.$filename."/</a></td></tr>";
			}
		} else {
			$img = str_replace('library/', '', $path);
			echo "
			<tr>
			<td> &nbsp; &nbsp; <img src='".$url.$filename."' alt='"._PREVIEW."' style='cursor:hand;' height='50' onclick='javascript:xoopsGetElementById(\"preview\").src=this.src;'></td>
			<td>&raquo;<a title='"._ADD."' href='javascript:addImage(\"".$img.$filename."\"); self.close();'>$filename</a>&laquo;</td>
			<td>";
			if ($admin) {
				echo "<a href='"._REQUEST_URI."&delete=$path$filename'><img src='".XOOPS_URL."/images/x.gif' border='0' alt='"._DELETE."'></a>";
				} else {
					echo "&nbsp;";
				}
			echo "</td></tr>";
		}
	}
}
//---------------------------------------------------------------------------------------//
?>
<script type="text/javascript">
<!--
function addImage(imgurl) {
var txtbox  = window.opener.xoopsGetElementById("<?php echo $target;?>");
var align   = prompt("<?php echo _ENTERIMGPOS;?>\n<?php echo _IMGPOSRORL;?>", "");
while ( ( align != "" ) && ( align != "r" ) && ( align != "R" ) && ( align != "l" ) && ( align != "L" ) && ( align != null ) ) {
	align = prompt("<?php echo _ERRORIMGPOS;?>\n<?php echo _IMGPOSRORL;?>", "");
}
if ( align == "l" || align == "L" ) {
	align = " align=left";
	} else if ( align == "r" || align == "R" ) {
		align = " align=right";
		} else {
			align = "";
		}
txtbox.value += "[lib" + align + "]" + imgurl + "[/lib]";
return;
}
//-->
</script>
<br />
<h4 align="center"><?php echo _IMG_PICKER;?></h4>
<table width="95%" border="0" align="center" cellpadding="5" cellspacing="0"><tr>
<?php
if ($admin || ($xoopsUser && $bcoosConfig['lib_allow_upload'] == 1)) {
?>
<td>
<?php
if ( !empty($_FILES['image']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/upload.class.php");
	$upload = new Upload();
	$upload->setAllowedMimeTypes(".gif|.jpeg|.jpg|.png");
	$upload->setUploadPath(XOOPS_ROOT_PATH . "/images/$path");
	$upload->setMaxImageSize($bcoosConfig['lib_width'], $bcoosConfig['lib_height']);
	$upload->setMaxFileSize($bcoosConfig['lib_maxsize']);
	$upload->setDestinationFileName('image');
	
	$result = $upload->doUpload();
	if (!$result) {
			$errors = $upload->getUploadErrors();
			$errorstring = "";
			$errorstring .= "<strong>::Errors occured::</strong><br />\n";
			while(list($filename,$values) = each($errors)) {
				$errorstring .= "File: " . $filename . "<br />";
				$count = count($values);
				for($i=0; $i<$count; $i++) { $errorstring .= "==>" . $values[$i] . "<br />"; }
			}
			
		}
} elseif ($admin && preg_match("'^library/[a-z0-9_/-]+\.(gif|jpg|png)$'i", $_GET["delete"])) {
	if ( @file_exists(XOOPS_ROOT_PATH . "/images/" . $_GET["delete"]) ) {
			unlink(XOOPS_ROOT_PATH . "/images/" . $_GET["delete"]);
	}
}
?>
<form name="upload" enctype="multipart/form-data" method="post" action="<?php echo _REQUEST_URI;?>">
<input type="file" class="file" name="image" id="image" onchange="xoopsGetElementById('preview').src=this.value;" />
<br /><input type="submit" class="button" name="submit" value="<?php echo _SUBMIT;?>">
</td></form>
</tr><tr>
<?php } ?>
<td align="center"><hr size="1" noshade="noshade" /></td>
</tr><tr>
<td align="center" height="70"><img src="<?php echo XOOPS_URL;?>/images/pixel.gif" id="preview" name="preview" width="150" /></td>
</tr><tr>
<td align="center"><hr size="1" noshade="noshade" /></td>
</tr><tr>
<td>
<a href="<?php echo _REQUEST_URI;?>&path=images/"><b>...</b></a><br /><br />
 <img src="<?php echo XOOPS_URL;?>/images/icons/<?php echo $bcoosConfig['iconset'];?>"menu_tree_open.gif" alt=""> <?php echo $path;?>
<table><?php getImages($path);?></table>
<br />
<div align="center">
<a href="<?php echo _REQUEST_URI;?>&showall=1"><b>:: <?php echo _IMG_EXPAND;?> ::</b></a>
 &nbsp; &nbsp;
<a href="<?php echo _REQUEST_URI;?>&showall=0"><b>:: <?php echo _IMG_COLLAPSE;?> ::</b></a>
</div>
<br />
</td>
</tr></table>
