<?php
include "../includes/session.php";
include "../includes/config.php";
include "../includes/function.php";
$tmpsiteid = $_SESSION['session_currenttempsiteid'];
$templateid = $_SESSION['session_currenttemplateid'];
$userid = $_SESSION["session_userid"];
$siteid = $_SESSION["session_currenttempsiteid"];
$relativepathtoserver = "../";
require_once './includes/class.cropinterface.php';
$ci = new cropInterface();
/*The parent window(editor.php) will pass the image to be edited as GET varibale*/
$imagenamefromparentwindow = $_GET['imagename'];
/* clear all session varibale.*/
if ($imagenamefromparentwindow != "") {
				$_SESSION['sitetype'] = "";
				$_SESSION['session_texttodisplay'] = "";
				$_SESSION['session_currentsamplename'] = "";
				$_SESSION['session_imagetoedit'] = "";
				$_SESSION['session_currentimage'] = "";
				$_SESSION['session_editedimage'] = "";
				$_SESSION['session_templatebandlocation'] = "";
				$_SESSION['session_actualbanlocation'] = "";
} 
$var_sitetype = $_GET['type'];
/* $imagenamefromparentwindow will be like tp_logoimage.jpg,ug_logoimage_5timestamp.extn
*  from this image name identify the image whether it is logo image or company etc 
*/ 
$imagetoedit = substr($imagenamefromparentwindow, 3, 7); 
/*              $texttodisplay ->button value to be displayed and it will be availabe in all pages using  'session_texttodisplay'
				$samplename -> sample text to be displayed in textarea and it will be availabe in all pages using  'session_currentsamplename'
				$tp_band  ->the jpg image band within which we append logo image or add text.
				            eg->tp_logoband.jpg when u user tries to add text to logoimage    
				$tp_band_gif ->the gif image band within which we append logo image or add text.
				$newimagename ->starting string for the final image name(ie ug_logoimage_)
*/ 
if ($imagetoedit == "logoima") {
				$texttodisplay = "Add your logo";
				$_SESSION['session_texttodisplay'] = $texttodisplay;
				$samplename = "your logo";
				$_SESSION['session_currentsamplename'] = $samplename;
				$tp_band = "tp_logoband.jpg";
				$tp_band_gif = "tp_logoband.gif";
				$newimagename = "ug_logoimage_";
} else if ($imagetoedit == "company") {
				$texttodisplay = "Add your company";
				$_SESSION['session_texttodisplay'] = $texttodisplay;
				$samplename = "your company";
				$_SESSION['session_currentsamplename'] = $samplename;
				$tp_band = "tp_companyband.jpg";
				$tp_band_gif = "tp_companyband.gif";

				$newimagename = "ug_company_";
} else if ($imagetoedit == "caption") {
				$texttodisplay = "Add your caption";
				$_SESSION['session_texttodisplay'] = $texttodisplay;
				$samplename = "your caption";
				$_SESSION['session_currentsamplename'] = $samplename;
				$tp_band = "tp_captionband.jpg";
				$tp_band_gif = "tp_captionband.gif";

				$newimagename = "ug_caption_";
} else if ($imagetoedit == "innerca") {
				$texttodisplay = "Add your caption";
				$_SESSION['session_texttodisplay'] = $texttodisplay;
				$samplename = "your caption";
				$_SESSION['session_currentsamplename'] = $samplename;
				$tp_band = "tp_innercaptionband.jpg";
				$tp_band_gif = "tp_innercaptionband.gif";

				$newimagename = "ug_innercaption_";
} else if ($imagetoedit == "innerco") {
				$texttodisplay = "Add your company";
				$_SESSION['session_texttodisplay'] = $texttodisplay;
				$samplename = "your company";
				$_SESSION['session_currentsamplename'] = $samplename;
				$tp_band = "tp_innercompanyband.jpg";
				$tp_band_gif = "tp_innercompanyband.gif";

				$newimagename = "ug_innercompany_";
} else if ($imagetoedit == "innerlo") {
				$texttodisplay = "Add your logo";
				$_SESSION['session_texttodisplay'] = $texttodisplay;
				$samplename = "your logo";
				$_SESSION['session_currentsamplename'] = $samplename;
				$tp_band = "tp_innerlogoband.jpg";
				$tp_band_gif = "tp_innerlogoband.gif"; 
				// $tp_band="tp_innerlogoimage.jpg";
				$newimagename = "ug_innerlogoimage_";
} 

if ($tp_band != "") {
				/* check logoband exist in template location*/
				if (!is_file($relativepathtoserver . $_SESSION["session_template_dir"] . "/" . $templateid . "/images/" . $tp_band)) {
								if (!is_file($relativepathtoserver . $_SESSION["session_template_dir"] . "/" . $templateid . "/images/" . $tp_band_gif)) {
												echo "Image could not found.Please try again";
												exit;
								} 
				} 

				$_SESSION['sitetype'] = $var_sitetype; 
				/* check band is jpg image*/
				if (is_file($relativepathtoserver . $_SESSION["session_template_dir"] . "/" . $templateid . "/images/" . $tp_band)) {
								$_SESSION['session_imagetoedit'] = $tp_band;
								$actuallogbandloc = $relativepathtoserver . $_SESSION["session_template_dir"] . "/" . $templateid . "/images/" . $tp_band;
                                /* if site is not published*/ 
								if ($_SESSION['sitetype'] == "new") { 
								               /* set 'session_currentimage' to $newimagename(ug_logoimage)+reststring
											   *  copy the template band to workarea location(tempsites)
											   *  set this image(image in workarea location) to 'session_editedimage'
											   */
												$_SESSION['session_currentimage'] = $newimagename . $userid . "_t" . $templateid . ".jpg";
												copy($actuallogbandloc, $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $tp_band);
												$templatebandlocation = $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $tp_band;
												$_SESSION['session_editedimage'] = $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $tp_band;
								} else {
								               /* set 'session_currentimage' to $newimagename(ug_logoimage)+reststring
											   *  copy the template band to workarea location(sites)
											   *  set this image(image in workarea location) to 'session_editedimage'
											   */
												$_SESSION['session_currentimage'] = $newimagename . $userid . "_s" . $templateid . ".jpg";
												copy($actuallogbandloc, $relativepathtoserver . "workarea/sites/$siteid/images/" . $tp_band);
												$templatebandlocation = $relativepathtoserver . "workarea/sites/$siteid/images/" . $tp_band;
												$_SESSION['session_editedimage'] = $relativepathtoserver . "workarea/sites/$siteid/images/" . $tp_band;
								}
								/* 'session_templatebandlocation' will be set to bandimage in workarea loction
								*  'session_actualbanlocation' will be set to bandimage in template location
								*/  
								$_SESSION['session_templatebandlocation'] = $templatebandlocation;
								$_SESSION['session_actualbanlocation'] = $actuallogbandloc;
				} else if (is_file($relativepathtoserver . $_SESSION["session_template_dir"] . "/" . $templateid . "/images/" . $tp_band_gif)) {
								$_SESSION['session_imagetoedit'] = $tp_band_gif; 
								// $actuallogbandloc=$relativepathtoserver.$_SESSION["session_template_dir"]."/".$templateid."/images/".$tp_band;
								$actuallogbandloc = $relativepathtoserver . $_SESSION["session_template_dir"] . "/" . $templateid . "/images/" . $tp_band_gif;

								if ($_SESSION['sitetype'] == "new") { 
								                 /* set 'session_currentimage' to $newimagename(ug_logoimage)+reststring
											   *  copy the template band to workarea location(tempsites)
											   *  set this image(image in workarea location) to 'session_editedimage'
											   */
												$_SESSION['session_currentimage'] = $newimagename . $userid . "_t" . $templateid . ".gif";
												copy($actuallogbandloc, $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $tp_band_gif);
												$templatebandlocation = $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $tp_band_gif;
												$_SESSION['session_editedimage'] = $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $tp_band_gif;
								} else {
								               /* set 'session_currentimage' to $newimagename(ug_logoimage)+reststring
											   *  copy the template band to workarea location(sites)
											   *  set this image(image in workarea location) to 'session_editedimage'
											   */
												$_SESSION['session_currentimage'] = $newimagename . $userid . "_s" . $templateid . ".gif";
												copy($actuallogbandloc, $relativepathtoserver . "workarea/sites/$siteid/images/" . $tp_band_gif);
												$templatebandlocation = $relativepathtoserver . "workarea/sites/$siteid/images/" . $tp_band_gif;
												$_SESSION['session_editedimage'] = $relativepathtoserver . "workarea/sites/$siteid/images/" . $tp_band_gif;
								} 
								/* 'session_templatebandlocation' will be set to bandimage in workarea loction
								*  'session_actualbanlocation' will be set to bandimage in template location
								*/  
								$_SESSION['session_templatebandlocation'] = $templatebandlocation;
								$_SESSION['session_actualbanlocation'] = $actuallogbandloc;
				} 
} 

$errormessagefileupload = ""; 

if ($_POST['btnimageeditaddtext'] != "") {
                /* if user want to add text to band image*/ 
				header("location:imageeditor_addtext.php");
				exit;
} else if ($_POST['btnimageediteimage'] != "") {
                /* if user want to embed his/her image on band image*/
				header("location:imageeditor_embedimage.php");
				exit;
} 

if ($_POST['saveimage'] == "save") {
               /* sometimes basename('session_editedimage') and  'session_currentimage' will be different
			   *  when user click on save without entering text or embed image
			   */ 
				if (strcasecmp(basename($_SESSION['session_editedimage']), $_SESSION['session_currentimage']) == "0") {
								$filename = $_SESSION['session_currentimage'];
				} else {
				                
								$filename = basename($_SESSION['session_editedimage']);
				} 

				$assignedname = $filename; 
				/* for site that are not published*/
				if ($_SESSION['sitetype'] == "new") {
				                /* copy the edited image to workarea(tempsite) location and to usergallery
								*  created image name will be $newassignedname
								*/       
								$len = strrpos($assignedname, ".");
								$newassignedname = substr($assignedname, 0, $len) . time() . substr($assignedname, $len);
								@copy($relativepathtoserver . "usergallery/$userid/images/" . $assignedname, $relativepathtoserver . "usergallery/$userid/images/" . $newassignedname);
								@copy($relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $assignedname, $relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $newassignedname);
								@unlink($relativepathtoserver . "workarea/tempsites/$tmpsiteid/images/" . $assignedname);
								@unlink($relativepathtoserver . "usergallery/$userid/images/" . $assignedname); 
								
				} else {
								$len = strrpos($assignedname, ".");
								$newassignedname = substr($assignedname, 0, $len) . time() . substr($assignedname, $len);
								@copy($relativepathtoserver . "usergallery/$userid/images/" . $assignedname, $relativepathtoserver . "usergallery/$userid/images/" . $newassignedname);
								@copy($relativepathtoserver . "workarea/sites/$siteid/images/" . $assignedname, $relativepathtoserver . "workarea/sites/$siteid/images/" . $newassignedname);
								@unlink($relativepathtoserver . "workarea/sites/$siteid/images/" . $assignedname);
								@unlink($relativepathtoserver . "usergallery/$userid/images/" . $assignedname); 
							
				} 
				$assignedname = $newassignedname;
				echo "<script>returnValue = new Object(); returnValue.name=\"" . $assignedname . "\"; window.close();</script>";
				exit;
} else if ($_POST['closeimage'] == "close") {
				echo "<script>window.close();</script>";
				exit;
} 

$currentloc = "../";
include "../includes/applicationheader.php";

?>
<script>
window.name="targ";
function loading()
{
window.setTimeout("loadAfter()",200);
}

function loadAfter() {
	if(document.readyState != "complete") {
		window.setTimeout("loadAfter()",200);
		return;
	}else{
	  loaddhtml();
	
	}
}
</script>
</head>
<body>
<?php
  if(!validateSizePerUser($_SESSION["session_userid"],$size_per_user,$allowed_space)) {
					   $message = "The space taken by you has exceeded the allowed 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;";
						echo "<table border=0 width='100%' align=center valign=middle><tr><br><br><br><br><br><br><td class=errormessage align=center>".$message."</td></tr></table>";		
						
  }else{
?>  
<table width="100%" cellpadding=2 align=center>
   <tr>
      <td>
	                           <FIELDSET>
							    <LEGEND class=maintextbigbold>Image Manager</LEGEND>
								 <form name=frmImageeditor target="targ" method=post action=imageeditor.php > 
								<table width="97%" border="0">
								  <tr><td colspan=6>&nbsp;</td></tr>
								  <?php if($_SESSION['session_editedimage'] !=""){?>
								     <tr>
									   <td align=center><img id=myimage ></td>
									   <td>
									      <input type=hidden name="samplename" value="<?php echo $samplename;?>">
									      <input  class=button type=submit name=btnimageeditaddtext value="<?php echo $_SESSION['session_texttodisplay'];?>"><br><br>
										   <input class=button type=submit name=btnimageediteimage value="Embed your image">
									   </td>
									   
									 </tr>
								  
								  <?php } ?>
							    <tr><td colspan=2>&nbsp;</td></tr>
								<tr><td colspan=2 align=center >
								         <form name=frmImageSave target="targ" method=post>
										   
									     <input type=submit name=saveimage value="save" class=button>	
										  <input type=submit name=closeimage value="close" class=button>	
										 
										 </form>
								   
								   </td></tr>
								</table>   
								
							  </FIELDSET>       
	  </td>
   </tr>
  
		  
</table>
					 <?php if($_SESSION['session_editedimage'] !=""){
								  if(!is_dir("../tmpeditimages/$userid/")){
								   	@mkdir("../tmpeditimages/$userid/",0777);
									@chmod("../tmpeditimages/$userid/",0777);
								  }
								
								  $imagetodispaly="../tmpeditimages/$userid/".time().basename($_SESSION['session_editedimage']);
								  @copy($_SESSION['session_editedimage'],$imagetodispaly)
					?>
								<SCRIPT>
								  document.all("myimage").src="http://";
								  document.all("myimage").src="<?php echo $imagetodispaly; ?>";
								</SCRIPT>
					<?php } ?> 
  
<?php  } ?>

<?php
  include "./includes/editorfooter.php";
?> 


