<?

/*
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
*/



	// ========================================================================================
	// save changes to existing photo
	// ========================================================================================
	
	$PageTitle = $MsgText[3][64];
	$BrowserTitle = $MsgText[3][64];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/photos.html">'.$MsgText[3][2].'</a> > '.$MsgText[3][64];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		ReadFormArguments();
		Global $FormArguments;
		
		
		$query55 = "select * from `".$Settings['DBPrefix']."pg-Images` where RecordNumber='".$FormArguments['RecordNumberToEdit']."' limit 1";
		$result55 = mysql_query($query55) or die (mysql_error());
		$row55 = mysql_fetch_array($result55);
		
		$query55 = "select MinLevel,MinEditLevel from `".$Settings['DBPrefix']."pg-Galleries` where RecordNumber='".$row55['GalleryNumber']."' limit 1";
		$result55 = mysql_query($query55) or die (mysql_error());
		$row55 = mysql_fetch_array($result55);
		
		
		if ($Permissions[301] == 'y' && $row55['MinLevel'] <= $UserLevel && $row55['MinEditLevel'] <= $UserLevel) {
			
			if (count($FormArguments) > 0) {
			
				if ($FormArguments['HidePoster'] == 'y') {
					$PostedBy = 0;
				}
				else {
					$PostedBy = $UserNumber;
				}
			
																																
				
				$query = "select * from `".$Settings['DBPrefix']."pg-Settings`";
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
					$pgSettings[$row['Name']] = $row['Value'];
				}
			
				if ($pgSettings['StorageLocationHTTP'] == '') {
					$pgSettings['StorageLocationHTTP'] = $BaseURL.'/pg_images';
				}
				if ($pgSettings['StorageLocationLocal'] == '') {
					$pgSettings['StorageLocationLocal'] = './pg_images';
				}
				
				
				if ($_FILES['FileData'] != '' && $_FILES['FileData']['size'] > 0) {
					// replacement image attached, so process it
					
					$FileData = $_FILES['FileData']['tmp_name'];
					$FileSize = $_FILES['FileData']['size'];
					
												
					
					$SendingPageRandom = $FormArguments['RandomNumber'];
					$RandomNumberTwo = sprintf("%03d", rand(0,999)) . sprintf("%02d", rand(0,99));
					
					$NewFilename = date('Y',GetTimestamp()).date('m',GetTimestamp()).date('d',GetTimestamp()).'_'.substr(date('U',GetTimestamp()),6,4).'_'.$SendingPageRandom.$RandomNumberTwo;
					
					
					
					$DestinationPath = $pgSettings['StorageLocationLocal'].'/';
					
					
					move_uploaded_file($FileData, $DestinationPath.$NewFilename);
					
					
					$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>'.$MsgText[0][332].'<br><br>(1)</b>';
						
						@unlink($FileData);
					}
					else if (($FileType != 1 || $GIFWriteSupport != 'true') && $FileType != 2 && $FileType != 3) {
				   		$PageTitle = $MsgText[0][328];
						$PageContents = '<b><br>'.$MsgText[3][67].'<br><br>(2)</b>';
		
						@unlink($FileData);
					}
					else if ($FileSize == 0) {
			   			$PageTitle = $MsgText[0][328];
						$PageContents = '<b><br>'.$MsgText[0][330].': '.number_format($Settings['MaxImageFileSize'],0,'','').' KBytes<br><br>(2)</b>';
	
						@unlink($FileData);
					}
					else if ($FileSize > ($pgSettings['MaxFileSize']*1024)) {
						$PageTitle = $MsgText[0][328];
						$PageContents = '<b><br>'.$MsgText[0][331].': '.number_format($Settings['MaxImageFileSize'],0,'','').' KBytes.<br><br>(3)</b>';
	
						@unlink($FileData);
					}
					else {
   													
						$DBFileName = $NewFilename;
						
						// generate the thumbnail and medium size preview images
						
						$FileSource = $DestinationPath.$NewFilename;
			
						// thumbnail
						$NewWidth = $pgSettings['ThumbMaxWidth'];
						$NewHeight = $pgSettings['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); 
						}
						
									
						imagedestroy($SourceData);
						imagedestroy($DestinationImage);
						
						
						if ($ImageWidth > $pgSettings['MaxWidth'] || $ImageHeight > $pgSettings['MaxHeight']) {
	
							$NewWidth = $pgSettings['MaxWidth'];
							$NewHeight = $pgSettings['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);
							} 
							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
							
							
							
						$query5 = "select FileName,FileType from `".$Settings['DBPrefix']."pg-Images` where RecordNumber='".$FormArguments['RecordNumberToEdit']."' limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
						$row5 = mysql_fetch_array($result5);
						
						if ($row5['FileType'] == 1) {
							$Extension = '.gif';
						}
						else if ($row5['FileType'] == 2) {
							$Extension = '.jpg';
						}
						else if ($row5['FileType'] == 3) {
							$Extension = '.png';
						}
							
						rename($DestinationPath.$row5['FileName'].$Extension,$DestinationPath.'replaced_'.$row5['FileName'].$Extension);
						rename($DestinationPath.$row5['FileName'].'-thumb'.$Extension, $DestinationPath.'replaced_'.$row5['FileName'].'-thumb'.$Extension);
						
						
						$query5 = "update `".$Settings['DBPrefix']."pg-Images` set ImageTitle='".$FormArguments['Title']."', ImageDescription='".$FormArguments['Description']."', GalleryNumber='".$FormArguments['Gallery']."', ImageWidth='".$ImageWidth."', ImageHeight='".$ImageHeight."', FileName='".$NewFilename."', FileSize='".number_format(($FileSize/1024),0,'','')."', FileType='".$FileType."', PostedBy='".$PostedBy."' where RecordNumber='".$FormArguments['RecordNumberToEdit']."' limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
				
				
						// if it's been moved to a different gallery
						if ($FormArguments['Gallery'] != $FormArguments['CurrentGallery']) {
					
							$query55 = "update `".$Settings['DBPrefix']."pg-Galleries` set GalleryImages=(GalleryImages-1) where RecordNumber='".$FormArguments['CurrentGallery']."'";
							$result55 = mysql_query($query55) or die (mysql_error());
					
							$query55 = "update `".$Settings['DBPrefix']."pg-Galleries` set GalleryImages=(GalleryImages+1) where RecordNumber='".$FormArguments['Gallery']."'";
							$result55 = mysql_query($query55) or die (mysql_error());
				
						}
						
						
						
						$PageContents = '<br><b>'.$MsgText[0][334].'<br><br><a href="'.$MainURL.'/admin/photos.html">'.$MsgText[3][2].'</a></b>';
						
										
					} // end if upload success
					
					
				}
				else {
					// not replacing image, so just update the settings
			
					if ($FormArguments['Delete'] == 'y') {
					
						$query = "select * from `".$Settings['DBPrefix']."pg-Images` where RecordNumber='".$FormArguments['RecordNumberToEdit']."' limit 1";
						$result = mysql_query($query) or die (mysql_error());
						$row = mysql_fetch_array($result);
				
						
						$query5 = "delete from `".$Settings['DBPrefix']."pg-Images` where RecordNumber='".$FormArguments['RecordNumberToEdit']."' limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
				
						$query55 = "update `".$Settings['DBPrefix']."pg-Galleries` set GalleryImages=(GalleryImages-1) where RecordNumber='".$row['GalleryNumber']."'";
						$result55 = mysql_query($query55) or die (mysql_error());
	
	
						$Extensions[1] = '.gif';
						$Extensions[2] = '.jpg';
						$Extensions[3] = '.png';
			
						$ThumbExtensions[1] = '-thumb.gif';
						$ThumbExtensions[2] = '-thumb.jpg';
						$ThumbExtensions[3] = '-thumb.png';
				
						unlink($pgSettings['StorageLocationLocal'].'/'.$row['FileName'].$Extensions[$row['FileType']]);
						unlink($pgSettings['StorageLocationLocal'].'/'.$row['FileName'].$ThumbExtensions[$row['FileType']]);
				
				
				
						header('Location: ' . $MainURL . '/admin/photos.html');
						
					} // end if deleting
					else {
																									
						$query5 = "update `".$Settings['DBPrefix']."pg-Images` set ImageTitle='".$FormArguments['Title']."', ImageDescription='".$FormArguments['Description']."', GalleryNumber='".$FormArguments['Gallery']."', PostedBy='".$PostedBy."' where RecordNumber='".$FormArguments['RecordNumberToEdit']."' limit 1";
						$result5 = mysql_query($query5) or die (mysql_error());
				
				
						// if it's been moved to a different gallery
						if ($FormArguments['Gallery'] != $FormArguments['CurrentGallery']) {
				
							$query55 = "update `".$Settings['DBPrefix']."pg-Galleries` set GalleryImages=(GalleryImages-1) where RecordNumber='".$FormArguments['CurrentGallery']."'";
							$result55 = mysql_query($query55) or die (mysql_error());
					
							$query55 = "update `".$Settings['DBPrefix']."pg-Galleries` set GalleryImages=(GalleryImages+1) where RecordNumber='".$FormArguments['Gallery']."'";
							$result55 = mysql_query($query55) or die (mysql_error());
				
						}
				
							
						header('Location: ' . $MainURL . '/admin/photos.html');
						
					} // end if not deleting
					
					
				} // end if not replacing image
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
			
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}
	
	

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




?>
