<?php
/*
*/
    error_reporting(0);
date_default_timezone_set('PRC'); //中国时间
header("Content-Type: text/html; charset=UTF-8"); //编码
$data   = 'data/'; //数据目录
$ext    = '.txt'; //后缀名
$cookie = (isset($_COOKIE['cookie'])) ? $_COOKIE['cookie'] : '';
$id     = (isset($_GET ['id'])) ? intval(trim($_GET['id'])) : '';
$config = $data.'config/config.php';
if (!is_dir($data))          mkdir($data); //创建目录
if (!is_dir($data.'config')) mkdir($data.'config');
if (!is_file($config)){ //创建配置文件
    $value = '<?php $audit = 0; $name = "My Notepad"; $password = "'.md5('123456').'"; $bulletin = "各种记";?>';
    $fp    = fopen($config,'w'); fwrite($fp,$value); fclose($fp);
}
include($config); //调用
function files(){ //遍历数据目录 返回文件名数组
    global $data,$ext;
    $arr = glob("$data*.*");
    $arr = str_replace($data,'',$arr);
    return str_replace($ext,'',$arr);
}
if (isset($_POST['login'])){ //登录
    if (md5($_POST['login']) == $password){ setcookie('cookie',md5('abcbook')); header('location:?');}
    else exit('<script>alert("密码错误！"); history.back();</script>');
}
if (isset($_GET['exit'])){ setcookie('cookie',''); header('location:?');} //退出
if (isset($_POST['add']) && $_POST['add']){ //添加
    $newid = files() ? max(files()) + 1 : 1;
    $value = '<?php $time = '.time().'; $audit = '.$audit.'; $reply = \'\'; $content = \''.htmlspecialchars(str_replace('\\','',$_POST['add']),ENT_QUOTES).'\';?>';
    $fp    = fopen($data.$newid.$ext,'w'); fwrite($fp,$value); fclose($fp);

}
if ($cookie == md5('abcbook')){ //登录后才能执行的操作
    if (isset($_GET['delete'])){ if (is_file($data.$id.$ext)) unlink($data.$id.$ext);} //删除
    if (isset($_POST['id'])){ //回复 修改 审核
        $id      = $_POST['id'];
        $audit   = $_POST['audit'] ? 1 : 0;
        $time    = $_POST['time'];
        $content = htmlspecialchars(str_replace('\\','',$_POST['content']),ENT_QUOTES);
        $reply   = htmlspecialchars(str_replace('\\','',$_POST['reply']),ENT_QUOTES);
        $value   = '<?php $audit = '.$audit.'; $time = '.$time.'; $reply = \''.$reply.'\'; $content = \''.$content.'\';?>';
        $fp      = fopen($data.$id.$ext,'w'); fwrite($fp,$value); fclose($fp);
    exit('<script>alert("春风得意喜发财！"); location = "?";</script>');
    }
    if (isset($_POST['name']) && $_POST['name']){ //修改配置
        if ($_POST['password']) $password = md5($_POST['password']);
        $name     = htmlspecialchars(str_replace('\\','',$_POST['name']),ENT_QUOTES);
        $bulletin = htmlspecialchars(str_replace('\\','',$_POST['bulletin']),ENT_QUOTES);
        $audit    = ($_POST['audit']) ? 1 : 0;
        $value    = '<?php $audit = '.$audit.'; $name = \''.$name.'\'; $password = "'.$password.'"; $bulletin = \''.$bulletin.'\';?>';
        $fp       = fopen($config,'w'); fwrite($fp,$value); fclose($fp);
        exit('<script>alert("修改成功！"); location = "?";</script>');
    }
}
?>
<!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" />
<link rel="shortcut icon" href="/data/img/000.png" />
<style type="text/css">
  body,html{background:#889579;}
    body{width:90%;max-width:900px;margin:50px auto;border:#555 8px solid;overflow:auto;padding:28px;word-wrap:break-word;border-radius: 45px;}
    textarea{width:98%;height:200px;margin:20px auto;border:#555 8px solid;border-radius: 45px;}
    input,select{font-size:36px;margin:20px auto;}
    body,textarea{font-size:36px;font-family:;line-height:80px;color:#000;}
    a{color:#223;text-decoration:none;}
    hr{height:2px;border:none;border-bottom:2px dashed #abc;}
    div{padding:20px;}
    span{color:#223;}
    form{margin:0;}
    audio{width:80%;height:38px;}
		.rounded-image {
			border-radius: 38px;
		}
</style>
<title>My Notepad 各种记</title>
<meta name="referrer" content="never">
</head>
<body>
    <p>
<CENTER><a href="/ys.php" target="_blank">●</a> <a href="?" style="font-size: 38px;"><B>青蛙总是忘记自己从前是蝌蚪</B></a></CENTER><hr/>
<?php if (isset($_GET['login']) && !$cookie){ ?>
<form method="post">管理密码：<input name="login" type="password" /> <input type="submit" value=" 提 交 " /></form>
<?php exit; } ?>
<?php if (isset($_GET['reply']) && $cookie){ include($data.$id.$ext);?>
    管理留言：<?php echo "$id # ".date('Y-m-d H:i:s',$time);?>
    <form method="post">
        修改：<textarea name="content"><?php echo $content;?></textarea><br />
        回复：<textarea name="reply"><?php echo $reply;?></textarea><br />
        审核：
        <input type="radio"  name="audit" value="0"<?php if (!$audit) echo ' checked="checked"';?> />已审
        <input type="radio"  name="audit" value="1"<?php if ($audit)  echo ' checked="checked"';?> />未审
        <input type="hidden" name="id"    value="<?php echo $id;?>" />
        <input type="hidden" name="time"  value="<?php echo $time;?>" />
        <input type="submit" value=" 提 交 " />　
        <a href="?delete&id=<?php echo $id;?>" onclick="return confirm('确定删除？');">删除</a>
    </form>
<?php exit; } ?>
<?php if ($cookie){ ?>
    <form method="post">
        名称：<input name="name"     type="text" value="<?php echo $name;?>" /><br />
        密码：<input name="password" type="password" />　管理密码 如不修改请留空 忘记密码请删除"<?php echo $config;?>"<br />
        审核：<input name="audit"    type="radio" value="1" <?php if ($audit == 1) echo 'checked="checked"';?> /> 开
              <input name="audit"    type="radio" value="0" <?php if ($audit == 0) echo 'checked="checked"';?> /> 关<br />
        公告：<textarea name="bulletin"><?php echo $bulletin;?></textarea>
        <input type="submit" value="提交修改" />　<a href="?exit">退出管理</a>　<span>红色的'管理'为未审核留言</span>
    </form><p></p>
<?php }else{ echo nl2br($bulletin);?>
    <form method="post">
        <textarea name="add">  </textarea>
        <p><center><img src="/data/img/kong.png" width="130" height="1" border="0">
            <a href=""><img src="/data/img/sua.png" width="188" height="80" border="0"></a><img src="/data/img/kong.png" width="20" height="1" border="0"><input type="image" style="margin:0 100px;" src="/data/img/tj.png" width="178" height="90" /></center></p>
    </form>
<?php }
$size      = 10;
$count     = count(files()); //统计总数
$pagecount = ceil($count/$size);
if (isset($_GET['page']))     $page = trim($_GET['page']);
if (!isset($page) || $page<1) $page = 1;
if ($page > $pagecount)       $page = $pagecount;
$page  = intval($page);
$jump  = ($page - 1) * $size;
$files = files(); rsort($files);
foreach($files as $i=>$id){
if ($id && $i >= $jump && $i < $size + $jump){
    include($data.$id.$ext);
    $content = nl2br($content);
    $reply   = nl2br($reply);
    $date    = date('Y-m-d',$time);
    $time    = date('Y-m-d H:i:s',$time);
    ?>
    <div <?php echo ($i%2) ? '' : 'style="background-color:#f5f5f5;border:#eee 1px solid;"';?>>
    <?php
    $admin = ($audit) ? 'style="color:red;"' : '';
    if ($cookie) echo "<a href='?reply&id=$id' $admin>管理</a> ";
    echo "$id # <a title='$time'>$date</a> ";
    if (strlen($content) > 100) echo '<br />';
    echo ($cookie || !$audit) ? $content : '<span>等待审核...</span>';
    if ($reply) echo '<hr /><span>回复：</span>';
    if (strlen($reply) > 100) echo '<br />';
    echo $reply,'</div>';
}}?><hr />
<form method="get">
    <b><CENTER>
    <a href="?page=<?php echo $page - 1;?>"><< 前页</a>&nbsp;
    <select name="page" onchange="window.location='?page='+this.value;">
        <?php for ($i=0; $i<$pagecount; $i++){ ?>
            <option value="<?php echo $i+1;?>" <?php if ($i+1 == $page) echo 'selected="selected"';?>>
                第<?php echo $i+1;?>/<?php echo $pagecount;?>页
            </option>
        <?php } ?>
    </select>&nbsp;
    <a href="?page=<?php echo $page + 1;?>">后页 >></a>　
    <a href="?login">后台管理</a>
    ［<?php echo $count;?>］
    </CENTER></b>
</form>
    <CENTER><p>
        <a href="http://123.5151.us.kg">
             <img src="http://p0.meituan.net/dptrust/9036eee42b439ec8b4dc1218df091dd416326.png" width="128" height="60"/></a>   
    </CENTER>
</body>
</html>