<?

/*
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][37];
	$BrowserTitle = $MsgText[9][37];
	$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][37];
	
	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'] != '') {
			
				$query = "insert into `".$Settings['DBPrefix']."ec-Products` (Category, Category2, Category3, Category4, Category5, ProductID, Name, ShortDescription, Description, ImageWidth, ImageHeight, Active) values ('".$FormArguments['Parent']."', '".$FormArguments['Category2']."', '".$FormArguments['Category3']."', '".$FormArguments['Category4']."', '".$FormArguments['Category5']."', '".$FormArguments['ProductID']."', '".$FormArguments['ProductName']."', '".$FormArguments['ShortDescription']."', '".$FormArguments['ProductDescription']."', '".$FormArguments['ImageWidth']."', '".$FormArguments['ImageHeight']."', '1')";
				$result = mysql_query($query) or die (mysql_error());
				$RecordNumberCreated = mysql_insert_id();
				
				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);
									
					$query = "update `".$Settings['DBPrefix']."ec-Products` set ProductID='".($row['MaxID']+rand(1,10))."' where RecordNumber='".$RecordNumberCreated."' 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_'.$RecordNumberCreated.$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='".$RecordNumberCreated."' 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='".$RecordNumberCreated."' 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='".$RecordNumberCreated."' limit 1";
							$result = mysql_query($query) or die (mysql_error());
				
						} // successful upload
				
				
					} // end if image attached
					
				} // end images loop					
					
					
				if ($FormArguments['ProductPrice'] != '') {
					if ($FormArguments['NoInventory'] == 'y' || $FormArguments['ProductInventory'] == '') {
						$FormArguments['ProductInventory'] = -1;
					}
					else {
						$FormArguments['ProductInventory'] = intval($FormArguments['ProductInventory']);
					}	
					
					$FormArguments['ProductPrice'] = str_replace($ecSettings['ThousandsSep'],'',$FormArguments['ProductPrice']);
					$FormArguments['ProductPrice'] = str_replace($ecSettings['DecimalSymbol'],'.',$FormArguments['ProductPrice']);
					
					$FormArguments['ComparePrice'] = str_replace($ecSettings['ThousandsSep'],'',$FormArguments['ComparePrice']);
					$FormArguments['ComparePrice'] = str_replace($ecSettings['DecimalSymbol'],'.',$FormArguments['ComparePrice']);
					
					$FormArguments['AdditionalShipping'] = str_replace($ecSettings['ThousandsSep'],'',$FormArguments['AdditionalShipping']);
					$FormArguments['AdditionalShipping'] = str_replace($ecSettings['DecimalSymbol'],'.',$FormArguments['AdditionalShipping']);
					
															
					$query = "insert into `".$Settings['DBPrefix']."ec-Items` (Product, ItemID, Price1, PriceQty1, ComparePrice, Stock, StockMin, StockMax, ShippingWeight, AdditionalShipping) values ('".$RecordNumberCreated."', '".$FormArguments['ItemID']."', '".number_format($FormArguments['ProductPrice'],4,'.','')."', '1', '".number_format($FormArguments['ComparePrice'],4,'.','')."', '".$FormArguments['ProductInventory']."', '".$FormArguments['ProductInventoryMin']."', '".$FormArguments['ProductInventoryMax']."', '".number_format(($FormArguments['ShippingWeight']+($FormArguments['ShippingWeightOz']/16)),4,'.','')."', '".number_format($FormArguments['AdditionalShipping'],4,'.','')."')";
					$result = mysql_query($query) or die (mysql_error());
					
					if ($FormArguments['ItemID'] == '') {
						$RecordNumberCreated = mysql_insert_id();
					
						$query = "update `".$Settings['DBPrefix']."ec-Items` set ItemID='".$RecordNumberCreated."' where RecordNumber='".$RecordNumberCreated."' limit 1";
						$result = mysql_query($query) or die (mysql_error());
					}
					
					
					header('Location: '.$MainURL.'/admin/store-products.html');
				}
				else if ($FormArguments['CloneFrom'] != '') {
					header('Location: '.$MainURL.'/admin/store-products_new_clone/'.$RecordNumberCreated.'/'.$FormArguments['CloneFrom'].'.html');
				}
				else {
					header('Location: '.$MainURL.'/admin/store-products_edit/'.$RecordNumberCreated.'.html');
				}
				
			}
			else {
			
				$PageContents = $MsgText[0][98];
			
			}
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
