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.

365 lines
16 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 favorites (
* fav_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
* stop INT,
* priority INT,
* description VARCHAR(2048),
* key VARCHAR(64)
* );
*
* CREATE TABLE history (
* history_id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
* stop INT,
* 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="style.css" rel="stylesheet">
<meta name="theme-color" content="#facade">
</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'];
$historyRemove = $_POST['history-remove'];
// 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>
<h1>停留所のNexTrip情報</h1>
<ul class="inline-list">
<li>[<?php if(!isset($stop) && !$keyMenu) echo '<b>'; ?><a href="<?php echo $_SERVER['PHP_SELF']; ?>" accesskey="h">ホーム</a><?php if(!isset($stop) && !$keyMenu) echo '</b>'; ?>]</li>
<li>[<?php if($keyMenu) echo '<b>'; ?><a href="?key-menu" accesskey="k">キー設定</a><?php if($keyMenu) echo '</b>'; ?>]</li>
<li>[<a href="?source" accesskey="s">ソース]</a></li>
<li>[<a href="//xn--rck9c.xn--tckwe">戻る</a>]</li>
</ul>
</nav>
</header>
<main>
<?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/nextripv2/$stop");
$json = json_decode($content);
if(isset($json)) {
// 情報をプリント
$stop_name = str_replace(' & ', 'と', $json->stops[0]->description);
echo "<h2>停留所{$stop} $stop_name</h2>";
echo '<div class="grid-wrapper">';
$now = intdiv(time(), 60);
foreach($json->departures as $bus) {
if($bus->actual) {
$time = str_replace(" Min", "分", $bus->departure_text);
$time = str_replace("Due", "今すぐ", $time);
$minutes = intval($time);
$time .= '<br>(' . date('H:i', $bus->departure_time) . ')';
} else {
$time = date('H:i', $bus->departure_time);
}
$warn = "";
$warnClass = "";
if($bus->schedule_relationship == "Skipped") {
$warn = "キャンセル";
$warnClass = 'danger';
} else if($bus->schedule_relationship == "NoData") {
$warn = "データなし";
$warnClass = 'warning';
} else if($bus->schedule_relationship != "Scheduled") {
$warn = $bus->schedule_relationship;
$warnClass = 'warning';
}
echo "<p>";
$route_name = str_replace(' Line', '線', $bus->route_short_name) . $bus->terminal;
echo "<strong>{$route_name}{$directions[$bus->direction_text]}</strong>";
echo "<br><span class=\"$warnClass\">$time";
if($warn)
echo "<br>$warn";
echo '</span></p>';
}
echo '</div>';
if($json->alerts)
echo '<h3>アラート</h3>';
foreach($json->alerts as $alert) {
$alertClass = $alert->stop_closed ? "danger" : "warning";
echo "<blockquote lang=\"en\" class=\"alert $alertClass\">$alert->alert_text</blockquote>";
}
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 '<h3><label for="desc">お気に入り</label></h3>';
echo '<input type="text" id="desc" name="desc" placeholder="説明" value="' . $row['description'] . '" required>';
echo '<input type="submit" value="' . ($row ? '更新' : '追加') . '">';
echo '</form>';
// 亭の古い行を削除
$query = 'SELECT history_id FROM history WHERE history.key=$1 AND history.stop=$2 LIMIT 1';
$res = pg_query_params($query, [$key, $stop]) or die('Query failed: ' . pg_last_error());
while($row = pg_fetch_array($res)) {
$query = 'DELETE FROM history WHERE history_id=$1';
$res = pg_query_params($query, [$row['history_id']]) or die('Query failed: ' . pg_last_error());
}
// 新たな行を入れる
$query = 'INSERT INTO history (stop, key) VALUES ($1, $2)';
$res = pg_query_params($query, [$stop, $key]) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_array($res);
}
} else {
echo "<blockquote class=\"alert danger\">{$stop}は無効な停留所です。</blockquote>";
}
} else if($keyMenu) { //キーを設定
echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<h2><label for="key-set">キー設定</label></h2>';
echo '<input type="password" id="key-set" name="key-set" value="' . $_COOKIE['key'] . '">';
echo '<input type="submit" value="設定">';
echo '</form>';
echo '<p>キーを設定して、お気に入りを使用できます。キーを持つ誰でもあなたのお気に入りを見ることができますので、秘密にしておきましょう。</p>';
} else { // HPを表示
// 停留所フォームをプリント
echo '<form method="get" action="' . $_SERVER['PHP_SELF'] . '">';
echo '<h2><label for="stop">停留所の番号</label></h2>';
echo '<input type="number" tabindex="0" min="0" id="stop" name="stop" placeholder="12345" required>';
echo '<input type="submit" value="検索">';
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 "<blockquote class=\"alert success\">$favSet {$desc}は設定しました。</blockquote>";
} 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 "<blockquote class=\"alert success\">$favSet {$desc}は更新しました。</blockquote>";
} else {
echo "<blockquote class=\"alert info\">{$favSet}はすでに設定しました。</blockquote>";
}
} 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 "<blockquote class=\"alert danger\">$stop {$desc}は削除しました。</blockquote>";
} else {
echo "<blockquote class=\"alert warning\">亭はお気に入りにありません。</blockquote>";
}
} 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 "<blockquote class=\"alert success\">$stop {$desc}は{$priority}に上げました。</blockquote>";
} else {
echo "<blockquote class=\"alert warning\">亭はお気に入りにありません。</blockquote>";
}
} 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 "<blockquote class=\"alert success\">$stop {$desc}は{$priority}に下げました。</blockquote>";
} else {
echo "<blockquote class=\"alert warning\">亭はお気に入りにありません。</blockquote>";
}
} else if(isset($keySet)) {
if(!empty($keySet)) {
echo "<blockquote class=\"alert info\">ようこそ、{$shortKey}さん。</blockquote>";
} else {
echo "<blockquote class=\"alert info\">さようなら、{$shortKey}さん。</blockquote>";
}
} else if(isset($historyRemove)) { // 履歴から削除
$query = 'SELECT history_id, stop FROM history WHERE history.key=$1 AND history_id=$2';
$res = pg_query_params($query, [$key, $historyRemove]) or die('Query failed: ' . pg_last_error());
$row = pg_fetch_array($res);
if($row) {
$stop = $row['stop'];
$query = 'DELETE FROM history WHERE history_id=$1';
$res = pg_query_params($query, [$row['history_id']]) or die('Query failed: ' . pg_last_error());
echo "<blockquote class=\"alert danger\">{$stop}は削除しました。</blockquote>";
} else {
echo "<blockquote class=\"alert warning\">亭は履歴にありません。</blockquote>";
}
}
// お気に入り一覧
$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="unstyled-list">';
$priority = -1;
$i = 1;
while($row = pg_fetch_array($res)) {
if($priority != $row['priority'] && $priority != -1)
echo '<hr>';
$priority = $row['priority'];
if($i <= 10) {
$accesskey = ' accesskey="' . ($i % 10) . '"';
$i++;
} else {
$accesskey = '';
}
echo '<li class="d-flex">';
echo '<button type="submit" tabindex="0" formmethod="get" name="stop" value="' . $row['stop'] . '" class="flex-fill"' . $accesskey . '>' . $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'] . 'の優先を上げる">↑</button>';
echo '<button type="submit" formmethod="post" name="fav-down" value="' . $row['fav_id'] . '" title="' . $row['stop'] . 'の優先を下げる" aria-label="' . $row['stop'] . 'の優先を下げる">↓</button>';
echo '<button type="submit" formmethod="post" name="fav-remove" value="' . $row['fav_id'] . '" title="' . $row['stop'] . 'を削除" aria-label="' . $row['stop'] . 'を削除"class="btn danger">×</button>';
echo '</li>';
}
echo '</ul>';
echo '</form>';
}
// 履歴一覧
$query = 'SELECT history_id, stop FROM history WHERE history.key=$1 ORDER BY history_id DESC LIMIT 5';
$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="unstyled-list">';
while($row = pg_fetch_array($res)) {
echo '<li class="mb-1 d-flex">';
echo '<button type="submit" tabindex="0" formmethod="get" name="stop" value="' . $row['stop'] . '" class="btn btn-sm btn-secondary flex-fill me-1 text-start">' . $row['stop'] . '</button>';
echo '<button type="submit" formmethod="post" name="history-remove" value="' . $row['history_id'] . '" title="' . $row['stop'] . 'を削除" aria-label="' . $row['stop'] . 'を削除" class="btn danger">×</button>';
echo '</li>';
}
echo '</ul>';
echo '</form>';
}
} else {
echo '<blockquote class="alert info"><a href="?key-menu">キーを設定する</a>と、お気に入りの停留所を保存できます。既存のお気に入りため、<a href="old.php">古い版</a>も使用可能です。</blockquote>';
}
}
pg_close($dbc);
if(strstr($_SERVER['HTTP_USER_AGENT'], "KAIOS"))
echo '<script src="//xn--rck9c.xn--tckwe/assets/js/kaios.js"></script>';
?>
</main>
</body>
</html>