X-Git-Url: https://git.cweiske.de/noxon-gateway.git/blobdiff_plain/931d5803649e32b043d7af9034dde326ddb1fefa..daeda7055cd10cc88ca0966c8791c1c001e4e30f:/www/index.php diff --git a/www/index.php b/www/index.php index cc0be0e..65f6715 100644 --- a/www/index.php +++ b/www/index.php @@ -1,129 +1,273 @@ array('method' => 'HEAD')) - ); - //get_headers follows redirects automatically - $headers = get_headers($url, 1); - if ($headers !== false && isset($headers['Location'])) { - return end($headers['Location']); + 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'); } - return $url; } +function pathEncode($urlPath) +{ + return str_replace('%2F', '/', rawurlencode($urlPath)); +} -function sendList($path) +function sendDir($path) { - $startitems = 1; - $enditems = 10; - if (isset($_GET['startitems'])) { - $startitems = (int) $_GET['startitems']; + 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 (isset($_GET['enditems'])) { - $enditems = (int) $_GET['enditems']; + if (!$count) { + $listItems[] = getMessageItem('No files or folders'); } + sendListItems($listItems); +} - header('Content-type: text/xml'); - echo << - - -1 - - Message - $path - - - Dir - $path - http://radio567.vtuner.com/$path - http://radio5672.vtuner.com/$path - - - -XML; +function sendScript($path) +{ + global $varDir; + + $listItems = array(); + addPreviousItem($listItems, $path); + + $fullPath = $varDir . $path; + addScriptOutput($listItems, $fullPath); + sendListItems($listItems); } -function sendMessage($msg) +function addScriptOutput(&$listItems, $fullPath) { - header('Content-type: text/xml'); - $xMsg = htmlspecialchars($msg); - echo << - - - Message - $xMsg - - + 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); -XML; + $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 '' + . 'Display' + . '' . utf8_decode(htmlspecialchars($line)) . '' + . ''; +} + +function getDirItem($title, $urlPath) +{ + global $host1, $host2; + return '' + . 'Dir' + . '' . utf8_decode(htmlspecialchars($title)) . '' + . '' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '' + . '' . $host2 . utf8_decode(htmlspecialchars($urlPath)) . '' + . ''; +} + +function getEpisodeItem($title, $fullUrl, $desc, $type) +{ + return '' + . 'ShowEpisode' + . '' . utf8_decode(htmlspecialchars($title)) . '' + . '' . htmlspecialchars($fullUrl) . '' + . '' . utf8_decode(htmlspecialchars($desc)) . '' + . '' . $type . '' + . ''; +} + +function getPodcastItem($title, $urlPath) +{ + global $host1; + return '' + . 'ShowOnDemand' + . '' . utf8_decode(htmlspecialchars($title)) . '' + . '' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '' + . ''; +} + +function getMessageItem($msg) +{ + return '' + . 'Message' + . '' . utf8_decode(htmlspecialchars($msg)) . '' + . ''; +} + +function getPreviousItem($urlPath) +{ + global $host1, $host2; + return '' + . 'Previous' + . '' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '' + . '' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '' + . ''; +} + +function addPreviousItem(&$listItems, $urlPath) +{ + $parentDir = dirname($urlPath) . '/'; + if ($parentDir == '/') { + return; + } + $listItems[] = getPreviousItem($parentDir); +} + +function sendMessage($msg) +{ + sendListItems(array(getMessageItem($msg))); } function sendListItems($listItems) { + $startitems = 1; + $enditems = 10; + if (isset($_GET['startitems'])) { + $startitems = (int) $_GET['startitems']; + } + if (isset($_GET['enditems'])) { + $enditems = (int) $_GET['enditems']; + } + //TODO: limit list + $xml = '' . "\n"; + $xml .= '' . "\n"; $xml .= '' . "\n"; foreach ($listItems as $item) { $xml .= $item . "\n"; } $xml .= "\n"; - + header('Content-type: text/xml'); echo $xml; } - ?>