summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authorTheTroll <trolldev@gmail.com>2010-03-16 15:16:59 +0100
committerTheTroll <trolldev@gmail.com>2010-03-16 15:16:59 +0100
commite89fdef6064741bf6d26c11b782c7fecf994caf3 (patch)
tree2d3214ed31948f344ca069fb214c53ce298e3778 /bin
parent5400c2d66985a85591b2b78085615cd2826911a4 (diff)
downloadistreamdev-e89fdef6064741bf6d26c11b782c7fecf994caf3.tar.gz
istreamdev-e89fdef6064741bf6d26c11b782c7fecf994caf3.tar.bz2
Function implementation WIP
Diffstat (limited to 'bin')
-rw-r--r--bin/backend.php5
-rwxr-xr-xbin/svdrp_old.php446
-rwxr-xr-xbin/utils.php95
-rwxr-xr-xbin/vdr.php255
4 files changed, 734 insertions, 67 deletions
diff --git a/bin/backend.php b/bin/backend.php
index 4fd9ba2..631330d 100644
--- a/bin/backend.php
+++ b/bin/backend.php
@@ -6,6 +6,7 @@ if (file_exists('../config.php'))
include ('../config.php');
else
include ('../config_default.php');
+include ('./utils.php');
include ('./vdr.php');
$action=$_REQUEST['action'];
@@ -22,12 +23,12 @@ switch ($action)
break;
case ("getFullChanList"):
- $tree = file_get_contents("textfiles/getFullChanList.txt");
+ $tree = getFullChanList();
print $tree;
break;
case ("getTvChan"):
- $tree = file_get_contents("textfiles/getTvChan.txt");
+ $tree = GetTvChan($_REQUEST['cat']);
print $tree;
break;
diff --git a/bin/svdrp_old.php b/bin/svdrp_old.php
new file mode 100755
index 0000000..da83b49
--- /dev/null
+++ b/bin/svdrp_old.php
@@ -0,0 +1,446 @@
+<?php
+
+// SVDRP is a class do communicate with a vdr via svdrp
+class SVDRP
+{
+ var $cfgServer;
+ var $cfgPort;
+ var $cfgTimeOut;
+
+ var $handle;
+ var $debug;
+
+ function SVDRP($server = "localhost", $port=2001, $timeout = 30, $debug = 0)
+ {
+ $this->cfgServer = $server;
+ $this->cfgPort = $port;
+ $this->cfgTimeOut = $timeout;
+ $this->debug = $debug;
+ $this->handle = 0;
+ }
+
+ function DebugMessage($msg)
+ {
+ if($this->debug) echo ($msg);
+ }
+
+ function Connect()
+ {
+ if($this->handle) Disconnect();
+ $errno = 0;
+ $errstr = "";
+ $this->handle = fsockopen($this->cfgServer, $this->cfgPort, &$errno, &$errstr, $this->cfgTimeOut);
+
+ if(!$this->handle)
+ {
+ $this->DebugMessage("error $errno: $errstr");
+ return false;
+ }
+
+ $this->DebugMessage("handle: $this->handle<br>\n");
+
+
+ $input = fgets($this->handle,128);
+
+ if(!preg_match("/^220 /", $input) || $input == "")
+ {
+ $this->DebugMessage("wrong welcome message: '$input'<br>\n");
+ $this->Disconnect();
+ return false;
+ }
+
+
+ $this->DebugMessage("Welcome message: $input<br><br>\n");
+
+ return true;
+ }
+
+ function Command($cmd)
+ {
+ if(!$this->handle) return false;
+
+ $ret = array();
+
+ $this->DebugMessage("Kommando $cmd<br><pr"."e>");
+ fputs($this->handle, $cmd . "\n");
+ $s = "";
+ $nline = 0;
+ while($s .= fgets($this->handle,4096))
+ {
+ $nline++;
+
+ $this->DebugMessage($s);
+ if(!preg_match("/^(\\d{3})([ -])(.*)$/", $s, $data))
+ {
+ continue;
+ }
+
+
+
+
+ $number = $data[1];
+ // TODO: Fehlernummer bearbeiten
+ $ret[] = $data[3];
+ if($data[2] != "-" && $nline == 1) $ret = $data[3] ;
+ if($data[2] != "-") break;
+ $s = "";
+
+ }
+
+ $this->DebugMessage("</pr"."e>");
+ return $ret;
+ }
+
+ function ListChannels($numberorname="")
+ {
+ if(!$this->handle) return false;
+ $channels = array();
+ $lines = $this->Command("LSTC$numberorname");
+ if(!$lines) return false;
+ foreach($lines as $a => $l)
+ {
+ $a = split(":", $l);
+ $name = $a[0];
+ $freq = $a[1];
+ $b = split(";", $name);
+ $name = $b[0];
+ if(!isset($b[1])) $b[1] = $name;
+ $group = $b[1];
+
+
+ $c["name"] = $name;
+ $c["group"] = $group;
+ $c["frequency"] = $freq;
+
+ $channels[] = $c;
+
+
+ }
+ return $channels;
+ }
+
+ function Help()
+ {
+ return $this->Command("HELP");
+ }
+ function Disconnect()
+ {
+ if(!$this->handle) return;
+ $this->Command("QUIT");
+
+ fclose($this->handle);
+ $this->handle = 0;
+ $this->DebugMessage("disconnected");
+ }
+ function ClearEpg()
+ {
+ if(!$this->handle) return false;
+ $this->Command("CLRE");
+ return true;
+ }
+ function SwitchUp()
+ {
+ if(!$this->handle) return false;
+ $this->Command("CHAN +");
+ return true;
+ }
+ function SwitchDown()
+ {
+ if(!$this->handle) return false;
+ $this->Command("CHAN -");
+ return true;
+ }
+ function SwitchChannel($channel)
+ {
+ if(!$this->handle) return false;
+ $this->Command("CHAN $channel");
+ return true;
+ }
+ function DeleteChannel($id)
+ {
+ if(!$this->handle) return false;
+ $this->Command("DELC $id");
+ return true;
+ }
+ function DeleteRecord($id)
+ {
+ if(!$this->handle) return false;
+ $this->Command("DELR $id");
+ return true;
+ }
+
+ function GrabImage($filename, $type="jpeg", $quality="", $width="", $height)
+ {
+ if(!$this->handle) return false;
+ $this->Command("GRAB $filename $type $quality $width $height");
+ return true;
+ }
+ function HitKey($key)
+ {
+ if(!$this->handle) return false;
+ $this->Command("HITK $key");
+ return true;
+ }
+ function PowerOff()
+ {
+ if(!$this->handle) return false;
+ $this->Command("HITK Power");
+ return true;
+
+ }
+ function GetKeys()
+ {
+ if(!$this->handle) return false;
+
+ $lines = $this->Command("HITK");
+ $keys = array();
+ foreach($lines as $l)
+ {
+ if(!preg_match("/^ {4}(.*)$/", $l, $m)) continue;
+ $keys[] = $m[1];
+ }
+
+ return $keys;
+ }
+ function ListEPG($pStrChannel="", $pStrTime="")
+ {
+ if(!$this->handle) return false;
+ $lines = $this->Command("LSTE");
+
+ $epg = array ();
+ $channel = array();
+ $event = array();
+
+ $channelname = "";
+ foreach($lines as $l)
+ {
+ preg_match("/^(.)\\s*(.*)$/", $l, $m);
+ $type = $m[1];
+ $text = $m[2];
+ switch($type)
+ {
+ case 'C': // Channel
+ list( $channeldata, $channelname ) = explode( ' ', $text, 2 );
+
+ break;
+ case 'E': // new Event
+ sscanf($text, "%u %ld %d %X", $event["EventID"], $event["StartTime"], $event["Duration"], $event["TableID"]);
+
+ break;
+ case 'T': // Title
+ $event["Title"] = $text;
+ break;
+ case 'S': // Short text
+ $event["Shottext"] = $text;
+ break;
+ case 'D': // Description
+ $event["Desc"] = $text;
+ break;
+ case 'V': // VPS
+ $event["VPS"] = $text;
+ break;
+ case 'e': // Event end
+ if ((trim($pStrTime) != '') && (( $event['StartTime'] > $pStrTime ) || ($event['StartTime'] + $event["Duration"] < $pStrTime)))
+ continue;
+
+ $channel[] = $event;
+ $event = array();
+
+ break;
+ case 'c': // Channel end
+ if ((trim($pStrChannel) != '') && ($channelname != $pStrChannel))
+ continue;
+
+ $epg[$channelname] = $channel;
+ $channel = array();
+
+ break;
+ }
+
+ }
+ if ((trim($pStrTime) == '') || (( $event['StartTime'] < $pStrTime ) && ($event['StartTime'] + $event["Duration"] > $pStrTime)))
+ $channel[] = $event;
+
+ if ((trim($pStrChannel) != '') || ($channelname == $pStrChannel))
+ $epg[$channelname] = $channel;
+
+ return $epg;
+ }
+
+ function Message($msg)
+ {
+ if(!$this->handle) return false;
+ $this->Command("MESG $msg");
+ return true;
+ }
+
+
+ // Volume commands
+ function ToggleMute()
+ {
+ if(!$this->handle) return false;
+ $this->Command("VOLU mute");
+ return true;
+ }
+ function VolumeUp()
+ {
+ if(!$this->handle) return false;
+ $this->Command("VOLU +");
+ return true;
+ }
+ function VolumeDown()
+ {
+ if(!$this->handle) return false;
+ $this->Command("VOLU -");
+ return true;
+ }
+ function SetVolume($v)
+ {
+ if(!$this->handle) return false;
+ $this->Command("VOLU $v");
+ return true;
+ }
+ function GetVolume()
+ {
+ if(!$this->handle) return false;
+ $v = $this->Command("VOLU");
+ if($v == "Audio is mute") return 0;
+ if(!preg_match("/Audio volume is (.*)/", $v, $m)) return false;
+
+ return $m[1];
+ }
+ function GetDiskStat()
+ {
+ if(!$this->handle) return false;
+ $stat = $this->Command("STAT DISK");
+ sscanf($stat, "%dMB %dMB %d%%", $FreeMUsedMB, $FreeMB, $Percent);
+ $ret["FreeMB + UsedMB"] = $FreeMUsedMB;
+ $ret["FreeMB"] = $FreeMB;
+ $ret["UsedMB"] = $FreeMUsedMB - $FreeMB;
+ $ret["Percent"] = $Percent;
+ return $ret;
+ }
+ function StartScan()
+ {
+ if(!$this->handle) return false;
+ $this->Command("SCAN");
+ return true;
+ }
+ function MoveChannel($number, $to)
+ {
+ if(!$this->handle) return false;
+ $this->Command("MOVC $number $to");
+ return true;
+
+ }
+
+ function DeleteTimer($id)
+ {
+ if(!$this->handle) return false;
+ $this->Command("DELT $id");
+ return true;
+ }
+
+ function MoveTimer($number, $to)
+ {
+ if(!$this->handle) return false;
+ $this->Command("MOVT $number $to");
+ return true;
+ }
+
+ function TimerOnOff($n, $state = "on")
+ {
+ if(!$this->handle) return false;
+ //if($state == "1") $state = "on";
+ //if($state == "0") $state = "off";
+ //if($state == false) $state = "off";
+ // if($state == true) $state = "on";
+ switch($state)
+ {
+ case false:
+ case "off":
+ case "0":
+ $state = "off";
+ break;
+ default:
+ $state = "on";
+ break;
+ }
+
+ return $this->Command("MODT $n $state");
+ }
+
+ function ListTimers()
+ {
+
+ }
+
+ function ShowMessage($msg = "")
+ {
+ if(!$this->handle) return false;
+ return $this->Command("MESG $msg");
+ }
+
+ function ListRecords()
+ {
+ if(!$this->handle) return false;
+
+ $lines = $this->Command("LSTR");
+ $records = array();
+ foreach($lines as $l)
+ {
+
+ if(!preg_match("/^(\\d)\s(\\d*)\\.(\\d*)\\.(\\d*) (\\d*)\\:(\\d*).\s(.*)$/", $l, $m)) continue;
+ $id = $m[1];
+ $m["id"] = $m[1];
+ $m["day"] = $m[2];
+ $m["month"] = $m[3];
+ $m["year"] = $m[4];
+ $m["hour"] = $m[5];
+ $m["minute"] = $m[6];
+ $m["desc"] = $m[7];
+
+ $records[$id] = $m;
+ }
+
+ return $records;
+
+ }
+
+ // TODO: perhaps better implementation
+ function ListRecord($n)
+ {
+ if(!$this->handle) return false;
+ $m = $this->Command("LSTR $n");
+ return $m[0];
+ }
+
+
+
+ //TODO: Implement following commands:
+ /*
+
+
+ LSTT MODT NEWT UPDT
+ MODC NEWC
+ NEXT
+ PUTE
+
+ */
+}
+
+// Small Example
+/*
+echo "<pr"."e>";
+$a = new SVDRP();
+$a->Connect();
+print_r($a->Help());
+print_r($a->ListChannels());
+$a->GetKeys();
+print_r($a->GetVolume());
+print_r($a->GetDiskStat());
+$a->Disconnect();
+*/
+?>
+
+
diff --git a/bin/utils.php b/bin/utils.php
new file mode 100755
index 0000000..e748c64
--- /dev/null
+++ b/bin/utils.php
@@ -0,0 +1,95 @@
+<?php
+function is_utf8($str)
+{
+ $c=0; $b=0;
+ $bits=0;
+ $len=strlen($str);
+ for($i=0; $i<$len; $i++)
+ {
+ $c=ord($str[$i]);
+ if($c > 128)
+ {
+ if(($c >= 254)) return false;
+ elseif($c >= 252) $bits=6;
+ elseif($c >= 248) $bits=5;
+ elseif($c >= 240) $bits=4;
+ elseif($c >= 224) $bits=3;
+ elseif($c >= 192) $bits=2;
+ else return false;
+ if(($i+$bits) > $len) return false;
+ while($bits > 1)
+ {
+ $i++;
+ $b=ord($str[$i]);
+ if($b < 128 || $b > 191) return false;
+ $bits--;
+ }
+ }
+ }
+ return true;
+}
+
+function php2js ($var)
+{
+ if (is_array($var))
+ {
+ $array = array();
+
+ foreach ($var as $a_var)
+ $array[] = php2js($a_var);
+
+ return str_replace("\"", "'", join(",", $array));
+
+
+ }
+
+ elseif (is_bool($var))
+ return ($var ? "true" : "false");
+
+ elseif (is_int($var) || is_integer($var) || is_double($var) || is_float($var))
+ return $var;
+
+ elseif (is_string($var))
+ return "\"" .$var . "\"";
+
+ else
+ return false;
+}
+
+function sec2hms ($sec, $padHours = false)
+{
+
+ // holds formatted string
+ $hms = "";
+
+ // there are 3600 seconds in an hour, so if we
+ // divide total seconds by 3600 and throw away
+ // the remainder, we've got the number of hours
+ $hours = intval(intval($sec) / 3600);
+
+ // add to $hms, with a leading 0 if asked for
+ $hms .= ($padHours)
+ ? str_pad($hours, 2, "0", STR_PAD_LEFT). ':'
+ : $hours. ':';
+
+ // dividing the total seconds by 60 will give us
+ // the number of minutes, but we're interested in
+ // minutes past the hour: to get that, we need to
+ // divide by 60 again and keep the remainder
+ $minutes = intval(($sec / 60) % 60);
+
+ // then add to $hms (with a leading 0 if needed)
+ $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':';
+
+ // seconds are simple - just divide the total
+ // seconds by 60 and keep the remainder
+ $seconds = intval($sec % 60);
+
+ // add to $hms, again with a leading 0 if needed
+ $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
+
+ // done!
+ return $hms;
+
+}
+?>
diff --git a/bin/vdr.php b/bin/vdr.php
index beda66c..5aa3c65 100755
--- a/bin/vdr.php
+++ b/bin/vdr.php
@@ -1,5 +1,5 @@
<?php
-include ('./svdrp.php');
+include ('./svdrp_old.php');
function getGlobals()
{
@@ -16,6 +16,50 @@ function getGlobals()
function getTvCat()
{
+ $ret = array();
+ $ret['categories'] = vdrgetcategories();
+
+ return json_encode($ret);
+}
+
+function getFullChanList()
+{
+ $catlist = array();
+
+ // Get all categories
+ $categories = vdrgetcategories();
+
+ // For all categories
+ $count = count($categories);
+ for ($i = 0; $i < $count; $i++)
+ {
+ $tmpcat = array();
+
+ $tmpcat['name'] = $categories[$i]['name'];
+ $tmpcat['channel'] = vdrgetchannels($tmpcat['name'], 0);
+
+ $catlist[] = $tmpcat;
+ }
+
+ $ret = array();
+ $ret['category'] = $catlist;
+
+ return json_encode($ret);
+}
+
+function getTvChan($cat = "")
+{
+ $ret = array();
+ $ret['channel'] = vdrgetchannels($cat, 1);
+
+ return json_encode($ret);
+}
+
+
+/********************* LOCAL ************************/
+
+function vdrgetcategories()
+{
global $vdrchannels;
$catlist = array();
@@ -23,16 +67,14 @@ function getTvCat()
if (!file_exists($vdrchannels))
{
print "Error: channels file not found";
- $ret['categories'] = $catlist;
- return json_encode($ret);
+ return $catlist;
}
$fp = fopen ($vdrchannels,"r");
if (!fp)
{
print "Unable to open channels file";
- $ret['categories'] = $catlist;
- return json_encode($ret);
+ return $catlist;
}
$curcat = "";
@@ -43,7 +85,7 @@ function getTvCat()
// Check if it is a categorie
if ($line[0] == ":")
{
- // Close current cat
+ // Close current category
if ($curcat != "")
{
$tmpcat = array();
@@ -77,12 +119,73 @@ function getTvCat()
fclose($fp);
- $ret['categories'] = $catlist;
- return json_encode($ret);
+ return $catlist;
+}
+
+function vdrgetchannels($category = "", $now)
+{
+ global $vdrchannels;
+
+ $chanlist=array();
+
+ if (!file_exists($vdrchannels))
+ {
+ print "Error: channels file not found";
+ return $chanlist;
+ }
+
+ $fp = fopen ($vdrchannels,"r");
+ if (!fp)
+ {
+ print "Unable to open channels file";
+ return $chanlist;
+ }
+
+ while ($line = fgets($fp, 1024))
+ {
+ if (!$cat_found)
+ {
+ if ($line[0] != ":")
+ continue;
+
+ // Get category name
+ $cat = substr($line, 1, -1);
+ if($cat[0] == '@')
+ {
+ $catarray = explode(' ', $cat);
+ $cat = substr($cat, strlen($cat[0])+1);
+ }
+
+ if ($cat = $category)
+ $cat_found = TRUE;
+ }
+ else if ($line[0] != "")
+ {
+ if ($line[0] == ":")
+ break;
+
+ $channame = explode(":", $line);
+ $channame = explode(";", $channame[0]);
+ $channame = $channame[0];
+
+ $tmpchan = array();
+ $tmpchan['name'] = $channame;
+ $tmpchan['number'] = vdrgetchannum($channame);
+ if ($now)
+ $tmpchan['now_title'] = vdrgetchannow($channame);
+ $chanlist[] = $tmpchan;
+ }
+ }
+
+ fclose($fp);
+
+ return $chanlist;
}
+/********************* LOCAL ************************/
+
function vdrsendcommand($cmd)
{
global $svdrpip, $svdrpport;
@@ -95,6 +198,85 @@ function vdrsendcommand($cmd)
return $ret;
}
+
+function vdrgetchannum($chan = "NULL")
+{
+ global $channels;
+
+ if ($channels == "")
+ $channels = vdrsendcommand("LSTC");
+
+ // Get channel number
+ $chans = preg_grep(quotemeta('"'.$chan.';|'.$chan.':"'), $channels);
+
+ $chans = explode(" ", $chans[key($chans)]);
+ $channum = $chans[0];
+
+ return $channum;
+}
+
+function vdrgetchannow($channame)
+{
+ global $allepg, $allepgfilled;
+
+ // Fill epg if not yet done
+ if ($allepgfilled == 0)
+ {
+ $allepg = vdrsendcommand("LSTE NOW");
+ $allepgfilled = 1;
+ }
+
+ $chanfound = 0;
+ $epgtitle="";
+
+ // For all epg
+ $count = count($allepg);
+ for ($i = 0; $i < $count; $i++)
+ {
+ // Find the right chan (take the first one)
+ if ($chanfound == 0)
+ {
+ if(!ereg("^C", $allepg[$i]))
+ continue;
+
+ if (strstr($allepg[$i], $channame) == $channame)
+ $chanfound = 1;
+ }
+ else
+ {
+ // Now find T or C
+ if(ereg("^C", $allepg[$i]))
+ {
+ if (strstr($allepg[$i], $channame) != $channame)
+ {
+ $chanfound = 0;
+ continue;
+ }
+ }
+ else if(ereg("^T", $allepg[$i]))
+ {
+ $epgtitle = substr($allepg[$i], 2);
+ break;
+ }
+ }
+ }
+
+ // Convert if needed
+ if (!is_utf8($epgtitle))
+ $epgtitle = utf8_encode($epgtitle);
+
+ return $epgtitle;
+}
+
+
+
+
+
+
+
+
+
+
function vdrgetinfostream($stream = "NULL", $ischan = 1)
{
global $allepg, $allepgfilled;
@@ -216,20 +398,6 @@ function vdrgettimerinfo($timernum=-1)
return array($type, $channame, $date, $stime, $etime, $desc);
}
-function vdrgetchannum($chan = "NULL")
-{
- $channels = vdrsendcommand("LSTC");
-
- // Get channel number
- $channels = preg_grep(quotemeta('"'.$chan.';|'.$chan.':"'), $channels);
- reset($channels);
-
- $channels = explode(" ", $channels[key($channels)]);
- $channum = $channels[0];
-
- return $channum;
-}
-
function vdrgetchanname($channum = 0)
{
$channel = vdrsendcommand("LSTC " .$channum);
@@ -243,49 +411,6 @@ function vdrgetchanname($channum = 0)
return $channame;
}
-
-function vdrlistcategories()
-{
- global $vdrchannels;
-
- // All chans
- print "<li class=\"menu\"><a class=\"noeffect\" href=\"javascript:sendForm('All');\"><span class=\"name\">All channels</span><span class=\"arrow\"></span></a></li>\r\n";
- print "<form name=\"All channels\" id=\"All\" method=\"post\" action=\"index.php\"><input name=\"action\" type=\"hidden\" id=\"action\" value=\"listchannels\"/><input name=\"cat\" type=\"hidden\" id=\"cat\" value=\"All\" /></form>\r\n";
-
- if (!file_exists($vdrchannels))
- {
- print "Error: channels file not found";
- return;
- }
-
- $fp = fopen ($vdrchannels,"r");
- if (!fp)
- {
- print "Unable to open channels file";
- return;
- }
- while ($line = fgets($fp, 1024))
- {
- // Check if it is a categorie
- if ($line[0] == ":")
- {
- // Remove : and @
- $cat = substr($line, 1, -1);
- if($cat[0] == '@')
- {
- $cat_array = explode(' ', $cat);
- $cat = substr($cat, strlen($cat_array[0])+1);
- }
-
- $cat2 = addslashes($cat);
-
- print "<li class=\"menu\"><a class=\"noeffect\" href=\"javascript:sendForm('$cat2');\"><span class=\"name\">$cat</span><span class=\"arrow\"></span></a></li>\r\n";
- print "<form name=\"$cat\" id=\"$cat\" method=\"post\" action=\"index.php\"><input name=\"action\" type=\"hidden\" id=\"action\" value=\"listchannels\"/><input name=\"cat\" type=\"hidden\" id=\"cat\" value=\"$cat\" /></form>\r\n";
- }
- }
- fclose($fp);
-}
-
function vdrlistchannels($category = "NULL")
{
global $epgtitle;