<?

/*
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 photo gallery prefs
	// ========================================================================================
	
	$PageTitle = $MsgText[3][4];
	$BrowserTitle = $MsgText[3][4];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[3][4];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[302] == 'y') {
	
	
			ReadFormArguments();
			Global $FormArguments;
			
			if (count($FormArguments) > 0) {
											
				if (substr($FormArguments['StorageLocationLocal'],strlen($FormArguments['StorageLocationLocal'])-1,1) == '/') {
					$FormArguments['StorageLocationLocal'] = substr($FormArguments['StorageLocationLocal'],0,strlen($FormArguments['StorageLocationLocal'])-1);
				}
				if (substr($FormArguments['StorageLocationHTTP'],strlen($FormArguments['StorageLocationHTTP'])-1,1) == '/') {
					$FormArguments['StorageLocationHTTP'] = substr($FormArguments['StorageLocationHTTP'],0,strlen($FormArguments['StorageLocationHTTP'])-1);
				}
				
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['TitleAlignment']."' where Name='TitleAlignment' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['Title']."' where Name='Title' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				
				
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['ThumbMaxWidth']."' where Name='ThumbMaxWidth' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['ThumbMaxHeight']."' where Name='ThumbMaxHeight' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['MaxWidth']."' where Name='MaxWidth' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['MaxHeight']."' where Name='MaxHeight' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['MaxFileSize']."' where Name='MaxFileSize' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['StorageLocationLocal']."' where Name='StorageLocationLocal' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['StorageLocationHTTP']."' where Name='StorageLocationHTTP' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				
				
				$query = "select * from `".$Settings['DBPrefix']."pg-Settings` where Name='TitleImage' limit 1";
				$result = mysql_query($query) or die (mysql_error());
				if (mysql_num_rows($result) > 0) {
					$query = "update `".$Settings['DBPrefix']."pg-Settings` set Value='".$FormArguments['TitleImage']."' where Name='TitleImage' limit 1";
					$result = mysql_query($query) or die (mysql_error());
				}
				else {
					$query = "insert into `".$Settings['DBPrefix']."pg-Settings` (Name, Value) values ('TitleImage','".$FormArguments['TitleImage']."')";
					$result = mysql_query($query) or die (mysql_error());
				}
				
				
				
				if ($FormArguments['RebuildThumbs'] == 'y') {
					// rebuild existing thumbnail images
					
					$Extensions[2] = '.jpg';
					$Extensions[3] = '.png';
					
					if ($FormArguments['StorageLocationLocal'] == '') {
						$FormArguments['StorageLocationLocal'] = './pg_images';
					}
					
					
					$query = "select * from `".$Settings['DBPrefix']."pg-Images`";
					$result = mysql_query($query) or die (mysql_error());
					while ($row = mysql_fetch_array($result)) {
						
						$FileSource = $FormArguments['StorageLocationLocal'].'/'.$row['FileName'].$Extensions[$row['FileType']];
						//print $FileSource.'<br>';
						
						$ImageWidth = $row['ImageWidth'];
						$ImageHeight = $row['ImageHeight'];
						
						$FileType = $row['FileType'];
						
						
						// thumbnail
						$NewWidth = $FormArguments['ThumbMaxWidth'];
						$NewHeight = $FormArguments['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 == 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);
						
						
						imagejpeg($DestinationImage, $FormArguments['StorageLocationLocal'].'/'.$row['FileName'].$Extension); 
									
						imagedestroy($SourceData);
						imagedestroy($DestinationImage);
						
					} // end while images loop
					
				} // end if re-creating thumbnail images
				
	
			
			
				header('Location: ' . $MainURL . '/options.html');
				
			}
			else {
				$PageContents = $MsgText[0][98];
			}
			
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}
	
	

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




?>
