summaryrefslogtreecommitdiff
path: root/sounds/upload.php
blob: 155bd428a2b9b71b381fdee2026cf157e069a433 (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
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/config.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/sounds.php';
include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/partials.php';

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $db = new PDO(DB_URL, DB_USER, DB_PASS);

    try {
        if (!isset($_FILES['file'], $_POST['name'], $_POST['tos'])) {
            throw new RuntimeException("Not a valid request.");
        }

        $file = $_FILES['file'];

        if (!isset($file['error']) || is_array($file['error'])) {
            throw new RuntimeException('Invalid file.');
        }

        switch ($file['error']) {
            case UPLOAD_ERR_OK:
                break;
            case UPLOAD_ERR_NO_FILE:
                throw new RuntimeException("No file sent.");
            case UPLOAD_ERR_INI_SIZE:
            case UPLOAD_ERR_FORM_SIZE:
                throw new RuntimeException("Exceeded filesize limit.");
            default:
                throw new RuntimeException("Unknown errors.");
        }

        if (!is_dir(SOUND_DIRECTORY)) {
            mkdir(SOUND_DIRECTORY, 0777, true);
        }

        $db->prepare('INSERT INTO sounds(code) VALUES (?)')
            ->execute([$_POST['name']]);

        $id = $db->lastInsertId();

        compress_sound($file['tmp_name'], sprintf('%s/%s.ogg', SOUND_DIRECTORY, $id));

        exit("Uploaded!");
    } catch (Exception $e) {
        if (isset($id)) {
            $db->prepare('DELETE FROM sounds WHERE id = ?')
                ->execute([$id]);
        }
        exit($e->getMessage());
    }
}
?>
<!DOCTYPE html>
<html>

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

<body>
    <?php html_header(); ?>

    <main>
        <div class="row">
            <div class="box">
                <div class="tab">
                    <p>Upload a new sound</p>
                </div>
                <div class="content">
                    <form action="/sounds/upload.php" method="post" enctype="multipart/form-data">
                        <div class="column gap-8">
                            <label for="file"><b>Sound<span class="red">*</span></b></label>
                            <input type="file" name="file" id="file" required>
                        </div>
                        <div class="column gap-8">
                            <label for="name"><b>Sound name<span class="red">*</span></b></label>
                            <input type="text" name="name" id="name" required>
                        </div>

                        <div class="row gap-8">
                            <label for="tos">Do you accept <a href="/tos.php">the rules</a>?<span
                                    class="red">*</span></label>
                            <input type="checkbox" name="tos" id="tos" value="1">
                        </div>

                        <button type="submit">Upload</button>
                    </form>
                </div>
            </div>
        </div>
    </main>
</body>

</html>