update to current services_mediatomb api
[enigtomb.git] / DreamboxBouqets.php
1 <?php
2 /**
3 * Fetches bouqet information from the dreambox
4 *
5 * @author Christian Weiske <cweiske@cweiske.de>
6 * @license  LGPL http://www.gnu.org/copyleft/lesser.html
7 */
8 class DreamboxBouqets
9 {
10     /**
11     * Fetch radio bouqets
12     *
13     * @param string  $ip       IP-address or hostname of the dreambox
14     * @param boolean $bEpgData If EPG data shall be fetched, too
15     *
16     * @return array Array of bouqets and the items
17     */
18     public static function fetchRadioBouqets($ip, $bEpgData = true)
19     {
20         return self::fetchBouqets(
21             $ip,
22             //TODO: fetch from dreambox config.js
23             '1:7:2:0:0:0:0:0:0:0:(type == 2)FROM BOUQUET "bouquets.radio" ORDER BY bouquet',
24             $bEpgData
25         );
26     }//public static function fetchRadioBouqets(..)
27
28
29
30     /**
31     * Fetch TV bouqets
32     *
33     * @param string  $ip       IP-address or hostname of the dreambox
34     * @param boolean $bEpgData If EPG data shall be fetched, too
35     *
36     * @return array Array of bouqets and the items
37     */
38     public static function fetchTvBouqets($ip, $bEpgData = true)
39     {
40         return self::fetchBouqets(
41             $ip,
42             //TODO: fetch from dreambox config.js
43             '1:7:2:0:0:0:0:0:0:0:(type == 2)FROM BOUQUET "bouquets.tv" ORDER BY bouquet',
44             $bEpgData
45         );
46     }//public static function fetchTvBouqets(..)
47
48
49
50     /**
51     * Fetch bouqets
52     *
53     * @param string  $ip       IP-address or hostname of the dreambox
54     * @param string  $sql      SQL query to fetch bouqet from database
55     * @param boolean $bEpgData If EPG data shall be fetched, too
56     *
57     * @return array Array of bouqets (name is key) and the items (sub arrays)
58     */
59     public static function fetchBouqets($ip, $sql, $bEpgData = true)
60     {
61         $host       = 'http://' . $ip;
62         $streamhost = 'http://' . $ip . ':8001';
63         $url_getServices     = $host . "/web/getservices?sRef=";
64         $url_stream_playlist = $host . '/web/stream.m3u?ref=';
65         $url_bouqet_epg      = $host . '/web/epgnow?bRef=';
66         $url_stream          = $streamhost . '/';
67
68
69         $url = $url_getServices . urlencode($sql);
70         $radiobouqets = new SimpleXMLElement(
71             file_get_contents($url)
72         );
73
74         $arBouqets = array();
75         foreach ($radiobouqets->e2service as $bouqet) {
76             $strBouqetName = self::properBouqetName($bouqet->e2servicename);
77             $arBouqets[$strBouqetName] = array();
78
79             $strRef = $bouqet->e2servicereference;
80             $url    = $url_getServices . urlencode($strRef);
81             $epgurl = $url_bouqet_epg  . urlencode($strRef);
82
83             $stations = new SimpleXMLElement(
84                 file_get_contents(
85                     $url
86                 )
87             );
88
89             foreach ($stations->e2service as $station) {
90                 $strStationRef = (string)$station->e2servicereference;
91                 $arBouqets[$strBouqetName][$strStationRef] = array(
92                     'name'     => (string)$station->e2servicename,
93                     'playlist' => $url_stream_playlist . self::encodeUrl($strStationRef),
94                     'stream'   => $url_stream . self::encodeUrl($strStationRef),
95                 );
96             }
97
98             if ($bEpgData) {
99                 $epgs = new SimpleXMLElement(
100                     file_get_contents($epgurl)
101                 );
102                 foreach ($epgs->e2event as $epg) {
103                     $strStationRef = (string)$epg->e2eventservicereference;
104                     $arBouqets[$strBouqetName][$strStationRef] = array_merge(
105                         $arBouqets[$strBouqetName][$strStationRef],
106                         array(
107                             'epgtitle'        => (string)$epg->e2eventtitle,
108                             'epgdescription'  => (string)$epg->e2eventdescription,
109                             'epgdescription2' => (string)$epg->e2eventdescriptionextendet,
110                         )
111                     );
112                 }
113             }//if $bEpgData
114         }
115
116         return $arBouqets;
117     }//public static function fetchRadioBouqets(..)
118
119
120
121     /**
122     * Fix the given bouqet name, make it a nice readable one.
123     * Removes some special chars and spaces.
124     *
125     * @param string $name Bouqet name
126     *
127     * @return string Fixed bouqet name
128     */
129     protected static function properBouqetName($name)
130     {
131         return trim(preg_replace('#\(.+\)$#', '', $name));
132     }//protected static function properBouqetName(..)
133
134
135
136     protected static function encodeUrl($strUrl)
137     {
138         return str_replace(
139             array(' ',   '"'),
140             array('%20', '%22'),
141             $strUrl
142         );
143     }//protected static function encodeUrl(..)
144 }
145
146 ?>