summaryrefslogtreecommitdiff
path: root/public/report.php
blob: 72f49a32dee3adc3aafefd9f601c772cb089ea15 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/partials.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/alert.php';

if (!FILE_REPORT) {
    generate_alert(
        '/',
        'No reports allowed!',
        403,
        null
    );
    exit();
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if (!isset($_POST['id'], $_POST['reason'])) {
        generate_alert(
            '/report.php',
            'Not enough data.',
            400,
            null
        );
        exit();
    }

    $file_id = $_POST['id'];
    $file_id = explode('.', $file_id);
    if (count($file_id) != 2) {
        generate_alert(
            '/report.php',
            'Not enough data.',
            400,
            null
        );
        exit();
    }
    $file_ext = $file_id[1];
    $file_id = $file_id[0];

    if (!is_file(FILE_UPLOAD_DIRECTORY . "/{$file_id}.{$file_ext}")) {
        generate_alert(
            '/report.php',
            'Invalid file.',
            404,
            null
        );
        exit();
    }

    $reason = str_safe($_POST['reason'] ?? '', null);

    if (empty($reason)) {
        generate_alert(
            '/report.php',
            'Report reason is empty',
            400,
            null
        );
        exit();
    }

    $email = str_safe($_POST['email'] ?? '(Anonymous)', null);
    if (empty($email)) {
        $email = '(Anonymous)';
    }

    if (!is_dir(FILE_REPORT_DIRECTORY) && !mkdir(FILE_REPORT_DIRECTORY, 0777, true)) {
        generate_alert(
            '/report.php',
            'Failed to create a folder for reports. Try again later.',
            500,
            null
        );
        exit();
    }

    do {
        $report_id = generate_random_char_sequence(FILE_ID_CHARACTERS, 16);
    } while (is_file(FILE_REPORT_DIRECTORY . "/{$report_id}.txt"));

    $contents = "File ID: {$file_id}.{$file_ext}
Feedback Email: {$email}

Reason:
{$reason}";

    if (!file_put_contents(FILE_REPORT_DIRECTORY . "/{$report_id}.txt", $contents)) {
        generate_alert(
            '/report.php',
            'Failed to save the report. Try again later!',
            500,
            null
        );
        exit();
    }

    generate_alert(
        '/report.php',
        'Success!',
        201,
        ['id' => $report_id]
    );
    exit();
}

$file_id = $_GET['f'] ?? '';

if (!is_file(FILE_UPLOAD_DIRECTORY . "/{$file_id}")) {
    $file_id = null;
}

?>
<html>

<head>
    <title>Report - <?= INSTANCE_NAME ?></title>
    <link rel="stylesheet" href="/static/style.css">
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon">
</head>

<body>
    <main>
        <?php html_mini_navbar() ?>
        <?php display_alert() ?>

        <h1>Report a file</h1>
        <hr>
        <form action="/report.php" method="post">
            <table class="vertical">
                <tr>
                    <th>File ID with extension:</th>
                    <td><input type="text" name="id" value="<?= $file_id ?>" placeholder="XXXXX.png" required></td>
                </tr>
                <tr>
                    <th>What is wrong with that file?</th>
                    <td><textarea name="reason" placeholder="..." required></textarea></td>
                </tr>
                <tr>
                    <th>Feedback E-Mail:</th>
                    <td><input type="email" name="email" placeholder="Optional"></td>
                </tr>
                <tr>
                    <th></th>
                    <td><button type="submit">Send</button></td>
                </tr>
            </table>
        </form>
    </main>
</body>

</html>