+handleRequest(ltrim($path, '/'));
+
+function handleRequest($path)
+{
+ global $varDir;
+ if (strpos($path, '..') !== false) {
+ sendMessage('No');
+ return;
+ }
+
+ $fullPath = $varDir . $path;
+ if (!file_exists($fullPath)) {
+ sendMessage('Not found: ' . $path);
+ return;
+ }
+
+ if (is_dir($fullPath)) {
+ sendDir($path);
+ } else if (substr($path, -4) == '.url') {
+ require_once 'podcasts.php';
+ sendPodcast($path);
+ } else if (substr($path, -4) == '.txt') {
+ sendTextFile($path);
+ } else {
+ sendMessage('Unknown file type');
+ }
+}
+
+function sendDir($path)
+{
+ global $varDir;
+
+ $listItems = array();
+ addPreviousItem($listItems, $path);
+
+ $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
+ $count = 0;
+ foreach ($entries as $entry) {
+ $urlPath = substr($entry, strlen($varDir));
+ if (is_dir($entry)) {
+ ++$count;
+ $listItems[] = getDirItem(basename($entry), $urlPath . '/');
+ } else if (substr($entry, -4) == '.url') {
+ //podcast
+ ++$count;
+ $listItems[] = getPodcastItem(basename($entry, '.url'), $urlPath);
+ } else if (substr($entry, -4) == '.txt') {
+ //plain text file
+ ++$count;
+ $listItems[] = getDirItem(basename($entry, '.txt'), $urlPath);
+ }
+ }
+ if (!$count) {
+ $listItems[] = getMessageItem('No files or folders');
+ }
+ sendListItems($listItems);
+}
+
+function sendTextFile($path)
+{
+ global $varDir;
+ $listItems = array();
+ addPreviousItem($listItems, $path);
+
+ $lines = file($varDir . $path);
+ foreach ($lines as $line) {
+ $listItems[] = getDisplayItem($line);
+ }
+ sendListItems($listItems);
+}
+
+function getDisplayItem($line)
+{
+ return '<Item>'
+ . '<ItemType>Display</ItemType>'
+ . '<Display>' . utf8_decode(htmlspecialchars(trim($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 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);
+}