<?

/*
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[0][324];
	$BrowserTitle = $MsgText[0][324];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/images.html">'.$MsgText[0][302].'</a> > '.$MsgText[0][324];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[17] == 'y') {
		
					
			ReadFormArguments();
			Global $FormArguments;
			
			if (count($FormArguments) > 0) {
										
				$FileData = $_FILES['FileData']['tmp_name'];
				$FileSize = $_FILES['FileData']['size'];
				$FileType = $_FILES['FileData']['type'];
				$PreviousName = $_FILES['FileData']['name'];
				
							
				$TypeExtensions[1] = '.gif';
				$TypeExtensions[2] = '.jpg';
				$TypeExtensions[3] = '.png';
				
				
				if (function_exists("gd_info")) {
					$GDInfo = @gd_info();
				}
				if ($GDInfo['GIF Create Support'] == true) {
					$GIFWriteSupport = true;
				}
				
				
				if ($FormArguments['NewName'] == 'Previous') {
				
					// take the previous file name and strip out any slashes
					//that windows may have allowed in the file name
					$NewFilename = str_replace('/','-',$PreviousName);
				
				}
				else if ($FormArguments['NewName'] == 'Random') {
				
					$NewFilename = sprintf("%06d", rand(0,999999)) . sprintf("%06d", rand(0,999999));
																				
					$NewFilename .= $TypeExtensions[$FileType];
							
				}
				else {
				
					// strip out any slashes that may have been entered
					$NewFilename = str_replace('/','-',$FormArguments['NewNameEnter']);
				
				} 

								
				$DestinationPath = './images/uploaded/';
				
				if (!is_file($DestinationPath.$NewFilename)){
				
					move_uploaded_file($FileData, $DestinationPath.$NewFilename);
					
					$FileData = $DestinationPath.$NewFilename;
				
				
					$ImageInfo = @getimagesize($FileData);
				
					$ImageWidth = $ImageInfo[0];
					$ImageHeight = $ImageInfo[1];
					$FileType = $ImageInfo[2];
					
					
					
					if (strtolower(substr($NewFilename,strlen($NewFilename)-4,4)) != $TypeExtensions[$FileType]) {
						$OldFilename = $NewFilename;
						$NewFilename .= $TypeExtensions[$FileType];
						$FileData = $DestinationPath.$NewFilename;
						rename($DestinationPath.$OldFilename,$DestinationPath.$NewFilename);
					}
									
					
					
					if ($FileType != 1 && $FileType != 2 && $FileType != 3) {
			   			$PageTitle = $MsgText[0][328];
						$PageContents = '<b><br>'.$MsgText[0][329].'<br><br>(1)</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 > ($Settings['MaxImageFileSize']*1024)) {
						$PageTitle = $MsgText[0][328];
						$PageContents = '<b><br>'.$MsgText[0][331].': '.number_format($Settings['MaxImageFileSize'],0,'','').' KBytes.<br><br>(3)</b>';

						@unlink($FileData);
					}
					else if (!is_file($FileData)){
						// if the uploaded file doesn't exist on the server
			
						$PageTitle = $MsgText[0][328];
						$PageContents = '<b><br>'.$MsgText[0][332].'<br><br>(4)</b>';
   
					}	
					else {
   		
					
						if (($FormArguments['ImageResize'] == 'ResizeDimensions' || $FormArguments['ImageResize'] == 'ScalePercent') && ($FileType != 1 || $GIFWriteSupport == true)) {
					
							$FileSource = $DestinationPath.$NewFilename;
						
							if ($FormArguments['ImageResize'] == 'ResizeDimensions') {
								
								if ($ImageWidth > $FormArguments['ResizeMaxWidth'] || $ImageHeight > $FormArguments['ResizeMaxHeight']) {
							
									$NewWidth = intval($FormArguments['ResizeMaxWidth']);
									$NewHeight = intval($FormArguments['ResizeMaxHeight']);
									
									if ($NewWidth > 0) { $WidthRatio = $ImageWidth / $NewWidth;	}
									else { $WidthRatio = 1;	}
									
									if ($NewHeight > 0) { $HeightRatio = $ImageHeight / $NewHeight;	}
									else { $HeightRatio = 1; }
						
						
									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);
						
									}
								
								}
								else {
								
									$NewWidthCalc = $ImageWidth;
									$NewHeightCalc = $ImageHeight;
								
								}	
								
								
							}
							else {
							
								$NewWidthCalc = intval($ImageWidth*($FormArguments['ResizePercent']/100));
								$NewHeightCalc = intval($ImageHeight*($FormArguments['ResizePercent']/100));
													
							}
						
			
						
							if ($FileType == 1) {
								$SourceData = imagecreatefromgif($FileSource);
							}
							else if ($FileType == 2) {
								$SourceData = imagecreatefromjpeg($FileSource);
							}
							else if ($FileType == 3) {
								$SourceData = imagecreatefrompng($FileSource);
							} 
						
							$DestinationImage = imagecreatetruecolor($NewWidthCalc, $NewHeightCalc);
							imagecopyresampled($DestinationImage, $SourceData, 0, 0, 0, 0, $NewWidthCalc, $NewHeightCalc, $ImageWidth, $ImageHeight);
							
							
							if ($FileType == 1) {
								imagegif($DestinationImage, $DestinationPath.$NewFilename);
							}
							else if ($FileType == 2) {
								imagejpeg($DestinationImage, $DestinationPath.$NewFilename);
							}
							else if ($FileType == 3) {
								imagepng($DestinationImage, $DestinationPath.$NewFilename);
							} 
							 
										
							imagedestroy($SourceData);
							imagedestroy($DestinationImage);
						
						
						} // end if resizing
						
						
						
						$PageContents = '<br><b>'.$MsgText[0][334].'<br><br><a href="'.$MainURL.'/admin/images.html">'.$MsgText[0][302].'</a></b>';
					
						
					} // end if successful upload
					
				
				}
				else {				
					$PageTitle = $MsgText[0][328];
					$PageContents = '<b><br>'.$MsgText[0][333].'<br><br>(5)</b>';
				}
				
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
		
		} // end if permissions are sufficient
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
