Svdrp-php

Aus VDR Wiki
Version vom 11. Dezember 2004, 03:39 Uhr von Nollsen (Diskussion | Beiträge)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche


 <?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 = 1)
 	{	
 		$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");
 		
 		while($s = fgets($this->handle,512))
 		{
 			 
                          $this->DebugMessage($s);
 			 preg_match("/^(\\d{3})(.)(.*)$/", $s, $data);
 			 $number = $data[1];
 			 // TODO: Fehlernummer bearbeiten
 			 $ret[] = $data[3];
  			 if($data[2] != "-") break; 
 			 
 		}
 		$this->DebugMessage("</p"."re>");
 		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");
 	}
 }
 
 // Small Example
 $a = new SVDRP();
 $a->Connect();
 print_r($a->Help());
 print_r($a->ListChannels());
 $a->Disconnect();
 ?>