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