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