<?php
@session_start();
set_time_limit(0);
include('constants.php');

if (!isset($_SESSION['ftpusr'])){
	print "<script>self.location='login.php';</script>";
}

function listfolder($dir){
	include('constants.php');

	$ffs = scandir($dir);
	echo '<ul>';
	foreach($ffs as $ff){
		if($ff != '.' && $ff != '..' && $ff != 'view.php' && $ff != 'source2.php'){
			$filepath = $dir.'/'.$ff;
			$filepath = str_replace($path,'',$filepath);
			$aext1 = explode('.',$ff);
			$aext = end($aext1);
			if(in_array($aext,$excludearr)){
				echo '<li><a href="?file='.$filepath.'">'.$ff.'</a>';
			} else {
				echo '<li>'.$ff.'';
			}
			if(is_dir($dir.'/'.$ff)) listfolder($dir.'/'.$ff);
			echo '</li>';
		}
	}
	echo '</ul>';
}

function cut($data,$from,$to){
	$newdata2 = explode($from,$data);
	$newdata1 = explode($to,$newdata2[1]);
	$newdata = trim(strip_tags($newdata1[0]));
	return $newdata;
}

if($_POST){

	$action = $_POST['hidaction'];
	$filepath = $path.''.$_GET['file'];
	$handle = fopen($filepath, "rb");
	$contents = fread($handle, filesize($filepath));
	$fullcontent = $contents;
	
	if($action  == 'editmeta')	{
		
	}else if($action  == 'edit')	{		
		
		$filename = explode('/',$_GET['file']);
		$onlyfile = array_pop($filename);
		$sBackupFile = './editor_backup/'.$onlyfile;
		
		if(!$Handle = fopen("{$sBackupFile}", 'w+')){
			echo "Error 66: Cannot open file $sFileName";
			exit();
		}
		if(fwrite($Handle, $fullcontent) === FALSE){
			echo "Cannot write to file $sFileName";
			exit();
		}
		fclose($Handle);
		
		$acontents = explode('<!-- #BeginEditable "content" -->',$contents);
		$acontents1 = explode('<!-- #EndEditable -->',$acontents[1]);
		$newcontent = $_POST['content'];
		
		$ncontent = $acontents[0].'<!-- #BeginEditable "content" -->'.$newcontent.'<!-- #EndEditable -->'.$acontents1[1];
			
		$sFileName = $path.''.$_GET['file'];
		
		if(!$sHandle = fopen("{$sFileName}", 'w+')){
			echo "Error 88: Cannot open file $sFileName";
			exit();
		}
		if(fwrite($sHandle, $ncontent) === FALSE){
			echo "Cannot write to file $sFileName";
			exit();
		}
		fclose($sHandle);
		print "<script>";
		print "self.location='?file=".$_GET['file']."&action=update';";
		print "</script>";
	}
}

if($_GET['action']== 'delete' && $_GET['file']){
	$filepath = "htmls/".$_GET['file'];	
	@unlink($filepath);
	
	$path = "index.php?msg=File deleted Successfully";
	print "<script>";
	print "self.location='".$path."'";
	print "</script>";
}

if($_GET['action']== 'undo' && $_GET['file']){

	$filename = explode('/',$_GET['file']);
	$onlyfile = array_pop($filename);
	$sBackupFile = '../editor_backup/'.$onlyfile;
	$handle = fopen($sBackupFile, "rb");
	$contents = fread($handle, filesize($sBackupFile));
	fclose($handle);
	
	$sFile = $path.$_GET['file'];
	
	if(!$Handle = fopen("{$sFile}", 'w+')){
		echo "Error 129: Cannot open file $sFile";
		exit();
	}
	if(fwrite($Handle, $contents) === FALSE) 
	{
		echo "Cannot write to file $sFileName";
		exit();
	}
	fclose($Handle);
	print "<script>";
	print "self.location='?file=".$_GET['file']."';";
	print "</script>";
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>Solution21 Online Website Editor</title>
	<META NAME="description" content="Solution21 is a leader in Website 
	Design and Marketing for Dentists, Physicians, Dental Labs,
	Plastic Surgeons, Optometrists, Chiropractors and other Medical doctors. See Samples">
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="./css/style.css">
	<link media="screen" rel="stylesheet" href="css/colorbox.css" />
	
	<script type="text/javascript" src="jquery-1.4.2.js"></script>
	<script src="js/jquery.colorbox.js"></script>
	<script>	
		function setaction(){
			window.frames["sourceframe"].document.getElementById("sourcefrm").submit();
			//alert();
		}	
	</script>
	<script>
		$(document).ready(function(){
			$(".example70").colorbox({width:730, height:440, iframe:true, scrolling:false});
		});
	</script>

	<style type="text/css">
	.auto-style1 {
		font-size: 8pt;
		text-align: center;
	}
	.auto-style2 {
		text-align: center;
	}
	</style>
</head>
<body id="_body" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0">
	<table width="100%" height="100%" cellpadding="0" cellspacing="0">
		<tr>
			<td><img src="./images/pix.gif" width="720" height="1"></td>
		</tr>
		<tr>
			<td>
				<table width="100%" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td style="width: 6px"><img src="./images/lg.gif"></td>
						<td width="100%" align="right">
							<table cellpadding="0" cellspacing="0" style="width: 185px">
								<tr>
									<td class="auto-style2">&nbsp;<a href="http://www.solution21.com/support/editor.htm" target="_blank"><img height="28" src="images/support.gif" width="25"></a></td>
									<td class="auto-style2">&nbsp;<a href="http://www.solution21.com/support/index.htm" target="_blank"><img height="24" src="images/instructions.gif" width="24"></a></td>
									<td class="auto-style2">&nbsp;<a href="http://www.solution21.com/home/contact_us.htm" target="_blank"><img height="23" src="images/contact.gif" width="23"></a></td>
									<td>&nbsp;</td>
								</tr>
								<tr>
									<td class="auto-style2">
									<a href="http://www.solution21.com/support/editor.htm" target="_blank"><font size="1" color="black">
									Instructions</font></a><font size="1" color="black">&nbsp;</td>
									<td class="auto-style2">
									<a href="http://www.solution21.com/support/index.htm" target="_blank"><font size="1" color="black">
									Support</font></a><font size="1" color="black">&nbsp;</td>
									<td class="auto-style1">
									<a href="http://www.solution21.com/home/contact_us.htm" target="_blank"><font size="1" color="black">
									Contact&nbsp;</font></a></td>
									<td>&nbsp;</td>
								</tr>
							</table>
						</td>
					</tr>
					<tr>
						<td style="width: 6px"><img src="./images/pix.gif" height="1"></td>
					</tr>
				</table>
			</td>
		</tr>

		<tr>
			<td height="100%" valign="top" bgcolor="7083E1" height="1px"></td>
		</tr>

		<tr>
			<td height="100%" valign="top">
				<table valign="top" class="bar" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">	
					<tr>
						<td id="_qwa" height="100%">
							<table width="100%" height="100%" cellpadding="0" cellspacing="0">
								<tr>
									<td id="_na" valign="top" width="190" height="100%" align="left" bgcolor="#D7DEF8">
										<table width="100%" border="0" cellpadding="0" cellspacing="0">
											<tr>
												<td bgcolor="#7083E1"><img src="./images/pix.gif" width="1" height="3"></td>
											</tr>
											<tr>
												<td>
													<table width="100%" border="0" cellpadding="0" cellspacing="0">
														<tr height="29">
															<td class="grad" width="191" align="left">Pages<img src="./images/pix.gif" valign="bottom" width="173" height="1"></td> <!-- <img src="/./images/grad2.gif" align="right" height="31"/> -->
														</tr>
													</table>
												</td>
											</tr>											
										</table>

										<iframe frameborder="0" scrolling="yes" style="width:<?php echo $leftpanelwidth;?>;height:<?php echo $leftpanelheight; ?>;" src="left.php"></iframe>

									</td>
								</tr>
							</table>
						</td>
						<td style="cursor: col-resize;" onDblClick="ondb()" onMouseDown="ond()" unselectable="on" bgcolor="#7083E1"><img src="./images/pix.gif" width="3"></td>

		
						<td valign="top" width="1" bgcolor="#F7F7F7"><img src="./images/pix.gif" width="1" height="1"></td>
						<td valign="top" height="100%">
							<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
								<tr bgcolor="#F7F7F7">
									<td width="100%">
										<table valign="top" width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
											<tr bgcolor="#f5f5f5">
												<td valign="top" width="100%" height="100%">
													<table border="0" width="100%" id="table8" cellspacing="0" cellpadding="0">
														<?php if($_GET['file']){ ?>
														<tr>
															<td colspan="2" align="left" class="leftText" style="padding-bottom:15px;padding-left:20px;">
																<?php if($_GET['action'] == 'edit'){ ?>
																<div style="float:left;width:300px;text-align:left;font-weight:bold;">Editing page <?php print $_GET['file']; ?></div>
																<?php } ?>
				
																<?php if($_GET['action'] == 'editmeta'){ ?>																
																<div style="float:left;width:300px;text-align:left;font-weight:bold;">Editing Meta Tags of <?php print $_GET['file']; ?></div>
																<?php } ?>

																<?php 
																if($_GET['action'] == 'update'){ 
																	$sFileName = $path.$_GET['file'];
																	$sFileName = str_replace('//','/',$sFileName);
																?>
																<div style="float:left;width:370px;text-align:left;font-weight:bold; color: orange;">The changes have been applied, please review.</div>
																<?php } ?>

																<?php if($_GET['action'] == ''){ ?>
																<a href="?file=<?php print $_GET['file'];?>&action=edit">
																<img src="images/editpage.png" style="cursor:pointer;" title="edit this page" border="0"/></a>
																&nbsp;&nbsp;
																<a href="editmeta.php?file=<?php print $_GET['file'];?>&action=editmeta" class="example70"><img src="images/editmeta.png" border="0" title="edit meta tag of this page"/></a>
																<?php } ?>

																<?php if($_GET['action'] == 'edit'){ ?>																
																<img src="images/save.png" title="save" border="0" style="cursor:pointer;" onClick="setaction();"/>
																&nbsp;<a href="?file=<?php print $_GET['file']; ?>"><img src="images/cancel.png" title="cencel" border="0"/></a>
																<?php } ?>

																<?php if($_GET['action'] == 'update'){ ?>
																<a href="?file=<?php print $_GET['file']; ?>&action=undo"><img src="images/undochanges.png" title="cencel" border="0"/></a>
																<?php } ?>
															</td>
														</tr>				
														<tr><td bgcolor="#7083E1" colspan="2" height="3"></td></tr>
														<?php } else { ?>
														<tr><td bgcolor="#7083E1" colspan="2" height="3"><span style="padding-left:20px;"><strong>Please select a page to edit.</strong></span></td></tr>														
														<?php } ?>									
									
														<?php if($_GET['msg']){ ?>
														<tr>
															<td></td><td class="leftText" style="color:red;"><?php print $_GET['msg']; ?></td>
														</tr>
														<?php } 		
														if($_GET['file'] && $_GET['action'] == 'editmeta'){ ?>
														<tr>
															<td colspan="2">
																<form name="form1" action="?file=<?php print $_GET['file']; ?>" method="POST" />
																	<input type="hidden" name="hidaction" value="<?php print $_GET['action'];?>" />
																	<table cellpadding="4" style="padding-left:15px;">
																		<tr>
																			<td width="60%">Page Title<br /><input type="text" name="txttitle" style="width:400px;" value="<?php print $pagetitle; ?>"/></td>
																			<td style="padding-top: 5px"><br />20/70<br />Under 70 recommended</td>
																		</tr>
																		<tr>
																			<td width="60%">Page Description<br /><textarea name="tadesc" style="width:400px;height:100px;"/><?php print $metadesc; ?></textarea></td>
																			<td valign="top"><br />40/170<br />Under 170 recommended</td>
																		</tr>
																		<tr>
																			<td width="60%">Page Keywords<br /><input type="text" name="txtkeyw" style="width:400px;" value="<?php print $metakey; ?>"/></td>
																			<td style="padding-top: 5px"><br />Maximum three keywords<br />is recommended</td>
																		</tr>
																		<tr><td colspan="2">For best search engine optimization effect, try to keep the lenght of each section under recommended charactor<br />limits and the subject focused and relevant to the page content.</td></tr>
																		<tr><td colspan="2"><strong><img src="images/cancel.png" title="cencel"/>&nbsp;<input type="image" src="images/save.png" style="border:none"/></td></tr>
																	</table>
																</form>
															</td>
														</tr>
														<?php } ?>
														<tr>
															<td width="20">&nbsp;</td>
															<td style="padding-top: 5px"></td>
														</tr>						
													</table>							
													<?php 
													$filepath = $path.''.$_GET['file'];
																						
													if($_GET['file'] and $_GET['action'] == 'edit'){
														$arr=explode("/",$_GET['file']);
														$dirs=str_replace($arr[(count($arr)-1)],'',$_GET['file']);
														if(!file_exists(substr($path,0,-1).$dirs.'source1.php')){
															copy('./source.php',substr($path,0,-1).$dirs.'source1.php');
														}
														if(file_exists(substr($path,0,-1).$dirs.'source1.php')){
														?>
														<iframe id="sourceframe" name="sourceframe" frameborder="0" scrolling="auto"style="height:1500px;overflow:scroll" width="<?php echo $preview_width;?>" src="<?php echo substr($sspath,0,-1).$dirs.'source1.php?file='.$arr[(count($arr)-1)];?>&ofile=<?php print $_GET[file]; ?>"></iframe>
														<?php 
														} else 
															print '<div style="padding-left:30px;color:red;font-weight:bold;">you don\'t have write permission to edit this file. please contact to site administrator</div>'; 
													} 

													if($_GET['file'] and $_GET['action'] == '' or $_GET['action']== 'update'){
														$arr=explode("/",$_GET['file']);
														$dirs=str_replace($arr[(count($arr)-1)],'',$_GET['file']);
														if(!file_exists(substr($path,0,-1).$dirs.'view.php')){
															copy('./view.php',substr($path,0,-1).$dirs.'view.php');	
														}
														if(file_exists(substr($path,0,-1).$dirs.'view.php')){
													?>
													<iframe frameborder="0" scrolling="auto" height="<?php echo $preview_height;?>" width="<?php echo $preview_width;?>" src="<?php echo substr($sspath,0,-1).$dirs.'view.php?file='.$arr[(count($arr)-1)];?>"></iframe>
												<?php } else { ?>	
													<div style="padding-left:30px;font-weight:bold;color: orange;">You don't have write permission to edit this file. Please contact to site administrator.</div>
													<?php } 													
												}?>
												</td>
											</tr>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>

					<tr>
						<td colspan="2" height="3" bgcolor="#7083E1"><img src="./images/pix.gif" valign="top" width="1" height="3" border="0"></td>
						<td height="3" colspan="2"></td>
					</tr>
					<tr>
						<td class="sb-leftmenu-copyright" style="padding-top: 10px; padding-bottom: 5px; padding-right: 10px;" valign="midle" bgcolor="#7083E1">						
							<table valign="middle" width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr>
									<td class="sb-leftmenu-copyright" align="left">&nbsp;
										
									</td>
								</tr>
								<tr><td class="sb-leftmenu-horizontal-separator" align="left"><img style="border-width: 0px; height: 1px;" src="./images/blank.gif"></td></tr>
								<tr><td align="left">&copy; Solution21 Inc.<br> All Right Reserved.</td></tr>
							</table>	
						</td>	
						<td valign="bottom" bgcolor="#7083E1"><img src="./images/pix.gif" width="3" height="100%"></td>
	
						<td valign="bottom" width="100%" colspan="2">
							<table width="100%" border="0" cellpadding="0" cellspacing="0">
								<tr height="3"><td colspan="7" align="left" bgcolor="#7083E1">
									<img src="./images/pix.gif" width="1" height="3"></td>
								</tr>
								<tr height="28" bgcolor="#e5e5e5">
									<td width="9"><img src="./images/ft-1.gif" width="9" height="24"></td>
									<td class="gt" width="95%" align="left">
										<table width="100%" border="0" cellpadding="0" cellspacing="0">
											<tr>
												<td width="16"></td>
												<td class="gt"></td>
											</tr>
										</table>
									</td>
									<td width="5%" height="24" bgcolor="#e5e5e5">
									
									</td>
									<td width="5%" height="24" bgcolor="#e5e5e5">
										<table width="100%" border="0" cellpadding="2" cellspacing="0">
											<tr>
												<td valign="middle" width="18" height="24" nowrap="nowrap">
												<?php if($_SESSION['ftpusr']){ ?>
												<a href="#"><img src="./images/nav_logout.gif" alt=" Log out" width="16" height="16" border="0"></a>
												<?php } ?>
												</td>
												<td valign="middle" height="24" nowrap="nowrap">
												<?php if($_SESSION['ftpusr']){ ?>
												<a class="logout" href="logout.php"> Log out</a>
												<?php } ?>
												</td>
											</tr>
										</table>
										<img src="./images/pix.gif" width="1" height="29">
									</td>
								</tr>
							</table>							
						</td>						
					</tr>
				</table>
			</td>
		</tr>
	</table>
</body>
</html>