<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
// +----------------------------------------------------------------------+
// | PHP version 4/5                                                      |
// +----------------------------------------------------------------------+
// | Copyright (c) 2005-2006 ARMIA INC                                    |
// +----------------------------------------------------------------------+
// | This source file is a part of iScripts EasyCreate 1.1                 |
// +----------------------------------------------------------------------+
// | Authors: jimmy<jimmy.jos@armia.com>        		              	  |
// +----------------------------------------------------------------------+
//The page with the File Dialog Box is shown initially.  The user selects a local file and clicks the 
//'Upload button'.  The form is posted back with the  $_POST['btnuploadtype']=="Upload".
//IF type == "new" then
//	work location is set as workarea/tempsites/tempsiteid/flash/
//ELSE
//	work location is set as workarea/sites/siteid/flash/
//END IF
//New Flash name is found for the file in the format 'fl_userid_timestamp.imagetype'
//(i)	Imagetype is taken and ckecked for validity ie. if it is not .swf user is asked to upload 
//		a valid file type.
//(ii)	Checked for size limit using the function validateSizePerUser()
//		If false then user is shown an alert saying size limit exceeded and window closed.
//(iii)	move flash file from temporary location to usergallery/userid/flash/newFlashName
//(iv)	set permission of newFlashName as 777
//(v)	copy  usergallery/userid/flash/newFlashName to worklocation/newFlashName
//(vi)	set permission of worklocation/newFlashName as 777
//(vii)	set the returnUrl value as worklocation/newflashName and close the window. 
include "../includes/session.php";
include "../includes/config.php";
include "../includes/function.php";
$tmpsiteid=$_SESSION['session_currenttempsiteid'];
$templateid=$_SESSION['session_currenttemplateid'];
$userid=$_SESSION["session_userid"];
$var_type=($_GET["type"] != "")?$_GET["type"]:$_POST["type"];
$siteid=$tmpsiteid;

if($var_type == "new") {
	$rootserver = fetchBasicLocation(1) . "/workarea/tempsites";
	$dirpath="../workarea/tempsites/$siteid/flash/";
}
else {
	$rootserver = fetchBasicLocation(1) . "/workarea/sites";
	$dirpath="../workarea/sites/$siteid/flash/";
}
function ImageType_forFlash($image){
//echo "<br>image==".$image;
list($width, $height, $type, $attr) = @getimagesize($image);
switch($type){
	case 1 :
		  $returntype="gif";
		  break;
	case 2 :
		  $returntype="jpg";
		  break;
	case 3 :
		  $returntype="png";
		  break;
	case 4 :
		  $returntype="swf";
		  break;
	case 5 :
		  $returntype="psd";
		  break;
	case 6 :
		  $returntype="bmp";
		  break;
	case 7 :
		  $returntype="tiff";
		  break;
	case 8 :
		  $returntype="tiff";
		  break;
	case 13:
			$returntype="swf";
			break;	  
	default :
	$returntype= "notsupported";
		break;
}

return $returntype.":$width:$height";
}

// watermarkimage("./gallery/images/".$assignedname);
if($_POST['btnuploadtype']=="Upload"){
	$size_per_user=0; 
	$allowed_space=0;		

	$filename=$_FILES['imgfile']['name'];
	$imagewidth_height_type_array=explode(":",ImageType_forFlash($_FILES['imgfile']['tmp_name']));
	$imagetype=$imagewidth_height_type_array[0];
	$assignedname="fl_" . $userid . "_" . time() . "." . $imagetype;
	if($imagetype == "swf"){
		if(validateSizePerUser($userid,$size_per_user,$allowed_space,"../")) {
			move_uploaded_file($_FILES['imgfile']['tmp_name'],"../usergallery/$userid/flash/".$assignedname);
			chmod("../usergallery/$userid/flash/".$assignedname,0777);
			copy("../usergallery/$userid/flash/".$assignedname,$dirpath.$assignedname);
			chmod($dirpath.$assignedname,0777);
			echo "<script>returnValue = new Object(); returnValue.url=\"$rootserver/$siteid/flash/".$assignedname."\"; window.close();</script>";
		}
		else {
			echo "<script> returnValue = new Object(); returnValue.url=''; alert('Your space exceeded permitted limit." . 
			"(Space taken by you: " . human_read($size_per_user) . " " . 
			"Allowed space: " . human_read($allowed_space) . ") \\r\\n" . 
			"Delete unused images, or any/all of the sites created by you to proceed further.'); window.close(); </script>";			
		}
	}
	else {
		echo "<script> returnValue = new Object(); returnValue.url=''; alert('Please upload a valid flash file.'); window.close(); </script> ";  
	}		
}		
$currentloc="../";	  
include "../includes/applicationheader.php"; 
?>
	<SCRIPT>
	window.name="targ";
	 function canceltblpro(){
	 		returnValue = new Object();
			returnValue.url="";
	        window.close();
	  }
	  function savetblpro(){
			document.frmImageUpload.submit();
	  }
	
	  
	</SCRIPT>

<body>
<table width="100%" cellpadding=2 align=center>
   <tr>
      <td>
	                  <FIELDSET>
					      <legend class=maintextbold>Upload Flash</legend>
						    <table>
							      <form  method="post" enctype="multipart/form-data"  name="frmImageUpload" target="targ">
							      <input type="hidden" name="type" id="type" value="<?php echo($var_type); ?>">
								  <tr>
							        <td class=maintext>&nbsp;</td>
							        <td class=maintext>&nbsp;</td>
							        </tr>
							      <tr>
								     <td class=maintext>File</td>
								     <td class=maintext height="30">
									   <input class="textbox" type=file name=imgfile>
									 </td>
								  </tr>
							      <tr>
							        <td colspan=4 align=center height="30"><input id=btnuploadtype  name=btnuploadtype class=editorbutton  type=submit value="Upload" >
                                      <input class=editorbutton  type=button value="Cancel" onClick="canceltblpro();"></td>
							        </tr>
							      <tr>
								    <td colspan=4 align=center>&nbsp;</td>
								    </tr>
								 
								  </form>
							  </table>
						  
						  
					  </FIELDSET>
	  </td>
	</tr>
</table>	  				  	  
<?php
  include "./includes/editorfooter.php";
?> 
