<?php
/*******************************************************************************
* Ourphp - CMS建站系统
* Copyright (C) 2024 www.ourphp.net
* 开发者：哈尔滨伟成科技有限公司
* 
* 在线模板管理系统 v1.3.0 (由哈尔滨伟成科技有限公司开发) 盗版必究！！！
*******************************************************************************/
include '../../config/ourphp_code.php';
include '../../config/ourphp_config.php';
include '../../function/ourphp_function.class.php';

$safecode2 = $ourphp['opcode'];

if(isset($_GET['path'])){
	if(strpos(strtolower($_GET['path']),"ourphp_filebox") !== false || strpos(strtolower($_GET['path']),"ourphp_config") !== false){
		echo '出错';
		exit;
	}
}

function listDirFiles($dir)
{
    $arr = array();
    if (is_dir($dir)) {
        $d = opendir($dir);
        if ($d) {
            while (($file = readdir($d)) !== false) {
                if  ($file != '.' && $file != '..') {
                    if (is_dir($file)) {
                        $arr[$file] = listDirFiles($file);
                    } else {
                        $arr[] = $file;
                    }
                }
            }
        }
        closedir($d);
    }
    return $arr;
}

function format($ourphp)
{
    $format = 'ico_txt.png';
    $formatfont = '不可编辑文件';
    if(strstr($ourphp,".html"))
    {
        $format = 'ico_html.png';
        $formatfont = 'html文件可编辑';
    }
    if(strstr($ourphp,".php"))
    {
        $format = 'ico_php.png';
        $formatfont = 'php文件可编辑';
    }
    if(strstr($ourphp,".css"))
    {
        $format = 'ico_css.png';
        $formatfont = 'css文件可编辑';
    }
    if(strstr($ourphp,".js"))
    {
        $format = 'ico_js.png';
        $formatfont = 'js文件可编辑';
    }

    return array($format,$formatfont);
}

function pw($a,$b)
{
    global $db,$ourphp,$safecode2;
    session_start();
    if ($a == $ourphp['validation'] && $b == $ourphp['safecode']){
         
        $_SESSION['ourphp_out'] = "ourphp"; echo $safecode2;
        
    }else{

        if(empty($_SESSION['ourphp_out']))
        {
            include 'ourphp_checkadmin.php';
			if(strstr($OP_Adminpower,"06")): else: echo "无权限操作"; exit; endif;

        }else{

            session_start();

        }

    }
}

function filearray($ourphp)
{
    $a = array(
        'wap' => '移动端模板',
        'default' => '默认模板',
        'user' => '会员模板',
        'images' => '图片目录',
        'js' => 'JS文件目录',
        'css' => '样式文件目录',
    );

    if(isset($a[$ourphp]))
    {
        return $a[$ourphp];
    }else{

        if(strstr($ourphp,".html") || strstr($ourphp,".css") || strstr($ourphp,".js") || strstr($ourphp,".php") || strstr($ourphp,".jpg") || strstr($ourphp,".jpeg") || strstr($ourphp,".png") || strstr($ourphp,".gif"))
        {
            $format = format($ourphp);
            return $format[1];
        }else{
            return "模板目录";
        }
    }
    
}

$v = (empty($_GET['validation']))? "0" : $_GET['validation'];
$c = (empty($_GET['code']))? "0" : $_GET['code'];
pw($v,$c);
if(isset($_GET['out'])){
    unset($_SESSION['ourphp_out']);
}

$list = '';
if(!isset($_GET['path']))
{

    if(empty($_SESSION['ourphp_out']))
    {
        $file = listDirFiles('../../templates'); 
    }else{
        $file = listDirFiles('../../'); 
    }
    
    foreach ($file as $op) {
        if(!strstr($op,".")){
        $list .= '
        <li>
                <a href="?path='.$op.'&dir"><img src="../../skin/ico_file.png" width="80"><p>'.$op.'<br /><span>'.filearray($op).'</span></p></a>
        </li>
        ';
        }
    }

}else{

    if(empty($_SESSION['ourphp_out']))
    {
        $file = listDirFiles('../../templates/'.str_replace("../","",$_GET['path']));
        $file2 = '../../templates/'.str_replace("../","",$_GET['path']);
		$offedit = 1;
    }else{
        $file = listDirFiles('../../'.$_GET['path']);
        $file2 = '../../'.$_GET['path'];
		$offedit = 2;
    }

    if(isset($_GET['dir']))
    {
        foreach ($file as $op) {
            if(!strstr($op,".")){
                $list .= '
                <li>
                        <a href="?path='.$_GET['path'].'/'.$op.'&dir"><img src="../../skin/ico_file.png" width="80"><p>'.$op.'<br /><span>'.filearray($op).'</span></p></a>
                </li>
                ';
            }
            if(strstr($op,".html") || strstr($op,".css") || strstr($op,".js") || strstr($op,".php") || strstr($op,".htaccess")){

                $format = format($op);
                $list .= '
                <li>
                        <a href="?path='.$file2.'/'.$op.'&edit"><img src="../../skin/'.$format[0].'" width="80"><p>'.$op.'<br /><span>'.filearray($op).'</span></p></a>
                </li>
                ';
            }
            if(strstr($op,".jpg") || strstr($op,".jpeg") || strstr($op,".png") || strstr($op,".gif")){
                $list .= '
                <li>
                        <a href="'.$file2.'/'.$op.'" target="_blank"><img src="'.$file2.'/'.$op.'" width="80" height="70"><p>'.$op.'<br /><span>'.filearray($op).'</span></p></a>
                </li>
                ';
            }
        }
    }

    if(isset($_GET['edit']))
    {
        if(isset($_GET['path'])){
			if($offedit == 1)
			{
				if(!strpos($_GET['path'],'templates') !== false){
					   exit(0);
				}
				if(strpos(strtolower($_GET['path']),'config/ourphp') !== false){
					   exit(0);
				}
			}

            $openfile = file_get_contents($_GET['path']);
            $openfile = str_replace("<textarea", "<ourphp_", $openfile);
            $openfile = str_replace("</textarea>", "</ourphp_>", $openfile);
			$openhtml = 'scrollbars=0,resizable=0,scrollbars=yes,width=1300,height=700,left=150,top=150,screenX=50,screenY=50';
            $list = '
            <form id="form1" name="form1" method="post" action="?path=edit&ok">
                <div class="boxedit">
                        <textarea id="code" name="code">'.$openfile.'</textarea>
                </div>
                <div class="boxok">
                    <p>
						<a href="#"" onClick=window.open("tags.php","go","'.$openhtml.'")>【在新窗口中弹出模板标签】</a>
					</p>
                    <p>
						<a href="#"" onClick=window.open("ourphp_column.php?id=ourphp","go","'.$openhtml.'")>【在新窗口中弹出栏目管理】</a>
					</p>
					<p style="padding-top:20px;">
						<input type="password" name="safecode2" placeholder="输入OP码" class="win3" />
						<br />
						<input type="submit" name="Submit" value="保存修改代码" class="an" />
                    </p>
					<p style="line-height:20px; font-size:12px; color:#666">OP码不是系统口令码，OP码安装系统时自动生成，可在config/ourphp_config.php中查看或修改。</p>
                    <p>
                </div>
                <input type="hidden" value="'.$_GET['path'].'|'.MD5($_GET['path'].$ourphp['safecode']).'" name="md">
            </form>
                <script id="script">
                var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
                    lineNumbers: true,
					styleActiveLine: true,
					matchBrackets: true
                });
				editor.setOption("theme", "ayu-dark");
                </script>
            ';
        }
    }

    if(isset($_GET['ok']))
    {
        if(empty($_POST['code']) || empty($_POST['md'])){
           $list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">不能为空呀！</h1>';
        }
        $md = explode("|", $_POST['md']);
        $md2 = MD5($md[0].$ourphp['safecode']);
        if($md[1] != $md2){
            $list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">验证不通过呀！</h1>';
        }

        $code = $_POST['code'];
		if(get_magic_quotes_gpc())
		{
			$code = stripslashes($code);
		}
        $code = str_replace("<ourphp_", "<textarea", $code);
        $code = str_replace("</ourphp_>", "</textarea>", $code);

        if(empty($_SESSION['ourphp_out']))
        {
			$code = str_ireplace("[.system", "违禁词", $code);
			$code = str_ireplace("[.passthru", "违禁词", $code);
			$code = str_ireplace("[.exec", "违禁词", $code);
			$code = str_ireplace("[.shell_exec", "违禁词", $code);
			$code = str_ireplace("[.popen", "违禁词", $code);
			$code = str_ireplace("[.proc_open", "违禁词", $code);
			$code = str_ireplace("[.pcntl_exec", "违禁词", $code);
			$code = str_ireplace("[.eval", "违禁词", $code);
			
            if($safecode2 == 'ourphp' || empty($safecode2) || $safecode2 == ''){
				
				$list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">安全口令不正确！</h1>';
				
			}else{
				
				if($_POST['safecode2'] != $safecode2){
					
					$list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">安全口令不正确！</h1>';
					
				}else{
					
					if(stristr($code,"<?php") || stristr($code,"<%") || stristr($code,"language=\"php\"") || stristr($code,"language='php'") || stristr($code,"language=php") || stristr($code,"<?=") || stristr($md[0],".php") || stristr($md[0],".asp") || stristr($md[0],".aspx") || stristr($md[0],".jsp") || stristr($md[0],".htaccess") || stristr($md[0],".ini") || stristr($md[0],".user.ini"))
					{
						$list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">不要提交违法代码！</h1>';
					}else{

						$filego = fopen($md[0],'w');
						fwrite($filego,$code);
						fclose($filego);

						plugsclass::logs('编辑模板:'.$md[0],$_SESSION['ourphp_adminname']);
						$list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">编辑成功！</h1>';

					}
					
				}
				
			}
			

        }else{
            
			
            if(stristr($md[0],".asp") || stristr($md[0],".aspx") || stristr($md[0],".jsp") || stristr($md[0],".htaccess") || stristr($md[0],".ini") || stristr($md[0],".user.ini"))
            {
                $list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">不要提交违法代码！</h1>';
            }else{

                $filego = fopen($md[0],'w');
                fwrite($filego,$code);
                fclose($filego);

                $list = '<h1 style="float:left; margin-top:30px; padding-bottom:30px; font-size:20px; width:100%; text-align:center;">编辑成功！</h1>';

            }

        }

    }

}


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OurPHP Filebox 1.3.0</title>
<link href="templates/images/ourphp_login.css?<?php echo time();?>" rel="stylesheet" type="text/css"> 
<script type="text/javascript" src="../../function/plugs/jquery/1.7.2/jquery-1.7.2.min.js"></script>
<link rel='stylesheet' href='../../function/plugs/codemirror5/codemirror.css'>
<link rel='stylesheet' href='../../function/plugs/codemirror5/ayu-dark.css'>
<script src='../../function/plugs/codemirror5/codemirror.js'></script>
<script src='../../function/plugs/codemirror5/css.js'></script>
<script src='../../function/plugs/codemirror5/http.js'></script>
<script src='../../function/plugs/codemirror5/javascript.js'></script>
<script src='../../function/plugs/codemirror5/php.js'></script>
<script src='../../function/plugs/codemirror5/sql.js'></script>
<script src='../../function/plugs/codemirror5/xml.js'></script>
<style type="text/css">
.CodeMirror {
	height:700px;
}
.menu0 li.path {width:500px;}
</style>
</head>
<body>
<div id="tabs0" class="mt-50">
    <ul class="menu0" id="menu0">
        <li class="hover">在线模板编辑</li>
        <li><a href="javascript:window.history.go(-1);">返回上一层</a></li>
        <li class="path">当前路径：<?php echo $_GET['path'];?></li>
    </ul>
    <div class="main" id="main0" style="border-top:2px #488fcd solid; clear:both;">
        <ul class="block filelist">
            <?php
            echo $list;
            ?>
        </ul>
    </div>
</div>
</body>
</html>