<?php
class channel
{
	public $name='';
	public $srvref='';
	public $channum=-1;
	public $tsserviceid;
	public $xmltvid;
}
$chanlist=array();

//try to find xmltvid for all the channels in $chanlist array
//sourceid is optional, if it is not >0 all existing channels will be searched
//usefull if the dreambox channels overlap with one or more other sources
function getxmltvids($dbhost,$dbuser,$dbpassword,$sourceid)
{
	global $chanlist;
	$mysqli = new mysqli($dbhost,$dbuser,$dbpassword,"mythconverg");
	if ($mysqli->connect_errno)
	{
		exit("Failed to connect to MySQL: " . $mysqli->connect_error);
	}
	
	$query="SELECT DISTINCT name,xmltvid FROM channel WHERE length(xmltvid)>0";
	if(is_numeric($sourceid) && $sourceid>0)
	{
		$tmp=$mysqli->real_escape_string($sourceid);
		$query=$query." AND sourceid=$tmp";
	}
	
	$res = $mysqli->query($query);
	while($row=$res->fetch_assoc())
	{
		foreach($chanlist as $channel)
		{
			if($channel->name == $row["name"])
			{
				$channel->xmltvid=$row["xmltvid"];
			}
		}
	}
}

//defaults
$host='';
$webifport=80;
$streamport=8001;
$bouquet='Favourites (TV)';
$sourceid=0;

//database access
//change as needed
$mysqlserver='127.0.0.1';
$mysqluser='mythtv';
$mysqlpass='mythtv';


if(empty($_REQUEST))
{
	?>
	<form method="get">
	<table>
	<tr><td>Dreambox hostname:</td><td><input type="text" name="host" value="<?php echo $host?>"></td><td>mandatory</td></tr>
	<tr><td>Dreambox webif port:</td><td><input type="text" name="webifport" value="<?php echo $webifport?>"></td><td>optional</td></tr>
	<tr><td>Bouquet:</td><td><input type="text" name="bouquet" value="<?php echo $bouquet?>"></td><td>mandatory</td></tr>
	<tr><td>Stream port:</td><td><input type="text" name="streamport" value="<?php echo $streamport?>"></td><td>optional</td></tr>
	<tr><td>sourceid:</td><td><input type="text" name="sourceid" value="<?php echo $sourceid?>"></td><td>optional, used for matching channel names with xmltvids</td></tr>
	<tr><td colspan=2><input type="submit"></td></tr>
	</table>
	</form>
	<?php
	exit();
}
else
{
	if(empty($_REQUEST["host"]) || empty($_REQUEST["bouquet"]))
	{
		exit('both host and bouquet must be specified');
	}
	$host=$_REQUEST["host"];
	$bouquet=$_REQUEST["bouquet"];
	
	if(!empty($_REQUEST["webifport"]) && is_numeric($_REQUEST["webifport"]))
	{
		$webifport=$_REQUEST["webifport"];
	}
	if(!empty($_REQUEST["streamport"]) && is_numeric($_REQUEST["streamport"]))
	{
		$streamport=$_REQUEST["streamport"];
	}
	if(!empty($_REQUEST["sourceid"]) && is_numeric($_REQUEST["sourceid"]))
	{
		$sourceid=$_REQUEST["sourceid"];
	}
}

$fp=fopen("http://$host:$webifport/web/getallservices",'rb')
	or exit("unable to open service list from dreambox");
$contents=stream_get_contents($fp);
fclose($fp);

$doc=new DOMDocument();
$doc->loadXML($contents);

$xpath=new DOMXPath($doc);
//$elements=$xpath->query('/e2servicelistrecursive/e2bouquet/e2servicename');
$services=$xpath->query("/e2servicelistrecursive/e2bouquet/e2servicelist/e2service[../../e2servicename='$bouquet']");
if(!is_null($services))
{
	//e2service
	$cnt=1;
	foreach($services as $service)
	{

		$srvref="";
		$srvname="";
		foreach($service->childNodes as $node)
		{
			if($node->nodeName=='e2servicereference')
			{
				$srvref=$node->nodeValue;
			}
			else if($node->nodeName=='e2servicename')
			{
				$srvname=$node->nodeValue;
			}
		}
		$channel=new channel();
		$channel->name=$srvname;
		$channel->srvref=$srvref;
		$channel->channum=$cnt;
		
		//Try to find the serviceid.
		//Note that if you have grouped several channels on the dreambox this
		//wont work because the service reference from the dreambox doesn't
		//contain the serviceid.
		$srvrefparts=explode(":",$srvref);
		if(count($srvrefparts)>4)
		{
			$tmp=hexdec($srvrefparts[3]);
			if($tmp>0)
			{
				$channel->tsserviceid=$tmp;
			}
		}
		getxmltvids($mysqlserver,$mysqluser,$mysqlpass,$sourceid);
		
		$chanlist[]=$channel;
		$cnt++;
	}
	
	//create the m3u file
	//header('Content-type: audio/x-mpegurl');
	//header('Content-Disposition: attachment; filename="channels.m3u"');
	echo "#EXTM3U\n";
	foreach($chanlist as $channel)
	{
		echo "#EXTINF:0,$channel->channum - $channel->name\n";
		if(is_string($channel->xmltvid) && strlen($channel->xmltvid)>0)
		{
			echo "#EXTMYTHTV:xmltvid=$channel->xmltvid\n";
		}
		if(is_int($channel->tsserviceid))
		{
			echo "#EXTMYTHTV2:serviceid=$channel->tsserviceid\n";
		}
		echo "http://$host:$streamport/$channel->srvref\n";
	}
}
	
?>