<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
       <title>Upload Image</title>
       <LINK href="images/admin/interface.css" rel=stylesheet>
</head>
<body>
<?php
include "config.inc.php";
$path="images/topic/";
switch($action)
{
	case "upload":
	    $file = $_FILES[files];
	    if(!$file[name]) error(_LANG_010);
	   	include(PLUS_ROOT_PATH."/class/fileupload-class.php");
		// upload images
		if(empty($path)) error(_LANG_011);
		$upload_file_name = "files";
		$acceptable_file_types = "gif|jpg|bmp|jpeg|png|tif|tiff|gif";
		$default_extension = "";
		$mode = 2;
		$my_uploader = new uploader;
		$my_uploader -> max_filesize(90000);
		$my_uploader -> max_image_size(150, 150);
		if ($my_uploader -> upload($upload_file_name, $acceptable_file_types, $default_extension)) {
			$my_uploader -> save_file($path, $mode);
		} //endif
		if ($my_uploader -> error) {
			error($my_uploader -> error);
		} else {
			// Successful upload!
			$image_name = $my_uploader -> file['name'];
			echo $image_name;
		} //end if
			succ("upload.php","上传完成");
	break;
	default:
	$path=$_GET[path];
	echo html_table_header("Upload Image",2);
	echo form_header("","upload","myform",$req,1);
	echo html_input_hidden("path",$path);
	echo "<tr class=" . form_class() . "><td >选择图片:</td><td ><input type=\"file\" name=\"files\" size=\"20\" onchange=\"imagePreview.innerHTML = '<img style=\'display: none\' src=\'' + this.value + '\'>'\"></td></tr>";
	echo "<tr class=" . form_class() . "><td>预览图片:</td><td  id=\"imagePreview\" style=\"padding: 8\"></td></tr>";
	echo form_footer("上传", "重置", 2,"返回");
	echo html_table_footer();
}
?>
<script>
	previewPic()

function previewPic()
{
try
    {
    for (i = 0; i < document.images.length; i++)
        {
        if (document.images[i].style.display == "none")
            {
            document.images[i].style.display = ""
            width = document.images[i].width
            height = document.images[i].height
            if (width > 350)
                {
                n = width / 350
                document.images[i].width = 350
                document.images[i].height = height / n
                }
            document.images[i].insertAdjacentHTML("AfterEnd", "<table border=0 width=100% cellspacing=0 cellpadding=0><tr><td style='padding-top: 4'>" + width + " × " + height + "</td></tr></table>")
            }
        }
    window.setTimeout("previewPic()", 500)
    }
catch(e)
    {
    window.setTimeout("previewPic()", 500)
    }
}
</script>
<?
	$path="images/topic/";
	$form="addCategoryForm";
	$IMAGES_BASE_URL = "/".$path;
	$IMAGES_BASE_DIR = PLUS_ROOT_PATH.$IMAGES_BASE_URL;

// End int var

// Thanks : php dot net at phor dot net
function walk_dir($path) {
	if ($dir = opendir($path)) {
		while (false !== ($file = readdir($dir)))
		{
			if ($file[0]==".") continue;
			if (is_dir($path."/".$file))
				$retval = array_merge($retval,walk_dir($path."/".$file));
			else if (is_file($path."/".$file))
				//$retval[]=$path."/".$file;
				$retval[]="/".$file;
			}
		closedir($dir);
		}
	return $retval;
}

function CheckImgExt($filename) {
	$img_exts = array("gif","jpg", "jpeg","png");
	foreach($img_exts as $this_ext) {
		if (preg_match("/\.$this_ext$/", $filename)) {
			return TRUE;
		}
	}
	return FALSE;
}

foreach (walk_dir($IMAGES_BASE_DIR) as $file) {
	//$file = preg_replace("#//+#", '/', $file);
	//$IMAGES_BASE_DIR = preg_replace("#//+#", '/', $IMAGES_BASE_DIR);
	if (CheckImgExt($file)) {
		$files[] = $file;	//adding filenames to array
	}
}

sort($files);	//sorting array

// generating $html_img_lst
foreach ($files as $file) {
	$html_img_lst .= "<a href=\"javascript:getImage('$file');\">$file</a><br>\n";
}

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
	<HEAD>
		<TITLE>Image Browser</TITLE>
		<LINK rel="stylesheet" type="text/css" href="FCKeditor/css/fck_dialog.css">
		<SCRIPT language="javascript">
var sImagesPath  = "<?php echo PLUS_URL.$IMAGES_BASE_URL; ?>";
var sActiveImage = "" ;

function getImage(imageName)
{
	sActiveImage = sImagesPath + imageName ;
	imgPreview.src = sActiveImage ;
}

function ok()
{	
	window.opener.<?=$form?>.icon.value=sActiveImage;
	window.close() ;
}
		</SCRIPT>
	</HEAD>
	<BODY bottommargin="5" leftmargin="5" topmargin="5" rightmargin="5">
<TABLE cellspacing="1" cellpadding="1" border="0" width="100%" class="dlg" height="100%">
	<TR height="100%">
		<TD>
			<TABLE cellspacing="0" cellpadding="0" width="100%" border="0" height="100%">
				<TR>
					<TD width="45%" valign="top">
						<table cellpadding="0" cellspacing="0" height="100%" width="100%">
							<tr>
								<td width="100%">File : </td>
							</tr>
							<tr height="60">
								<td>
									<DIV class="ImagePreviewArea"><? echo $html_img_lst ?></DIV>
								</td>
							</tr>
						</table>
					</TD>
					<TD width="10%" >&nbsp;&nbsp;&nbsp;</TD>
					<TD>
						<table cellpadding="0" cellspacing="0" height="100%" width="100%">
							<tr>
								<td width="100%">Preview : </td>
							</tr>
							<tr>
								<td height="100%" align="center" valign="middle">
									<DIV class="ImagePreviewArea"><IMG id="imgPreview" border=1"></DIV>
								</td>
							</tr>
						</table>
					</TD>
				</TR>
			</TABLE>
		</TD>
	</TR>
	<TR>
		<TD align="center">
			<INPUT style="WIDTH: 80px" type="button" value="OK"     onclick="ok();"> &nbsp;&nbsp;&nbsp;&nbsp;
			<INPUT style="WIDTH: 80px" type="button" value="Cancel" onclick="window.close();"><BR>
		</TD>
	</TR>
</TABLE>
	</BODY>
</HTML>

</body>
</html>
