-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmusic.php
30 lines (26 loc) · 966 Bytes
/
music.php
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
<?php
//get config
require 'config.php';
//get parameter
$category = $_GET['game'] ?? '.*';
$type = $_GET['type'] ?? 'raw';
$url = CONFIG['RES_URL'] ?? http_response_code(500) && die('RES_URL_NOT_DEFINED');
//generate the list
$regexp = '/^(music\/' . $category . '\/).*(\.(mp3|ogg|wav))$/i';
$musics = array();
foreach (json_decode(file_get_contents(__DIR__ . '/contents.json')) as $name => $path) {
if (preg_match($regexp, $path) == 1) {
array_push($musics, array('name' => $name, 'path' => $url . str_replace('%2F', '/', rawurlencode($path))));
}
}
$musics ?: http_response_code(500) && die('list of music is empty');
//get a random image
$music = $musics[array_rand($musics)];
//output
if ($type == 'json') {
header('Content-Type: application/json');
header('Charset: UTF-8');
echo json_encode(array('name' => $music['name'], 'url' => $music['path']), JSON_UNESCAPED_UNICODE);
} else {
header("Location:" . $music['path']);
}