Advertisement
yamagami2211

php

Oct 11th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. <?php
  2. $topic_num = 10;
  3. $topic_all = 50;
  4.  
  5. function get_form($str, $multi = false) {
  6. $str = htmlspecialchars($str);
  7. $str = str_replace("\t", "&nbsp;&nbsp;&nbsp;&nbsp;", $str);
  8. if ($multi) {
  9. $str = str_replace("\r\n", "<br>", $str);
  10. $str = str_replace("\r", "<br>", $str);
  11. $str = str_replace("\n", "<br>", $str);
  12. }
  13. return $str;
  14. }
  15.  
  16. function error($msg) {
  17. print "<html>\n<body>\n";
  18. print "<p><font color='red'>$msg</font></p>\n";
  19. print "</body>\n</html>\n";
  20. exit();
  21. }
  22.  
  23. $page = $_GET['page'];
  24. if (!$page) $page = '0';
  25. if ($_POST['write'] || $_POST['delete']) $log_change = true;
  26. else $log_change = false;
  27. $lines = array();
  28. $fp = fopen('bbs4.txt', 'r+');
  29. if (!$fp) error("ログファイルオープンエラー");
  30. if ($log_change) flock($fp, LOCK_EX);
  31. while (!feof($fp)) $lines[] = fgets($fp, 10000);
  32. array_pop($lines);
  33. if (!$log_change) fclose($fp);
  34. if ($_POST['write']) {
  35. $maxno = 0;
  36. foreach($lines as $line) {
  37. $items = explode("\t", $line);
  38. if ($maxno < $items[0]) $maxno = $items[0];
  39. }
  40. $no = $maxno + 1;
  41. if ($_POST['resno']) $resno = $_POST['resno'];
  42. else $resno = $no;
  43. $name = get_form($_POST['name']);
  44. if (!$name) $name = "名無しさん";
  45. $mail = get_form($_POST['mail']);
  46. $title = get_form($_POST['title']);
  47. if (!$title) $title = "無題";
  48. $contents = get_form($_POST['contents'], true);
  49. if (!$contents) error("本文を入力してください");
  50. $delkey = get_form($_POST['delkey']);
  51. $time = date("Y/m/d H:i:s");
  52. $expire = time() + 3600 * 24 * 30;
  53. setcookie("name", $name, $expire);
  54. setcookie("mail", $mail, $expire);
  55. setcookie("delkey", $delkey, $expire);
  56. $data = "$no\t$resno\t$name\t$mail\t$title\t$contents\t$delkey\t$time\n";
  57. if ($no == $resno) {
  58. array_unshift($lines, $data);
  59. } else {
  60. $rf = false;
  61. for ($i = 0; $i < count($lines); $i++) {
  62. $items = explode("\t", $lines[$i]);
  63. if (!$rf) {
  64. if ($resno == $items[1]) $rf = true;
  65. } else {
  66. if ($resno != $items[1]) {
  67. array_splice($lines, $i, 0, $data);
  68. $rf = false;
  69. break;
  70. }
  71. }
  72. }
  73. if ($rf) array_push($lines, $data);
  74. }
  75. } else {
  76. $name = $_COOKIE['name'];
  77. $mail = $_COOKIE['mail'];
  78. $delkey = $_COOKIE['delkey'];
  79. }
  80. if ($_POST['delete']) {
  81. $dno = $dnum = 0;
  82. for ($i = 0; $i < count($lines); $i++) {
  83. $items = explode("\t", $lines[$i]);
  84. if ($items[0] == $_POST['delno']) {
  85. if ($items[6] == $_POST['delkey2']) {
  86. $dno = $i;
  87. $dnum++;
  88. } else error("削除キーが違います");
  89. } else if ($items[1] == $_POST['delno']) {
  90. $dnum++;
  91. }
  92. }
  93. if ($dnum) array_splice($lines, $dno, $dnum);
  94. else error("指定した投稿番号の記事はありません");
  95. }
  96. if ($log_change) {
  97. $past = false;
  98. $topic_no = 0;
  99. rewind($fp);
  100. ftruncate($fp, 0);
  101. for ($i = 0; $i < count($lines); $i++) {
  102. $items = explode("\t", $lines[$i]);
  103. if ($items[0] == $items[1]) $topic_no++;
  104. if ($past == false && $topic_no > $topic_all) {
  105. $past = true;
  106. fclose($fp);
  107. $fno = 0;
  108. $dir = opendir("bbs4_log");
  109. if (!$dir) error("過去ログディレクトリが開けません");
  110. while ($fname = readdir($dir)) {
  111. if (is_file("bbs4_log/$fname")) $fno++;
  112. }
  113. closedir($dir);
  114. if ($fno == 0) $fno = 1;
  115. $fname = "bbs4_log/$fno.txt";
  116. touch($fname);
  117. $fp = fopen($fname, 'r');
  118. if (!$fp) error("過去ログファイルオープンエラー");
  119. flock($fp, LOCK_EX);
  120. $ptn = 0;
  121. while (!feof($fp)) {
  122. $it = explode("\t", fgets($fp, 10000));
  123. if ($it[0] && $it[0] == $it[1]) $ptn++;
  124. }
  125. fclose($fp);
  126. if ($ptn >= $topic_all) $fno++;
  127. $fname = "bbs4_log/$fno.txt";
  128. touch($fname);
  129. $fp = fopen($fname, 'a');
  130. if (!$fp) error("過去ログファイルオープンエラー");
  131. flock($fp, LOCK_EX);
  132. }
  133. fputs($fp, $lines[$i]);
  134. }
  135. fclose($fp);
  136. }
  137. ?>
  138. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  139. <html lang="ja">
  140. <head>
  141. <meta http-equiv="Content-Type" content="text/html; charset=Shift_JIS">
  142. <title>テスト掲示板</title>
  143. </head>
  144. <body>
  145. <form method="post" action="index.php">
  146. お名前:<input type="text" name="name" value="<?php print $name ?>"><br>
  147. メール:<input type="text" name="mail" value="<?php print $mail ?>"><br>
  148. 題 名:<input type="text" name="title"><br>
  149. 削除キー:<input type="password" name="delkey" value="<?php print $delkey ?>"><br>
  150. <textarea name="contents" cols="60" rows="5"></textarea><br>
  151. <?php
  152. if ($_GET['resno']) {
  153. print "<input type='submit' name='write' value='No.{$_GET['resno']} に返信'>\n";
  154. print "<input type='hidden' name='resno' value='{$_GET['resno']}'>\n";
  155. } else {
  156. print "<input type='submit' name='write' value='送信'><br><br>\n";
  157. print "<a href='log.php'>過去ログ</a><br>\n";
  158. print "<hr>\n";
  159. print "記事番号:<input type='text' name='delno'>\n";
  160. print " 削除キー: <input type='password' name='delkey2'>\n";
  161. print " <input type='submit' name='delete' value='記事削除'>\n";
  162. }
  163. ?>
  164. </form>
  165. <?php
  166. $topic_no = 0;
  167. $next_page = 0;
  168. foreach ($lines as $line) {
  169. $items = explode("\t", rtrim($line));
  170. if ($items[0] != $items[1]) $res = true; else $res = false;
  171. if (!$res) $topic_no++;
  172. if ($_GET['resno']) {
  173. if ($items[1] != $_GET['resno']) continue;
  174. } else if ($topic_no <= $page * $topic_num) {
  175. continue;
  176. } else if ($topic_no > ($page + 1) * $topic_num) {
  177. $next_page = $page + 1;
  178. break;
  179. }
  180. if ($res) print "<blockquote>";
  181. else print "<hr>";
  182. print "<p>No.{$items[0]} ";
  183. print "<b>{$items[4]}</b> 投稿者:";
  184. if ($items[3]) print "<a href='mailto:{$items[3]}'>";
  185. print $items[2];
  186. if ($items[3]) print "</a>";
  187. print " 投稿時間:{$items[7]}";
  188. if (!$res && !$_GET['resno']) {
  189. print " <a href='index.php?resno={$items[0]}'>返信</a>";
  190. }
  191. print "<br><br>{$items[5]}</p>";
  192. if ($res) print "</blockquote>";
  193. print "\n";
  194. }
  195. print "<hr>\n";
  196. if ($page > 0) {
  197. $prev_page = $page - 1;
  198. print "<a href='index.php?p$prev_page'>前ページ</a> ";
  199. }
  200. if ($next_page) {
  201. print "<a href='index.php?p$next_page'>次ページ</a>";
  202. }
  203. print "\n";
  204. ?>
  205. </body>
  206. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement