2 set_include_path(get_include_path() . PATH_SEPARATOR . __DIR__ . '/../src/');
3 $fullUri = $_SERVER['REQUEST_URI'];
4 if (isset($_SERVER['REDIRECT_URL'])) {
5 $path = $_SERVER['REDIRECT_URL'];
9 $dataDir = __DIR__ . '/../data/';
10 $varDir = realpath(__DIR__ . '/../var') . '/';
11 $host1 = 'http://radio567.vtuner.com/';
12 $host2 = 'http://radio5672.vtuner.com/';
13 if ($_SERVER['HTTP_HOST'] !== '') {
14 $host1 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
15 $host2 = 'http://' . $_SERVER['HTTP_HOST'] . '/';
17 $cfgFile = $dataDir . 'config.php';
18 if (file_exists($cfgFile)) {
22 if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token=0'
23 || $fullUri == '/RadioNativeLogin.php'
25 //initial login for "internet radio", podcasts and "my noxon"
26 header('Content-type: text/html');
27 readfile($dataDir . 'login-camelcase.xml');
29 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/LoginXML.asp') {
31 $path = '/internetradio/';
32 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/navXML.asp') {
35 } else if ($path == '/RadioNative.php') {
38 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
39 //Internet Radio Station favorites favorited on device
40 sendMessage('Unsupported');
41 } else if ($path == '/RadioNativeFavorites.php') {
42 //Favorites, defined via web interface
43 sendMessage('Unsupported');
44 } else if (substr($path, 0, 9) == '/play-url') {
45 //play a given URL, but first follow all redirects
46 //noxon iRadio Cube does not like too many redirections
47 // 3 redirects did not work.
49 header('HTTP/1.0 301 Moved Permanently');
50 header('Location: ' . getFinalUrl($url));
54 handleRequest(ltrim($path, '/'));
56 function handleRequest($path)
59 if (strpos($path, '..') !== false) {
64 if (substr($path, 0, 14) == 'internetradio/') {
65 require_once 'mediatomb.php';
66 handleRequestMediatomb($path, 'internetradio/');
71 $fullPath = $varDir . $path;
72 if (!file_exists($fullPath)) {
73 sendMessage('Not found: ' . $path);
77 $ext = pathinfo($path, PATHINFO_EXTENSION);
78 if (is_dir($fullPath)) {
80 } else if ($ext == 'url') {
81 require_once 'podcasts.php';
83 } else if ($ext == 'txt') {
85 } else if ($ext == 'sh') {
88 sendMessage('Unknown file type');
92 function pathEncode($urlPath)
94 return str_replace('%2F', '/', rawurlencode($urlPath));
97 function sendDir($path)
101 $listItems = array();
102 addPreviousItem($listItems, $path);
104 $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
106 foreach ($entries as $entry) {
107 $urlPath = pathEncode(substr($entry, strlen($varDir)));
108 $ext = pathinfo($entry, PATHINFO_EXTENSION);
110 $titleBase = basename($entry);
111 $titleBase = preg_replace('#^[0-9]+_#', '', $titleBase);
112 if (is_dir($entry)) {
114 $listItems[] = getDirItem($titleBase, $urlPath . '/');
115 } else if ($ext == 'url') {
118 $listItems[] = getPodcastItem(basename($titleBase, '.url'), $urlPath);
119 } else if (substr($entry, -8) == '.auto.sh') {
120 //automatically execute script while listing this directory
121 addScriptOutput($listItems, $entry);
122 } else if ($ext == 'txt' || $ext == 'sh') {
125 $listItems[] = getDirItem(basename($titleBase, '.' . $ext), $urlPath);
129 $listItems[] = getMessageItem('No files or folders');
131 sendListItems($listItems);
134 function sendScript($path)
138 $listItems = array();
139 addPreviousItem($listItems, $path);
141 $fullPath = $varDir . $path;
142 addScriptOutput($listItems, $fullPath);
143 sendListItems($listItems);
146 function addScriptOutput(&$listItems, $fullPath)
148 exec($fullPath . ' 2>&1', $output, $retVal);
151 addTextLines($listItems, $output);
153 $listItems[] = getMessageItem('Error executing script');
154 addTextLines($listItems, $output);
158 function sendTextFile($path)
161 $listItems = array();
162 addPreviousItem($listItems, $path);
164 $lines = file($varDir . $path);
165 addTextLines($listItems, $lines);
166 sendListItems($listItems);
169 function addTextLines(&$listItems, $lines)
171 foreach ($lines as $line) {
174 $listItems[] = getDisplayItem($line);
179 function getDisplayItem($line)
181 $line = preg_replace('#\s+#', ' ', $line);
183 . '<ItemType>Display</ItemType>'
184 . '<Display>' . utf8_decode(htmlspecialchars($line)) . '</Display>'
188 function getDirItem($title, $urlPath)
190 global $host1, $host2;
192 . '<ItemType>Dir</ItemType>'
193 . '<Title>' . utf8_decode(htmlspecialchars($title)) . '</Title>'
194 . '<UrlDir>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDir>'
195 . '<UrlDirBackUp>' . $host2 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDirBackUp>'
199 function getEpisodeItem($title, $fullUrl, $desc, $type)
202 . '<ItemType>ShowEpisode</ItemType>'
203 . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
204 . '<ShowEpisodeURL>' . $fullUrl . '</ShowEpisodeURL>'
205 . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
206 . '<ShowMime>' . $type . '</ShowMime>'
210 function getPodcastItem($title, $urlPath)
214 . '<ItemType>ShowOnDemand</ItemType>'
215 . '<ShowOnDemandName>' . utf8_decode(htmlspecialchars($title)) . '</ShowOnDemandName>'
216 . '<ShowOnDemandURL>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</ShowOnDemandURL>'
220 function getMessageItem($msg)
223 . '<ItemType>Message</ItemType>'
224 . '<Message>' . utf8_decode(htmlspecialchars($msg)) . '</Message>'
228 function getPreviousItem($urlPath)
230 global $host1, $host2;
232 . '<ItemType>Previous</ItemType>'
233 . '<UrlPrevious>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPrevious>'
234 . '<UrlPreviousBackUp>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPreviousBackUp>'
238 function addPreviousItem(&$listItems, $urlPath)
240 $parentDir = dirname($urlPath) . '/';
241 if ($parentDir == '/') {
244 $listItems[] = getPreviousItem($parentDir);
247 function getFinalUrl($url)
249 $ctx = stream_context_set_default(
250 array('http' => array('method' => 'HEAD'))
252 //get_headers follows redirects automatically
253 $headers = get_headers($url, 1);
254 if ($headers !== false && isset($headers['Location'])) {
255 return end($headers['Location']);
260 function sendMessage($msg)
262 sendListItems(array(getMessageItem($msg)));
265 function sendListItems($listItems)
269 if (isset($_GET['startitems'])) {
270 $startitems = (int) $_GET['startitems'];
272 if (isset($_GET['enditems'])) {
273 $enditems = (int) $_GET['enditems'];
277 $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
278 $xml .= '<?xml-stylesheet type="text/xsl" href="/html.xsl"?>' . "\n";
279 $xml .= '<ListOfItems>' . "\n";
280 foreach ($listItems as $item) {
281 $xml .= $item . "\n";
283 $xml .= "</ListOfItems>\n";
285 header('Content-type: text/xml');