diff options
| author | ilotterytea <iltsu@alright.party> | 2025-10-25 20:12:11 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-10-25 20:12:11 +0500 |
| commit | 6e731c328aa66d61d882dc73080175f352d82ebc (patch) | |
| tree | 9f100129406aa0d29397493c2b2df3ec92b69315 | |
| parent | 2dfa25d9ac9358d2bade1f164d5650bded678de7 (diff) | |
feat: sound upload
| -rw-r--r-- | lib/config.php | 13 | ||||
| -rw-r--r-- | lib/sounds.php | 12 | ||||
| -rw-r--r-- | sounds/upload.php | 70 |
3 files changed, 95 insertions, 0 deletions
diff --git a/lib/config.php b/lib/config.php new file mode 100644 index 0000000..8705999 --- /dev/null +++ b/lib/config.php @@ -0,0 +1,13 @@ +<?php + +$file_path = $_SERVER['DOCUMENT_ROOT'] . '/config.ini'; + +$c = parse_ini_file($file_path, true) ?: []; + +define('DB_URL', "{$c['database']['driver']}:dbname={$c['database']['name']};host={$c['database']['host']}"); +define('DB_USER', $c['database']['user'] ?? null); +define('DB_PASS', $c['database']['pass'] ?? null); + +define('SOUND_DIRECTORY', $c['sound']['directory'] ?? "{$_SERVER['DOCUMENT_ROOT']}/static/userdata/sounds"); + +define('IS_JSON_REQUEST', isset($_SERVER['HTTP_ACCEPT']) && str_contains($_SERVER['HTTP_ACCEPT'], 'application/json'));
\ No newline at end of file diff --git a/lib/sounds.php b/lib/sounds.php new file mode 100644 index 0000000..23f566f --- /dev/null +++ b/lib/sounds.php @@ -0,0 +1,12 @@ +<?php +function compress_sound(string $input_file, string $output_file) +{ + if (!is_file($input_file)) { + throw new RuntimeException("Sound input file does not exist."); + } + + $input_file = escapeshellarg($input_file); + $output_file = escapeshellarg($output_file); + + shell_exec("ffmpeg -i $input_file -c:a libvorbis -q:a 1 $output_file"); +}
\ No newline at end of file diff --git a/sounds/upload.php b/sounds/upload.php new file mode 100644 index 0000000..035775b --- /dev/null +++ b/sounds/upload.php @@ -0,0 +1,70 @@ +<?php +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/config.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/sounds.php'; + +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + 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); + } + + compress_sound($file['tmp_name'], sprintf('%s/test.ogg', SOUND_DIRECTORY)); + + exit("Uploaded!"); + } catch (Exception $e) { + 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> + <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> +</body> + +</html>
\ No newline at end of file |
