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. * @access private
  11. * @var string
  12. */
  13. private $lBoutiques;
  14. /**
  15. * @access private
  16. * @var string
  17. */
  18. private $lType;
  19. /**
  20. * @access private
  21. * @var int
  22. */
  23. private $lBoutiqueQueueCounter;
  24. /**
  25. * @access private
  26. * @var string
  27. */
  28. private $lCurrentBoutique;
  29. /**
  30. * @access private
  31. * @var string
  32. */
  33. private $lCurrentChannel;
  34. /**
  35. * @access private
  36. * @var ProgramGuide
  37. */
  38. private $lProgramGuide;
  39. /**
  40. * @access private
  41. * @var string
  42. */
  43. private $lRecordings;
  44. /**
  45. * @access private
  46. * @var int
  47. */
  48. private $lRecordingsQueueCounter;
  49. /**
  50. * @access private
  51. * @var array
  52. */
  53. private $lAuthentication;
  54. /**
  55. * Constructor. This creates the Dreambox object. If it exists in the session cache, it will be recreated from the cahce instead a new object.
  56. * @param string $pIP
  57. * @param string $pType
  58. * @param array $pAuthentication
  59. * @param int $pReNew
  60. * @return Dreambox
  61. */
  62. function __construct($pIP = "", $pType = "", $pAuthentication = array(), $pReNew = 0)
  63. {
  64. // Session based caching....
  65. if ($pReNew == 0 && isset($_SESSION["dreambox"])) {
  66. $lTMPObj = unserialize($_SESSION["dreambox"]);
  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->lRecordings = $lTMPObj->lRecordings;
  75. $this->lProgramGuide = $lTMPObj->lProgramGuide;
  76. $this->saveObjectCache();
  77. } else {
  78. $this->lIPNummer = $pIP;
  79. $this->lType = $pType;
  80. $this->lAuthentication = array();
  81. $this->lAuthentication[0] = trim($pAuthentication[0]);
  82. $this->lAuthentication[1] = trim($pAuthentication[1]);
  83. $this->lBoutiques = array();
  84. $this->lCurrentBoutique = null;
  85. $this->lCurrentChannel = null;
  86. $this->lProgramGuide = new ProgramGuide($this->lIPNummer, $this->lType, $this->getAthentication());
  87. $this->lRecordings = array();
  88. // This function also saves the object to session cache..
  89. $this->resetBoutiqueCounter();
  90. //$this->saveObjectCache(); // So a manual request is not needed
  91. }
  92. }
  93. /**
  94. * Save the current Dreambox object state to a session. This is used for caching.
  95. */
  96. private function saveObjectCache()
  97. {
  98. $_SESSION["dreambox"] = serialize($this);
  99. }
  100. /**
  101. * Get the Boutique url. This is used for getting boutique information from your dreambox.
  102. * @return string
  103. */
  104. private function getBoutiqueUrl()
  105. {
  106. $lExtraUrl = "";
  107. if ($this->lAuthentication[0] != "" || $this->lAuthentication[1] != "") {
  108. $lExtraUrl = $this->lAuthentication[0] . ":" . $this->lAuthentication[1] . "@";
  109. }
  110. switch ($this->lType) {
  111. case "enigma1":
  112. return "http://" . $lExtraUrl . $this->lIPNummer . "/body?mode=zap&zapmode=0&zapsubmode=4";
  113. break;
  114. case "enigma2";
  115. 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";
  116. break;
  117. default:
  118. return false;
  119. break;
  120. }
  121. }
  122. /**
  123. * Get the Recording url. This is used for getting recoring information from your dreambox.
  124. * @return string
  125. */
  126. public function getRecordingUrl()
  127. {
  128. $lExtraUrl = "";
  129. if ($this->lAuthentication[0] != "" || $this->lAuthentication[1] != "") {
  130. $lExtraUrl = $this->lAuthentication[0] . ":" . $this->lAuthentication[1] . "@";
  131. }
  132. switch ($this->lType) {
  133. case "enigma1":
  134. return "http://" . $lExtraUrl . $this->lIPNummer . "/body?mode=zap&zapmode=3&zapsubmode=1";
  135. break;
  136. case "enigma2";
  137. return "http://" . $lExtraUrl . $this->lIPNummer . "/web/movielist?tag=";
  138. break;
  139. default:
  140. break;
  141. }
  142. }
  143. /**
  144. * Get all recordings from the dreambox. The recordings will be save in a recording object array.
  145. * It can self see with enigma version is used, and what the code will be that will be returned from the dreambox
  146. */
  147. public function getRecordings()
  148. {
  149. $this->lRecordings = array();
  150. switch ($this->lType) {
  151. case "enigma1":
  152. $lData = file_get_contents($this->getRecordingUrl());
  153. $lStartPos = stripos($lData, "channels[0] = new Array(\"") + strlen("channels[0] = new Array(\"");
  154. $lStopPos = stripos($lData, "\");", $lStartPos + 1);
  155. $lDataNames = explode(",", str_ireplace("\"", "", trim(substr($lData, $lStartPos, $lStopPos - $lStartPos))));
  156. $lStartPos = stripos($lData, "channelRefs[0] = new Array(\"") + strlen("channelRefs[0] = new Array(\"");
  157. $lStopPos = stripos($lData, "\");", $lStartPos + 1);
  158. $lDataIDs = explode(",", str_ireplace("\"", "", trim(substr($lData, $lStartPos, $lStopPos - $lStartPos))));
  159. for ($i = 0; $i < sizeof($lDataIDs); $i++) {
  160. $lDataLine = trim($lDataNames[$i]);
  161. $lTitle = trim(substr($lDataLine, stripos($lDataLine, "]") + 1));
  162. $lFileSize = substr($lDataLine, 1, stripos($lDataLine, "]") - 4) * 1024;
  163. $lFileDate = explode("-", trim(substr($lDataLine, stripos($lDataLine, "]") + 1, 9)));
  164. if (sizeof($lFileDate) == 3) {
  165. $lFileDate = mktime(0, 0, 0, $lFileDate[1], $lFileDate[2], $lFileDate[0]);
  166. $lTitle = trim(substr($lTitle, 11));
  167. } else {
  168. $lFileDate = mktime();
  169. }
  170. $this->lRecordings[] = new Recording($lDataIDs[$i], $this->lIPNummer, $this->lType, $lTitle, "", "", $lFileDate, "", $lDataIDs[$i], $lFileSize);
  171. }
  172. break;
  173. case "enigma2";
  174. $lData = Utils::xml2array(file_get_contents($this->getRecordingUrl()));
  175. if (!is_array($lData) || !is_array($lData["e2movielist"][0]["e2movie"])) return false;
  176. foreach ($lData["e2movielist"][0]["e2movie"] as $lRecording) {
  177. $this->lRecordings[] = new Recording($lRecording["e2servicereference"], $this->lIPNummer, $this->lType, $lRecording["e2title"], $lRecording["e2description"], $lRecording["e2servicename"], $lRecording["e2time"], $lRecording["e2length"], $lRecording["e2filename"], $lRecording["e2filesize"]);
  178. }
  179. break;
  180. default:
  181. break;
  182. }
  183. $this->lRecordingsQueueCounter = -1;
  184. }
  185. /**
  186. * Iterate trough the recordings.
  187. * @return Recording
  188. */
  189. function getNextRecording()
  190. {
  191. $this->lRecordingsQueueCounter++;
  192. $this->saveObjectCache();
  193. if (isset($this->lRecordings[$this->lRecordingsQueueCounter])) return $this->lRecordings[$this->lRecordingsQueueCounter];
  194. return null;
  195. }
  196. /**
  197. * Set the Dreambox IP nummer. If the IP nummer is not valid, it will be ignored.
  198. * @param string $pIp
  199. * @return boolean
  200. */
  201. public function setDreamboxIP($pIp)
  202. {
  203. function checkIP($pIp)
  204. {
  205. // Should be Regex.... but to lazy at the moment.... ;)
  206. $pIp = explode(".", trim($pIp));
  207. return(sizeof($pIp) == 4 && $pIp[0] > 0 && $pIp[0] <= 255 && $pIp[1] > 0 && $pIp[1] <= 255 && $pIp[2] > 0 && $pIp[2] <= 255 && $pIp[3] > 0 && $pIp[3] <= 255);
  208. }
  209. $pIp = trim($pIp);
  210. if (checkIP($pIp) || checkIP(gethostbyname($pIp))) {
  211. $this->lIPNummer = $pIp;
  212. $this->saveObjectCache();
  213. return true;
  214. }
  215. return false;
  216. }
  217. /**
  218. * Get the current IP of the Dreambox object.
  219. * @return boolean
  220. */
  221. public function getDreamboxIP()
  222. {
  223. return trim($this->lIPNummer);
  224. }
  225. /**
  226. * Check if the Dreambox is online.
  227. * @return boolean
  228. */
  229. public function isOnline()
  230. {
  231. $ping = explode("\n", trim(shell_exec('ping -l 1 -w 2 ' . $this->lIPNummer)));
  232. for ($i = 0; $i < sizeof($ping); $i++) {
  233. if ($i >= 3 && trim($ping[$i]) == "") {
  234. $i += 2;
  235. break;
  236. }
  237. }
  238. $ping = explode(",", $ping[$i]);
  239. $ping = explode(" ", trim($ping[1]));
  240. if (is_numeric(trim($ping[0]))) {
  241. // Linux
  242. return(trim($ping[0]) != 0);
  243. } else {
  244. // Windows
  245. return(trim($ping[2]) != 0);
  246. }
  247. }
  248. /**
  249. * Set the Dreambox type. Valid values are 'enigma1' and 'enigma2'.
  250. * @param string $pType
  251. * @return boolean
  252. */
  253. public function setDreamboxType($pType)
  254. {
  255. switch ($pType) {
  256. case "enigma1":
  257. $this->lType = "enigma1";
  258. break;
  259. case "enigma2";
  260. $this->lType = "enigma2";
  261. break;
  262. default:
  263. return false;
  264. break;
  265. }
  266. $this->saveObjectCache();
  267. return true;
  268. }
  269. /**
  270. * Get the dreambox username for authorization
  271. * @return string
  272. */
  273. public function getDreamboxUserName()
  274. {
  275. return trim($this->lAuthentication[0]);
  276. }
  277. /**
  278. * Get the dreambox password for authorization in plain text
  279. * @return password
  280. */
  281. public function getDreamboxPassWord()
  282. {
  283. return trim($this->lAuthentication[1]);
  284. }
  285. /**
  286. * Get the authentication array. This will containt the username and password in an array.
  287. * @return array
  288. */
  289. public function getAthentication()
  290. {
  291. return array($this->getDreamboxUserName(), $this->getDreamboxPassWord());
  292. }
  293. /**
  294. * Load the data from the Dreambox. Here all youre boutiques will be collected and saved in the dreambox object.
  295. * Set $pReNew to 1 to force a reload of the boutique data.
  296. * @param int $pReNew
  297. * @return boolean
  298. */
  299. public function loadBoutiques($pReNew = 0)
  300. {
  301. if (sizeof($this->lBoutiques) == 0 || $pReNew == 1) {
  302. // Only load boutique data the first time ... later.. it should be cached
  303. switch ($this->lType) {
  304. case "enigma1":
  305. $lData = file_get_contents($this->getBoutiqueUrl());
  306. $lStartPos = stripos($lData, "var bouquets = new Array(");
  307. $lStartPos = stripos($lData, "\n", $lStartPos);
  308. $lStopPos = stripos($lData, "\n", $lStartPos + 1);
  309. $lDataNames = explode(",", str_ireplace("\"", "", trim(substr($lData, $lStartPos, $lStopPos - $lStartPos))));
  310. $lStartPos = stripos($lData, "var bouquetRefs = new Array(");
  311. $lStartPos = stripos($lData, "\n", $lStartPos);
  312. $lStopPos = stripos($lData, "\n", $lStartPos + 1);
  313. $lDataIDs = explode(",", str_ireplace("\"", "", trim(substr($lData, $lStartPos, $lStopPos - $lStartPos))));
  314. for ($i = 0; $i < sizeof($lDataIDs); $i++) {
  315. $this->lBoutiques[] = new Boutique($lDataIDs[$i], $this->lIPNummer, $this->lType, $this->getAthentication(), $lDataNames[$i]);
  316. }
  317. break;
  318. case "enigma2";
  319. $lData = Utils::xml2array(file_get_contents($this->getBoutiqueUrl()));
  320. if (!is_array($lData) || !is_array($lData["e2servicelist"][0]["e2service"])) return false;
  321. foreach ($lData["e2servicelist"][0]["e2service"] as $lBoutique) {
  322. // Suhosin Patch wierdness....?
  323. $lBoutique["e2servicereference"] = str_replace("\\", "", $lBoutique["e2servicereference"]);
  324. if (strpos("&quot;", $lBoutique["e2servicereference"]) !== false) {
  325. $lBoutiqueId = explode("&quot;", $lBoutique["e2servicereference"]);
  326. } else {
  327. $lBoutiqueId = explode("\"", $lBoutique["e2servicereference"]);
  328. }
  329. $lBoutiqueId = trim($lBoutiqueId[1]);
  330. $this->lBoutiques[] = new Boutique($lBoutiqueId, $this->lIPNummer, $this->lType, $this->getAthentication(), $lBoutique["e2servicename"]);
  331. }
  332. break;
  333. default:
  334. return false;
  335. break;
  336. }
  337. $this->saveObjectCache();
  338. }
  339. return true;
  340. }
  341. /**
  342. * Load the channel data. This is done after the boutiques are loaded. The actual loading of the data happens in the Boutique class.
  343. * Set $pReNew to 1 to force a reload of the channel data
  344. * @param int $pReNew
  345. */
  346. function loadChannels($pReNew = 0)
  347. {
  348. foreach ($this->lBoutiques as $lBoutique) {
  349. $lBoutique->loadChannels($pReNew);
  350. }
  351. $this->saveObjectCache();
  352. }
  353. /**
  354. * Reset the boutiquecounter. This is an internal counter to keep track of the possituin in the boutique list.
  355. */
  356. function resetBoutiqueCounter()
  357. {
  358. $this->lBoutiqueQueueCounter = -1;
  359. $this->saveObjectCache();
  360. }
  361. /**
  362. * Get the total boutiques.
  363. * @return int
  364. */
  365. function getBoutiquesCount()
  366. {
  367. return sizeof($this->lBoutiques);
  368. }
  369. /**
  370. * Get the next boutique. This function gives the next boutique and updates it internal counter.
  371. * @return Boutique
  372. */
  373. function getNextBoutique()
  374. {
  375. $this->lBoutiqueQueueCounter++;
  376. $this->saveObjectCache();
  377. if (isset($this->lBoutiques[$this->lBoutiqueQueueCounter])) {
  378. return $this->lBoutiques[$this->lBoutiqueQueueCounter];
  379. }
  380. return null;
  381. }
  382. /**
  383. * Search for a boutique base on boutiqueID. This function returns a boutique when ID is valid and found. Else it returns false.
  384. * @param string $pBoutiqueID
  385. * @return Boutique
  386. */
  387. function searchBoutique($pBoutiqueID)
  388. {
  389. if (($pBoutiqueID = trim($pBoutiqueID)) == "") return false;
  390. foreach ($this->lBoutiques as $lBoutique) {
  391. if ($lBoutique->getID() == $pBoutiqueID) return $lBoutique;
  392. }
  393. return false;
  394. }
  395. /**
  396. * Find the boutique that holds the channel. So you can search for the boutique that contains a specifeid channel. Return null when not found
  397. * @return Boutique
  398. */
  399. function findBoutiqueWithChannel($pChannelID)
  400. {
  401. if (($pChannelID = trim($pChannelID)) == "") return false;
  402. foreach ($this->lBoutiques as $lBoutique) {
  403. if ($lBoutique->getChannel($pChannelID) != false) return $lBoutique;
  404. }
  405. return false;
  406. }
  407. /**
  408. * 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.
  409. * @param string $pBoutiqueID
  410. * @param string $pChannelID
  411. */
  412. function zap($pBoutiqueID, $pChannelID)
  413. {
  414. $this->lCurrentBoutique = $pBoutiqueID;
  415. $this->lCurrentChannel = $pChannelID;
  416. $this->saveObjectCache();
  417. }
  418. /**
  419. * Return the programguide object. You can use the programguide to find programs and show a soort of tv guide.
  420. * @return ProgramGuide
  421. */
  422. public function programGuide()
  423. {
  424. return $this->lProgramGuide;
  425. }
  426. /**
  427. * Load the program guide of a channel. The function uses the ProgramGuide object to load the data.
  428. * @param string $pChannelID
  429. */
  430. public function loadProgramGuide($pChannelID = "")
  431. {
  432. if ($pChannelID == "") $pChannelID = $this->lCurrentChannel;
  433. $this->lProgramGuide->loadProgramGuide($pChannelID);
  434. $this->saveObjectCache();
  435. }
  436. /**
  437. * Sanity check. This checks all settings that are needed. You can easy append the checks. When everything is ok, a empty string is returned. Else the error message is returned
  438. * @return string
  439. */
  440. public function sanityCheck()
  441. {
  442. $lSafeMode = strtolower(ini_get("safe_mode"));
  443. $lMaxTime = ini_get("max_execution_time");
  444. if ($lSafeMode == "on" || $lSafeMode == "true" || $lSafeMode == "1") return "PHP Safe_mode is active. Change it in the php.ini file to Off";
  445. if ($lMaxTime < 60) return "Your PHP max_execution_time setting is to low in your php.ini";
  446. return "";
  447. }
  448. /**
  449. * Dump the complete object to screen.
  450. */
  451. public function dump()
  452. {
  453. print_r($this);
  454. }
  455. }
  456. ?>

Documentation generated on Tue, 24 Jun 2008 18:59:32 +0200 by phpDocumentor 1.3.0RC3