<?php
/*
 * CodePress - Real Time Syntax Highlighting Editor written in JavaScript -  http://codepress.fermads.net/
 *
 * Copyright (C) 2006 Fernando M.A.d.S. <fermads@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify it under the terms 
 * of the GNU Lesser General Public License as published by the Free Software Foundation.
 *
 * Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
 *
 * Very simple implementation of server side script to open files and send to CodePress interface
 */

// RELATIVE to codepress directory instalation: will open file from [root path of your server]/[codepress directory]/examples/
$path['files'] = ""; 

//GETTING MFA-DATA
define("IN_MFA",true);
include("../../../work/data/constants.php");
include("../../../includes/file.php");
$tmppw = "0";
$username = "";

if(IsSet($_COOKIE['mfatmppw']))
	$tmppw = $_COOKIE['mfatmppw'];
if(IsSet($_COOKIE['mfausername']))
	$username = $_COOKIE['mfausername'];

$fcontent = "";
$user_file = new file(SCRIPT_PATH."/work/data/user_".strtolower($username).".php");
$fcontent = $user_file->getContent();
if(!strstr($fcontent,$tmppw) || $tmppw == "0")
	exit;
	 
$fcontent = explode(";",$fcontent);
$fcontent = $fcontent[1];
$fcontent = explode(",",$fcontent);
$user_rights = $fcontent[4];


$display = new file($_GET['file']);
if(substr($user_rights, 3,1) == 0) exit;

$_GET['file'] = str_replace("//","/",$_GET['file']);
$_GET['file'] = $fcontent[8].$_GET['file'];
if(stristr($_GET['file'],SCRIPT_PATH)) exit;
if(strstr($_GET['file'],"..")) exit;

$path['webdocs'] = "";
$path['server'] = ROOT_DIR;

$code = "";
$engine = $_GET['engine'];
$language = (isset($_GET['language'])) ? $_GET['language'] : "generic" ;

if(isset($_GET['file'])) {
    $file = preg_replace("/\.\.\//","",$_GET['file']); // don't let users go up with ../../
	if($path['files']{0}=="/") $full_file = $path['server'].'/'.$path['files']."/".$file; // absolute path
	else $full_file = $path['server'].'/'.$path['webdocs'].'/'.$path['files']."/".$file; // relative path

	if(file_exists($full_file)) {
	    $code = file_get_contents($full_file);
	    $code = preg_replace("/&/","&amp;",$code);
	    $code = preg_replace("/</","&lt;",$code);
	    $code = preg_replace("/>/","&gt;",$code);
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
	<title>CodePress</title>
	<meta name="description" content="CodePress source code editor window" />
	<link type="text/css" href="../themes/default/codepress.css?timestamp=<?=time()?>" rel="stylesheet" />
	<link type="text/css" href="../languages/<?=$language?>.css?timestamp=<?=time()?>" rel="stylesheet" id="cp-lang-style" />
	<script type="text/javascript" src="../engines/<?=$engine?>.js?timestamp=<?=time()?>"></script>
	<script type="text/javascript" src="../languages/<?=$language?>.js?timestamp=<?=time()?>"></script>
</head>
<? 
if ($engine == "gecko") echo "<body id='code'>".$code."</body>";
else if($engine == "msie") echo "<body><pre id='code'>".$code."</pre></body>";
?>
</html>