<?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/PhotoBlog.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");
  require_once(dirname(__FILE__)."/../../common/WriteFile.class.php");  
  
  // Check for authorizations
  $auth = new Auth();
  if (!$auth->isAuth()){
  	header("Location: login.php");
  	die();
  }
  // ------ end auth's check
  	
  $maxFileSize = 0; // Unlimited	  
  unset($fileID);

  $pb = new PhotoBlog($blogID);
  if (isset($_POST['photoBlogID'])){
  	if (!($_POST['photoBlogID']=='')){
  	  $photoBlogID = stripslashes($_POST['photoBlogID']);	
    }
  }else {
  	if (isset($_GET['pblogID'])){
  		if (!($_GET['pblogID']=='')){
  			$photoBlogID = stripslashes($_GET['pblogID']);
  		}
  	} else
  		$photoBlogID = $pb->getFirst($blogID);
  }  


  /**
   * Delete action
   */
  if (isset($_GET['delete'])){
  	$id = stripslashes($_GET['delete']);
  	$pbID = stripslashes($_GET['pblogID']);
  	$fn  = BLOG_PATH.$blogID.PHOTOBLOG_UPLOADS.$pbID.'/'.$id;
  	$fn2 = BLOG_PATH.$blogID.PHOTOBLOG_UPLOADS.$pbID.PHOTOBLOG_THUMBS.$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."'");
  	}
  	// delete comment
  	if (file_exists($fn.'.comment')){
  		shell_exec("rm -f '".$fn.'.comment'."'");
  	}
	// delete thumbnail file
	if (file_exists($fn2)){
	  	shell_exec("rm -f '".$fn2."'");
	}  	
  }

  if (trim($_FILES['fileID']['name'])!=''){	
  	$uploaddir = BLOG_PATH.$blogID.PHOTOBLOG_UPLOADS.$photoBlogID;
  	$uploadfile = $uploaddir . '/' . basename($_FILES['fileID']['name']);
	echo '<pre>';
  	if (move_uploaded_file($_FILES['fileID']['tmp_name'], $uploadfile)) {
     	// Now it's time to create thumbnail 
		$tnFile = $uploaddir.PHOTOBLOG_THUMBS.basename($_FILES['fileID']['name']);
		createthumb($uploadfile,$tnFile,250,250);     	
  	} else {
    	// print('<font color="red">'.UPLOAD_FAILED.'</font>');
  	}
  	print "</pre>";
  }
  
?>	
<form name="form1" 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?>">
	<input type="hidden" name="photoBlogID" value="<?=$photoBlogID?>">
	
	Select your PhotoBlog &nbsp;&nbsp; 
	<SELECT name="photoBlogID" onChange="form1.submit();">
		<?
			$pbs = $pb->getPhotoBlogID();
			foreach($pbs as $id){
				$pbName = $pb->getPhotoBlogText($id);
				if (strcasecmp($photoBlogID,$id)==0)
					$selected = "selected";
				else
					$selected = "";
				print('<OPTION value='.$id.' '.$selected.'>'.$pbName.'</OPTION>');
			}
		?>
	</SELECT>
	<hr size="1">
	File&nbsp;<input type="file" name="fileID">	<input type="submit" value="upload">
	&nbsp;&nbsp;
	max upload filesize is set to:&nbsp;<?=ini_get('upload_max_filesize')?>
	<br><br>
</form> 

<script>
function commentWin(str) {
   Win = window.open(str,'comment','scrollbars=yes,resizable=no,width=640,height=480,status=no,location=no,toolbar=no');
}
</script>
<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>
		<td width="1%"><b>note<b></td>
	</tr>
<?php
	$file = new File();
	$dir = BLOG_PATH.$blogID.PHOTOBLOG_UPLOADS.$photoBlogID;
	$files=$file->scandir($dir,1);
	foreach($files as $key=>$val) {
		if ($val[0]!=".") {
			$id = $val;
			if ((strcasecmp(substr($id,-8),'.comment')!=0)&&(strcasecmp($id,'thumbs')!=0)){	
				$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.'&pblogID='.$photoBlogID?>"><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.PHOTOBLOG_UPLOADS.$photoBlogID.'/'.$id?>">
			<img src="<?='../blogs/'.$blogID.PHOTOBLOG_UPLOADS.$photoBlogID.PHOTOBLOG_THUMBS.$id?>" border="1" width="48" height="48"></a>
		</td>
		<td><a href="javascript:commentWin('viewComment.php?id=<?="../blogs/".$blogID.PHOTOBLOG_UPLOADS.$photoBlogID.PHOTOBLOG_THUMBS.$id?>');"><img src="./images/comment.png" width="16px" height="16px" border="0" alt="View Comment"></a></td>
	</tr>
<?php
			}
	}} // end for
?>
	<tr bgcolor="white">
		<td colspan="6"><hr size="-1"></td>
	</tr>
</table>