Add podcast proxy support for Martin
[noxon-gateway.git] / www / index.php
1 <?php
2 require_once __DIR__ . '/../src/header.php';
3
4 $radioPodcastLogin = isset($_GET['token']) && $_GET['token'] == '0'
5     && strtolower($path) == '/setupapp/radio567/asp/browsexpa/loginxml.asp';
6 $myNoxonLogin = $path == '/RadioNativeLogin.php';
7
8 if ($radioPodcastLogin || $myNoxonLogin) {
9     //initial login for "internet radio", podcasts and "my noxon"
10     header('Content-type: text/html');
11     readfile($dataDir . 'login-camelcase.xml');
12     exit();
13 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/LoginXML.asp') {
14     //"Internet Radio"
15     $path = '/internetradio/';
16 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/navXML.asp') {
17     //"Podcasts"
18     $path = '/podcasts/';
19 } else if ($path == '/RadioNative.php') {
20     //"My Noxon"
21     $path = '/mynoxon/';
22 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
23     //Internet Radio Station favorites favorited on device
24     sendMessage('Unsupported');
25 } else if ($path == '/RadioNativeFavorites.php') {
26     //Favorites, defined via web interface
27     sendMessage('Unsupported');
28 }
29
30 handleRequest(ltrim($path, '/'));
31
32 function handleRequest($path)
33 {
34     global $varDir;
35     if (strpos($path, '/../') !== false) {
36         sendMessage('No');
37         return;
38     }
39
40     if (substr($path, 0, 14) == 'internetradio/') {
41         require_once 'mediatomb.php';
42         handleMediatomb('browse', $path, 'internetradio/');
43         return;
44     } else if (substr($path, 0, 11) == '.mt-single/') {
45         require_once 'mediatomb.php';
46         handleMediatomb('single', $path, '.mt-single/');
47         return;
48     }
49
50
51     $fullPath = $varDir . $path;
52     if (!file_exists($fullPath)) {
53         sendMessage('Not found: ' . $path);
54         return;
55     }
56
57     $ext = pathinfo($path, PATHINFO_EXTENSION);
58     if (is_dir($fullPath)) {
59         sendDir($path);
60     } else if ($ext == 'url') {
61         require_once 'podcasts.php';
62         sendPodcast($path);
63     } else if ($ext == 'txt') {
64         sendTextFile($path);
65     } else if (is_executable($fullPath)) {
66         sendScript($path);
67     } else {
68         sendMessage('Unknown file type');
69     }
70 }
71
72 function pathEncode($urlPath)
73 {
74     return str_replace('%2F', '/', rawurlencode($urlPath));
75 }
76
77 function sendDir($path)
78 {
79     global $varDir;
80
81     $listItems = array();
82     $enablePaging = true;
83
84     $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
85     $count = 0;
86     $noCache = false;
87     foreach ($entries as $entry) {
88         $urlPath = pathEncode(substr($entry, strlen($varDir)));
89         $ext = pathinfo($entry, PATHINFO_EXTENSION);
90
91         $titleBase = basename($entry);
92         $titleBase = preg_replace('#^[0-9]+_#', '', $titleBase);
93         if (is_dir($entry)) {
94             ++$count;
95             $listItems[] = getDirItem($titleBase, $urlPath . '/');
96         } else if ($ext == 'url') {
97             //podcast
98             ++$count;
99             $listItems[] = getPodcastItem(basename($titleBase, '.url'), $urlPath);
100         } else if (is_executable($entry)
101             && strpos(basename($entry), '.auto') !== false
102         ) {
103             //automatically execute script while listing this directory
104             addScriptOutput($listItems, $entry);
105             $enablePaging = false;
106         } else if ($ext == 'txt' || is_executable($entry)) {
107             //plain text file
108             ++$count;
109             $listItems[] = getDirItem(basename($titleBase, '.' . $ext), $urlPath);
110         } else  if (basename($entry) == 'nocache') {
111             $noCache = true;
112         }
113     }
114     if (!$count) {
115         $listItems[] = getMessageItem('No files or folders');
116     }
117     sendListItems(
118         $listItems, buildPreviousItem($path),
119         $enablePaging, $noCache
120     );
121 }
122
123 function sendScript($path)
124 {
125     global $varDir;
126
127     $listItems = array();
128
129     $fullPath = $varDir . $path;
130     addScriptOutput($listItems, $fullPath);
131     sendListItems($listItems, buildPreviousItem($path), false);
132 }
133
134 function addScriptOutput(&$listItems, $fullPath)
135 {
136     exec($fullPath . ' 2>&1', $output, $retVal);
137
138     if ($retVal == 0) {
139         addTextLines($listItems, $output);
140     } else {
141         $listItems[] = getMessageItem('Error executing script');
142         addTextLines($listItems, $output);
143     }
144 }
145
146 function sendTextFile($path)
147 {
148     global $varDir;
149     $listItems = array();
150
151     $lines = file($varDir . $path);
152     addTextLines($listItems, $lines);
153     sendListItems($listItems, buildPreviousItem($path));
154 }
155
156 function addTextLines(&$listItems, $lines)
157 {
158     foreach ($lines as $line) {
159         $line = trim($line);
160         if ($line != '') {
161             $listItems[] = getDisplayItem($line);
162         }
163     }
164 }
165
166 function getDisplayItem($line)
167 {
168     $line = preg_replace('#\s+#', ' ', $line);
169     return '<Item>'
170         . '<ItemType>Display</ItemType>'
171         . '<Display>' . nox_esc($line) . '</Display>'
172         . '</Item>';
173 }
174
175 function getDirItem($title, $urlPath)
176 {
177     global $host1, $host2;
178     return '<Item>'
179         . '<ItemType>Dir</ItemType>'
180         . '<Title>' . nox_esc($title) . '</Title>'
181         . '<UrlDir>' . $host1 . nox_esc($urlPath) . '</UrlDir>'
182         . '<UrlDirBackUp>' . $host2 . nox_esc($urlPath) . '</UrlDirBackUp>'
183         . '</Item>';
184 }
185
186 function getEpisodeItem($title, $fullUrl, $desc, $type)
187 {
188     return '<Item>'
189         . '<ItemType>ShowEpisode</ItemType>'
190         . '<ShowEpisodeName>' . nox_esc($title) . '</ShowEpisodeName>'
191         . '<ShowEpisodeURL>' . htmlspecialchars($fullUrl) . '</ShowEpisodeURL>'
192         . '<ShowDesc>' . nox_esc($desc) . '</ShowDesc>'
193         . '<ShowMime>' . $type . '</ShowMime>'
194         . '</Item>';
195 }
196
197 function getPodcastItem($title, $urlPath)
198 {
199     global $host1;
200     return '<Item>'
201         . '<ItemType>ShowOnDemand</ItemType>'
202         . '<ShowOnDemandName>' . nox_esc($title) . '</ShowOnDemandName>'
203         . '<ShowOnDemandURL>' . $host1 . nox_esc($urlPath) . '</ShowOnDemandURL>'
204         . '</Item>';
205 }
206
207 function getMessageItem($msg)
208 {
209     return '<Item>'
210         . '<ItemType>Message</ItemType>'
211         . '<Message>' . nox_esc($msg) . '</Message>'
212         . '</Item>';
213 }
214
215 function getPreviousItem($urlPath)
216 {
217     global $host1, $host2;
218     return '<Item>'
219         . '<ItemType>Previous</ItemType>'
220         . '<UrlPrevious>' . $host1 . nox_esc($urlPath) . '</UrlPrevious>'
221         . '<UrlPreviousBackUp>' . $host1 . nox_esc($urlPath) . '</UrlPreviousBackUp>'
222         . '</Item>';
223 }
224
225 function buildPreviousItem($urlPath)
226 {
227     $parentDir = dirname($urlPath) . '/';
228     if ($parentDir == '/') {
229         return null;
230     }
231     return getPreviousItem($parentDir);
232 }
233
234 function nox_esc($string)
235 {
236     return utf8_decode(htmlspecialchars($string));
237 }
238
239 function sendMessage($msg)
240 {
241     sendListItems(array(getMessageItem($msg)));
242 }
243
244 function sendListItems(
245     $listItems, $previous = null, $enablePaging = true, $noCache = false
246 ) {
247     $startitems = 1;
248     $enditems   = 100000;
249     if (isset($_GET['startitems'])) {
250         $startitems = (int) $_GET['startitems'];
251     }
252     if (isset($_GET['enditems'])) {
253         $enditems = (int) $_GET['enditems'];
254     }
255
256     if ($enablePaging) {
257         $itemCount = count($listItems);
258     } else {
259         $itemCount = -1;
260     }
261     if ($previous !== null) {
262         $previous .= "\n";
263     }
264
265     $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
266     $xml .= '<?xml-stylesheet type="text/xsl" href="/html.xsl"?>' . "\n";
267     $xml .= '<ListOfItems>' . "\n";
268     if ($noCache) {
269         $xml .= "<NoCache>1</NoCache>\n";
270     }
271     $xml .= '<ItemCount>' . $itemCount . '</ItemCount>' . "\n";
272     $xml .= $previous;
273
274     $num = 0;
275     foreach ($listItems as $item) {
276         ++$num;
277         if (!$enablePaging || ($num >= $startitems && $num <= $enditems)) {
278             $xml .= $item . "\n";
279         }
280     }
281     $xml .= "</ListOfItems>\n";
282
283     header('Content-type: text/xml; charset=iso-8859-1');
284     echo $xml;
285 }
286 ?>