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