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