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 //initial login for "internet radio" and podcasts
25 header('Content-type: text/html');
26 readfile($dataDir . 'initial-login.xml');
28 } else if ($fullUri == '/RadioNativeLogin.php') {
29 //initial login for "My noxon"
30 //this one wants CamelCased tags
31 header('Content-type: text/html');
32 readfile($dataDir . 'login-mynoxon.xml');
34 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/LoginXML.asp') {
36 $path = '/internetradio/';
37 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/navXML.asp') {
40 } else if ($path == '/RadioNative.php') {
43 $path = '/internetradio/';
44 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
45 //Internet Radio Station favorites favorited on device
46 sendMessage('Unsupported');
47 } else if ($path == '/RadioNativeFavorites.php') {
48 //Favorites, defined via web interface
49 sendMessage('Unsupported');
50 } else if (substr($path, 0, 9) == '/play-url') {
51 //play a given URL, but first follow all redirects
52 //noxon iRadio Cube does not like too many redirections
53 // 3 redirects did not work.
55 header('HTTP/1.0 301 Moved Permanently');
56 header('Location: ' . getFinalUrl($url));
60 handleRequest(ltrim($path, '/'));
62 function handleRequest($path)
65 if (strpos($path, '..') !== false) {
70 if (substr($path, 0, 14) == 'internetradio/') {
71 require_once 'mediatomb.php';
72 handleRequestMediatomb($path, 'internetradio/');
77 $fullPath = $varDir . $path;
78 if (!file_exists($fullPath)) {
79 sendMessage('Not found: ' . $path);
83 $ext = pathinfo($path, PATHINFO_EXTENSION);
84 if (is_dir($fullPath)) {
86 } else if ($ext == 'url') {
87 require_once 'podcasts.php';
89 } else if ($ext == 'txt') {
91 } else if ($ext == 'sh') {
94 sendMessage('Unknown file type');
98 function pathEncode($urlPath)
100 return str_replace('%2F', '/', rawurlencode($urlPath));
103 function sendDir($path)
107 $listItems = array();
108 addPreviousItem($listItems, $path);
110 $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
112 foreach ($entries as $entry) {
113 $urlPath = pathEncode(substr($entry, strlen($varDir)));
114 $ext = pathinfo($entry, PATHINFO_EXTENSION);
115 if (is_dir($entry)) {
117 $listItems[] = getDirItem(basename($entry), $urlPath . '/');
118 } else if ($ext == 'url') {
121 $listItems[] = getPodcastItem(basename($entry, '.url'), $urlPath);
122 } else if (substr($entry, -8) == '.auto.sh') {
123 //automatically execute script while listing this directory
124 addScriptOutput($listItems, $entry);
125 } else if ($ext == 'txt' || $ext == 'sh') {
128 $listItems[] = getDirItem(basename($entry, '.' . $ext), $urlPath);
132 $listItems[] = getMessageItem('No files or folders');
134 sendListItems($listItems);
137 function sendScript($path)
141 $listItems = array();
142 addPreviousItem($listItems, $path);
144 $fullPath = $varDir . $path;
145 addScriptOutput($listItems, $fullPath);
146 sendListItems($listItems);
149 function addScriptOutput(&$listItems, $fullPath)
151 exec($fullPath . ' 2>&1', $output, $retVal);
154 addTextLines($listItems, $output);
156 $listItems[] = getMessageItem('Error executing script');
157 addTextLines($listItems, $output);
161 function sendTextFile($path)
164 $listItems = array();
165 addPreviousItem($listItems, $path);
167 $lines = file($varDir . $path);
168 addTextLines($listItems, $lines);
169 sendListItems($listItems);
172 function addTextLines(&$listItems, $lines)
174 foreach ($lines as $line) {
177 $listItems[] = getDisplayItem($line);
182 function getDisplayItem($line)
184 $line = preg_replace('#\s+#', ' ', $line);
186 . '<ItemType>Display</ItemType>'
187 . '<Display>' . utf8_decode(htmlspecialchars($line)) . '</Display>'
191 function getDirItem($title, $urlPath)
193 global $host1, $host2;
195 . '<ItemType>Dir</ItemType>'
196 . '<Title>' . utf8_decode(htmlspecialchars($title)) . '</Title>'
197 . '<UrlDir>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDir>'
198 . '<UrlDirBackUp>' . $host2 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDirBackUp>'
202 function getEpisodeItem($title, $fullUrl, $desc, $type)
205 . '<ItemType>ShowEpisode</ItemType>'
206 . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
207 . '<ShowEpisodeURL>' . $fullUrl . '</ShowEpisodeURL>'
208 . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
209 . '<ShowMime>' . $type . '</ShowMime>'
213 function getPodcastItem($title, $urlPath)
217 . '<ItemType>ShowOnDemand</ItemType>'
218 . '<ShowOnDemandName>' . utf8_decode(htmlspecialchars($title)) . '</ShowOnDemandName>'
219 . '<ShowOnDemandURL>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</ShowOnDemandURL>'
223 function getMessageItem($msg)
226 . '<ItemType>Message</ItemType>'
227 . '<Message>' . utf8_decode(htmlspecialchars($msg)) . '</Message>'
231 function getPreviousItem($urlPath)
233 global $host1, $host2;
235 . '<ItemType>Previous</ItemType>'
236 . '<UrlPrevious>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPrevious>'
237 . '<UrlPreviousBackUp>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPreviousBackUp>'
241 function addPreviousItem(&$listItems, $urlPath)
243 $parentDir = dirname($urlPath) . '/';
244 if ($parentDir == '/') {
247 $listItems[] = getPreviousItem($parentDir);
250 function getFinalUrl($url)
252 $ctx = stream_context_set_default(
253 array('http' => array('method' => 'HEAD'))
255 //get_headers follows redirects automatically
256 $headers = get_headers($url, 1);
257 if ($headers !== false && isset($headers['Location'])) {
258 return end($headers['Location']);
263 function sendMessage($msg)
265 sendListItems(array(getMessageItem($msg)));
268 function sendListItems($listItems)
272 if (isset($_GET['startitems'])) {
273 $startitems = (int) $_GET['startitems'];
275 if (isset($_GET['enditems'])) {
276 $enditems = (int) $_GET['enditems'];
280 $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
281 $xml .= '<?xml-stylesheet type="text/xsl" href="/html.xsl"?>' . "\n";
282 $xml .= '<ListOfItems>' . "\n";
283 foreach ($listItems as $item) {
284 $xml .= $item . "\n";
286 $xml .= "</ListOfItems>\n";
288 header('Content-type: text/xml');