gaku-ura/hide_php/mkboard.php

296 行
10 KiB
PHP

<?php
//直接アクセスしちゃだめ
require $d_root.'/hide_php/conf/c.php';
require $d_root.'/hide_php/conf/submit.php';
require $d_root.'/hide_php/conf/form.php';
function get_image($text){
global $d_root;
if (preg_match('/{{[0-9]+\.(gif|png|jpeg|jpg|webp|ico)}}/', $text) === 1){
$name = subrpos('{{', '}}', $text);
if (file_exists($d_root.'/sqlike/bbs/file/'.$name)){
return $name;
}
}
return '';
}
//スレッド削除の場合はその中の画像も削除
function remove_images_from_thread($thread_file){
global $d_root;
$thread_rows = get_rows($thread_file, 2);
foreach ($thread_rows as $row){
if (strpos($row, "'") === false){
continue;
}
$d = explode("'", $row);
$image_file = get_image($d[5]);
if (!empty($image_file)){
unlink($d_root.'/sqlike/bbs/file/'.$image_file);
}
}
}
$date = date('Y年m月d日 H時i分');
$bbs_dir = $d_root.'/sqlike/bbs';
$reason_session = '<h1>認証に失敗</h1>フォームの認証に失敗しました。大変申し訳ございませんが、このエラーの場合は<b>入力内容が保存されない</b>場合がございます。<br>';
$reason_input = '<h1>入力ミス</h1><p>未入力の項目があります。</p><p>タイトルと管理パスワードは必須です。</p>';
//定義
if (isset($_POST['submit'], $_POST['session_num'])){
$url = (isset($_SERVER['HTTP_REFERER'])?$_SERVER['HTTP_REFERER']:'unset');
if (check_session('mkboard', $_POST['session_num'], $url, true)){
//switch長いよ
switch ($_POST['submit']){
case 'mkboard':
$post_list = [];
$form_key = ['title', 'description', 'content', 'html', 'access_pass', 'edit_pass', 'full_name'];
foreach ($form_key as $post){
if (!isset($_POST[$post])){
form_die();
} else {
$post_list[] = $_POST[$post];
}
}
list($title, $description, $content, $html, $access_pass, $edit_pass, $full_name) = $post_list;
if (not_empty($title) && not_empty($html) && not_empty($edit_pass)){
if ($html !== 'yes'){
$content = h($content);
}
//セッションの代用
$session_num = one_time_pass(50, 100);
set_session('mkboard', $session_num);
$output = '<h1>プレビュー</h1><p>以下の内容で板を作成します。</p>
<p>タイトル:'.h($title).'</p>
<p>説明:'.h($description).'</p>
<p>内容:'.$content.'</p>
<p>アクセスパスワード:'.h($access_pass).'</p>
<p>管理パスワード:'.h($edit_pass).'</p>
<form action="" method="POST">
<input type="hidden" name="title" value="'.h($title).'">
<input type="hidden" name="description" value="'.h($description).'">
<input type="hidden" name="content" value="'.h($content).'">
<input type="hidden" name="html" value="'.h($html).'">
<input type="hidden" name="access_pass" value="'.h($access_pass).'">
<input type="hidden" name="edit_pass" value="'.h($edit_pass).'">
<input type="hidden" name="full_name" value="'.h($full_name).'">
<input type="hidden" name="session_num" value="'.$session_num.'">
<p class="c"><button type="submit" name="submit" value="post" class="enter">作成</button></p>
</form>';
} else {
$output = $reason_input;
}
break;
case 'post':
$post_list = [];
$form_key = ['title', 'description', 'content', 'html', 'access_pass', 'edit_pass', 'full_name'];
foreach ($form_key as $post){
if (!isset($_POST[$post])){
form_die();
} else {
$post_list[] = $_POST[$post];
}
}
list($title, $description, $content, $html, $access_pass, $edit_pass, $full_name) = $post_list;
if (not_empty($title) && not_empty($html) && not_empty($edit_pass)){
if ($html !== 'yes'){
$content = p($content);
} else {
$content = str_replace("'", '', $content);
}
//ロック
file_lock('make_board');
//利用者作成板の最大番号
$user_boards = scandir($bbs_dir.'/user_board');
$max_board_id = 199;
foreach ($user_boards as $user_board){
if (substr($user_board, 0, 1) === '.'){
continue;
}
if (preg_match('/^[0-9]+\.txt/', $user_board) === 1){
$board_id = (int)str_replace('.txt', '', $user_board);
if ($board_id > $max_board_id){
$max_board_id = $board_id;
}
}
}
//新規ファイル
$board_id = $max_board_id + 1; //最低でも200
$board_file = $bbs_dir.'/user_board/'.$board_id.'.txt';
$string = L1(implode("'", [h($title), h($description), $content, pass(h($access_pass)), pass(h($edit_pass))])).PHP_EOL.$full_name.PHP_EOL;
file_put_contents($board_file, $string, LOCK_EX);
$board_dir = $bbs_dir.'/'.$board_id;
mkdir($board_dir, 0777);
//解除
file_unlock('make_board');
$output = '<h1>作成成功</h1><p>板の作成に成功したと思います。</p><p><a href="/board.php?Board='.$board_id.'">板のURL</a></p><p>管理パスワードは「マスターキー」として使えます。あるいは、設定変更に必要となります。</p>';
} else {
form_die();
}
break;
case 'edit':
(isset($_POST['board'])?$board=(int)$_POST['board']:form_die());
$s_edit_pass=(isset($_SESSION['edit_pass'])?$_SESSION['edit_pass']:'');
//セッションの代用
$session_num = one_time_pass(50, 100);
set_session('mkboard', $session_num);
$output = '<h1>パスワードを入力</h1><p>管理パスワードを入力して下さい。</p>
<form action="" method="POST">
<input type="hidden" name="session_num" value="'.$session_num.'">
<input type="hidden" name="board" value="'.$board.'">
<label>パスワード<input type="text" name="edit_passwd" value="'.h($s_edit_pass).'" style="background:#fff;color:#a04;border:solid 0.1em #111;"></label>
<button type="submit" name="submit" value="editing" class="enter">編集</button>
</form>';
break;
case 'editing':
(isset($_POST['board'])?$board=(int)$_POST['board']:form_die());
(isset($_POST['edit_passwd'])?$edit_passwd=h($_POST['edit_passwd']):form_die());
$board_file = $bbs_dir.'/user_board/'.$board.'.txt';
$board_head = get($board_file, 1);
if ($board_head === false){
form_die();
}
list($board_title, $board_description, $board_content, $board_access_passwd, $board_edit_passwd) = explode("'", $board_head);
if ($board_edit_passwd === pass($edit_passwd)){
//セッションの代用
$session_num = one_time_pass(50, 100);
set_session('mkboard', $session_num);
$output = '<h1>'.$board_title.'を編集</h1>
<form action="" method="POST" id="form">
<h2>板を編集</h2>
<input type="hidden" name="session_num" value="'.$session_num.'">
<input type="hidden" name="board" value="'.$board.'">
<input type="hidden" name="full_name" value="'.get($board_file, 2).'">
<input type="hidden" name="edit_pass_p" value="'.$edit_passwd.'">
<P><label>タイトル<input type="text" name="title" value="'.$board_title.'"></label></p>
<p><label>説明<input type="text" name="description" value="'.$board_description.'" style="width:75%;"></label></p>
<p><label>コンテンツ<br><textarea rows="8" name="content">'.h($board_content).'</textarea></label></p>
<p>コンテンツをHTMLとしますか? (HTMLが分からない方は「いいえ」にチェックして下さい)<br>
<label><input type="radio" name="html" value="yes" checked>はい</label> <label><input type="radio" name="html" value="no">いいえ</label></p>
<p><br></p>
<p>パスワード:<br><label>入室<input type="text" name="access_pass"></label><label><b>管理</b><input type="text" name="edit_pass" value="'.$edit_passwd.'"></label></p>
<p style="float:right;"><button type="submit" name="submit" value="edited" class="enter">編集完了</button></p>
<p><br></p>
<h3>危険な操作</h2>
<p>以下の操作でこの板とそのスレッドが消滅します。これは復元不能です。</p>
<p>実行するには、<strong>「削除します」</strong>と入力して下さい。</p>
<p><label>確認<input type="text" name="kill_board"></label></p>
</form>';
} else {
$output = '<h1>認証エラー</h1><p>パスワードが違います。</p>';
}
break;
case 'edited':
(isset($_POST['board'])?$board=(int)$_POST['board']:form_die());
$post_list = [];
$form_key = ['title', 'description', 'content', 'html', 'access_pass', 'edit_pass', 'edit_pass_p', 'full_name', 'kill_board'];
foreach ($form_key as $post){
if (!isset($_POST[$post])){
form_die();
} else {
$post_list[] = $_POST[$post];
}
}
list($title, $description, $content, $html, $access_pass, $edit_pass, $edit_pass_p, $full_name, $kill_board) = $post_list;
if (not_empty($title) && not_empty($edit_pass)){
if ($html !== 'yes'){
$content = p($content);
} else {
$content = str_replace("'", '', $content);
}
$board_file = $bbs_dir.'/user_board/'.$board.'.txt';
$board_dir = $bbs_dir.'/'.$board;
$board_head = get($board_file, 1);
$board_maker = get($board_file, 2);
if ($board_maker === false){
not_found();
}
list($board_title, $board_description, $board_content, $board_access_passwd, $board_edit_passwd) = explode("'", $board_head);
if (($board_edit_passwd === pass($edit_pass_p)) && ($full_name === $board_maker)){
##危険##
if ($kill_board === '削除します'){
if (file_exists($board_dir)){
file_lock('make_thread:'.$board);
$thread_files = scandir($board_dir);
foreach ($thread_files as $thread_file){
if (preg_match('/^[0-9]+\.txt$/', $thread_file)){
$thread = str_replace('.txt', '', $thread_file);
file_lock('thread:'.$board.'-'.$thread);
remove_images_from_thread($board_dir.'/'.$thread_file);
unlink($d_root.'/sqlike/bbs/'.$board.'/'.$thread_file);
file_unlock('thread:'.$board.'-'.$thread);
}
}
unlink_by_date($board_dir, 0);
rmdir($board_dir);
file_unlock('make_thread:'.$board);
}
unlink($board_file);
} else {
$string = L1(implode("'", [h($title), h($description), $content, pass($access_pass), pass($edit_pass)])).PHP_EOL.$full_name.PHP_EOL;
file_put_contents($board_file, $string, LOCK_EX);
}
$output = '<h1>編集成功?</h1><p>板の編集に成功したと思います。</p><p><a href="/board.php?Board='.$board.'">板のURL</a></p>';
} else {
form_die();
}
} else {
$output = $reason_input;
}
break;
default:
form_die();
break;
}
} else {
$output = $reason_session;
}
} else {
form_die();
}
###########html開始
html_head('板の作成・編集', '', $d_root.'/sqlike/css/form/bbs.css', false);
if (isset($output)){
echo $output;
}
echo '<p><br></p><p><a href="./">戻る</a></p>';
html_foot('', '');