Source for file Dreambox.class.php

Documentation is available at Dreambox.class.php

  1. <?php
  2. class Dreambox {
  3.  
  4. /**
  5. * @access private
  6. * @var string
  7. */
  8. private $lIPNummer;
  9.  
  10. /**
  11. * @access private
  12. * @var string
  13. */
  14. private $lBoutiques;
  15.  
  16. /**
  17. * @access private
  18. * @var string
  19. */
  20. private $lType;
  21.  
  22. /**
  23. * @access private
  24. * @var int
  25. */
  26. private $lBoutiqueQueueCounter;
  27.  
  28. /**
  29. * @access private
  30. * @var string
  31. */
  32. private $lCurrentBoutique;
  33.  
  34. /**
  35. * @access private
  36. * @var string
  37. */
  38. private $lCurrentChannel;
  39.  
  40. /**
  41. * @access private
  42. * @var ProgramGuide
  43. */
  44. private $lProgramGuide;
  45.  
  46.  
  47. /**
  48. * @access private
  49. * @var array
  50. */
  51. private $lAuthentication;
  52.  
  53. /**
  54. * Constructor. This creates the Dreambox object. If it exists in the session cache, it will be recreated from the cahce instead a new object.
  55. * @param string $pIP
  56. * @param string $pType
  57. * @param array $pAuthentication
  58. * @param int $pReNew
  59. * @return Dreambox
  60. */
  61. function __construct($pIP = "",$pType = "",$pAuthentication = array(), $pReNew = 0) {
  62.  
  63. // Session based caching....
  64. if ($pReNew == 0 && isset($_SESSION["dreambox"])) {
  65. $lTMPObj = unserialize($_SESSION["dreambox"]);
  66.  
  67. $this->lIPNummer = $lTMPObj->lIPNummer;
  68. $this->lType = $lTMPObj->lType;
  69. $this->lAuthentication = $lTMPObj->lAuthentication;
  70. $this->lBoutiques = $lTMPObj->lBoutiques;
  71. $this->lBoutiqueQueueCounter = $lTMPObj->lBoutiqueQueueCounter;
  72. $this->lCurrentBoutique = $lTMPObj->lCurrentBoutique;
  73. $this->lCurrentChannel = $lTMPObj->lCurrentChannel;
  74. $this->lProgramGuide = $lTMPObj->lProgramGuide;
  75. $this->saveObjectCache();
  76. } else {
  77. $this->lIPNummer = $pIP;
  78. $this->lType = $pType;
  79. $this->lAuthentication = array();
  80. $this->lAuthentication[0] = trim($pAuthentication[0]);
  81. $this->lAuthentication[1] = trim($pAuthentication[1]);
  82. $this->lBoutiques = array();
  83. $this->lCurrentBoutique = null;
  84. $this->lCurrentChannel = null;
  85. $this->lProgramGuide = new ProgramGuide($this->lIPNummer,$this->lType,$this->getAthentication());
  86. $this->resetBoutiqueCounter(); // This function also saves the object to session cache..
  87. //$this->saveObjectCache(); // So a manual request is not needed
  88. }
  89. }
  90.  
  91. /**
  92. * Save the current Dreambox object state to a session. This is used for caching.
  93. */
  94. private function saveObjectCache() {
  95. $_SESSION["dreambox"] = serialize($this);
  96. }
  97.  
  98. /**
  99. * Get the Boutique url. This is used for getting boutique information from your dreambox.
  100. * @return string
  101. */
  102. private function getBoutiqueUrl() {
  103. $lExtraUrl = "";
  104. if ($this->lAuthentication[0] != "" || $this->lAuthentication[1] != "") {
  105. $lExtraUrl = $this->lAuthentication[0] . ":" . $this->lAuthentication[1] . "@";
  106. }
  107. switch ($this->lType) {
  108. case "enigma1":
  109. return "http://" . $lExtraUrl . $this->lIPNummer . "/body?mode=zap&zapmode=0&zapsubmode=4";
  110. break;
  111. case "enigma2";
  112. return "http://" . $lExtraUrl . $this->lIPNummer . "/web/getservices?sRef=1:7:1:0:0:0:0:0:0:0:FROM%20BOUQUET%20%22bouquets.tv%22%20ORDER%20BY%20bouquet";
  113. break;
  114. default:
  115. return false;
  116. break;
  117. }
  118. }
  119.  
  120. /**
  121. * Set the Dreambox IP nummer. If the IP nummer is not valid, it will be ignored.
  122. * @param string $pIp
  123. * @return boolean
  124. */
  125. public function setDreamboxIP($pIp) {
  126. function checkIP($pIp) { // Should be Regex.... but to lazy at the moment.... ;)
  127. $pIp = explode(".",trim($pIp));
  128. return (sizeof($pIp) == 4 &&
  129. $pIp[0] > 0 && $pIp[0] <= 255 &&
  130. $pIp[1] > 0 && $pIp[1] <= 255 &&
  131. $pIp[2] > 0 && $pIp[2] <= 255 &&
  132. $pIp[3] > 0 && $pIp[3] <= 255 );
  133. }
  134.  
  135. $pIp = trim($pIp);
  136. if (checkIP($pIp) || checkIP(gethostbyname($pIp))) {
  137. $this->lIPNummer = $pIp;
  138. $this->saveObjectCache();
  139. return true;
  140. }
  141. return false;
  142. }
  143.  
  144. /**
  145. * Get the current IP of the Dreambox object.
  146. * @return boolean
  147. */
  148. public function getDreamboxIP() {
  149. return trim($this->lIPNummer);
  150. }
  151.  
  152. /**
  153. * Check if the Dreambox is online.
  154. * @return boolean
  155. */
  156. public function isOnline() {
  157. $ping = explode("\n",shell_exec ('ping -l 1 -w 2 ' . $this->lIPNummer));
  158. $ping = explode(",",$ping[sizeof($ping)-3]);
  159. $ping = explode(" ",trim($ping[1]));
  160. return ($ping[0] != 0);
  161. }
  162.  
  163. /**
  164. * Set the Dreambox type. Valid values are 'enigma1' and 'enigma2'.
  165. * @param string $pType
  166. * @return boolean
  167. */
  168. public function setDreamboxType($pType) {
  169. if ( ($pType == trim($pType)) == "") return false;
  170. switch ($pType) {
  171. case "enigma1":
  172. $this->lType = "enigma1";
  173. break;
  174. case "enigma2";
  175. $this->lType = "enigma2";
  176. break;
  177. default:
  178. return false;
  179. break;
  180. }
  181. // Should only reached when a valid type is entered
  182. $this->saveObjectCache();
  183. return true;
  184. }
  185.  
  186.  
  187. public function getDreamboxUserName() {
  188. return trim($this->lAuthentication[0]);
  189. }
  190.  
  191. public function getDreamboxPassWord() {
  192. return trim($this->lAuthentication[1]);
  193. }
  194.  
  195. public function getAthentication() {
  196. return array($this->getDreamboxUserName(),$this->getDreamboxPassWord());
  197. }
  198.  
  199.  
  200. /**
  201. * Load the data from the Dreambox. Here all youre boutiques will be collected and saved in the dreambox object.
  202. * Set $pReNew to 1 to force a reload of the boutique data.
  203. * @param int $pReNew
  204. * @return boolean
  205. */
  206. public function loadBoutiques($pReNew = 0) {
  207. if (sizeof($this->lBoutiques) == 0 || $pReNew == 1) { // Only load boutique data the first time ... later.. it should be cached
  208. switch ($this->lType) {
  209. case "enigma1":
  210. $lData = file_get_contents($this->getBoutiqueUrl());
  211. //$lData = file_get_contents("./enigma1.txt"); // Test
  212.  
  213. $lStartPos = stripos($lData,"var bouquets = new Array(");
  214. $lStartPos = stripos($lData,"\n",$lStartPos);
  215. $lStopPos = stripos($lData,"\n",$lStartPos+1);
  216. $lDataNames = explode(",",str_ireplace("\"","",trim(substr($lData,$lStartPos,$lStopPos-$lStartPos))));
  217.  
  218. $lStartPos = stripos($lData,"var bouquetRefs = new Array(");
  219. $lStartPos = stripos($lData,"\n",$lStartPos);
  220. $lStopPos = stripos($lData,"\n",$lStartPos+1);
  221. $lDataIDs = explode(",",str_ireplace("\"","",trim(substr($lData,$lStartPos,$lStopPos-$lStartPos))));
  222.  
  223. for ($i = 0; $i < sizeof($lDataIDs); $i++) {
  224. $this->lBoutiques[] = new Boutique($lDataIDs[$i],$this->lIPNummer,$this->lType,$this->getAthentication(),$lDataNames[$i]);
  225. }
  226. break;
  227. case "enigma2";
  228.  
  229. $lData = Utils::xml2array(file_get_contents($this->getBoutiqueUrl()));
  230. if (!is_array($lData) || !is_array($lData["e2servicelist"][0]["e2service"])) return false;
  231. foreach ($lData["e2servicelist"][0]["e2service"] as $lBoutique) {
  232. $lBoutiqueId = explode("&quot;",$lBoutique["e2servicereference"]);
  233. $lBoutiqueId = $lBoutiqueId[1];
  234. $this->lBoutiques[] = new Boutique($lBoutiqueId,$this->lIPNummer,$this->lType,$this->getAthentication(),$lBoutique["e2servicename"]);
  235. }
  236.  
  237.  
  238.  
  239. break;
  240. default:
  241. return false;
  242. break;
  243. }
  244. $this->saveObjectCache();
  245. }
  246. return true;
  247. }
  248.  
  249. /**
  250. * Load the channel data. This is done after the boutiques are loaded. The actual loading of the data happens in the Boutique class.
  251. * Set $pReNew to 1 to force a reload of the channel data
  252. * @param int $pReNew
  253. */
  254. function loadChannels($pReNew = 0) {
  255. foreach ($this->lBoutiques as $lBoutique) {
  256. $lBoutique->loadChannels($pReNew);
  257. }
  258.  
  259. $this->saveObjectCache();
  260. }
  261.  
  262. /**
  263. * Reset the boutiquecounter. This is an internal counter to keep track of the possituin in the boutique list.
  264. */
  265. function resetBoutiqueCounter() {
  266. $this->lBoutiqueQueueCounter = -1;
  267. $this->saveObjectCache();
  268. }
  269.  
  270. /**
  271. * Get the total boutiques.
  272. * @return int
  273. */
  274. function getBoutiquesCount() {
  275. return sizeof($this->lBoutiques);
  276. }
  277.  
  278. /**
  279. * Get the next boutique. This function gives the next boutique and updates it internal counter.
  280. * @return Boutique
  281. */
  282. function getNextBoutique() {
  283. $this->lBoutiqueQueueCounter++;
  284. $this->saveObjectCache();
  285. if (isset($this->lBoutiques[$this->lBoutiqueQueueCounter])) {
  286. return $this->lBoutiques[$this->lBoutiqueQueueCounter];
  287. }
  288. return null;
  289. }
  290.  
  291. /**
  292. * Search for a boutique base on boutiqueID. This function returns a boutique when ID is valid and found. Else it returns false.
  293. * @param string $pBoutiqueID
  294. * @return Boutique
  295. */
  296. function searchBoutique($pBoutiqueID) {
  297.  
  298. if ( ($pBoutiqueID = trim($pBoutiqueID)) == "") return false;
  299. foreach ($this->lBoutiques as $lBoutique) {
  300. if ($lBoutique->getID() == $pBoutiqueID) {
  301. return $lBoutique;
  302. }
  303. }
  304. return null;
  305. }
  306.  
  307. /**
  308. * Zap the dreambox to the new channel. This function saves the boutique and channel in the dreambox object so that you can ask the dreambox what you are wachting.
  309. * @param string $pBoutiqueID
  310. * @param string $pChannelID
  311. */
  312. function zap($pBoutiqueID,$pChannelID) {
  313. $this->lCurrentBoutique = $pBoutiqueID;
  314. $this->lCurrentChannel = $pChannelID;
  315. $this->saveObjectCache();
  316. }
  317.  
  318. /**
  319. * Return the programguide object. You can use the programguide to find programs and show a soort of tv guide.
  320. * @return ProgramGuide
  321. */
  322. public function programGuide() {
  323. return $this->lProgramGuide;
  324. }
  325.  
  326. /**
  327. * Load the program guide of a channel. The function uses the ProgramGuide object to load the data.
  328. * @param string $pChannelID
  329. */
  330. public function loadProgramGuide($pChannelID = "") {
  331. if ($pChannelID == "") $pChannelID = $this->lCurrentChannel;
  332. $this->lProgramGuide->loadProgramGuide($pChannelID);
  333. $this->saveObjectCache();
  334. }
  335.  
  336. /**
  337. * Dump the complete object to screen.
  338. */
  339. public function dump() {
  340.  
  341. print_r($this);
  342. }
  343.  
  344. }
  345. ?>

Documentation generated on Sat, 19 Jan 2008 12:56:15 +0100 by phpDocumentor 1.3.0RC3