<?

error_reporting (E_ALL ^ E_NOTICE);


// read the database settings from the file
include_once('./settings.php');

// DB Connection Info read from settings file
Global $DBServer, $DBUser, $DBPass, $DBDatabase;
			
//establish a connection to database (server, database, password)
mysql_connect($DBServer, $DBUser, $DBPass) or die ("Could not connect to database");
	
//select database
mysql_select_db($DBDatabase) or die ("Could not select database");


$Settings['DBPrefix'] = $DBPrefix;


	$query = "select * from `".$Settings['DBPrefix']."core-Settings`";
	$result = mysql_query($query) or die (mysql_error());
	while ($row = mysql_fetch_array($result)) {
		$Settings[$row['Name']] = $row['Value'];
	}


// remove slashes from language id to avoid any possible attempts to load other code in
$Settings['DefaultLanguage'] = str_replace('/','',$Settings['DefaultLanguage']);

if ($Settings['DefaultLanguage'] == '') {
	$Settings['DefaultLanguage'] = 'english';
}

// see if the file exists
if (file_exists('./languages/inc-core-lang-'.$Settings['DefaultLanguage'].'.php')) {
	$LanguageFileLocated = true;
}
else {
	$Settings['DefaultLanguage'] = 'english';
	// check again for default file
	if (file_exists('./languages/inc-core-lang-'.$Settings['DefaultLanguage'].'.php')) {
		$LanguageFileLocated = true;
	}
	else {
		print 'Error.  Language files could not be loaded.<br><br>Please ensure at least the English FlexCMS language pack is present in the <i>languages</i> folder under the main FlexCMS directory.';
	}
}

if ($LanguageFileLocated == true) {
	include_once('./languages/inc-core-lang-'.$Settings['DefaultLanguage'].'.php');
}


$MainURL = $Settings['BaseURL'].'/'.$Settings['ScriptName'];

$FontFace = 'Arial';
$FontSize = 2;


foreach ($_POST as $PostKey=>$PostValue){
	if (!get_magic_quotes_gpc()) {
		$PostValue = addslashes($PostValue);
	}
	$FormArguments[$PostKey] = $PostValue;
}


if ($FormArguments['BW'] == 'BW') {
	$BWPart = 'BW';
}
if ($FormArguments['BW'] == 'BW2') {
	$BWPart = 'BW2';
}
	

			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);
					
					if ($Settings['FilesCHMOD'] != '') {
						@chmod($DestinationPath.$NewFilename, $Settings['FilesCHMOD']);
					}
					
					$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);
							} 
							
							if ($Settings['FilesCHMOD'] != '') {
								@chmod($DestinationPath.$NewFilename, $Settings['FilesCHMOD']);
							}
							 
										
							imagedestroy($SourceData);
							imagedestroy($DestinationImage);
						
						
						} // end if resizing
						
						
						
						if ($NewWidthCalc != '' && $NewHeightCalc != '') {
							$ImageWidth = $NewWidthCalc;
							$ImageHeight = $NewHeightCalc;
						}
						
						
						$PageContents = '<div align="center"><br><br><b>'.$MsgText[0][334].'<br><br>
											
						<script>
						function ContinueClicked() {
							window.opener.UploadedImageSelected'.$BWPart.'(\''.$NewFilename.'\', '.$ImageWidth.', '.$ImageHeight.');
						}
						</script>
						
						<form><input type="button" value="Continue" onClick="ContinueClicked();"></form></div>';
					
						
					} // end if successful upload
					
				
				}
				else {				
					$PageTitle = $MsgText[0][328];
					$PageContents = '<b><br>'.$MsgText[0][333].'<br><br>(5)</b>';
				}
				
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
	

			
			print '<html><head>
<title></title>
<meta name="Description" content="">
<meta name="Keywords" content="">
<meta name="Author" content="DCSun Internet Technologies">
<meta name="Generator" content="DCSun PageBuilder">
<meta name="MSSmartTagsPreventParsing" content="TRUE">
</head>
<body bgcolor="#eeeeee" text="#000000" link="#000000" vlink="#000000" alink="#000000" leftmargin="0" marginwidth="0" topmargin="0" marginheight="0" rightmargin="0" style="margin:0">
<font face="Arial" size="2">
'.$PageContents.'

</body>
</html>';
			
?>
