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