<?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>        		              	  |
// +----------------------------------------------------------------------+
//Insert a flash object into the page
//Two options are given  for the user
//	"type" parameter is received and assigned to a hidden variable, value is retained on page
//	being posted back.  value will be "new"/"edit"	
//	(i)		Upload a new flash object
//	(ii)	Insert a flash object from the list of pre-uploaded objects by the user
//	(i)		returnType is set to "1" and returnUrl is set to "" and window is closed.
//	(ii)	selected flash image name is set to "selectedimage" and posted back to the same page
//			On PostBack type is checked
//			IF type == "new" then
//				work location is set to workarea/tempsites/tempsiteid/flash/
//			ELSE
//				work location is set to workarea/sites/siteid/flash/
//			END IF
//			checked for size limit for the user.
//			IF false THEN alert the user saying that size limit exceeded and close the window
//			ELSE 
//			i.copy the flash object from usergallery/userid/flash/flashobject to worklocation/flashobject
//			ii.set permissions for the flash object as 777
//			iii.using javascript set returnType as 2, returnUrl as worklocation/flashobject 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"];
 //$rootserver="../workarea/tempsites";
 $siteid=$tmpsiteid;
 
$actual_loc="../usergallery/$userid/flash/";
 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/";
}

$errormessage = "<br>&nbsp;<br>&nbsp;";
if($_POST['selectedimage'] !=""){
$size_per_user=0; 
$allowed_space=0;		
	if(validateSizePerUser($userid,$size_per_user,$allowed_space,"../")) {
		 $filename=$_POST['selectedimage'];
		 $dirpath .= $filename;
		 @copy("../usergallery/$userid/flash/".$filename,$dirpath);
		 @chmod($dirpath,0777);
		 echo "<script>returnValue = new Object(); returnValue.type=2; returnValue.url=\"$rootserver/$tmpsiteid/flash/".$filename."\"; 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>";
	}
  }		
$currentloc="../";	  
include "../includes/applicationheader.php";
?>
	<SCRIPT>
		window.name="targ";
	  function checkNumber(t) {
	   t.value= parseInt(t.value);
		if(t.value.length == 0 ||  isNaN(t.value) || t.value.substr(0,1) == " " || parseInt(t.value) < 0) {
			t.value="";
			alert("Please Enter numeric value");
		}
		
		
      }
	  //cancel button clicked
	  function canceltblpro(){
	  returnValue = new Object();
	  returnValue.url = "";
	        window.close();
	  }
	  //If Upload flash selected then set returnType to "1", returnUrl to "" and close the window. 
	  //If a flash object is selected from the list then assign the name of the flash object to the 
	  //"selectedimage" field and postback the page.  
	  function savetblpro(){
			returnValue = new Object();
			if(document.imageproper.uptype[0].checked == true) {
				returnValue.type=1;
				returnValue.url="";
				window.close();
				return;
			}
			else if(document.imageproper.cmbUploads.value == "") {
				alert('Please select a flash object to be inserted.');
				return false;
			}
			document.all("selectedimage").value=document.all("cmbUploads").value;
			document.all("imageproper").method="post";
			document.all("imageproper").submit();
	  }
	</SCRIPT>

<body>
<table  width="100%" cellpadding=2>
   <tr>
      <td align="center">
	                  <FIELDSET>
					      <legend class=maintextbold align="center">Insert Flash</legend>
						    <table>
							      <form name="imageproper" method="post" target="targ">
								  <input type="hidden" name="type" id="type" value="<?php echo($var_type); ?>">
							      <tr>
								     <td><input name=uptype type=radio class=raiobutton checked value="0"></td>
								     <td class=maintext>Upload Flash </td>
								  </tr>
								  <tr>
								     <td><input  name=uptype type=radio class=raiobutton value="1"></td>
								     <td class=maintext>Select from the list </td>
								  </tr>
								  <tr>
								     <td colspan="2">
									 	<div align="center">
									 	  <select name="cmbUploads" id="cmbUploads">
										      <option value="">--Select one from the list--</option>
										      <?php
											    
											   $handle = opendir($actual_loc);
											   while (false !== ($file = readdir($handle))) {
											   //echo $file;
												if ($file != "." && $file != ".." && $file !="Thumbs.db" ) {
											 ?>
												       <?php //echo("<Option value=\"$rootserver/$siteid/flash/$file\">$file</Option>");
													    	echo("<Option value=\"$file\">$file</Option>");
														?>
								               <?php	  
												}
											   }	
											 ?>
									      </select>
										  <input type=hidden name=selectedimage id=selectedimage value="">
                                        </div></td>
							        </tr>
								
								  <tr>
								    <td colspan=4 align=center><input id=btnuploadtype  name=btnuploadtype class=editorbutton  type=button value="Submit" onclick="savetblpro();">
									<input class=editorbutton  type=button value="Cancel" onclick="canceltblpro();"></td>
								  </tr>
								 
								  </form>
							  </table>
						  
						  
					  </FIELDSET>
	  </td>
	</tr>
</table>	  				  	  
<?php
  include "./includes/editorfooter.php";
?> 