<?php
// $Id: image_manager.php,v 1.1 2004/07/11 10:27:44 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Half-Dead
// Author Website : http://www.e-xoops.com
// License Type   : Proprietary: See /manual/LICENSES/E-Xoops.txt
// ------------------------------------------------------------------------- //

global $HTTP_SERVER_VARS;
if ( preg_match("/image_manager\.php/i", $HTTP_SERVER_VARS['PHP_SELF']) ) {
	exit();
}

$admin = 0;
if ($xoopsUser && $xoopsUser->isAdmin()) {
	$admin = 1;
}

if (!$admin && $xoopsConfig['allow_library'] == 0) {
	exit();
}

$path = $HTTP_GET_VARS['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 $HTTP_GET_VARS, $admin;

$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 ($HTTP_GET_VARS['showall'] == 1) {
			echo "<tr><td colspan='3'><img src='".XOOPS_URL."/images/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/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='15' 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 && $xoopsConfig['lib_allow_upload'] == 1)) {
?>
<td>
<?php
if ( !empty($HTTP_POST_FILES['image']['name']) ) {
	include_once(XOOPS_ROOT_PATH."/class/fileupload.php");
	$upload = new fileupload();
	$upload->set_upload_dir(XOOPS_ROOT_PATH . "/images/$path", 'image');
	$upload->set_accepted("gif|jpg|png", 'image');
	if (!$admin) {
		$upload->set_max_image_height($xoopsConfig['lib_height'], 'image');
		$upload->set_max_image_width($xoopsConfig['lib_width'], 'image');
		$upload->set_max_file_size($xoopsConfig['lib_maxsize'], 'image');
	}
	$upload->set_overwrite(1, 'image');
	$result = $upload->upload();
	if (!$result) {
		$upload->errors(1);
	}

	} elseif ($admin && preg_match("'^library/[a-z0-9_/-]+\.(gif|jpg|png)$'i", $HTTP_GET_VARS["delete"])) {
		if ( @file_exists(XOOPS_ROOT_PATH . "/images/" . $HTTP_GET_VARS["delete"]) ) {
			unlink(XOOPS_ROOT_PATH . "/images/" . $HTTP_GET_VARS["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="50" /></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/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>
