1670931256fb90df76956f3b46b181dae5b9503c
[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 (is_executable($fullPath)) {
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 (is_executable($entry)
98             && strpos(basename($entry), '.auto') !== false
99         ) {
100             //automatically execute script while listing this directory
101             addScriptOutput($listItems, $entry);
102         } else if ($ext == 'txt' || is_executable($entry)) {
103             //plain text file
104             ++$count;
105             $listItems[] = getDirItem(basename($titleBase, '.' . $ext), $urlPath);
106         }
107     }
108     if (!$count) {
109         $listItems[] = getMessageItem('No files or folders');
110     }
111     sendListItems($listItems);
112 }
113
114 function sendScript($path)
115 {
116     global $varDir;
117
118     $listItems = array();
119     addPreviousItem($listItems, $path);
120
121     $fullPath = $varDir . $path;
122     addScriptOutput($listItems, $fullPath);
123     sendListItems($listItems);
124 }
125
126 function addScriptOutput(&$listItems, $fullPath)
127 {
128     exec($fullPath . ' 2>&1', $output, $retVal);
129
130     if ($retVal == 0) {
131         addTextLines($listItems, $output);
132     } else {
133         $listItems[] = getMessageItem('Error executing script');
134         addTextLines($listItems, $output);
135     }
136 }
137
138 function sendTextFile($path)
139 {
140     global $varDir;
141     $listItems = array();
142     addPreviousItem($listItems, $path);
143
144     $lines = file($varDir . $path);
145     addTextLines($listItems, $lines);
146     sendListItems($listItems);
147 }
148
149 function addTextLines(&$listItems, $lines)
150 {
151     foreach ($lines as $line) {
152         $line = trim($line);
153         if ($line != '') {
154             $listItems[] = getDisplayItem($line);
155         }
156     }
157 }
158
159 function getDisplayItem($line)
160 {
161     $line = preg_replace('#\s+#', ' ', $line);
162     return '<Item>'
163         . '<ItemType>Display</ItemType>'
164         . '<Display>' . utf8_decode(htmlspecialchars($line)) . '</Display>'
165         . '</Item>';
166 }
167
168 function getDirItem($title, $urlPath)
169 {
170     global $host1, $host2;
171     return '<Item>'
172         . '<ItemType>Dir</ItemType>'
173         . '<Title>' . utf8_decode(htmlspecialchars($title)) . '</Title>'
174         . '<UrlDir>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDir>'
175         . '<UrlDirBackUp>' . $host2 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDirBackUp>'
176         . '</Item>';
177 }
178
179 function getEpisodeItem($title, $fullUrl, $desc, $type)
180 {
181     return '<Item>'
182         . '<ItemType>ShowEpisode</ItemType>'
183         . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
184         . '<ShowEpisodeURL>' . htmlspecialchars($fullUrl) . '</ShowEpisodeURL>'
185         . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
186         . '<ShowMime>' . $type . '</ShowMime>'
187         . '</Item>';
188 }
189
190 function getPodcastItem($title, $urlPath)
191 {
192     global $host1;
193     return '<Item>'
194         . '<ItemType>ShowOnDemand</ItemType>'
195         . '<ShowOnDemandName>' . utf8_decode(htmlspecialchars($title)) . '</ShowOnDemandName>'
196         . '<ShowOnDemandURL>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</ShowOnDemandURL>'
197         . '</Item>';
198 }
199
200 function getMessageItem($msg)
201 {
202     return '<Item>'
203         . '<ItemType>Message</ItemType>'
204         . '<Message>' . utf8_decode(htmlspecialchars($msg)) . '</Message>'
205         . '</Item>';
206 }
207
208 function getPreviousItem($urlPath)
209 {
210     global $host1, $host2;
211     return '<Item>'
212         . '<ItemType>Previous</ItemType>'
213         . '<UrlPrevious>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPrevious>'
214         . '<UrlPreviousBackUp>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPreviousBackUp>'
215         . '</Item>';
216 }
217
218 function addPreviousItem(&$listItems, $urlPath)
219 {
220     $parentDir = dirname($urlPath) . '/';
221     if ($parentDir == '/') {
222         return;
223     }
224     $listItems[] = getPreviousItem($parentDir);
225 }
226
227 function sendMessage($msg)
228 {
229     sendListItems(array(getMessageItem($msg)));
230 }
231
232 function sendListItems($listItems)
233 {
234     $startitems = 1;
235     $enditems = 10;
236     if (isset($_GET['startitems'])) {
237         $startitems = (int) $_GET['startitems'];
238     }
239     if (isset($_GET['enditems'])) {
240         $enditems = (int) $_GET['enditems'];
241     }
242     //TODO: limit list
243
244     $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
245     $xml .= '<?xml-stylesheet type="text/xsl" href="/html.xsl"?>' . "\n";
246     $xml .= '<ListOfItems>' . "\n";
247     foreach ($listItems as $item) {
248         $xml .= $item . "\n";
249     }
250     $xml .= "</ListOfItems>\n";
251
252     header('Content-type: text/xml; charset=iso-8859-1');
253     echo $xml;
254 }
255 ?>