<?

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



	// ========================================================================================
	// edit page save
	// ========================================================================================
	
	$PageTitle = $MsgText[0][300];
	$BrowserTitle = $MsgText[0][300];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="'.$MainURL.'/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/pages.html">'.$MsgText[0][244].'</a> > '.$MsgText[0][300];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		ReadFormArguments();
		Global $FormArguments;
		
		
		if ($FormArguments['PageType'] == 2 || $FormArguments['PageType'] == 3) {
			$FormArguments['PageContents'] = $FormArguments['PageContents2'];
		}
		
		

		$query = "select MinLevel,MinEditLevel from `".$Settings['DBPrefix']."core-Pages` where RecordNumber='".$FormArguments['PageIDToEdit']."' limit 1";
		$result = mysql_query($query) or die (mysql_error());
		$row = mysql_fetch_array($result);
				
		if ($Permissions[18] == 'y' && $row['MinLevel'] <= $UserLevel && $row['MinEditLevel'] <= $UserLevel) {
				
		
			if ($FormArguments['DeletePage'] == 'delete') {
		
				DBConnect();
			
				$query = "DELETE FROM `".$Settings['DBPrefix']."core-Pages` WHERE RecordNumber='".$FormArguments['PageIDToEdit']."' LIMIT 1";
	
	            		$result = mysql_query($query) or die (mysql_error());
				
				header('Location: ' . $MainURL . '/admin/pages.html');
			
			}
			else if ($FormArguments['SaveAsCopy'] == 'copy') {
			
							
				$FormArguments['PageContents'] = str_replace('%7BFlexMainURL%7D','{FlexMainURL}',$FormArguments['PageContents']);
					
				
				if ($FormArguments['PageEnabled'] == '') {
					$FormArguments['PageEnabled'] = 1;				
				}
				
				// make sure the min permissions level hasn't been tampered with
				if ($FormArguments['MinLevel'] > $UserLevel) {
					$FormArguments['MinLevel'] = $UserLevel;
				} 
				if ($FormArguments['MinEditLevel'] > $UserLevel) {
					$FormArguments['MinEditLevel'] = $UserLevel;
				}
				
				// and make sure php is off if they don't have permission to enable it
				if ($Permissions[9] != 'y' && $FormArguments['EnablePHP'] = '1') {
					$FormArguments['EnablePHP'] = 0;
				}
				
				
				// if the pagelink was empty, set it as the pageid
				if ($FormArguments['PageLink'] == '') {
					$FormArguments['PageLink'] = $NewID;				
				}
				
				
				
				$StartDate = 0;
				$EndDate = 0;
				if ($FormArguments['UseStartDate'] == 'Yes') {
					if ($FormArguments['SDAmPm'] == 'pm') {
						$FormArguments['SDHour'] += 12;
					}
					$StartDate = mktime($FormArguments['SDHour'], $FormArguments['SDMinute'], 0, $FormArguments['SDMonth'], $FormArguments['SDDay'], $FormArguments['SDYear']);
				}
				if ($FormArguments['UseEndDate'] == 'Yes') {
					if ($FormArguments['EDAmPm'] == 'pm') {
						$FormArguments['EDHour'] += 12;
					}
					$EndDate = mktime($FormArguments['EDHour'], $FormArguments['EDMinute'], 0, $FormArguments['EDMonth'], $FormArguments['EDDay'], $FormArguments['EDYear']);
				}
		
			
				$query = "INSERT INTO `".$Settings['DBPrefix']."core-Pages` (AdminLabel, PageLink, PageTitle, TitleImage, PageParent, PageContents, PageAlignment, TitleAlignment, NLtoBR, EnablePHP, PageEnabled, MinLevel, MinEditLevel, StartDate, EndDate, ConvertLinks, HidePage, PageType, ShowPrintable, ShowEmail, MetaKeywords, MetaDescription, BrowserTitle) VALUES ('".$FormArguments['AdminLabel']."', '".$FormArguments['PageLink']."', '".$FormArguments['PageTitle']."', '".$FormArguments['TitleImage']."', '".$FormArguments['PageCategory']."', '".$FormArguments['PageContents']."', '".$FormArguments['PageAlignment']."', '".$FormArguments['TitleAlignment']."', '".$FormArguments['NLtoBR']."', '".$FormArguments['EnablePHP']."', '".$FormArguments['PageEnabled']."', '".$FormArguments['MinLevel']."', '".$FormArguments['MinEditLevel']."', '".$StartDate."', '".$EndDate."', '".$FormArguments['ConvertLinks']."', '".$FormArguments['HidePage']."', '".$FormArguments['PageType']."', '".$FormArguments['ShowPrintable']."', '".$FormArguments['ShowEmail']."', '".$FormArguments['MetaKeywords']."', '".$FormArguments['MetaDescription']."', '".$FormArguments['BrowserTitle']."')";
				$result = mysql_query($query) or die (mysql_error());
				
								
				$query = "select RecordNumber from `".$Settings['DBPrefix']."core-Pages` where PageTitle='".$FormArguments['PageTitle']."' and PageContents='".$FormArguments['PageContents']."' and TitleAlignment='".$FormArguments['TitleAlignment']."' and AdminLabel='".$FormArguments['AdminLabel']."' and PageAlignment='".$FormArguments['PageAlignment']."' order by RecordNumber desc limit 1";
		  		$result = mysql_query($query) or die (mysql_error());
				$row = mysql_fetch_array($result);
				
				if ($FormArguments['PageLink'] == '') {	
					$query = "update `".$Settings['DBPrefix']."core-Pages` set PageLink='".$row['RecordNumber']."' where RecordNumber='".$row['RecordNumber']."' limit 1";
		  			$result = mysql_query($query) or die (mysql_error());	
				}
				
				
				header('Location: ' . $MainURL . '/admin/pages.html');
				
			
			}
			else {
			
				$FormArguments['PageContents'] = str_replace('%7BFlexMainURL%7D','{FlexMainURL}',$FormArguments['PageContents']);
				
				
				if ($FormArguments['PageType'] != '') {
				
					if ($FormArguments['PageType'] == 'Photo1') {
					
						//if ($FormArguments['ExistingPhotos'] != '') {
						//	$FormArguments['PhotoURL'] = $FormArguments['ExistingPhotos'];
						//}
					
						$FormArguments['PageContents'] = $FormArguments['PhotoURL'] . '#&#&#&#' . $FormArguments['PhotoCaption'] . '#&#&#&#' . $FormArguments['PhotoPosition'] . '%*%*%*%*%' . $FormArguments['PageContents'];
					
					}
				
				}
				
				
				
				// make sure the min permissions level hasn't been tampered with
				if ($FormArguments['MinLevel'] > $UserLevel) {
					$FormArguments['MinLevel'] = $UserLevel;
				}
				if ($FormArguments['MinEditLevel'] > $UserLevel) {
					$FormArguments['MinEditLevel'] = $UserLevel;
				}
			
				// if they don't have permission to use php, make sure it's set to the same as before
				if ($Permissions[9] != 'y' && $FormArguments['EnablePHP'] == 1) {
					$query = "select EnablePHP,PageContents from `".$Settings['DBPrefix']."core-Pages` WHERE RecordNumber='".$FormArguments['PageIDToEdit']."' LIMIT 1";
			    	        $result = mysql_query($query) or die (mysql_error());
					$row = mysql_fetch_array($result);
					
					if ($row['EnablePHP'] == 1) {
						$FormArguments['EnablePHP'] = $row['EnablePHP'];
						$FormArguments['PageContents'] = addslashes($row['PageContents']);
					}
				}
			
				
				// if the pagelink was empty, set it as the pageid
				if ($FormArguments['PageLink'] == '') {
					$FormArguments['PageLink'] = $FormArguments['PageIDToEdit'];				
				}
				
				
				$StartDate = 0;
				$EndDate = 0;
				if ($FormArguments['UseStartDate'] == 'Yes') {
					if ($FormArguments['SDAmPm'] == 'pm') {
						$FormArguments['SDHour'] += 12;
					}
					$StartDate = mktime($FormArguments['SDHour'], $FormArguments['SDMinute'], 0, $FormArguments['SDMonth'], $FormArguments['SDDay'], $FormArguments['SDYear']);
				}
				if ($FormArguments['UseEndDate'] == 'Yes') {
					if ($FormArguments['EDAmPm'] == 'pm') {
						$FormArguments['EDHour'] += 12;
					}
					$EndDate = mktime($FormArguments['EDHour'], $FormArguments['EDMinute'], 0, $FormArguments['EDMonth'], $FormArguments['EDDay'], $FormArguments['EDYear']);
				}
				
				
				$query = "UPDATE `".$Settings['DBPrefix']."core-Pages` SET AdminLabel='".$FormArguments['AdminLabel']."', PageTitle='".$FormArguments['PageTitle']."', TitleImage='".$FormArguments['TitleImage']."', PageParent='".$FormArguments['PageCategory']."', TitleAlignment='".$FormArguments['TitleAlignment']."', PageAlignment='".$FormArguments['PageAlignment']."', PageContents='".$FormArguments['PageContents']."', NLtoBR='".$FormArguments['NLtoBR']."', EnablePHP='".$FormArguments['EnablePHP']."', PageLink='".$FormArguments['PageLink']."', PageEnabled='".$FormArguments['PageEnabled']."', MinLevel='".$FormArguments['MinLevel']."', MinEditLevel='".$FormArguments['MinEditLevel']."', StartDate='".$StartDate."', EndDate='".$EndDate."', ConvertLinks='".$FormArguments['ConvertLinks']."', HidePage='".$FormArguments['HidePage']."', PageType='".$FormArguments['PageType']."', ShowPrintable='".$FormArguments['ShowPrintable']."', ShowEmail='".$FormArguments['ShowEmail']."', MetaKeywords='".$FormArguments['MetaKeywords']."', MetaDescription='".$FormArguments['MetaDescription']."', BrowserTitle='".$FormArguments['BrowserTitle']."' WHERE RecordNumber='".$FormArguments['PageIDToEdit']."' LIMIT 1";

    	        		$result = mysql_query($query) or die (mysql_error());
			
				header('Location: ' . $MainURL . '/admin/pages.html');
			
		
			}
			

		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
