<?php
//https://xmltv.ch/
//header("Content-Type: audio/mpegurl;");
$prefix = isset($_REQUEST['x'])?true:false;
$double1 = isset($_REQUEST['y'])?true:false;
print("#EXTM3U\n");
$db = new SQLite3('iptv.db');
$req = "SELECT `groups`.`name` as grp, `channels`.`logo` as logo,`selchannels`.`name` AS name,`channels`.`url` AS url	
       FROM selchannels INNER JOIN `groups` on `groups`.`idg`=`selchannels`.`idg` INNER JOIN `channels` ON `selchannels`.`name`=`channels`.`name` 
	   ORDER BY `groups`.`order`,`selchannels`.`order`;";
	$ret = $db->query($req);
	$chans=array();
	$c = 0;
	while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {
		$group = $row['grp'];
		$logo = $row['logo'];
		$name = $row['name'];
		$url =  $row['url'].($prefix?"&".substr($group,0,1):"");
		print("#EXTINF:0 group-title=\"$group\"tvg-logo=\"$logo\",$name\n$url\n");
		if ($c == 0 && $double1)
		 print("#EXTINF:0 group-title=\"$group\"tvg-logo=\"$logo\",$name\n$url\n");
	    $c++;
			
	}
$req = "SELECT `channels`.`group` as grp, `channels`.`logo` as logo,`channels`.`name` AS name,`channels`.`url` AS url	
       FROM channels INNER JOIN `rawgroups` on `channels`.`group`=`rawgroups`.`name`";
	$ret = $db->query($req);

	
	while($row = $ret->fetchArray(SQLITE3_ASSOC) ) {
		$group = $row['grp'];
		$logo = $row['logo'];
		$name = $row['name'];
		$url =  $row['url'].($prefix?"&".substr($group,0,1):"");
		print("#EXTINF:0 group-title=\"$group\"tvg-logo=\"$logo\",$name\n$url\n");			
	}
	
	
		
		
?>