<?php
//
// BBWA
// delete.php
// 画像を削除する
// Teppei KAKU <teppei@sat.co.jp>
//

include("variables.php");
include("functions.php");

if (isset($_POST['delImg'])) {
  // 画像情報を読み込む
  $pizza = file("${dirName}Messages/${_POST['delImg']}_mes.txt");
  $piece = explode("|||", $pizza[0]);
} else {
  printHtml("エラー: 画像が指定されていません",
    "JavaScript:history.back()", "");
}

if ($_POST['delPasswd'] == $adminPass || $_POST['delPasswd'] == $piece[6]) {
  // ファイルを削除する
  if ($_POST['delConf'] == "true") {
    unlink("${dirName}${_POST['delImg']}");
    if (file_exists("${dirName}Thumbnails/${_POST['delImg']}_thumb.jpg")) {
      unlink("${dirName}Thumbnails/${_POST['delImg']}_thumb.jpg");
    }
    unlink("${dirName}Messages/${_POST['delImg']}_mes.txt");

    // インデックスから削除
    $oldList = file("${dirName}INDEX");
    $filePtr = fopen("${dirName}INDEX", "w");
    while (!flock($filePtr, LOCK_EX)) {};
    foreach ($oldList as $listText) {
      if (!strstr($listText, $_POST['delImg'])) {
        $listText = trim($listText);
        fwrite($filePtr, "${listText}\n");
      }
    }
    flock($filePtr, LOCK_UN);
    fclose($filePtr);

    printHtml("画像を削除しました", "right.php", "DEL");
  }

  // 投稿内容を修正する
  if ($_POST['editConf'] == "true") {
    if (trim($_POST['editName']) == "") {
      printHtml("エラー: 名前を入力してください",
        "JavaScript:history.back()", "");
    } else {
      $editName = mb_convert_encoding($_POST['editName'], "EUC-JP", "auto");
    }
    if (trim($_POST['editTitle']) == "") {
      $editTitle = "タイトルなし";
    } else {
      $editTitle = mb_convert_encoding($_POST['editTitle'], "EUC-JP", "auto");
    }
    if (trim($_POST['editMail']) == "") {
      $editMail = "NOMAIL";
    } else {
      $editMail = mb_convert_encoding($_POST['editMail'], "EUC-JP", "auto");
    }
    if (trim($_POST['editCom']) == "") {
      $editCom = "NOCOM";
    } else {
      $editCom = mb_convert_encoding($_POST['editCom'], "EUC-JP", "auto");
      $editCom = eregi_replace("\r\n|\r|\n", "{{BR}}", $editCom);
      $editCom = eregi_replace("<", "{{LT}}", $editCom);
      $editCom = eregi_replace(">", "{{GT}}", $editCom);
    }
    $editText = "${piece[0]}|||${piece[1]}|||${editTitle}|||${editName}|||" .
      "${editMail}|||${piece[5]}|||${piece[6]}|||{$editCom}\n";

    $oldTime = filemtime("${dirName}Messages/${_POST['delImg']}_mes.txt");
    $filePtr =
      fopen("${dirName}Messages/${_POST['delImg']}_mes.txt", "w");
    while (!flock($filePtr, LOCK_EX)) {};
    fwrite($filePtr, $editText);
    for ($i = 1; $i < count($pizza); $i++) {
      fwrite($filePtr, $pizza[$i]);
    }
    flock($filePtr, LOCK_UN);
    fclose($filePtr);
    touch("${dirName}Messages/${_POST['delImg']}_mes.txt", $oldTime);

    printHtml("投稿内容を修正しました",
      "right.php?img=${_POST['delImg']}",
      "");
  }

  echo <<<EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=euc-jp">
<link rel="stylesheet" type="text/css" href="Themes/${theme}.css">
</head>
<body class="right">
<table><tr><td class="right">
<table>
EOD;

  $piece[3] = htmlspecialchars($piece[3]);

  if (isset($_POST['editButton'])) {
    // 修正用ページの表示
    if ($piece[2] == "タイトルなし") {
      $piece[2] = "";
    } else {
      $piece[2] == htmlspecialchars($piece[2]);
    }
    if ($piece[4] == "NOMAIL") {
      $piece[4] = "";
    } else {
      $piece[4] == htmlspecialchars($piece[4]);
    }
    if (trim($piece[7]) == "NOCOM") {
      $piece[7] = "";
    } else {
      $piece[7] = messageParse2($piece[7]);
    }

    echo <<<EOD
<tr><td valign="top" style="padding-right:100px;">
<strong>投稿内容の修正</strong></td>
<td align="right">
<table><tr><td class="photoframe">
<img src="${picUrl}Thumbnails/${_POST['delImg']}_thumb.jpg" align="top">
</td></tr></table>
</td></tr>
<tr><td colspan="2"><hr class="right" size="1"></td></tr>
<tr><td colspan="2">
<table>
<form action="./delete.php" method="POST">
<tr><td align="right">題名</td>
<td><input type="text" name="editTitle" size="40" value="${piece[2]}">
</td></tr>
<tr><td align="right">名前</td>
<td><input type="text" name="editName" size="40" value="${piece[3]}">
<span class="notice"> (必須)</span></td></tr>
<tr><td align="right">メールアドレス</td>
<td><input type="text" name="editMail" size="40" value="${piece[4]}">
</td></tr>
<tr><td align="right">説明</td>
<td><textarea name="editCom" rows="10" cols="40">${piece[7]}</textarea>
</td></tr>
<tr><td></td><td>
<input type="hidden" name="editConf" value="true">
<input type="submit" value="修正">
<input type="reset" value="リセット">
EOD;

  } else {
    // 削除確認用ページの表示
    $piece[2] == htmlspecialchars($piece[2]);
    if (trim($piece[7]) == "NOCOM") {
      $piece[7] = "";
    } else {
      $piece[7] = messageParse($piece[7]);
    }

    echo <<<EOD
<tr><td valign="top" style="padding-right:100px;">
<strong>削除の確認</strong></td>
<td align="right">
<table><tr><td class="photoframe">
<img src="${picUrl}Thumbnails/${_POST['delImg']}_thumb.jpg" align="top">
</td></tr></table>
</td></tr>
<tr><td colspan="2"><hr class="right" size="1"></td></tr>
<tr><td colspan="2">
<table>
<form action="./delete.php" method="POST">
<tr><td nowrap align="right">タイトル:</td>
<td><strong>${piece[2]}</strong></td></tr>
<tr><td nowrap align="right">投稿者:</td>
<td><strong>${piece[3]}</strong></td></tr>
<tr><td nowrap align="right">投稿日時:</td>
<td><strong>${piece[5]}</strong></td></tr>
<tr><td nowrap align="right" valign="top"><br>説明:</td>
<td><br>${piece[7]}</td></tr>
<tr><td colspan="2">
<br><br>本当に削除しますか？<br>
<input type="hidden" name="delConf" value="true">
<input type="submit" value="削除">
EOD;

 }

  echo <<<EOD

<input type="button" value="キャンセル" onClick="JavaScript:history.back()">
<input type="hidden" name="delImg" value="${_POST['delImg']}">
<input type="hidden" name="delPasswd" value="${_POST['delPasswd']}">
</td></tr>
</form>
</table>
</td></tr></table>
</body>
</html>
EOD;

} else {
  printHtml("エラー: パスワードが違います",
    "JavaScript:history.back()", "");
}
?>
