|
|
<?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 favorites (
|
|
|
* fav_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
|
* stop INT,
|
|
|
* priority INT,
|
|
|
* 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>停留所のNexTrip情報</title>
|
|
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-gH2yIJqKdNHPEq0n4Mqa/HGKIhSkIHeL5AyhkYV8i59U5AR6csBvApHHNl/vI1Bx" crossorigin="anonymous">
|
|
|
<style>
|
|
|
main {
|
|
|
max-width: 768px;
|
|
|
margin-bottom: 5rem;
|
|
|
}
|
|
|
|
|
|
h2 {
|
|
|
border-bottom: 1px solid lightgray;
|
|
|
}
|
|
|
</style>
|
|
|
</head>
|
|
|
<body>
|
|
|
<?php
|
|
|
// formの変数
|
|
|
$stop = $_GET['stop'];
|
|
|
$favSet = $_POST['fav-set'];
|
|
|
$favRemove = $_POST['fav-remove'];
|
|
|
$favUp = $_POST['fav-up'];
|
|
|
$favDown = $_POST['fav-down'];
|
|
|
$keyMenu = isset($_GET['key-menu']);
|
|
|
$keySet = $_POST['key-set'];
|
|
|
|
|
|
// 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>
|
|
|
<nav class="navbar navbar-expand-sm navbar-light bg-light">
|
|
|
<div class="container-fluid">
|
|
|
<a class="navbar-brand" href="<?php echo $_SERVER['PHP_SELF']; ?>">停留所のNexTrip情報</a>
|
|
|
<ul class="navbar-nav">
|
|
|
<li class="nav-item"><a class="nav-link" href="?key-menu"><?php echo empty($key) ? 'キー' : ($shortKey . 'さん'); ?></a></li>
|
|
|
<li class="nav-item"><a class="nav-link" href="?source">ソース</a></li>
|
|
|
</ul>
|
|
|
</div>
|
|
|
</nav>
|
|
|
</header>
|
|
|
|
|
|
<main class="mt-3 container-fluid">
|
|
|
<?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($stop)) { // 停留所情報を表示
|
|
|
$directions = [
|
|
|
"NB" => "北行",
|
|
|
"EB" => "東行",
|
|
|
"SB" => "南行",
|
|
|
"WB" => "西行"
|
|
|
];
|
|
|
|
|
|
// APIデータを取得
|
|
|
$content = file_get_contents("https://svc.metrotransit.org/NexTrip/$stop?format=json");
|
|
|
$json = json_decode($content);
|
|
|
|
|
|
if(isset($json)) {
|
|
|
// 情報をプリント
|
|
|
echo "<h2>停留所{$stop}のNexTrip情報</h2>";
|
|
|
echo '<div class="row row-cols-2 row-cols-sm-4 row-cols-md-6">';
|
|
|
foreach($json as $bus) {
|
|
|
if(strchr($bus->DepartureText, ':')) {
|
|
|
preg_match("/(\d+)000/", $bus->DepartureTime, $matches);
|
|
|
$time = date('H:i', $matches[1]);
|
|
|
} else {
|
|
|
$time = preg_replace("/ Min/", "分", $bus->DepartureText);
|
|
|
$time = preg_replace("/Due/", "今すぐ", $time);
|
|
|
}
|
|
|
|
|
|
echo '<div class="col">';
|
|
|
echo "<p><strong>$bus->Route{$bus->Terminal}({$directions[$bus->RouteDirection]})</strong><br>$time</p>";
|
|
|
echo '</div>';
|
|
|
}
|
|
|
echo '</div>';
|
|
|
|
|
|
if(!empty($key)) {
|
|
|
// お気に入りフォームをプリント
|
|
|
$query = 'SELECT description FROM favorites WHERE favorites.key=$1 AND favorites.stop=$2 LIMIT 1';
|
|
|
$res = pg_query_params($query, [$key, $stop]) or die('Query failed: ' . pg_last_error());
|
|
|
$row = pg_fetch_array($res);
|
|
|
|
|
|
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
|
|
|
echo '<input type="hidden" name="fav-set" value="' . $stop . '">';
|
|
|
echo '<div class="input-group">';
|
|
|
echo '<label for="desc" class="input-group-text">お気に入り</label>';
|
|
|
echo '<input type="text" id="desc" name="desc" class="form-control" placeholder="説明" value="' . $row['description'] . '" required>';
|
|
|
echo '<input type="submit" class="btn btn-outline-secondary" value="' . ($row ? '更新' : '追加') . '">';
|
|
|
echo '</div>';
|
|
|
echo '</form>';
|
|
|
}
|
|
|
} else {
|
|
|
echo "<div class=\"alert alert-danger\">{$stop}は無効な停留所です。</div>";
|
|
|
}
|
|
|
} else if($keyMenu) { //キーを設定
|
|
|
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
|
|
|
echo '<div class="input-group mb-3">';
|
|
|
echo '<label for="key-set" class="input-group-text">キー</label>';
|
|
|
echo '<input type="password" id="key-set" name="key-set" class="form-control" value="' . $_COOKIE['key'] . '">';
|
|
|
echo '<input type="submit" class="btn btn-outline-secondary" value="設定">';
|
|
|
echo '</div>';
|
|
|
echo '</form>';
|
|
|
echo '<p>キーを設定して、お気に入りを使用できます。キーを持つ誰でもはあなたのお気に入りを見ることができますので、秘密にしておきましょう。</p>';
|
|
|
} else { // HPを表示
|
|
|
// 停留所フォームをプリント
|
|
|
echo '<form method="get" action="' . $_SERVER['PHP_SELF'] . '">';
|
|
|
echo '<div class="input-group mb-3">';
|
|
|
echo '<label for="stop" class="input-group-text">停留所</label>';
|
|
|
echo '<input type="number" min="0" id="stop" name="stop" class="form-control" placeholder="12345" required>';
|
|
|
echo '<input type="submit" class="btn btn-outline-secondary" value="検索">';
|
|
|
echo '</div>';
|
|
|
echo '</form>';
|
|
|
|
|
|
if(!empty($key)) {
|
|
|
|
|
|
if(isset($favSet)) { // お気に入りに追加
|
|
|
$query = 'SELECT fav_id, stop, description FROM favorites WHERE favorites.key=$1 AND favorites.stop=$2 LIMIT 1';
|
|
|
$res = pg_query_params($query, [$key, $favSet]) or die('Query failed: ' . pg_last_error());
|
|
|
$row = pg_fetch_array($res);
|
|
|
|
|
|
$desc = $_POST['desc'];
|
|
|
if(!$row) {
|
|
|
$query = 'INSERT INTO favorites (stop, priority, description, key) VALUES ($1, 0, $2, $3)';
|
|
|
pg_query_params($query, [$favSet, $desc, $key]);
|
|
|
echo "<div class=\"alert alert-success\">$favSet – {$desc}は設定しました。</div>";
|
|
|
} else if($desc != $row['description']) {
|
|
|
$query = 'UPDATE favorites SET description=$1 WHERE favorites.key=$2 AND favorites.fav_id=$3';
|
|
|
pg_query_params($query, [$desc, $key, $row['fav_id']]);
|
|
|
echo "<div class=\"alert alert-success\">$favSet – {$desc}は更新しました。</div>";
|
|
|
} else {
|
|
|
echo "<div class=\"alert alert-info\">{$favSet}はすでに設定しました。</div>";
|
|
|
}
|
|
|
} else if(isset($favRemove)) { // お気に入りから削除
|
|
|
$query = 'SELECT stop, description FROM favorites WHERE favorites.key=$1 AND favorites.fav_id=$2';
|
|
|
$res = pg_query_params($query, [$key, $favRemove]) or die('Query failed: ' . pg_last_error());
|
|
|
$row = pg_fetch_array($res);
|
|
|
if($row) {
|
|
|
$stop = $row['stop'];
|
|
|
$desc = $row['description'];
|
|
|
$query = 'DELETE FROM favorites WHERE favorites.fav_id=$1';
|
|
|
pg_query_params($query, [$favRemove]);
|
|
|
echo "<div class=\"alert alert-danger\">$stop – {$desc}は削除しました。</div>";
|
|
|
} else {
|
|
|
echo "<div class=\"alert alert-warning\">亭はお気に入りにありません。</div>";
|
|
|
}
|
|
|
} else if(isset($favUp)) { // お気に入りを上げる
|
|
|
$query = 'SELECT stop, description, priority FROM favorites WHERE favorites.key=$1 AND favorites.fav_id=$2';
|
|
|
$res = pg_query_params($query, [$key, $favUp]) or die('Query failed: ' . pg_last_error());
|
|
|
$row = pg_fetch_array($res);
|
|
|
if($row) {
|
|
|
$stop = $row['stop'];
|
|
|
$desc = $row['description'];
|
|
|
$priority = $row['priority'] + 1;
|
|
|
$query = 'UPDATE favorites SET priority=priority+1 WHERE favorites.fav_id=$1';
|
|
|
pg_query_params($query, [$favUp]) or die('Query failed: ' . pg_last_error());
|
|
|
echo "<div class=\"alert alert-success\">$stop – {$desc}は{$priority}に上げました。</div>";
|
|
|
} else {
|
|
|
echo "<div class=\"alert alert-warning\">亭はお気に入りにありません。</div>";
|
|
|
}
|
|
|
} else if(isset($favDown)) { // お気に入りを下げる
|
|
|
$query = 'SELECT stop, description, priority FROM favorites WHERE favorites.key=$1 AND favorites.fav_id=$2';
|
|
|
$res = pg_query_params($query, [$key, $favDown]) or die('Query failed: ' . pg_last_error());
|
|
|
$row = pg_fetch_array($res);
|
|
|
if($row) {
|
|
|
$stop = $row['stop'];
|
|
|
$desc = $row['description'];
|
|
|
$priority = $row['priority'] - 1;
|
|
|
$query = 'UPDATE favorites SET priority=priority-1 WHERE favorites.fav_id=$1';
|
|
|
pg_query_params($query, [$favDown]) or die('Query failed: ' . pg_last_error());
|
|
|
echo "<div class=\"alert alert-success\">$stop – {$desc}は{$priority}に下げました。</div>";
|
|
|
} else {
|
|
|
echo "<div class=\"alert alert-warning\">亭はお気に入りにありません。</div>";
|
|
|
}
|
|
|
} else if(isset($keySet)) {
|
|
|
if(!empty($keySet)) {
|
|
|
echo "<div class=\"alert alert-info\">ようこそ、{$shortKey}さん。</div>";
|
|
|
} else {
|
|
|
echo "<div class=\"alert alert-info\">さようなら、{$shortKey}さん。</div>";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// お気に入り一覧
|
|
|
$query = 'SELECT fav_id, stop, description, priority FROM favorites WHERE favorites.key=$1 ORDER BY favorites.priority DESC, favorites.stop ASC';
|
|
|
$res = pg_query_params($query, [$key]) or die('Query failed: ' . pg_last_error());
|
|
|
|
|
|
// お気に入りがある場合、プリント
|
|
|
if(pg_num_rows($res) > 0) {
|
|
|
echo '<h2>お気に入り</h2>';
|
|
|
echo '<form 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" formmethod="get" name="stop" value="' . $row['stop'] . '" class="btn btn-sm btn-secondary flex-fill me-1 text-start">' . $row['stop'] . ' – ' . $row['description'] . '</button>';
|
|
|
echo '<button type="submit" formmethod="post" name="fav-up" value="' . $row['fav_id'] . '" title="' . $row['stop'] . 'の優先を上げる" aria-label="' . $row['stop'] . 'の優先を上げる" class="btn btn-sm btn-outline-secondary me-1">↑</button>';
|
|
|
echo '<button type="submit" formmethod="post" name="fav-down" value="' . $row['fav_id'] . '" title="' . $row['stop'] . 'の優先を下げる" aria-label="' . $row['stop'] . 'の優先を下げる" class="btn btn-sm btn-outline-secondary me-1">↓</button>';
|
|
|
echo '<button type="submit" formmethod="post" name="fav-remove" value="' . $row['fav_id'] . '" title="' . $row['stop'] . 'を削除" aria-label="' . $row['stop'] . 'を削除" class="btn btn-sm btn-danger">×</button>';
|
|
|
echo '</li>';
|
|
|
}
|
|
|
echo '</ul>';
|
|
|
echo '</form>';
|
|
|
}
|
|
|
} else {
|
|
|
echo '<div class="alert alert-info"><a href="?key-menu">キーを設定する</a>と、お気に入りの停留所を保存できます。</div>';
|
|
|
}
|
|
|
}
|
|
|
|
|
|
pg_close($dbc);
|
|
|
?>
|
|
|
</main>
|
|
|
</body>
|
|
|
</html>
|