<?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>        		              	  |
// |          									                          |
// +----------------------------------------------------------------------+
//Initially the user is shown a File Dialog Box which helps him to select a local file and upload it to
//web server. Once the user select a local file and click 'Upload' it is posted back to the same page.
//On postback($_POST['btnuploadtype']=="Upload"), we check for the type of the file, 
//	If the file is not of type gif/jpg/png then we alert the user 'format incorrect'.
//Next we check for the size limit for the user using validateSizePerUser()
//	if false then we alert the user that size limit is exceeded. 
//We create a system defined name for the file of the format 'ug_userid_timestamp.imagetype'
//IF type == "new" then
//	work location is set as workarea/tempsites/tempsiteid/images/
//ELSE
//	work location is set as workarea/sites/siteid/images/
//END IF	
//(i)	set the destination newimagename as 'ug_userid_timestamp.imagetype'
//(ii)	move image from temporary location to usergallery/userid/images/newimagename 
//(iii)	set permission as 777 for the newimagename
//(iv)	copy image from usergallery/userid/images/newimagename to worklocation/newimagename
//(v)	set permission as 777 for worklocation/newimagename
//(vi)	If type == "new" then call watermarkimage() to watermarkimage in worklocation/newimagename
//(vii)	set the imagename in the javascript variable and close the window using javascript.
  include "../../../../../includes/session.php";
  include "../../../../../includes/config.php";
  include "../../../../../includes/function.php";
  echo '<script type="text/javascript" src="../ajaxfilemanager/jscripts/ajaxfilemanager.js"></script>';
    echo '<script type="text/javascript" src="../ajaxfilemanager/jscripts/for_tinymce.js"></script>';

  $tmpsiteid=$_SESSION['session_currenttempsiteid'];
  $templateid=$_SESSION['session_currenttemplateid'];
  $userid=$_SESSION["session_userid"];
  $var_type=($_GET["type"] != "")?$_GET["type"]:$_POST["type"];
 $var_type="edit";
 //print_r($_SERVER);	
  $basicLocation=substr($_SERVER['SCRIPT_URI'],0,strpos($_SERVER['SCRIPT_URI'],"/code/")); //fetchBasicLocation(1);
  if($var_type == "new") {
	  $rootserver = $basicLocation . "/workarea/tempsites";
	  $workLocation = "../../../../../workarea/tempsites/$tmpsiteid/images/";	
  }
  else {
	  $rootserver = $basicLocation . "/workarea/sites";
	  $workLocation="../../../../../workarea/sites/$tmpsiteid/images/";
  }

$errormessage = "<br>&nbsp;<br>&nbsp;<br>&nbsp;<br>&nbsp;";
 if($_POST['btnuploadtype']=="Upload"){
         //echo "heree";
$size_per_user=0; 
$allowed_space=0;		
		
		$filename=$_FILES['imgfile']['name'];
		$imagewidth_height_type_array=explode(":",ImageType($_FILES['imgfile']['tmp_name']));
		$imagetype=$imagewidth_height_type_array[0];
		$assignedname="ug_".$userid."_".time().".".$imagetype;
		if($imagetype=="gif" or $imagetype=="png" or $imagetype=="jpg"){
			if(validateSizePerUser($userid,$size_per_user,$allowed_space,"../")) {
		        move_uploaded_file($_FILES['imgfile']['tmp_name'],"../../../../../usergallery/$userid/images/".$assignedname);
				chmod("../../../../../usergallery/$userid/images/".$assignedname,0777);
				$workLocation .= $assignedname;
				copy("../../../../../usergallery/$userid/images/".$assignedname,$workLocation);
				chmod($workLocation,0777);
				if($var_type == "new") {
					watermarkimage($workLocation);
				}
				?><SCRIPT>
	selectFile('<?php echo $rootserver."/".$tmpsiteid."/images/".$assignedname; ?>');
	</SCRIPT>

	<?php
			    echo "<script>returnValue = new Object(); returnValue.url=\"$rootserver/$tmpsiteid/images/".$assignedname."\"; window.close();</script>";
			}
			else {
				$errormessage="Your space exceeded permitted limit.<br>
								(Space taken by you: " . human_read($size_per_user) . " <br>
								Allowed space: " . human_read($allowed_space) . ")<br>
								Delete unused images or any/all of the sites created by you to proceed further.<br>&nbsp;<br>";
			}
		}else{
		   $errormessage="Images of this format not supported.  Please upload only gif,jpg or png images";
		}
  }		 
   $currentloc="../../../../../";
  include "../../../../../includes/applicationheader.php";
?>

	<SCRIPT>
	
	window.name="targ";
	 function canceltblpro(){
	 		returnValue = new Object();
			returnValue.url="";
	        window.close();
	  }
	  function savetblpro(){
			document.frmImageUpload.submit();
	        //opener.settablervalue(tborder,tborderclr,bgclr,cells,cellp,talign,twidth);
            //window.close();
	  }
	
	  
	</SCRIPT>
</head>
<body>
<table width="100%" cellpadding=2 align=center>
   <tr>
      <td align="center" valign="middle">
	                  <FIELDSET>
					      <legend class=maintextbold>Upload Image </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=errormessage colspan=2><?php echo $errormessage;?></td>
								  </tr>
							      <tr>
								     <td class=maintext>File</td>
								     <td class=maintext>
									   <input class=textbox type=file name=imgfile>
									 </td>
								  </tr>
							      <tr>
								    <td colspan=4 align=center><input id=btnuploadtype  name=btnuploadtype class=editorbutton  type=submit value="Upload" >
										<input class=editorbutton  type=button value="Cancel" onclick="canceltblpro();"></td>
								    </tr>
								 
								  </form>
							  </table>
						  
						  
					  </FIELDSET>
	  </td>
	</tr>
</table>	  				  	  

</body>
</html>