update readme
[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 $path    = $_SERVER['REDIRECT_URL'];
5 $dataDir = __DIR__ . '/../data/';
6
7 if (strtolower($fullUri) == '/setupapp/radio567/asp/browsexpa/loginxml.asp?token=0') {
8     //initial login for "internet radio" and podcasts
9     //lowercase tags
10     header('Content-type: text/html');
11     readfile($dataDir . 'initial-login.xml');
12     exit();
13 } else if ($fullUri == '/RadioNativeLogin.php') {
14     //initial login for "My noxon"
15     //this one wants CamelCased tags
16     header('Content-type: text/html');
17     readfile($dataDir . 'login-mynoxon.xml');
18     exit();
19 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/LoginXML.asp') {
20     //"Internet Radio"
21     header('Content-type: text/xml');
22     sendList('internetradio');
23     exit();
24 } else if ($path == '/setupapp/radio567/asp/BrowseXPA/navXML.asp') {
25     //"Podcasts"
26     require_once 'podcasts.php';
27     sendPodcastList();
28     exit();
29 } else if (substr($path, 0, 9) == '/podcasts') {
30     require_once 'podcasts.php';
31     sendPodcast($path);
32     exit();    
33 } else if ($path == '/RadioNative.php') {
34     //"My Noxon"
35     header('Content-type: text/xml');
36     sendList('mynoxon');
37     exit();
38 } else if ($path == '/setupapp/radio567/asp/BrowseXML/FavXML.asp') {
39     //Internet Radio Station favorites favorited on device
40 } else if ($path == '/RadioNativeFavorites.php') {
41     //Favorites, defined via web interface
42 } else if (substr($path, 0, 9) == '/play-url') {
43     //play a given URL, but first follow all redirects
44     //noxon iRadio Cube does not like too many redirections
45     // 3 redirects did not work.
46     $url = $_GET['url'];
47     header('HTTP/1.0 301 Moved Permanently');
48     header('Location: ' . getFinalUrl($url));
49     exit();
50 } else {
51     sendList(ltrim($path, '/'));
52 }
53
54
55 function getFinalUrl($url)
56 {
57     $ctx = stream_context_set_default(
58         array('http' => array('method' => 'HEAD'))
59     );
60     //get_headers follows redirects automatically
61     $headers = get_headers($url, 1);
62     if ($headers !== false && isset($headers['Location'])) {
63         return end($headers['Location']);
64     }
65     return $url;
66 }
67
68
69 function sendList($path)
70 {
71     $startitems = 1;
72     $enditems = 10;
73     if (isset($_GET['startitems'])) {
74         $startitems = (int) $_GET['startitems'];
75     }
76     if (isset($_GET['enditems'])) {
77         $enditems = (int) $_GET['enditems'];
78     }
79
80     header('Content-type: text/xml');
81     echo <<<XML
82 <?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
83 <ListOfItems>
84   <ItemCount>-1</ItemCount>
85   <Item>
86     <ItemType>Message</ItemType>
87     <Message>$path</Message>
88   </Item>
89   <Item>
90     <ItemType>Dir</ItemType>
91     <Title>$path</Title>
92     <UrlDir>http://radio567.vtuner.com/$path</UrlDir>
93     <UrlDirBackUp>http://radio5672.vtuner.com/$path</UrlDirBackUp>
94   </Item>
95 </ListOfItems>
96
97 XML;
98 }
99
100 function sendMessage($msg)
101 {
102     header('Content-type: text/xml');
103     $xMsg = htmlspecialchars($msg);
104     echo <<<XML
105 <?xml version="1.0" encoding="iso-8859-1" standalone="yes" ?>
106 <ListOfItems>
107   <Item>
108     <ItemType>Message</ItemType>
109     <Message>$xMsg</Message>
110   </Item>
111 </ListOfItems>
112
113 XML;
114 }
115
116 function sendListItems($listItems)
117 {
118     $xml = '<?xml version="1.0" encoding="iso-8859-1"?>' . "\n";
119     $xml .= '<ListOfItems>' . "\n";
120     foreach ($listItems as $item) {
121         $xml .= $item . "\n";
122     }
123     $xml .= "</ListOfItems>\n";
124     
125     header('Content-type: text/xml');
126     echo $xml;
127 }
128
129 ?>