Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $topic_num = 10;
- $topic_all = 50;
- function get_form($str, $multi = false) {
- $str = htmlspecialchars($str);
- $str = str_replace("\t", " ", $str);
- if ($multi) {
- $str = str_replace("\r\n", "<br>", $str);
- $str = str_replace("\r", "<br>", $str);
- $str = str_replace("\n", "<br>", $str);
- }
- return $str;
- }
- function error($msg) {
- print "<html>\n<body>\n";
- print "<p><font color='red'>$msg</font></p>\n";
- print "</body>\n</html>\n";
- exit();
- }
- $page = $_GET['page'];
- if (!$page) $page = '0';
- if ($_POST['write'] || $_POST['delete']) $log_change = true;
- else $log_change = false;
- $lines = array();
- $fp = fopen('bbs4.txt', 'r+');
- if (!$fp) error("ログファイルオープンエラー");
- if ($log_change) flock($fp, LOCK_EX);
- while (!feof($fp)) $lines[] = fgets($fp, 10000);
- array_pop($lines);
- if (!$log_change) fclose($fp);
- if ($_POST['write']) {
- $maxno = 0;
- foreach($lines as $line) {
- $items = explode("\t", $line);
- if ($maxno < $items[0]) $maxno = $items[0];
- }
- $no = $maxno + 1;
- if ($_POST['resno']) $resno = $_POST['resno'];
- else $resno = $no;
- $name = get_form($_POST['name']);
- if (!$name) $name = "名無しさん";
- $mail = get_form($_POST['mail']);
- $title = get_form($_POST['title']);
- if (!$title) $title = "無題";
- $contents = get_form($_POST['contents'], true);
- if (!$contents) error("本文を入力してください");
- $delkey = get_form($_POST['delkey']);
- $time = date("Y/m/d H:i:s");
- $expire = time() + 3600 * 24 * 30;
- setcookie("name", $name, $expire);
- setcookie("mail", $mail, $expire);
- setcookie("delkey", $delkey, $expire);
- $data = "$no\t$resno\t$name\t$mail\t$title\t$contents\t$delkey\t$time\n";
- if ($no == $resno) {
- array_unshift($lines, $data);
- } else {
- $rf = false;
- for ($i = 0; $i < count($lines); $i++) {
- $items = explode("\t", $lines[$i]);
- if (!$rf) {
- if ($resno == $items[1]) $rf = true;
- } else {
- if ($resno != $items[1]) {
- array_splice($lines, $i, 0, $data);
- $rf = false;
- break;
- }
- }
- }
- if ($rf) array_push($lines, $data);
- }
- } else {
- $name = $_COOKIE['name'];
- $mail = $_COOKIE['mail'];
- $delkey = $_COOKIE['delkey'];
- }
- if ($_POST['delete']) {
- $dno = $dnum = 0;
- for ($i = 0; $i < count($lines); $i++) {
- $items = explode("\t", $lines[$i]);
- if ($items[0] == $_POST['delno']) {
- if ($items[6] == $_POST['delkey2']) {
- $dno = $i;
- $dnum++;
- } else error("削除キーが違います");
- } else if ($items[1] == $_POST['delno']) {
- $dnum++;
- }
- }
- if ($dnum) array_splice($lines, $dno, $dnum);
- else error("指定した投稿番号の記事はありません");
- }
- if ($log_change) {
- $past = false;
- $topic_no = 0;
- rewind($fp);
- ftruncate($fp, 0);
- for ($i = 0; $i < count($lines); $i++) {
- $items = explode("\t", $lines[$i]);
- if ($items[0] == $items[1]) $topic_no++;
- if ($past == false && $topic_no > $topic_all) {
- $past = true;
- fclose($fp);
- $fno = 0;
- $dir = opendir("bbs4_log");
- if (!$dir) error("過去ログディレクトリが開けません");
- while ($fname = readdir($dir)) {
- if (is_file("bbs4_log/$fname")) $fno++;
- }
- closedir($dir);
- if ($fno == 0) $fno = 1;
- $fname = "bbs4_log/$fno.txt";
- touch($fname);
- $fp = fopen($fname, 'r');
- if (!$fp) error("過去ログファイルオープンエラー");
- flock($fp, LOCK_EX);
- $ptn = 0;
- while (!feof($fp)) {
- $it = explode("\t", fgets($fp, 10000));
- if ($it[0] && $it[0] == $it[1]) $ptn++;
- }
- fclose($fp);
- if ($ptn >= $topic_all) $fno++;
- $fname = "bbs4_log/$fno.txt";
- touch($fname);
- $fp = fopen($fname, 'a');
- if (!$fp) error("過去ログファイルオープンエラー");
- flock($fp, LOCK_EX);
- }
- fputs($fp, $lines[$i]);
- }
- fclose($fp);
- }
- ?>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html lang="ja">
- <head>
- <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
- <title>テスト掲示板</title>
- </head>
- <body>
- <form method="post" action="index.php">
- お名前:<input type="text" name="name" value="<?php print $name ?>"><br>
- メール:<input type="text" name="mail" value="<?php print $mail ?>"><br>
- 題 名:<input type="text" name="title"><br>
- 削除キー:<input type="password" name="delkey" value="<?php print $delkey ?>"><br>
- <textarea name="contents" cols="60" rows="5"></textarea><br>
- <?php
- if ($_GET['resno']) {
- print "<input type='submit' name='write' value='No.{$_GET['resno']} に返信'>\n";
- print "<input type='hidden' name='resno' value='{$_GET['resno']}'>\n";
- } else {
- print "<input type='submit' name='write' value='送信'><br><br>\n";
- print "<a href='log.php'>過去ログ</a><br>\n";
- print "<hr>\n";
- print "記事番号:<input type='text' name='delno'>\n";
- print " 削除キー: <input type='password' name='delkey2'>\n";
- print " <input type='submit' name='delete' value='記事削除'>\n";
- }
- ?>
- </form>
- <?php
- $topic_no = 0;
- $next_page = 0;
- foreach ($lines as $line) {
- $items = explode("\t", rtrim($line));
- if ($items[0] != $items[1]) $res = true; else $res = false;
- if (!$res) $topic_no++;
- if ($_GET['resno']) {
- if ($items[1] != $_GET['resno']) continue;
- } else if ($topic_no <= $page * $topic_num) {
- continue;
- } else if ($topic_no > ($page + 1) * $topic_num) {
- $next_page = $page + 1;
- break;
- }
- if ($res) print "<blockquote>";
- else print "<hr>";
- print "<p>No.{$items[0]} ";
- print "<b>{$items[4]}</b> 投稿者:";
- if ($items[3]) print "<a href='mailto:{$items[3]}'>";
- print $items[2];
- if ($items[3]) print "</a>";
- print " 投稿時間:{$items[7]}";
- if (!$res && !$_GET['resno']) {
- print " <a href='index.php?resno={$items[0]}'>返信</a>";
- }
- print "<br><br>{$items[5]}</p>";
- if ($res) print "</blockquote>";
- print "\n";
- }
- print "<hr>\n";
- if ($page > 0) {
- $prev_page = $page - 1;
- print "<a href='index.php?p$prev_page'>前ページ</a> ";
- }
- if ($next_page) {
- print "<a href='index.php?p$next_page'>次ページ</a>";
- }
- print "\n";
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement