<?php 
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! 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, or (at your option) any later version.
 *
 * sflog! is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
   
  require_once(dirname(__FILE__)."/../../cms/Validator.class.php");
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../cms/Errors.inc.php");
  require_once(dirname(__FILE__)."/../../cms/Constants.inc.php");
  require_once(dirname(__FILE__)."/../../common/File.class.php");
  require_once(dirname(__FILE__)."/../../common/utils.func.php");
  
  	
  $maxFileSize = 0; // Unlimited	  
  unset($fileID);

  /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$id = $_GET['delete'];
  	$fn = BLOG_PATH.$blogID.BLOG_UPLOADS.$id;
  	if (file_exists($fn)){
  		/*
  		 * This forces to run under unix/linux enviroment, this is not a big deal,
  		 * but rm_dir() does not work here and I have no time to check it out.
  		 */
  		shell_exec("rm -f '".$fn."'");
  	}
  }

  /**
   * Restore action
   */
  if (isset($_GET['open'])){
  	$id = $_GET['open'];
  	$fn = BLOG_PATH.$blogID.BLOG_UPLOADS.$id;
  	if (file_exists($fn)){
		print('OPEN '.$id);			  
  	}
  }

  if (trim($_FILES['fileID']['name'])!=''){	
  	$uploaddir = BLOG_PATH.$blogID.BLOG_UPLOADS;
  	$uploadfile = $uploaddir . basename($_FILES['fileID']['name']);
	echo '<pre>';
  	if (move_uploaded_file($_FILES['fileID']['tmp_name'], $uploadfile)) {
     	print(UPLOAD_SUCCESS);
  	} else {
    	print('<font color="red">'.UPLOAD_FAILED.'</font>');
  	}
  	print "</pre>";
  }
?>	
<form action="<?=$_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
	<input type="hidden" name="blogID" value="<?=$blogID?>">
	<input type="hidden" name="contentType" value="<?=$contentType?>">	
	<input type="hidden" name="MAX_FILE_SIZE" value="<?=$maxFileSize?>">
	File&nbsp;<input type="file" name="fileID">
	<input type="submit" value="upload">&nbsp;&nbsp;&nbsp;
	max upload filesize is set to:&nbsp;<?=ini_get('upload_max_filesize')?>
</form> 

<TABLE width="99%" cellpadding="4" cellspacing="1" border="0" background="grey">
	<TR bgcolor="#C7D3E0">
		<TD width="1%">&nbsp;</TD>
		<TD width="18%"><b>filename<b></TD>
		<TD width="25%"><b>upload date<b></TD>
		<TD width="10%"><b>size<b></TD>
		<TD width="10%"><b>preview<b></TD>
	</TR>
<?php
	$file = new File();
	$dir = BLOG_PATH.$blogID.BLOG_UPLOADS;
	$files=$file->scandir($dir,1);
	foreach($files as $key=>$val) {
		if ($val[0]!=".") {
			$id = $val;
			$text = date("F d Y H:i:s", filectime($dir.$id));
			$size = filesize($dir.$id);	
?>
	<TR bgcolor="white">
		<TD><a href="<?=$_SERVER['PHP_SELF']."?delete=".$id.'&blogID='.$blogID.'&contentType='.$contentType?>"><img src="./images/delete.png" width="16px" height="16px" border="0" alt="delete"></a></TD>
		<TD><?=$id?></TD>
		<TD><?=$text?></TD>
		<TD><?=$size.' Bytes'?></TD>
		<TD align="center"><a target="new" href="<?='../blogs/'.$blogID.BLOG_UPLOADS.$id?>">
			<?
				$f = '../blogs/'.$blogID.BLOG_UPLOADS.$id;
				if (is_an_image($f)){								
			?>
			<img src="<?='../blogs/'.$blogID.BLOG_UPLOADS.$id?>" border="1" width="48" height="48">
			<?}else{
				$ext = getFileExt($f);
				$set = false;
				if (strcasecmp(trim($ext),'pdf')==0){
					print('<img src="../images/pdf.png" width="16" height="16" border=0>');
					$set = true;
				}
				if ((strcasecmp(trim($ext),'html')==0)||(strcasecmp(trim($ext),'htm')==0)){
					print('<img src="../images/html.png" width="16" height="16" border=0>');
					$set = true;
				}
				if ((strcasecmp(trim($ext),'doc')==0)||(strcasecmp(trim($ext),'rtf')==0)||(strcasecmp(trim($ext),'txt')==0)){
					print('<img src="../images/doc.png" width="16" height="16" border=0>');
					$set = true;
				}
				if ((strcasecmp(trim($ext),'tgz')==0)||(strcasecmp(trim($ext),'zip')==0)){
					print('<img src="../images/tar.png" width="16" height="16" border=0>');
					$set = true;
				}
				if ((strcasecmp(trim($ext),'avi')==0)||(strcasecmp(trim($ext),'wmf')==0)){
					print('<img src="../images/video.png" width="16" height="16" border=0>');
					$set = true;
				}
				
				if (!$set)
					print('<img src="../images/unknown.png" width="16" height="16" border=0>');
			  }?>
			</a>
		</TD>
	</TR>
<?php
	}} // end for
?>
	<TR bgcolor="white">
		<TD colspan="5"><hr size="-1"></TD>
	</TR>
</TABLE>