<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_ecom.gif" width="20" height="21" align="middle"> '.$MsgText[9][39];
	$BrowserTitle = $MsgText[9][39];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/store-products.html">'.$MsgText[9][3].'</a> > '.$MsgText[9][39];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[901] == 'y') {
		
			
			$query = "select * from `".$Settings['DBPrefix']."ec-Settings`";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				$ecSettings[$row['Name']] = $row['Value'];
			}
						
			if ($ecSettings['StorageLocationHTTP'] == '') {
				$ecSettings['StorageLocationHTTP'] = $ImagesURL.'/store';
			}
			if ($ecSettings['StorageLocationLocal'] == '') {
				$ecSettings['StorageLocationLocal'] = './images/store';
			}
		
		
			ReadFormArguments();
			Global $FormArguments;
									
			if (count($FormArguments) > 0 && $FormArguments['ProductName'] != '') {
			
				if ($FormArguments['DeleteProduct'] == 'yes') {
					//$query = "delete from `".$Settings['DBPrefix']."ec-Products` where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
					//$result = mysql_query($query) or die (mysql_error());
					
					//$query = "delete from `".$Settings['DBPrefix']."ec-Items` where Product='".$FormArguments['ProductIDToEdit']."'";
					//$result = mysql_query($query) or die (mysql_error());
					
					$query = "update `".$Settings['DBPrefix']."ec-Products` set Active=0 where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
					$result = mysql_query($query) or die (mysql_error());	
				}
				else {
					if ($FormArguments['ProductID'] == '') {
						$query = "SELECT MAX(CONVERT(ProductID, SIGNED INTEGER)) as MaxID FROM `".$Settings['DBPrefix']."ec-Products` WHERE CONVERT(ProductID, SIGNED INTEGER)>0";
						$result = mysql_query($query) or die (mysql_error());
						$row = mysql_fetch_array($result);
									
						$FormArguments['ProductID'] = ($row['MaxID']+rand(1,10));
					}
				
					$query = "update `".$Settings['DBPrefix']."ec-Products` set ProductID='".$FormArguments['ProductID']."', Name='".$FormArguments['ProductName']."', ShortDescription='".$FormArguments['ShortDescription']."', Description='".$FormArguments['ProductDescription']."', Category='".$FormArguments['Parent']."', Category2='".$FormArguments['Category2']."', Category3='".$FormArguments['Category3']."', Category4='".$FormArguments['Category4']."', Category5='".$FormArguments['Category5']."', Option1Title='".$FormArguments['Option1Title']."', Option2Title='".$FormArguments['Option2Title']."' where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
					$result = mysql_query($query) or die (mysql_error());
					
					for ($ImageCounter = 1; $ImageCounter <= 5; $ImageCounter++) {
				
						if ($ImageCounter > 1) {
							$NumberPart = $ImageCounter;
							$NewFilenamePart = '_'.$ImageCounter;
						}
					
										
						if ($_FILES['FileData'.$NumberPart] != '' && $_FILES['FileData'.$NumberPart]['size'] > 0) {
					
							$FileData = $_FILES['FileData'.$NumberPart]['tmp_name'];
							$FileSize = $_FILES['FileData'.$NumberPart]['size'];
							
							
							$NewFilename = 'store_'.$FormArguments['ProductIDToEdit'].$NewFilenamePart;
							
							$DestinationPath = $ecSettings['StorageLocationLocal'].'/';
							
							
							move_uploaded_file($FileData, $DestinationPath.$NewFilename);
							
							if ($Settings['FilesCHMOD'] != '') {
								@chmod($DestinationPath.$NewFilename, $Settings['FilesCHMOD']);
							}
							
							
							$FileData = $DestinationPath.$NewFilename;
							
							$ImageInfo = getimagesize($FileData);
							
							$ImageWidth = $ImageInfo[0];
							$ImageHeight = $ImageInfo[1];
							$FileType = $ImageInfo[2];
							
							
							if (function_exists("gd_info")) {
								$GDInfo = @gd_info();
							}
							if ($GDInfo['GIF Create Support'] == true) {
								$GIFWriteSupport = true;
							}
							
				
							if (!is_file($DestinationPath.$NewFilename)){
								$PageTitle = $MsgText[0][328];
								$PageContents .= '<b><br>Image '.$ImageCounter.': '.$MsgText[0][332].'<br>(1)</b><br><br>';
								
								@unlink($FileData);
							}
							else if (($FileType != 1 || $GIFWriteSupport != 'true') && $FileType != 2 && $FileType != 3) {
						   		$PageTitle = $MsgText[0][328];
								$PageContents .= '<b><br>Image '.$ImageCounter.': '.$MsgText[3][67].'<br>(2)</b><br><br>';
		
								@unlink($FileData);
							}
							else if ($FileSize == 0) {
						   		$PageTitle = $MsgText[0][328];
								$PageContents .= '<b><br>Image '.$ImageCounter.': '.$MsgText[0][330].': '.number_format($ecSettings['MaxImageFileSize'],0,'','').' KBytes<br>(2)</b><br><br>';
		
								@unlink($FileData);
							}
							else if ($FileSize > ($ecSettings['MaxImageFileSize']*1024)) {
								$PageTitle = $MsgText[0][328];
								$PageContents .= '<b><br>Image '.$ImageCounter.': '.$MsgText[0][331].': '.number_format($ecSettings['MaxImageFileSize'],0,'','').' KBytes.<br>(3)</b><br><br>';
		
								@unlink($FileData);
							}
							else {
   														
								$DBFileName = $NewFilename;
							
								
								// generate the thumbnail and medium size preview images
								
								$FileSource = $DestinationPath.$NewFilename;
					
								// thumbnail
								$NewWidth = $ecSettings['ThumbMaxWidth'];
								$NewHeight = $ecSettings['ThumbMaxHeight'];
							
								$WidthRatio = $ImageWidth / $NewWidth;
								$HeightRatio = $ImageHeight / $NewHeight;
							
								if ($WidthRatio >= $HeightRatio) {
								
									$NewWidthCalc = number_format($ImageWidth / $WidthRatio, 0);
									$NewHeightCalc = number_format($ImageHeight / $WidthRatio, 0);
								
								}
								else {
								
									$NewWidthCalc = number_format($ImageWidth / $HeightRatio, 0);
									$NewHeightCalc = number_format($ImageHeight / $HeightRatio, 0);
								
								}
							
								
								if ($FileType == 1) {
									$SourceData = imagecreatefromgif($FileSource);
									$Extension = '-thumb.gif';
								}
								else if ($FileType == 2) {
									$SourceData = imagecreatefromjpeg($FileSource);
									$Extension = '-thumb.jpg';
								}
								else if ($FileType == 3) {
									$SourceData = imagecreatefrompng($FileSource);
									$Extension = '-thumb.png';
								} 
								
								$DestinationImage = imagecreatetruecolor($NewWidthCalc, $NewHeightCalc);
								imagecopyresampled($DestinationImage, $SourceData, 0, 0, 0, 0, $NewWidthCalc, $NewHeightCalc, $ImageWidth, $ImageHeight);
							
							
								if ($FileType == 1) {
									imagegif($DestinationImage, $FileSource.$Extension);
								}
								else if ($FileType == 2) {
									imagejpeg($DestinationImage, $FileSource.$Extension);
								}
								else if ($FileType == 3) {
									imagepng($DestinationImage, $FileSource.$Extension);
								}
								
								if ($Settings['FilesCHMOD'] != '') {
									@chmod($FileSource.$Extension, $Settings['FilesCHMOD']);
								}
								
											
								imagedestroy($SourceData);
								imagedestroy($DestinationImage);
								
							
							
								if (($ImageWidth > $ecSettings['MaxWidth'] || $ImageHeight > $ecSettings['MaxHeight']) && ($ecSettings['LargeImageMaxWidth'] > $ecSettings['MaxWidth'] && $ecSettings['LargeImageMaxHeight'] > $ecSettings['MaxHeight'])) {
					
									if ($FileType == 1) {
										$SourceData = imagecreatefromgif($FileSource);
										$Extension = '-large.gif';
									}										
									else if ($FileType == 2) {
										$SourceData = imagecreatefromjpeg($FileSource);
										$Extension = '-large.jpg';
									}
									else if ($FileType == 3) {
										$SourceData = imagecreatefrompng($FileSource);
										$Extension = '-large.png';
									} 
						
						
									if ($ImageWidth > $ecSettings['LargeImageMaxWidth'] || $ImageHeight > $ecSettings['LargeImageMaxHeight']) {
									
										$NewWidth = $ecSettings['LargeImageMaxWidth'];
										$NewHeight = $ecSettings['LargeImageMaxHeight'];
								
										$WidthRatio = $ImageWidth / $NewWidth;
										$HeightRatio = $ImageHeight / $NewHeight;
								
										if ($WidthRatio >= $HeightRatio) {
										
											$NewWidthCalc = number_format($ImageWidth / $WidthRatio, 0);
											$NewHeightCalc = number_format($ImageHeight / $WidthRatio, 0);
								
										}
										else {
										
											$NewWidthCalc = number_format($ImageWidth / $HeightRatio, 0);
											$NewHeightCalc = number_format($ImageHeight / $HeightRatio, 0);
										
										}
										
										
										$DestinationImage = imagecreatetruecolor($NewWidthCalc, $NewHeightCalc);
										imagecopyresampled($DestinationImage, $SourceData, 0, 0, 0, 0, $NewWidthCalc, $NewHeightCalc, $ImageWidth, $ImageHeight);
		
										if ($FileType == 1) {
											imagegif($DestinationImage, $FileSource.$Extension);
										}										
										else if ($FileType == 2) {
											imagejpeg($DestinationImage, $FileSource.$Extension);
										}
										else if ($FileType == 3) {
											imagepng($DestinationImage, $FileSource.$Extension);
										} 										
								
										if ($Settings['FilesCHMOD'] != '') {
											@chmod($FileSource.$Extension, $Settings['FilesCHMOD']);
										}
																			
										imagedestroy($SourceData);
										imagedestroy($DestinationImage);
		
									}
									else {
										
										if ($FileType == 1) {
											imagegif($SourceData, $FileSource.$Extension);
										}										
										else if ($FileType == 2) {
											imagejpeg($SourceData, $FileSource.$Extension);
										}
										else if ($FileType == 3) {
											imagepng($SourceData, $FileSource.$Extension);
										} 										
								
										if ($Settings['FilesCHMOD'] != '') {
											@chmod($FileSource.$Extension, $Settings['FilesCHMOD']);
										}
										
										
										$NewWidthCalc = $ImageWidth;
										$NewHeightCalc = $ImageHeight;
																			
										imagedestroy($SourceData);
									
									}
							
																						
									$query = "update `".$Settings['DBPrefix']."ec-Products` set LargeImageType".$NumberPart."='".$FileType."', LargeImageWidth".$NumberPart."='".$NewWidthCalc."', LargeImageHeight".$NumberPart."='".$NewHeightCalc."' where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
									$result = mysql_query($query) or die (mysql_error());
								
						
								}
								else {
									$query = "update `".$Settings['DBPrefix']."ec-Products` set LargeImageType".$NumberPart."='0', LargeImageWidth".$NumberPart."='0', LargeImageHeight".$NumberPart."='0' where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
									$result = mysql_query($query) or die (mysql_error());
								
									$Extension = '-large.gif';
									@unlink($FileSource.$Extension);
								}
							
							
							
								if ($ImageWidth > $ecSettings['MaxWidth'] || $ImageHeight > $ecSettings['MaxHeight']) {
				
									$NewWidth = $ecSettings['MaxWidth'];
									$NewHeight = $ecSettings['MaxHeight'];
								
									$WidthRatio = $ImageWidth / $NewWidth;
									$HeightRatio = $ImageHeight / $NewHeight;
								
									if ($WidthRatio >= $HeightRatio) {
									
										$NewWidthCalc = number_format($ImageWidth / $WidthRatio, 0);
										$NewHeightCalc = number_format($ImageHeight / $WidthRatio, 0);
									
									}
									else {
									
										$NewWidthCalc = number_format($ImageWidth / $HeightRatio, 0);
										$NewHeightCalc = number_format($ImageHeight / $HeightRatio, 0);
									
									}
								
									if ($FileType == 1) {
										$SourceData = imagecreatefromgif($FileSource);
										$Extension = '.gif';
									}										
									else if ($FileType == 2) {
										$SourceData = imagecreatefromjpeg($FileSource);
										$Extension = '.jpg';
									}
									else if ($FileType == 3) {
										$SourceData = imagecreatefrompng($FileSource);
										$Extension = '.png';
									} 
									
									$DestinationImage = imagecreatetruecolor($NewWidthCalc, $NewHeightCalc);
									imagecopyresampled($DestinationImage, $SourceData, 0, 0, 0, 0, $NewWidthCalc, $NewHeightCalc, $ImageWidth, $ImageHeight);
								
									
									if ($FileType == 1) {
										imagegif($DestinationImage, $FileSource.$Extension);
									}										
									else if ($FileType == 2) {
										imagejpeg($DestinationImage, $FileSource.$Extension);
									}
									else if ($FileType == 3) {
										imagepng($DestinationImage, $FileSource.$Extension);
									} 									
								
									if ($Settings['FilesCHMOD'] != '') {
										@chmod($FileSource.$Extension, $Settings['FilesCHMOD']);
									}
									
									unlink($FileSource); 
											
									imagedestroy($SourceData);
									imagedestroy($DestinationImage);
								
									
									$ImageWidth = $NewWidthCalc;
									$ImageHeight = $NewHeightCalc;
									
									$FileSize = filesize($FileSource.$Extension);
									
											
								} // end if larger than max dimensions
								else {
							
									if ($FileType == 1) {
										$Extension = '.gif';
									}
									else if ($FileType == 2) {
										$Extension = '.jpg';
									}
									else if ($FileType == 3) {
										$Extension = '.png';
									}
									
									rename($FileSource,$FileSource.$Extension);
								 					
							
								} // end if not larger than max dimensions
							
							
						
								$query = "update `".$Settings['DBPrefix']."ec-Products` set ImageType".$NumberPart."='".$FileType."', ImageWidth".$NumberPart."='".$ImageWidth."', ImageHeight".$NumberPart."='".$ImageHeight."' where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
								$result = mysql_query($query) or die (mysql_error());
				
							} // successful upload
				
				
						} // end if image attached
						else if ($FormArguments['DeleteImage'.$ImageCounter] == 'yes') {
						
							$query = "select * from `".$Settings['DBPrefix']."ec-Products` where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
							$result = mysql_query($query) or die (mysql_error());
							$row = mysql_fetch_array($result);
						
							$query = "update `".$Settings['DBPrefix']."ec-Products` set ImageType".$NumberPart."='0', ImageWidth".$NumberPart."='0', ImageHeight".$NumberPart."='0' where RecordNumber='".$FormArguments['ProductIDToEdit']."' limit 1";
							$result = mysql_query($query) or die (mysql_error());
														
							
							$ImageExtension[1] = '.gif';
							$ImageExtension[2] = '.jpg';
							$ImageExtension[3] = '.png';
	
							$ThumbImageExtension[1] = '-thumb.gif';
							$ThumbImageExtension[2] = '-thumb.jpg';
							$ThumbImageExtension[3] = '-thumb.png';
							
							
							$NewFilename = 'store_'.$FormArguments['ProductIDToEdit'].$NewFilenamePart;
							
							$DestinationPath = $ecSettings['StorageLocationLocal'].'/';
							
													
							$FileData = $DestinationPath.$NewFilename;
							
							
							if ($row['LargeImageType'] > 0) {
								@unlink($FileData.'-large'.$ImageExtension[$row['LargeImageType']]);
							}
							@unlink($FileData.$ImageExtension[$row['ImageType']]);
							@unlink($FileData.$ThumbImageExtension[$row['ImageType']]);
						
						}
					
					} // end images loop
					
				} // end if not deleting
				
				
				header('Location: '.$MainURL.'/admin/store-products.html');
				
			}
			else {
			
				$PageContents = $MsgText[0][98];
			
			}
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

	// ========================================================================================




?>
