+handleRequest(ltrim($path, '/'));
+
+function handleRequest($path)
+{
+ global $varDir;
+ if (strpos($path, '..') !== false) {
+ sendMessage('No');
+ return;
+ }
+
+ if (substr($path, 0, 14) == 'internetradio/') {
+ require_once 'mediatomb.php';
+ handleRequestMediatomb($path, 'internetradio/');
+ return;
+ }
+
+
+ $fullPath = $varDir . $path;
+ if (!file_exists($fullPath)) {
+ sendMessage('Not found: ' . $path);
+ return;
+ }
+
+ $ext = pathinfo($path, PATHINFO_EXTENSION);
+ if (is_dir($fullPath)) {
+ sendDir($path);
+ } else if ($ext == 'url') {
+ require_once 'podcasts.php';
+ sendPodcast($path);
+ } else if ($ext == 'txt') {
+ sendTextFile($path);
+ } else if ($ext == 'sh') {
+ sendScript($path);
+ } else {
+ sendMessage('Unknown file type');
+ }
+}
+
+function pathEncode($urlPath)
+{
+ return str_replace('%2F', '/', rawurlencode($urlPath));
+}
+
+function sendDir($path)
+{
+ global $varDir;
+
+ $listItems = array();
+ addPreviousItem($listItems, $path);
+
+ $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
+ $count = 0;
+ foreach ($entries as $entry) {
+ $urlPath = pathEncode(substr($entry, strlen($varDir)));
+ $ext = pathinfo($entry, PATHINFO_EXTENSION);
+
+ $titleBase = basename($entry);
+ $titleBase = preg_replace('#^[0-9]+_#', '', $titleBase);
+ if (is_dir($entry)) {
+ ++$count;
+ $listItems[] = getDirItem($titleBase, $urlPath . '/');
+ } else if ($ext == 'url') {
+ //podcast
+ ++$count;
+ $listItems[] = getPodcastItem(basename($titleBase, '.url'), $urlPath);
+ } 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($titleBase, '.' . $ext), $urlPath);
+ }
+ }
+ if (!$count) {
+ $listItems[] = getMessageItem('No files or folders');
+ }
+ 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;
+ $listItems = array();
+ 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);
+ }
+ }
+}
+
+function getDisplayItem($line)
+{
+ $line = preg_replace('#\s+#', ' ', $line);
+ return '<Item>'
+ . '<ItemType>Display</ItemType>'
+ . '<Display>' . utf8_decode(htmlspecialchars($line)) . '</Display>'
+ . '</Item>';
+}
+
+function getDirItem($title, $urlPath)
+{
+ global $host1, $host2;
+ return '<Item>'
+ . '<ItemType>Dir</ItemType>'
+ . '<Title>' . utf8_decode(htmlspecialchars($title)) . '</Title>'
+ . '<UrlDir>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDir>'
+ . '<UrlDirBackUp>' . $host2 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDirBackUp>'
+ . '</Item>';
+}
+
+function getEpisodeItem($title, $fullUrl, $desc, $type)
+{
+ return '<Item>'
+ . '<ItemType>ShowEpisode</ItemType>'
+ . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
+ . '<ShowEpisodeURL>' . $fullUrl . '</ShowEpisodeURL>'
+ . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
+ . '<ShowMime>' . $type . '</ShowMime>'
+ . '</Item>';
+}
+
+function getPodcastItem($title, $urlPath)
+{
+ global $host1;
+ return '<Item>'
+ . '<ItemType>ShowOnDemand</ItemType>'
+ . '<ShowOnDemandName>' . utf8_decode(htmlspecialchars($title)) . '</ShowOnDemandName>'
+ . '<ShowOnDemandURL>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</ShowOnDemandURL>'
+ . '</Item>';
+}
+
+function getMessageItem($msg)
+{
+ return '<Item>'
+ . '<ItemType>Message</ItemType>'
+ . '<Message>' . utf8_decode(htmlspecialchars($msg)) . '</Message>'
+ . '</Item>';
+}
+
+function getPreviousItem($urlPath)
+{
+ global $host1, $host2;
+ return '<Item>'
+ . '<ItemType>Previous</ItemType>'
+ . '<UrlPrevious>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPrevious>'
+ . '<UrlPreviousBackUp>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPreviousBackUp>'
+ . '</Item>';
+}
+
+function addPreviousItem(&$listItems, $urlPath)
+{
+ $parentDir = dirname($urlPath) . '/';
+ if ($parentDir == '/') {
+ return;
+ }
+ $listItems[] = getPreviousItem($parentDir);
+}