<?

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



	// ========================================================================================
	// photo upload processing
	// ========================================================================================
	
	$PageTitle = $MsgText[3][2];
	$BrowserTitle = $MsgText[3][2];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[3][2];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[301] == 'y') {
		
		
			$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';
			}
			
			
			
			ReadFormArguments();
			Global $FormArguments;
			
			if (count($FormArguments) > 0) {
			
				for ($x = 1; $x <= 10; $x++) {
			
					if ($FormArguments['HidePoster'] == 'y') {
						$PostedBy = 0;
					}
					else {
						$PostedBy = $UserNumber;
					}
					
					
					
					$ImageNumberPart = $x;
					
					
					if ($FormArguments['FileData'.$ImageNumberPart] != '' || $_FILES['FileData'.$ImageNumberPart]['size'] > 0 || $FormArguments['Title'.$ImageNumberPart] != '') {
					
								
						$FileData = $_FILES['FileData'.$ImageNumberPart]['tmp_name'];
						$FileSize = $_FILES['FileData'.$ImageNumberPart]['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);
						
						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>#'.$x.': '.$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>#'.$x.': '.$MsgText[3][67].'<br><br>(2)</b>';
		
							@unlink($FileData);
						}
						else if ($FileSize == 0) {
				   			$PageTitle = $MsgText[0][328];
							$PageContents = '<b><br>#'.$x.': '.$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>#'.$x.': '.$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);
							}
													
							if ($Settings['FilesCHMOD'] != '') {
								@chmod($DestinationImage, $Settings['FilesCHMOD']);
							}
							
										
							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);
								}
								
								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);
																
								
								if ($Settings['FilesCHMOD'] != '') {
									@chmod($FileSource.$Extension, $Settings['FilesCHMOD']);
								}
								 					
							
							} // end if not larger than max dimensions
							
							
							
							$query = "insert into `".$Settings['DBPrefix']."pg-Images` (GalleryNumber, FileName, ImageWidth, ImageHeight, FileSize, FileType, ImageTitle, ImageDescription, PostedBy, DatePosted, ImageViews) values ('".$FormArguments['Gallery']."', '".$DBFileName."', '".$ImageWidth."', '".$ImageHeight."', '".number_format(($FileSize/1024),0,'','')."', '".$FileType."', '".$FormArguments['Title'.$ImageNumberPart]."', '".$FormArguments['Description'.$ImageNumberPart]."', '".$PostedBy."', '".GetTimestamp()."', '0')";
							$result = mysql_query($query) or die (mysql_error());
					
					
							$query = "update `".$Settings['DBPrefix']."pg-Galleries` set GalleryImages=GalleryImages+1 where RecordNumber=".$FormArguments['Gallery']." limit 1";
							$result = mysql_query($query) or die (mysql_error());
					
					
					
							$PageContents .= '<br><b>#'.$x.': '.$MsgText[0][334].'<br><br></b>';
					
						
							$SuccessfulUpload = true;


						} // end if file or title					
					
					} // end if successful upload
					
				} // end loop
				
				if ($SuccessfulUpload == true) {
					$PageContents .= '<br><b><a href="'.$MainURL.'/admin/photos.html">'.$MsgText[3][2].'</a></b>';
				}	
				
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
