<?php
/*
* date last modified 28-11-2017
* Edited by PI-Digi Logical Solutions
* HTML Files - add_user.html.php
* Mysql Tables - tbl_designation, tbl_staff
*/

$debug = true;
$contentExist = '';
$adminId = $_SESSION['adminId'];
$curDate = date('Y-m-d H:i:s');

$resPageRs = mysqli_query($con, "SELECT * FROM tbl_cms_pages");

if(isset($_POST['page_name']) && strlen($_POST['page_name']) >0 ) {
	$name=$_POST['page_name'];
	$res = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_cms_pages WHERE page_name = '$name'"));
 	$pageName = $res['page_name'];
	$title = $res['title'];
	$meta_keywords = $res['meta_keywords'];
	$meta_description = $res['meta_description'];
	$seo_url = $res['seo_url'];
	//$meta_title = $res['meta_title'];
	//$meta_keywords = $res['meta_keywords'];
	//$meta_description = $res['meta_description'];
 	$contentExist = $res['page_content'];
	$updatedBy = $res['updated_by'];
	$updatedOn = $res['date'];
} 

if(isset($_POST['update'])) {


	$content = trim($_POST['page_content']);
	if(strlen($content) == 0){$error = "Please Enter Content";}
	
	/*$meta_description = trim($_POST['meta_description']);
	if(strlen($meta_description) == 0){$error = "Please Enter Meta description";}
	
	$meta_keywords = trim($_POST['meta_keywords']);
	if(strlen($meta_keywords) == 0){$error = "Please Enter Meta keywords";}
	
	$meta_title = trim($_POST['meta_title']);
	if(strlen($meta_title) == 0){$error = "Please Enter Meta Title";}*/
		
	$title = trim($_POST['title']);
	if(strlen($title) == 0){$error = "Please Enter Title";}
	
	$seo_url = trim($_POST['seo_url']);
	if(strlen($seo_url) == 0){$seo_url = '';}
	if($seo_url!=''){
		if ($seo_url == trim($seo_url) && strpos($seo_url, ' ') !== false) {
		   $error = "SEO URL should not contain spaces";
		} else if (strpos($seo_url, '&') !== false) { 
		  	$error = "SEO URL should not contain &. Try to avoid special characters except hiphen and underscore";
		} else if (strpos($seo_url, '?') !== false) { 
		  	$error = "SEO URL should not contain ?. Try to avoid special characters except hiphen and underscore";
		} else if (strpos($seo_url, '/') !== false) { 
		  	$error = "SEO URL should not contain /. Try to avoid special characters except hiphen and underscore";
		}
	}
	
	$meta_description = trim($_POST['meta_description']);
	if(strlen($meta_description) == 0){$meta_description = '';}
	
	$meta_keywords = trim($_POST['meta_keywords']);
	if(strlen($meta_keywords) == 0){$meta_keywords = '';}
	
	$html_title = trim($_POST['html_title']);
	if(strlen($html_title) == 0){$html_title = '';}
				
	if(empty($error)) {
		$updateQry = "UPDATE `tbl_cms_pages` SET title = '" . mysqli_real_escape_string($con, $title) ."', 
		`html_title` = '" . mysqli_real_escape_string($con, $html_title) ."',
		`meta_keywords` = '" . mysqli_real_escape_string($con, $meta_keywords) ."',
		`meta_description` = '" . mysqli_real_escape_string($con, $meta_description) ."',
		`seo_url` = '" . mysqli_real_escape_string($con, $seo_url) ."',		
		`page_content` = '" . mysqli_real_escape_string($con, $content) ."', 
		`updated_by` = '".$adminId."',		
		`date` = '".$curDate."' WHERE page_name = '$name'";
		 		
		$execUpdate = mysqli_query($con, $updateQry);
		if($execUpdate) {
				
			//*********************Backup and Create htaccess ******************************/

		if(copy(BASE_URI.'.htaccess', BASE_URI.'.htaccess_back')){
				//clear the file
				file_put_contents(BASE_URI.".htaccess", "");		
				//add contents to file
				$f = fopen(BASE_URI.".htaccess", "a+");
				$defString = '#deny from all

RewriteEngine On

#------------ Re-directs index.php to root ------------
RewriteCond %{THE_REQUEST} ^.*/index.php 
RewriteRule ^(.*)index.php$ '.BASE_URL.'/$1 [R=301,L]

Header add Access-Control-Allow-Origin *
RewriteRule ^(.*)home?$ index.php?page=home';

//RewriteRule ^(.*)about-us?$ index.php?page=page&page_id=4

				fwrite($f, $defString);
				fclose($f);	
				
				//get all seo urls from db				
				$seoRs = mysqli_query($con, "SELECT id, seo_url FROM tbl_cms_pages WHERE seo_url != ''");
				if(mysqli_num_rows($seoRs)>0){
					$f = fopen(BASE_URI.".htaccess", "a+");
					while($seoRec = mysqli_fetch_array($seoRs)){
						$pageId = $seoRec['id'];
						$seoKey = $seoRec['seo_url'];		
						fwrite($f, "\n");									
						fwrite($f, "RewriteRule ^(.*)".$seoKey."?$ index.php?page=page&page_id=".$pageId);														
					}
					fclose($f);	
				}
		}
			
			$success = 'The page content has been updated';
   }
	
$res = mysqli_fetch_array(mysqli_query($con, "SELECT * FROM tbl_cms_pages WHERE page_name = '$name'"));
$pageName = $res['page_name'];
$title = $res['title'];
$contentExist = $res['page_content'];
}

}

?>