<?php
$addon_path="../../";
include $addon_path."includes/config.php";
include $addon_path."includes/database.php";
include $addon_path."includes/keyword.php";
include $addon_path."includes/function.php";
include $addon_path."includes/session.php";

?>

<html>
<head>

<LINK REL="SHORTCUT ICON" HREF="images/cloud.ico" >

<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">

        
<meta http-equiv=content-type content="text/html; charset=GB2312">
    
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath1; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath2; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath3; ?>" rel=stylesheet media="(min-width: 641px)">
<LINK href="<?php print $addon_path."theme/desktop/".$cssPath4; ?>" rel=stylesheet media="(min-width: 641px)">


<LINK href="<?php print $addon_path."theme/mobile/".$cssPath1; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath2; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath3; ?>" rel=stylesheet media="(max-width: 640px)">
<LINK href="<?php print $addon_path."theme/mobile/".$cssPath4; ?>" rel=stylesheet media="(max-width: 640px)">


<LINK href="<?php print $addon_path.$fontPath; ?>" rel=stylesheet>
    
    
    
    
    
<script language="javascript" src="<?php print $addon_path.$jsPath1; ?>"></script>
<script language="javascript" src="<?php print $addon_path.$jsPath2; ?>"></script>
<script language="javascript" src="<?php print $addon_path.$jsPath3; ?>"></script>
<script language="javascript" src="<?php print $addon_path.$jsPath4; ?>"></script>
   

<body leftmargin=0 topmargin=0 marginwidth=0 marginheight=0 onload="mon_on_load_init()">
<table align=left width=100% ><tr><td>
<?php




if(isset($_POST['upload']) && $_FILES['uploader_file']['size'] > 0)
{
    $fileName = $_FILES['uploader_file']['name'];
    $tmpName  = $_FILES['uploader_file']['tmp_name'];
    $fileSize = $_FILES['uploader_file']['size'];
    $fileType = $_FILES['uploader_file']['type'];
    
    
    $fp= fopen($tmpName, 'r');
	$content = fread($fp, filesize($tmpName));
	
	//$content = addslashes($content);
	fclose($fp);
					
	if(!get_magic_quotes_gpc())
		{
		$fileName = addslashes($fileName);
	}

	if(($fileType=="image/jpeg") or ($fileType=="image/pjpeg") or ($fileType=="image/gif"))
		{
						      
			//=========RESIZE===============
				$oSourceImage = imagecreatefromstring($content); 
						      
				$nWidth = imagesx($oSourceImage); // get original source image width
				$nHeight = imagesy($oSourceImage); // and height
							
				$img_ratio=$nHeight/$nWidth;
								
				// create small thumbnail
				if($file_resize_width!="")
				{
					$nDestinationWidth = $file_resize_width;
					$nDestinationHeight = $file_resize_width*$img_ratio;
                  	
				}
			    else
				{
					$nDestinationWidth = $nWidth;
					$nDestinationHeight = $nHeight;
					
				}         

				$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
				imagecopyresized(
					$oDestinationImage, $oSourceImage,
					0, 0, 0, 0,
					$nDestinationWidth, $nDestinationHeight,
					$nWidth, $nHeight
				); // resize the image
							
				
				if($file_stamp_logo)
					{
								  
						$logo = imagecreatefromgif("../../".$file_stamp_path);
						imagealphablending ($logo, true);
						imagecopy($oDestinationImage, $logo, $nDestinationWidth-120, $nDestinationHeight-80, 0, 0, 120, 80);
						
					}	
				
				
			
				$content=$sBinaryThumbnail;
				$fileType="image/jpeg";	
				
				
				if($file_storage_method=="OS")
				{
					$content="";
				}
				else
				{
				 
				 	ob_start(); // Start capturing stdout.
					imageJPEG($oDestinationImage);
					$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
					$sBinaryThumbnail = addslashes($sBinaryThumbnail);
					ob_end_clean(); // Dump the stdout so it does not screw other output.
		
					$content=$sBinaryThumbnail;
				}
				$query = "INSERT INTO t_".$key_table_prefix."_product_photo
										(
										pho_file, 
										pho_product_code 
										) 
						        		VALUES 
										(
										'$content',
										'".$_REQUEST["pdt_code"]."'
										)
										ON DUPLICATE KEY UPDATE pho_file = '".$content."' ";
										
										
										
										
				mysqli_query($dbconn,$query) or die('Photo Saving Fail - Err msg  '.mysqli_error($dbconn));     			
				$photo_id=mysqli_insert_id($dbconn);			
						
							
				if($file_storage_method=="OS")
				{
					//imageGIF($oDestinationImage); // As though output to browser.
					//imageJPEG($oDestinationImage ,"../../".$file_storage_folder.$photo_id.'.jpg');
				}
			
	
						      
				//======END of RESIZE============
		   			
			
		}				
        
    }
  
?>




<?php

if($_REQUEST["action"]=="DELETE")
{
  	
  
  
    $query_delete="delete from t_".$key_table_prefix."_product_photo where pho_product_code='".$_REQUEST["pdt_code"]."';";
    $result_delete = mysqli_query($dbconn,$query_delete) or die("Photo Deletion failed - Err msg ". mysqli_error($dbconn));
    
}



?>



<?php
//Open File:

echo "<table cellpadding=10 width=100% >";

	echo "<tr>";
		echo "<td width=10% valign=top  >";
			$photo_exist=false;
			
			
			$query_photo="SELECT pho_product_code FROM t_".$key_table_prefix."_product_photo";
			$query_photo=$query_photo." WHERE pho_product_code = '".$_REQUEST["pdt_code"]."';";
			$result_photo=mysqli_query($dbconn,$query_photo)or die("Get Data failed- Err msg  ". mysqli_error($dbconn));
					
			if($line_photo=mysqli_fetch_array($result_photo,MYSQLI_ASSOC))
			{
				$photo_exist=true;
				echo "<img src='photo.php?code=".$line_photo["pho_product_code"]."' height=500 align=right >";
			}
			else {
				
				
				echo "<div style='width:300px;height:300px;background:#ccc;'>";
					echo "<br>";
				echo "</div>";
			}

					
		echo "</td>";
		
		
		echo "<td valign=top >";
				
				
				
				 echo "<form name='uploader' action='' method='post' enctype='multipart/form-data' >";
				 echo "<table style='background:#eee;border-radius:3px;' cellpadding=10 cellspacing=2>";
				 	
				 	echo "<tr>";
				 		echo "<td valign=top >";
						
						
							 echo "<input size=10 type=file name='uploader_file' class=upload ><br><br>";
							 echo "<input type=hidden name='pdt_code' value='".$_REQUEST["pdt_code"]."' >";
							 echo "<input type=submit value='Attach Now' name='upload' class='button button-blue' ><br>";
							 
						 
						 
						 
						 
						 
						 	if($photo_exist)
							{
								
								echo "<br><br><input type=button class='button button-red' value='Remove File' onclick=\"delete_photo('".$_REQUEST["pdt_code"]."') \">";
							}
						 
						 echo "</td>";
				 	 echo "</tr>";
				 echo "</table>";
				 echo "</form>";	
					
		echo "</td>";
	
	echo "</tr>";
echo "</table>";


?>


				
				
								
								
				




<script language=javascript>
function delete_photo(code)
{
	  	if(confirm("Are you sure want to delete this file?"))
		  	{
		  	 
		  	window.location="photo_uploader.php?action=DELETE&pdt_code="+code+"";
			}
	
  
}
</script>

<?php
//include "../include/include_post_nofooter.php";
?>
</td></tr></table>
</body>
