<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

<script language="javascript">
	var files=new Array();
	var folders=new Array();
	
	function setImage(path,file)
	{	RgExp= new RegExp("(\.png)|(\.jpg)|(\.gif)|(\.jpe)|(\.jpeg)|(\.bmp)$");
		
		window.parent.document.getElementById("fileinfo").innerHTML="<b class='note'>FileName: </b>"+file;
		window.parent.document.getElementById("filepath").value=path+file;
		if (!RgExp.test(file))
		{	
			window.parent.document.getElementById("imgdisp").src="entities\\nopreview.jpg"; 
		}
		else
		{
			window.parent.document.getElementById("imgdisp").src=path+file;
		}
	}
	function setRename(name,type)
	{	//type 1 --> file  2--> folder
		err=false;
		if (type==1)
		{	
			newname=prompt("Enter Name for file",name);
			RgExp= new RegExp("[^\\s]+");
			if (newname!=null)
			{	if(!RgExp.test(newname))
				{
					alert("Enter File name.");
					err=true;
				}
				else
				{
					for(i=0;i<files.length;i++)
					{	
						if(files[i].toLowerCase()!=name.toLowerCase())
						{	if(files[i].toLowerCase()==newname.toLowerCase())
							{	
								alert("Specified File Already Exists, Specify Another.");
								//document.regform.catname.focus();
								err=true;
								break;
							}
						}
					}
				}
			}
		}
		else //folder
		{
			newname=prompt("Enter Name for Folder",name);
			RgExp= new RegExp("[^\\s]+");
			if (newname!=null)
			{	if(!RgExp.test(newname))
				{
					alert("Enter Folder name.");
					err=true;
				}
				else
				{
					for(i=0;i<folders.length;i++)
					{	
						if(folders[i].toLowerCase()!=name.toLowerCase())
						{	if(folders[i].toLowerCase()==newname.toLowerCase())
							{	
								alert("Specified Folder Already Exists, Specify Another.");
								//document.regform.catname.focus();
								err=true;
								break;
							}
						}
					}
				}
			}
		}
		if (err!=true)
		{
			parent.document.uploadform.opt.value=type;
			parent.document.uploadform.existname.value=name;
			parent.document.uploadform.newname.value=newname;
			parent.document.uploadform.submit();
		}
	}
</script>
<link href="entities/pageform.css" rel="stylesheet" type="text/css">
</head>
<body> 
<?php
		// TO SET ROOT FOLDER FOR SECURITY PURPOSE will not allow to access files/folder out of this folder
		
		$rootfolder="images"; //relative to this page 
		
		//rootpath absolute path of root folder on sever
			
			$curpath=$_SERVER['SCRIPT_FILENAME']; 
			$slashindex=strrpos($curpath,"/");  
			$curpath=substr($curpath,0,$slashindex+1);
			$rootpath=$curpath.$rootfolder;
			
		//dir is absolute path of current directory 
		
		$dir=""; //current directory
		
		if(isset($_GET["dir1"]))
		{	
		   $dir=$_GET["dir1"];
		   $Qstr=$dir;
		}
		else
		{
		   $dir=$rootpath;
		   $Qstr=$dir."/";
		}
									
?>
		<script language="javascript">
			parent.document.uploadform.curpath.value="<?php echo $Qstr; ?>";
		</script> 
<?php		 	

			//to get relative path to root folder	
			
			$relpath=substr($dir,strlen($curpath), strlen($dir));
			$relpath=$relpath."/";
						
		 
?> 
<table width="100%" border="0" align="center" cellpadding="2" cellspacing="0" bgcolor="#F9F9F9" class="font"> 
  <?php
  
		
    $files=array();
	$dirs=array();
	$f=0;
	$d=0;
  	
	$dirhndl=opendir($dir);
	$dirhnd2=opendir($rootpath);

	while(($file=readdir($dirhndl))!=false)
	{
  		if(is_file($dir."/".$file)) //files
		{	$files[$f]=$file;
			$filetype[$file]=filetype($dir."/".$file);
  			$f++;
		}
		else //dirs
		{	
			$dirs[$d]=$file;
  			$d++;
		
		}
	}


?> 
  <tr bgcolor="#8D8D7E"> 
    <td width="5" bgcolor="#C7C7C0">&nbsp;</td> 
    <td height="18" colspan="3" bgcolor="#C7C7C0"><strong class="heading2">Folders</strong></td>
    <td width="6" bgcolor="#C7C7C0">&nbsp;</td>
  </tr> 
<?php	
				
	  $test=dirname($dir)."/";
	 // echo $dir." ".$test." ".$curpath;
	  if($test!=$curpath)
	  {
	      
?> 
  <tr> 
    <td>&nbsp;</td> 
    <td height="30" colspan="3"><img src="entities/UpLevel.gif" width="20" height="18" border="0" align="absbottom">&nbsp;<a href="FolderList.php?dir1=<?php echo $test; ?>">Up One Level</a></td>
    <td>&nbsp;</td>
  </tr>  
<?php	
	  }			

?>
<script language="javascript">
	//clear folderlistbox
	for(i=0;i<parent.document.uploadform.folders.length;i++)
	{	parent.document.uploadform.folders.options[i]=null;
	}
	
	parent.document.uploadform.folders.options[0]=new Option("Select Folder","0");
	parent.document.uploadform.folders.options[1]=new Option("This Folder","thisfolder");
</script>
<?php	
  if(count($dirs)==2) 
  { 
 ?> 
  <tr> 
    <td>&nbsp;</td> 
    <td colspan="3"><span class="note">No Folder</span></td>
    <td>&nbsp;</td>
  </tr> 
<?php
	} 
	else
    {
		$indx=0;
		
		$num=count($dirs);
		for($indx=0;$indx < $num;$indx++)
		{
		 $dirname=$dirs[$indx];
			
			$newpath=$dir."/".$dirname;
			
			$bg="bgcolor='#ffffff'";
				if(($indx % 2)==0)
					$bg="bgcolor='#eeeeee'";
?> 
  <tr <?php echo $bg; ?>>
    <td>&nbsp;</td> 
    <td width="433" height="30"><img src="entities/Folder.gif" width="17" height="14" border="0" align="absbottom">&nbsp;<a href="FolderList.php?dir1=<?php echo $dir."/".$dirname ?>"><?php echo $dirname;  ?></a></td> 
    <td width="44"><a href="javascript:setRename('<?php echo $dirname; ?>',2);"><img src="entities/rename.gif" width="23" height="17" border="0"></a></td> 
    <td width="501"><?php echo "Folder"; // check here?></td> 
    <td>&nbsp;</td>
  </tr> 
<?php	
			
			//$newpath=str_replace("/","\\\\",$newpath);
?> 
  <script language="javascript">
  		folders[<?php echo $indx-2; ?>]="<?php echo $dirname; ?>";
		//append to list of folders
		newpath="<?php echo $newpath; ?>";
		parent.document.uploadform.folders.options[<?php echo $indx; ?>]=new Option("<?php echo $dirname; ?>",newpath);
  </script> 
<?php	
       }  
   }
?> 
  <tr bgcolor="#8D8D7E"> 
    <td bgcolor="#C7C7C0"><strong class="heading2"> </strong></td> 
    <td height="18" colspan="3" bgcolor="#C7C7C0"><strong class="heading2">Files</strong></td>
    <td bgcolor="#C7C7C0">&nbsp;</td>
  </tr> 
<?php	
   if(count($files)==0)
   {
 ?> 
  <tr>
    <td>&nbsp;</td>
    <td colspan="3">&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr> 
    <td>&nbsp;</td> 
    <td colspan="3"><span class="note">No File</span></td>
    <td>&nbsp;</td>
  </tr> 
<?php
    }
	else
	{
			$indx=0;
			
			foreach($files as $filename)
			{
			     $subfile=pathinfo($dir."\\".$filename);
				 $bg="bgcolor='#ffffff'";
				if (($indx % 2)==0)
					$bg="bgcolor='#eeeeee'";
				
				$indx=$indx+1;

						$newpath=$dir;
			$newpath=str_replace("/","\\\\",$newpath);
			
?> 
  <tr <?php echo $bg ?> >
    <td>&nbsp;</td> 
    <td><img src="entities/File.gif" width="14" height="18" border="0" align="absbottom">&nbsp;<a href="javascript:setImage('<?php echo $relpath; ?>','<?php echo $filename; ?>');"><?php echo $filename; ?></a></td> 
    <td><a href="javascript:setRename('<?php echo $filename; ?>',1);"><img src="entities/rename.gif" width="23" height="17" border="0"></a></td> 
    <td><?php echo $filetype[$filename];  // check here?></td> 
    <td>&nbsp;</td>
  </tr> 
  	<script language="javascript">
		files[<?php echo $indx-1; ?>]="<?php echo $filename; ?>";
	</script> 
<?php	  	}
  }
		
	closedir($dirhndl);
	//closedir($dirhnd2);

		?> 
		
		
</table> 
</body>
</html>
