You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

257 lines
10 KiB

This file contains invisible Unicode characters!

This file contains invisible Unicode characters that may be processed differently from what appears below. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to reveal hidden characters.

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
/*
* ##### 使用前に読んでね #####
*
* 「vars.php.example」を「vars.php」にコピーして、「<……>」を取り替えます。
* 次に、vars.phpに指定データベースを作成して、以下のテーブルを作成します。
*
* CREATE TABLE items (
* item_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
* priority INT DEFAULT 0,
* done BOOL DEFAULT FALSE,
* description VARCHAR(2048),
* key VARCHAR(64)
* );
*/
require_once('vars.php');
// ソースコードを送信
if(isset($_GET['source'])) {
header("Content-Type: text/plain");
die(file_get_contents(basename($_SERVER['PHP_SELF'])));
}
?>
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>To-Doリスト</title>
<link rel="stylesheet" href="/assets/css/style.css">
<meta name="theme-color" content="#facade">
</head>
<body>
<?php
// formの変数
$itemEdit = $_POST['item-edit'];
$itemDone = $_POST['item-done'];
$itemRemove = $_POST['item-remove'];
$keyMenu = isset($_GET['key-menu']);
$keySet = $_REQUEST['key-set'];
$setDescription = $_POST['set-description'];
// cookieの変数
if(isset($_COOKIE['key'])) {
$key = hash('sha256', $_COOKIE['key']);
$shortKey = substr($key, 0, 7);
}
// キーを更新
if(isset($keySet)) {
if(!empty($keySet)) {
setcookie("key", $keySet, 2147483647);
$key = hash('sha256', $keySet);
$shortKey = substr($key, 0, 7);
} else {
setcookie("key", "", 1);
unset($key);
}
}
?>
<header class="center container">
<h1>To-Doリスト</h1>
[<a href="<?php echo $_SERVER['PHP_SELF']; ?>">home</a>]
[<a href="?key-menu">key</a>]
[<a href="?source">source</a>]
[<a href="//xn--rck9c.xn--tckwe">back</a>]
</header>
<main class="center container block">
<?php
date_default_timezone_set('America/Chicago');
// DBに接続
$dbc = pg_connect("host=$DB_HOST dbname=$DB_NAME user=$DB_USER password=$DB_PASSWORD")
or die('Could not connect: ' . pg_last_error());
if(isset($itemEdit)) { // アイテムの編集
$query = 'SELECT item_id, description, priority FROM items WHERE items.key=$1 AND item_id=$2 LIMIT 1';
$res = pg_query_params($query, [$key, $itemEdit]) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_array($res);
echo '<h2>アイテムの編集<small>/edit item</small></h2>';
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<div class="mb-3 d-flex">';
echo '<label for="set-description"><ruby>説明<rp> (</rp><rt>description</rt><rp>)</rp></ruby></label>';
echo '<input id="set-description" name="set-description" class="flex-fill" value="' . $row['description'] . '" required>';
echo '</div>';
echo '<div class="mb-3 d-flex">';
echo '<label for="set-priority"><ruby>優先<rp> (</rp><rt>priority</rt><rp>)</rp></ruby></label>';
echo '<input type="number" id="set-priority" name="set-priority" class="flex-fill" value="' . ($row['priority'] ?? 0) . '">';
echo '</div>';
echo '<input type="hidden" name="item-id" value="' . $row['item_id'] . '">';
echo '<input type="submit" class="bg-green" value="設定">';
echo '<button type="submit" class="bg-red" name="item-remove" value="' . $itemEdit . '">削除</button>';
echo '</form>';
} else if($keyMenu) { //キーを設定
echo '<h2>キーの設定<small>/set key</small></h2>';
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<label for="key-set"><ruby>キー<rp> (</rp><rt>key</rt><rp>)</rp></ruby></label>';
echo '<input id="key-set" name="key-set" value="' . $_COOKIE['key'] . '">';
echo '<input type="submit" value="設定" class="bg-green">';
echo '</form>';
echo '<p>アプリを使用するには、キーを設定する必要がありま一覧を見ることができますので、秘密にしておきましょう。</p>';
} else { // HPを表示
if(!empty($key)) {
if(isset($itemRemove)) { // アイテムを削除
$query = 'SELECT description FROM items WHERE items.key=$1 AND item_id=$2';
$res = pg_query_params($query, [$key, $itemRemove]) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_array($res);
if($row) {
$desc = $row['description'];
$query = 'DELETE FROM items WHERE item_id=$1';
pg_query_params($query, [$itemRemove]);
echo "<div class=\"alert bg-red\">{$desc}は削除しました。</div>";
} else {
echo "<div class=\"alert bg-blue\">アイテムはありません。</div>";
}
} else if(isset($itemDone)) { // アイテム完了
$query = 'SELECT description, done FROM items WHERE items.key=$1 AND item_id=$2';
$res = pg_query_params($query, [$key, $itemDone]) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_array($res);
if($row) {
$desc = $row['description'];
$query = 'UPDATE items SET done = NOT done WHERE item_id=$1';
pg_query_params($query, [$itemDone]) or die('Query failed: ' . pg_last_error());
$doneText = $row['done'] == 'f' ? '完了しました' : '完了にしませんでした';
echo "<div class=\"alert bg-green\">{$desc}が{$doneText}。</div>";
} else {
echo "<div class=\"alert bg-red\">アイテムはありません。</div>";
}
} else if(isset($keySet)) {
if(!empty($keySet)) {
echo "<div class=\"alert bg-blue\">リスト{$shortKey}に変更しました。</div>";
}
} else if(isset($setDescription)) { // アイテムを編集・追加する
$itemId = $_POST['item-id'];
$setPriority = $_POST['set-priority'];
if($itemId) {
$query = 'SELECT FROM items WHERE items.key=$1 AND item_id=$2 LIMIT 1';
$res = pg_query_params($query, [$key, $itemId]) or die('Query failed: ' . pg_last_error());
}
if(!$res || pg_num_rows($res) == 0) {
$query = 'INSERT INTO items (description, key) VALUES ($1, $2)';
pg_query_params($query, [$setDescription, $key]);
echo "<div class=\"alert bg-green\">{$setDescription}は追加しました。</div>";
} else {
$query = 'UPDATE items SET priority=$1, description=$2 WHERE items.key=$3 AND item_id=$4';
pg_query_params($query, [$setPriority, $setDescription, $key, $itemId]);
echo "<div class=\"alert bg-green\">{$setDescription}は更新しました。</div>";
}
}
// アイテム一覧
$query = 'SELECT item_id, description, priority FROM items WHERE items.key=$1 AND done=FALSE ORDER BY items.priority DESC, items.description ASC';
$res = pg_query_params($query, [$key]) or die('Query failed: ' . pg_last_error());
echo '<h2>アイテム一覧<small>/item list</small></h2>';
if(pg_num_rows($res) > 0) {
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<ul class="list-unstyled">';
$priority = -1;
while($row = pg_fetch_array($res)) {
if($priority != $row['priority'] && $priority != -1)
echo '<hr>';
$priority = $row['priority'];
echo '<li class="mb-1 d-flex">';
echo '<button type="submit" name="item-done" value="' . $row['item_id'] . '" class="bg-green">✓</button>';
echo '<button type="submit" name="item-edit" value="' . $row['item_id'] . '" class="text-left flex-fill">' . $row['description'] . '</button>';
echo '<button type="submit" name="item-remove" value="' . $row['item_id'] . '" class="bg-red">×</button>';
echo '</li>';
}
echo '</ul>';
echo '</form>';
}
// 追加ボタン
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '" class="d-flex">';
echo '<button type="submit" name="item-done" class="bg-green" disabled>✓</button>';
echo '<input name="set-description" class="flex-fill" placeholder="新しい" required>';
echo '<input type="submit" value="+" class="bg-green">';
echo '</form>';
// 完了一覧
$query = 'SELECT item_id, description FROM items WHERE items.key=$1 AND done=TRUE ORDER BY items.description ASC';
$res = pg_query_params($query, [$key]) or die('Query failed: ' . pg_last_error());
echo '<h3>完了されたアイテム<small>/completed items</small></h3>';
if(pg_num_rows($res) > 0) {
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<ul class="list-unstyled">';
$priority = -1;
while($row = pg_fetch_array($res)) {
if($priority != $row['priority'] && $priority != -1)
echo '<hr>';
$priority = $row['priority'];
echo '<li class="mb-1 d-flex">';
echo '<button type="submit" name="item-done" value="' . $row['item_id'] . '">↑</button>';
echo '<button type="submit" name="item-edit" value="' . $row['item_id'] . '" class="text-left flex-fill">' . $row['description'] . '</button>';
echo '<button type="submit" name="item-remove" value="' . $row['item_id'] . '" class="bg-red">×</button>';
echo '</li>';
}
echo '</ul>';
echo '</form>';
}
} else {
echo '<div class="alert bg-blue"><a href="?key-menu">キーを設定してください。</a></div>';
}
}
pg_close($dbc);
?>
</main>
</body>
</html>