s refreshed by XHR:
// #div-volume -- volume sidebar (refreshed on each button click)
// #div-songlist -- MP3 browse list (static after initial load)
// #div-queue -- now playing + queue (polled every 10 seconds)
//
// JavaScript:
// xhr(url, divId) -- core XHR helper, IE4/5/6 + real browsers
// addSong(file) -- song link onclick: enqueue + refresh queue div
// volCmd(cmd) -- volume button onclick: send cmd + refresh sidebar
// pollQueue() -- setInterval callback, fires every 10 seconds
// ==================================================================
function render_page() {
?>
E.D.S. JukeBox
---- E.D.S JukeBox: ----
/dev/null');
$output = shell_exec("amixer sget Headphone | awk -F'[][]' '/%/ {gsub(\"%\",\"\",$2); print $2; exit}'");
$currvol = (int)trim($output);
//$lines = explode("\n", trim($gom_out));
//$last = $lines[count($lines) - 1];
//$currvol = (int) substr($last, 38, 4); // 0-based offset 38 = cut -b39
//if (strlen(trim($last)) == 0) {
// echo 'Error: cannot access volume';
// return;
//}
// ---- Apply command if present ------------------------------------
$cmd = isset($_GET['cmd']) ? $_GET['cmd'] : '';
if ($cmd == 'stop') {
shell_exec('irsend SIMULATE "000000000000d827 00 Stop koss.cfg"');
}
if ($cmd == 'rand') {
shell_exec('irsend SIMULATE "000000000000708F 00 Band koss.cfg"');
}
if ($cmd == 'mute') {
if (file_exists($mutefile)) {
// Un-mute: restore pre-mute volume
$newvol = (int) trim(file_get_contents($mutefile));
//shell_exec('gom -c14 -l ' . $newvol . ' -c4 -l ' . $newvol . ' >/dev/null 2>&1');
shell_exec("amixer sset Headphone {$newvol}%");
$currvol = $newvol;
unlink($mutefile);
} else {
// Mute: save current volume then zero it
file_put_contents($mutefile, $currvol);
//shell_exec('gom -c14 -l 0 -c4 -l 0 >/dev/null 2>&1');
shell_exec("amixer sset Headphone 0%");
}
}
// Volume up/down only applies when not muted
if (!file_exists($mutefile)) {
if ($cmd == 'vup2') {
$newvol = $currvol + 2;
//shell_exec('gom -c14 -l ' . $newvol . ' -c4 -l ' . $newvol . ' >/dev/null 2>&1');
shell_exec("amixer sset Headphone {$newvol}%");
$currvol = $newvol;
}
if ($cmd == 'vdn2') {
$newvol = $currvol - 2;
shell_exec("amixer sset Headphone {$newvol}%");
//shell_exec('gom -c14 -l ' . $newvol . ' -c4 -l ' . $newvol . ' >/dev/null 2>&1');
$currvol = $newvol;
}
}
$muted = file_exists($mutefile);
// ---- Render controls ---------------------------------------------
// Each button href is a real standalone URL (wget-able).
// onclick intercepts in the browser, fires XHR, refreshes the
// volume div in place, suppresses navigation.
$btn = create_function('$label,$img,$c',
'return \'\' .
\'
\' . "\n";'
);
echo 'VOL.
' . "\n";
echo $btn('Vol Up', 'vup.gif', 'vup2');
if ($muted) {
echo 'Mute
' . "\n";
} else {
echo '' . $currvol . '
' . "\n";
}
echo $btn('Vol Dn', 'vdown.gif', 'vdn2');
echo '
' . "\n";
echo $btn('Mute', 'vmute.jpg', 'mute');
echo '
' . "\n";
echo $btn('Stop', 'stop.gif', 'stop');
echo '
' . "\n";
echo $btn('Shuffle', 'dice3.gif', 'rand');
}
// ==================================================================
// TARGET: songList
// Returns an HTML fragment: alphabetically sorted, clickable list
// of all .mp3 files in MP3DIR.
//
// Clicking a song uses a real href (?target=queuedList&add=file.mp3)
// that is also wget-able from bash. The browser onclick intercepts,
// fires the same URL via XHR, refreshes the queue div in place, and
// suppresses navigation with return false.
//
// Sort is case-insensitive to match the original Perl:
// sort {uc($a) cmp uc($b)}
// ==================================================================
function render_song_list() {
$dh = @opendir($GLOBALS['MP3DIR']);
if (!$dh) {
echo 'Error: cannot open MP3 directory.';
return;
}
$songs = array();
while (($entry = readdir($dh)) !== false) {
// Match .mp3 (case-insensitive), capture the bare name
if (preg_match('/^(.+)\.mp3$/i', $entry, $m)) {
$songs[] = array('display' => $m[1], 'file' => $entry);
}
}
closedir($dh);
// Case-insensitive sort -- mirrors sort {uc($a) cmp uc($b)}
usort($songs, create_function('$a,$b',
'return strcasecmp($a["display"], $b["display"]);'
));
if (count($songs) == 0) {
echo '(no MP3s found)';
return;
}
foreach ($songs as $song) {
$display = htmlspecialchars($song['display']);
$file = urlencode($song['file']);
// href is a real working URL -- wget-able from bash:
// wget "http://server/jukebox.php?target=queuedList&add=Song.mp3"
// In the browser the onclick intercepts the click, fires the same
// request via XHR, refreshes the queue div, and returns false to
// suppress navigation. Without JS the href still works -- it just
// loads the queue fragment directly as a bare page.
$href = '?target=queuedList&add=' . $file;
echo ''
. $display . '' . "\n";
}
}
// ==================================================================
// TARGET: queuedList
// Returns an HTML fragment: now-playing line + interleaved queue.
// Also handles ?add= to enqueue a song first.
//
// Queue interleaving replicates the original Perl logic:
// - Each user has a file Requests/userlist.
// containing one full path per line in the order they queued.
// - We walk line-number 1, 2, 3 ... across ALL user files,
// printing one song per user per round, so no single user
// can monopolise the queue.
// ==================================================================
function render_queued_list() {
// ---- Enqueue a song if ?add=filename.mp3 is present -------------
// Replicates the original: echo $MP3DIR/$ARGV[0] >> $USERsLIST
// echo $ARGV[0] >> $HISTLIST
if (isset($_GET['add']) && $_GET['add'] !== '') {
// Accept only a bare filename -- no path separators allowed.
// This prevents directory traversal (../../etc/passwd etc.)
$add = basename($_GET['add']);
if (preg_match('/\.mp3$/i', $add)) {
$full_path = $GLOBALS['MP3DIR'] . '/' . $add;
$user_list = $GLOBALS['USERSDIR'] . '/userlist.' . $_SERVER['REMOTE_ADDR'];
// Append full path to this user's queue file
$fh = @fopen($user_list, 'a');
if ($fh) {
fwrite($fh, $full_path . "\n");
fclose($fh);
}
// Append bare name to the play history log
$fh = @fopen($GLOBALS['HISTLIST'], 'a');
if ($fh) {
fwrite($fh, $add . "\n");
fclose($fh);
}
}
}
// ---- Now playing ------------------------------------------------
$cur_song_file = $GLOBALS['USERSDIR'] . "/cur_song";
$playing = '';
$fh = @fopen($cur_song_file, 'r');
if ($fh) {
$line = fgets($fh);
fclose($fh);
// strip path prefix and .mp3 suffix e.g. /home/jukebox/mp3/Foo.mp3 -> Foo
if (preg_match('|/home/jukebox/mp3/(.+)\.mp3$|i', trim($line), $m)) {
$playing = $m[1];
}
}
// ---- Collect user-list files ------------------------------------
$dh = @opendir($GLOBALS['USERSDIR']);
$userfiles = array();
if ($dh) {
while (($entry = readdir($dh)) !== false) {
if (preg_match('/^userlist\./', $entry)) {
$userfiles[] = $GLOBALS['USERSDIR'] . '/' . $entry;
}
}
closedir($dh);
}
// ---- Round-robin interleave -------------------------------------
// Read line $line_num from every user file in turn.
// Stop when an entire pass finds no data in any file.
$queue = array();
$line_num = 1;
while (true) {
$found_any = false;
foreach ($userfiles as $uf) {
$fh = @fopen($uf, 'r');
if (!$fh) continue;
// Advance to line $line_num (1-based)
$data = null;
for ($i = 1; $i <= $line_num; $i++) {
$data = fgets($fh);
}
fclose($fh);
if ($data !== false && trim($data) !== '') {
if (preg_match('|/home/jukebox/mp3/(.+)\.mp3$|i', trim($data), $m)) {
$queue[] = $m[1];
$found_any = true;
}
}
}
if (!$found_any) break;
$line_num++;
}
// ---- Render -----------------------------------------------------
echo 'Playing:
' . "\n";
if ($playing !== '') {
echo htmlspecialchars($playing) . '
' . "\n";
} else {
echo '(nothing)
' . "\n";
}
echo '
Queued to Play:
' . "\n";
if (count($queue) == 0) {
echo '(queue is empty)
' . "\n";
} else {
foreach ($queue as $song) {
echo htmlspecialchars($song) . '
' . "\n";
}
}
}
?>