show mediatomb server contents in internetradio
[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     //initial login for "internet radio" and podcasts
24     //lowercase tags
25     header('Content-type: text/html');
26     readfile($dataDir . 'initial-login.xml');
27     exit();
28 } else if ($fullUri == '/RadioNativeLogin.php') {
29     //initial login for "My noxon"
30     //this one wants CamelCased tags
31     header('Content-type: text/html');
32     readfile($dataDir . 'login-mynoxon.xml');
33     exit();
34 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/LoginXML.asp') {
35     //"Internet Radio"
36     $path = '/internetradio/';
37 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/navXML.asp') {
38     //"Podcasts"
39     $path = '/podcasts/';
40 } else if ($path == '/RadioNative.php') {
41     //"My Noxon"
42     $path = '/mynoxon/';
43 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
44     //Internet Radio Station favorites favorited on device
45     sendMessage('Unsupported');
46 } else if ($path == '/RadioNativeFavorites.php') {
47     //Favorites, defined via web interface
48     sendMessage('Unsupported');
49 } else if (substr($path, 0, 9) == '/play-url') {
50     //play a given URL, but first follow all redirects
51     //noxon iRadio Cube does not like too many redirections
52     // 3 redirects did not work.
53     $url = $_GET['url'];
54     header('HTTP/1.0 301 Moved Permanently');
55     header('Location: ' . getFinalUrl($url));
56     exit();
57 }
58
59 handleRequest(ltrim($path, '/'));
60
61 function handleRequest($path)
62 {
63     global $varDir;
64     if (strpos($path, '..') !== false) {
65         sendMessage('No');
66         return;
67     }
68
69     if (substr($path, 0, 14) == 'internetradio/') {
70         require_once 'mediatomb.php';
71         handleRequestMediatomb($path, 'internetradio/');
72         return;
73     }
74
75
76     $fullPath = $varDir . $path;
77     if (!file_exists($fullPath)) {
78         sendMessage('Not found: ' . $path);
79         return;
80     }
81
82     if (is_dir($fullPath)) {
83         sendDir($path);
84     } else if (substr($path, -4) == '.url') {
85         require_once 'podcasts.php';
86         sendPodcast($path);
87     } else if (substr($path, -4) == '.txt') {
88         sendTextFile($path);
89     } else {
90         sendMessage('Unknown file type');
91     }
92 }
93
94 function sendDir($path)
95 {
96     global $varDir;
97
98     $listItems = array();
99     addPreviousItem($listItems, $path);
100
101     $entries = glob(str_replace('//', '/', $varDir . rtrim($path, '/') . '/*'));
102     $count = 0;
103     foreach ($entries as $entry) {
104         $urlPath = substr($entry, strlen($varDir));
105         if (is_dir($entry)) {
106             ++$count;
107             $listItems[] = getDirItem(basename($entry), $urlPath . '/');
108         } else if (substr($entry, -4) == '.url') {
109             //podcast
110             ++$count;
111             $listItems[] = getPodcastItem(basename($entry, '.url'), $urlPath);
112         } else if (substr($entry, -4) == '.txt') {
113             //plain text file
114             ++$count;
115             $listItems[] = getDirItem(basename($entry, '.txt'), $urlPath);
116         }
117     }
118     if (!$count) {
119         $listItems[] = getMessageItem('No files or folders');
120     }
121     sendListItems($listItems);
122 }
123
124 function sendTextFile($path)
125 {
126     global $varDir;
127     $listItems = array();
128     addPreviousItem($listItems, $path);
129
130     $lines = file($varDir . $path);
131     foreach ($lines as $line) {
132         $listItems[] = getDisplayItem($line);
133     }
134     sendListItems($listItems);
135 }
136
137 function getDisplayItem($line)
138 {
139     return '<Item>'
140         . '<ItemType>Display</ItemType>'
141         . '<Display>' . utf8_decode(htmlspecialchars(trim($line))) . '</Display>'
142         . '</Item>';
143 }
144
145 function getDirItem($title, $urlPath)
146 {
147     global $host1, $host2;
148     return '<Item>'
149         . '<ItemType>Dir</ItemType>'
150         . '<Title>' . utf8_decode(htmlspecialchars($title)) . '</Title>'
151         . '<UrlDir>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDir>'
152         . '<UrlDirBackUp>' . $host2 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlDirBackUp>'
153         . '</Item>';
154 }
155
156 function getEpisodeItem($title, $fullUrl, $desc, $type)
157 {
158     return '<Item>'
159         . '<ItemType>ShowEpisode</ItemType>'
160         . '<ShowEpisodeName>' . utf8_decode(htmlspecialchars($title)) . '</ShowEpisodeName>'
161         . '<ShowEpisodeURL>' . $fullUrl . '</ShowEpisodeURL>'
162         . '<ShowDesc>' . utf8_decode(htmlspecialchars($desc)) . '</ShowDesc>'
163         . '<ShowMime>' . $type . '</ShowMime>'
164         . '</Item>';
165 }
166
167 function getPodcastItem($title, $urlPath)
168 {
169     global $host1;
170     return '<Item>'
171         . '<ItemType>ShowOnDemand</ItemType>'
172         . '<ShowOnDemandName>' . utf8_decode(htmlspecialchars($title)) . '</ShowOnDemandName>'
173         . '<ShowOnDemandURL>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</ShowOnDemandURL>'
174         . '</Item>';
175 }
176
177 function getMessageItem($msg)
178 {
179     return '<Item>'
180         . '<ItemType>Message</ItemType>'
181         . '<Message>' . utf8_decode(htmlspecialchars($msg)) . '</Message>'
182         . '</Item>';
183 }
184
185 function getPreviousItem($urlPath)
186 {
187     global $host1, $host2;
188     return '<Item>'
189         . '<ItemType>Previous</ItemType>'
190         . '<UrlPrevious>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPrevious>'
191         . '<UrlPreviousBackUp>' . $host1 . utf8_decode(htmlspecialchars($urlPath)) . '</UrlPreviousBackUp>'
192         . '</Item>';
193 }
194
195 function addPreviousItem(&$listItems, $urlPath)
196 {
197     $parentDir = dirname($urlPath) . '/';
198     if ($parentDir == '/') {
199         return;
200     }
201     $listItems[] = getPreviousItem($parentDir);
202 }
203
204 function getFinalUrl($url)
205 {
206     $ctx = stream_context_set_default(
207         array('http' => array('method' => 'HEAD'))
208     );
209     //get_headers follows redirects automatically
210     $headers = get_headers($url, 1);
211     if ($headers !== false && isset($headers['Location'])) {
212         return end($headers['Location']);
213     }
214     return $url;
215 }
216
217 function sendMessage($msg)
218 {
219     sendListItems(array(getMessageItem($msg)));
220 }
221
222 function sendListItems($listItems)
223 {
224     $startitems = 1;
225     $enditems = 10;
226     if (isset($_GET['startitems'])) {
227         $startitems = (int) $_GET['startitems'];
228     }
229     if (isset($_GET['enditems'])) {
230         $enditems = (int) $_GET['enditems'];
231     }
232     //TODO: limit list
233
234     $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
235     $xml .= '<?xml-stylesheet type="text/xsl" href="/html.xsl"?>' . "\n";
236     $xml .= '<ListOfItems>' . "\n";
237     foreach ($listItems as $item) {
238         $xml .= $item . "\n";
239     }
240     $xml .= "</ListOfItems>\n";
241
242     header('Content-type: text/xml');
243     echo $xml;
244 }
245 ?>