From cb1f0f7b093ff79e2df4cdd90c11902bc1a693f1 Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Wed, 25 Nov 2015 17:14:19 +0100 Subject: [PATCH] Add support for shell scripts and auto-executed scripts. Now I'm finally able to control my house's heating system from the kitchen radio. --- README.rst | 62 ++++++++++++++++++++++++++++++++++++++++++++++----- www/index.php | 48 ++++++++++++++++++++++++++++++++++----- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/README.rst b/README.rst index c84f3e4..9d86b21 100644 --- a/README.rst +++ b/README.rst @@ -1,7 +1,8 @@ ******************** Noxon iRadio gateway ******************** -Push your own content onto Noxon iRadio devices. +Push your own content onto Noxon iRadio devices: +RSS feeds, text files and MediaTomb server structures. This tool makes it possible to push own data into the menu entries @@ -11,11 +12,54 @@ entries - My Noxon -================ -Adding a podcast -================ -Create a file "Title.url" in ``var/podcasts/`` and write -the URL of the podcast MP3 RSS feed into it. +=================================== +Customizung the directory structure +=================================== +The ``var/`` directory contains three directories you can fill with +your own content. + +================ ================== +Menu item ``var/`` Directory +================ ================== +Internet Radio ``internetradio`` +Podcasts ``podcasts`` +My Noxon ``mynoxon`` +================ ================== + +You can put folders and files into this directories. + +The ``internetradio`` directory is hard-coded to display the contents +of a MediaTomb UPnP server. +You can remove the check in ``index.php#handleRequest()`` if you do not +want this. + + +File types +========== +Directory + A directory is browsable by your Noxon radio +``.sh`` file + Shell script which is shown as directory and which gets executed + when navigating into it. + Output is shown as it is for ``.txt`` files. + + I use it to control my house's heating system from the radio. +``.auto.sh`` + Shell script which gets executed when browsing the folder. + The output is integrated into the directory listing with the same + rules as for ``.txt`` files. + + You can use this to show the current time within the directory listing. +``.txt`` file + Text files are rendered as un-actionable lists. + + Empty lines get removed, consecutive spaces get collapsed. +``.url`` file + Podcast feed URL file. + + Simply contains the URL to the podcast's MP3 RSS feed. + +File extensions get removed for display purposes. ===== @@ -32,6 +76,12 @@ by this tool:: gatekeeper.my-noxon.net +MediaTomb +========= +To be able to browse a MediaTomb server, copy ``data/config.php.dist`` to +``data/config.php`` and fill it with mediatomb web interface credentials. + + ======= License ======= diff --git a/www/index.php b/www/index.php index b742632..2a38037 100644 --- a/www/index.php +++ b/www/index.php @@ -80,13 +80,16 @@ function handleRequest($path) return; } + $ext = pathinfo($path, PATHINFO_EXTENSION); if (is_dir($fullPath)) { sendDir($path); - } else if (substr($path, -4) == '.url') { + } else if ($ext == 'url') { require_once 'podcasts.php'; sendPodcast($path); - } else if (substr($path, -4) == '.txt') { + } else if ($ext == 'txt') { sendTextFile($path); + } else if ($ext == 'sh') { + sendScript($path); } else { sendMessage('Unknown file type'); } @@ -108,17 +111,21 @@ function sendDir($path) $count = 0; foreach ($entries as $entry) { $urlPath = pathEncode(substr($entry, strlen($varDir))); + $ext = pathinfo($entry, PATHINFO_EXTENSION); if (is_dir($entry)) { ++$count; $listItems[] = getDirItem(basename($entry), $urlPath . '/'); - } else if (substr($entry, -4) == '.url') { + } else if ($ext == 'url') { //podcast ++$count; $listItems[] = getPodcastItem(basename($entry, '.url'), $urlPath); - } else if (substr($entry, -4) == '.txt') { + } else if (substr($entry, -8) == '.auto.sh') { + //automatically execute script while listing this directory + addScriptOutput($listItems, $entry); + } else if ($ext == 'txt' || $ext == 'sh') { //plain text file ++$count; - $listItems[] = getDirItem(basename($entry, '.txt'), $urlPath); + $listItems[] = getDirItem(basename($entry, '.' . $ext), $urlPath); } } if (!$count) { @@ -127,6 +134,30 @@ function sendDir($path) sendListItems($listItems); } +function sendScript($path) +{ + global $varDir; + + $listItems = array(); + addPreviousItem($listItems, $path); + + $fullPath = $varDir . $path; + addScriptOutput($listItems, $fullPath); + sendListItems($listItems); +} + +function addScriptOutput(&$listItems, $fullPath) +{ + exec($fullPath . ' 2>&1', $output, $retVal); + + if ($retVal == 0) { + addTextLines($listItems, $output); + } else { + $listItems[] = getMessageItem('Error executing script'); + addTextLines($listItems, $output); + } +} + function sendTextFile($path) { global $varDir; @@ -134,13 +165,18 @@ function sendTextFile($path) addPreviousItem($listItems, $path); $lines = file($varDir . $path); + addTextLines($listItems, $lines); + sendListItems($listItems); +} + +function addTextLines(&$listItems, $lines) +{ foreach ($lines as $line) { $line = trim($line); if ($line != '') { $listItems[] = getDisplayItem($line); } } - sendListItems($listItems); } function getDisplayItem($line) -- 2.30.2