<?php
function gen_icsjw($idnum)
{
  //ics
  //sala
  if($idnum==0){return;}
  	include('../_cfg/config.php');
 	include('php_session.php');
$eol="\r\n";  
// $eol="\n";  

//   file_put_contents($rutadoc.$docu_idnum.'.ics',$txt_ics);
     
  $sql_icse='select * from jwib11 where idnum1='.$idnum;
//   $sql_icse='select * from jwib11 where idnum1='.$idnum.' and fecha>="'.date('Y-m-d').'"';
  $res_icse=msql_query($sql_icse,$link_vcrx);
//   echo '***'.$sql_icse;
     
  
  $docu=msql_result($res_icse,0,'docu');
  $fecha=msql_result($res_icse,0,'fecha');
  $docudes=msql_result($res_icse,0,'docu_descri');
  $docu_idnum=$docu.'_'.fillzero($idnum);
     
  unlink($rutadoc.$docu_idnum.'.ics');
  if($fecha<date('Y-m-d')){return;}

     
//   $docu_idnum=$docu.'_'.($idnum);
  
 				if($docu=='VM30'){$jwol_smm=msql_result($res_icse,0,'jwol_smm0');} 
				if($docu=='VM31'){$jwol_smm=msql_result($res_icse,0,'jwol_smm1');} 
				if($docu=='VM32'){$jwol_smm=msql_result($res_icse,0,'jwol_smm2');} 
				if($docu=='VM33'){$jwol_smm=msql_result($res_icse,0,'jwol_smm3');} 
  
  $salades='PRINCIPAL';
  $sala=msql_result($res_icse,0,'sala');
  if($sala>0){$salades=$sala;}
  
  $nombre1=msql_result($res_icse,0,'nombre1');
  $nombre2=msql_result($res_icse,0,'nombre2');
  $xorat=msql_result($res_icse,0,'orat');

  $fechar=msql_result($res_icse,0,'fechar');
  $alarm=date('Ymd',strtotime('-1 day',strtotime($fechar)));
  $fechar=str_replace('-','',$fechar);
//   $alarm=str_replace('-','',$alarm);

// echo $alarm;
  $referencia=$jwol_smm;
  $referencia=substr($referencia,strpos($referencia,'http'),1000);
  $referencia=substr($referencia,0,strpos($referencia,'">'));

  $description=$jwol_smm;
  $description=substr($description,strpos($description,'<strong>')+8,1000);
  $description=substr($description,0,strrpos($description,'</strong>'));
    
  $summary=$jwol_smm;
  $summary=substr($summary,strpos($summary,'<strong>')+8,1000);
  $summary=substr($summary,0,strrpos($summary,'</strong>'));
  
  if($summary==''){$summary=$docudes;}


  $xrefn=$xorat;
  if($xorat>=1){$xrefn=$xorat+1;}
  if($xorat>=34){$xrefn=$xorat+2;}
  if($xorat>=35){$xrefn=$xorat+3;}
  $xref= 'http://wol.jw.org/es/wol/d/r4/lp-s/110200'.(1068+$xrefn); 
										
 
  
//  BEGIN:VALARM
// TRIGGER:-PT1440M
// ACTION:DISPLAY
// DESCRIPTION:Reminder
// END:VALARM
  
  $tx_tics='';
// BEGIN:VCALENDAR
// VERSION:1.0
  
  $txt_ics.='BEGIN:VCALENDAR'.$eol;
  $txt_ics.='VERSION:2.0'.$eol;
  
  
  $txt_ics.='BEGIN:VEVENT'.$eol;
  $txt_ics.='DTSTART:'.$fechar.'T190000'.''.$eol;
  $txt_ics.='DTEND:'.$fechar.'T210000'.''.$eol;
//   $txt_ics.='BEGIN:VTODO'.$eol;
//   $txt_ics.='DUE:'.$fechar.'T190000'.'Z'.$eol;
//   $txt_ics.='SUMMARY:'.$summary.$eol;
  $txt_ics.='SUMMARY:'.$summary.$eol;
  $txt_ics.='DESCRIPTION:';
  $txt_ics.=' - '.$nombre1.' / '.$nombre2;
  
  if($xorat!='')
  {$txt_ics.=' - ORATORIA :'.$xorat.' / '.$xref;}
  
  $txt_ics.=' - '.'REF : '.$referencia;
  $txt_ics.=$eol;
  $txt_ics.='LOCATION: Cong. '.r_fiel('cong',$cong_codigo).$eol;
  
  if($xorat!='')
  {$txt_ics.=' - Sala '.$salades.$eol;}

// TRIGGER:-P2D  
//   TRIGGER;VALUE=DATE-TIME:19980101T050000Z
//   REPEAT:23
//   DURATION:PT1H
  
  $txt_ics.='BEGIN:VALARM'.$eol;
  $txt_ics.='TRIGGER:-P1D'.$eol;
//   $txt_ics.='TRIGGER;VALUE=DATE-TIME:'.$alarm.'T120000'.$eol;
  $txt_ics.='ACTION:DISPLAY'.$eol;
  $txt_ics.='DESCRIPTION:'.$summary.$eol;
  $txt_ics.='END:VALARM'.$eol;
  
//   BEGIN:VALARM
// ACTION:AUDIO
// TRIGGER:19980414T120000
// ATTACH;FMTTYPE=audio/basic:http://example.com/pub/audio-
//  files/ssbanner.aud
// REPEAT:4
// DURATION:PT1H
// END:VALARM
  
  
  
  
  $txt_ics.='END:VEVENT'.$eol;
//   $txt_ics.='END:VTODO'.$eol;
  
  $txt_ics.='END:VCALENDAR'.$eol;
  
  file_put_contents($rutadoc.$docu_idnum.'.ics',$txt_ics);

// BEGIN:VEVENT
// DTSTART:20140925T090000
// DTEND:20140925T100000
// LOCATION:
// DESCRIPTION:Purpose: Provide example of this file type Document file type: ICS Version: 1.0 Created by http://www.online-convert.com More example files: http://www.online-convert.com/file-type License: http://creativecommons.org/licenses Feel free to use & share the file according to the license above.
// SUMMARY:ICS test file
// PRIORITY:3
// END:VEVENT
// END:VCALENDAR

// BEGIN:VCALENDAR
// VERSION:2.0
// PRODID:-//hacksw/handcal//NONSGML v1.0//EN
// BEGIN:VEVENT
// UID:uid1@example.com
// DTSTAMP:19970714T170000Z
// ORGANIZER;CN=John Doe:MAILTO:john.doe@example.com
// DTSTART:19970714T170000Z
// DTEND:19970715T035959Z
// SUMMARY:Bastille Day Party
// END:VEVENT
// END:VCALENDAR
 
}
  
  
  
function get_jwtv1($lang,$fecha1='') 
{
//      if($lang!='S'){return;}
	include('../_cfg/config.php');
//   echo $jworg1;
//                               ws wide screen
//                               ls long screen
//                               pn pannel
//                               sq square X
//                               r regular
//                               s small X

 $lang=strtoupper($lang); 
  echo '<br>';
  echo date('H:i:s',strtotime('now')); 

  $jworg='https://mediator.jw.org/v1/categories/'.$lang.'/';
	$jworg1=$jworg.'VideoOnDemand?detailed=1';
  $page = file_get_contents($jworg1);
	$json1=json_decode($page,true,512,JSON_BIGINT_AS_STRING);
  if($fecha1=='')
  {$fecha1=date("Y-m-d",strtotime('today -100 month'));}
     
//      echo'<br>'.$jworg1;
//      echo'<br>'.$json1;
//      return;

  $ccat=0;
	foreach ($json1 as $key1=>$val1)
	{
		foreach($val1 as $key2=>$val2)
		{
			if(gettype($val2)=="array")
			{
				foreach($val2 as $key3=>$val3)
				{
					if(gettype($val3)=="array")
					{
						foreach($val3 as $key4=>$val4)
						{
              if($key4=='key'){$categ=$val4;}
              if($key4=='name'){$categdes=$val4;}
              if($key4=='key')
              {
              $ccat++;
              $categ=$val4;
							echo '<br>'.str_repeat('=',20);
              echo 'CATEGORIA:';
              echo $key4.'==> '.$val4;
              get_jwtv2($lang,$categ);
//               get_jwtv2($lang,'VODChildren');
              }
 							if(gettype($val4)=="array")
							{
								foreach($val4 as $key5=>$val5)
								{
									if(gettype($val5)=="array")
									{
										foreach($val5 as $key6=>$val6)
										{
                      if(mb_stristr('"wsr"."lsr"',$key5))
                      {  
                        if($key6=='md'){$imgmd=$val6;}
                        if($key6=='lg'){$imglg=$val6;}
                        if($key6=='xl'){$imgxl=$val6;}
                        $imagen=$imgmd;
                        if($imglg!=''){$imagen=$imglg;}
                        if($imgxl!=''){$imagen=$imgxl;}
                      }
// 											if(gettype($val6)=="array")
// 											{
//                         if($key6=='files' or $key6=='images')
//                         {
// 												foreach($val6 as $key7=>$val7)
// 												{
// 													if(gettype($val7)=="array")
// 													{	
// 														foreach($val7 as $key8=>$val8)
// 														{
// 															if(gettype($val8)=="array")
// 															{
// 																foreach($val8 as $key9=>$val9)
// 																{
// // 																	echo '<br>9'.str_repeat('*',9);echo $key9.'==> '.$val9;
// 																}	
// 															}
// 														}
// 													}
// 												}
//                         }
// 											}
										}	
//                     {
//                       if(substr($categ,0,3)=='VOD'){$categ=substr($categ,3);}
//                       if(substr($categ1,0,3)=='VOD'){$categ1=substr($categ1,3);}
//                       $codigo=$guia.$lang.$categ.$categ1;

//                       $sql_jwtv='insert into jwtv0(jwtv_codigo,jwtv_idate)value("'.$codigo.'",now())';
//                       $res_jwtv=msql_query($sql_jwtv,$link_vcrx);

//                       $sql_jwtv ='update jwtv0 set ';
//                       $sql_jwtv.='jwtv_descri="'.$titulo.'",';
//                       $sql_jwtv.='jwtv_title="'.$titulo.'",';
//                       $sql_jwtv.='jwtv_guia="'.$guia.'",';
//                       $sql_jwtv.='jwtv_categ0="'.$categ.'",';
// //                       $sql_jwtv.='jwtv_categ0n="'.$categn.'",';
// //                       $sql_jwtv.='jwtv_categ0d="'.$categdes.'",';
//                       $sql_jwtv.='jwtv_categ1="'.$categ0.'",';
//                       $sql_jwtv.='jwtv_categ1d="'.$categ0des.'",';
//                       $sql_jwtv.='jwtv_lang="'.$lang.'",';
//                       $sql_jwtv.='jwtv_segundos="'.$segundos.'",';
//                       $sql_jwtv.='jwtv_publi="'.$publicado.'",';
//                       $sql_jwtv.='jwtv_modif="'.$modif.'",';
//                       $sql_jwtv.='jwtv_mediamp3="'.$mediamp3.'",';
//                       $sql_jwtv.='jwtv_media240="'.$media240.'",';
//                       $sql_jwtv.='jwtv_media360="'.$media360.'",';
//                       $sql_jwtv.='jwtv_media480="'.$media480.'",';
//                       $sql_jwtv.='jwtv_media720="'.$media720.'",';
//                       $sql_jwtv.='jwtv_media1080="'.$media1080.'",';
//                       $sql_jwtv.='jwtv_media240s="'.$media240s.'",';
//                       $sql_jwtv.='jwtv_media360s="'.$media360s.'",';
//                       $sql_jwtv.='jwtv_media480s="'.$media480s.'",';
//                       $sql_jwtv.='jwtv_media720s="'.$media720s.'",';
//                       $sql_jwtv.='jwtv_media1080s="'.$media1080s.'",';
//                       $sql_jwtv.='jwtv_image="'.$imagen.'",';
//                       $sql_jwtv.='jwtv_status="-"';
//                       $sql_jwtv.='where jwtv_codigo="'.$codigo.'"';
//                       $res_jwtv=msql_query($sql_jwtv,$link_vcrx);

//                       echo '<br>';
//                       echo  '<br>*  '.$key6;
//                       echo  '<br>**  '.$categ;
//                       echo  '<br>****  '.$categ0;
//                       echo  '<br>******  '.$categ0des;
//                       echo  '<br>********  GUID:';
//                       echo '<br>'.$categ1;
//                       echo '<br>'.$guia;
//                       echo '<br>'.$titulo;
//                       echo '<br>'.$tipo;
//                       echo '<br>'.$segundos;
//                       echo '<br>'.$publicado;
//                       echo '<br>'.$modif;
//                       echo '<br>'.$exte;
//                       echo '<br>'.$reso;
//                       echo '<br>'.$imagen;
//                       echo '<br>'.$mediamp3;
//                       echo '<br>'.$media240;
//                       echo '<br>'.$media360;
//                       echo '<br>'.$media480;
//                       echo '<br>'.$media720;
//                       echo '<br>'.$media1080;
//                       $tipo='';
//                       $mediamp3='';
//                       $media240='';
//                       $media360='';
//                       $media480='';
//                       $media720='';
//                       $media1080='';
//                       $media240s='';
//                       $media360s='';
//                       $media480s='';
//                       $media720s='';
//                       $media1080s='';
//                       $imagen='';
//                       $imgmd='';
//                       $imglg='';
//                       $imgxl='';
//                     }
                    
									}
								}	
							}
           
              
              
            }
            if($ccat>0)
            {
              $sql_jwtv ='update jwtv0 set ';
              $sql_jwtv.='jwtv_categ0n="'.$ccat.'",';
              $sql_jwtv.='jwtv_categ0="'.$categ.'",';
              $sql_jwtv.='jwtv_categ0d="'.$categdes.'",';
              $sql_jwtv.='jwtv_image0="'.$imagen.'",';
              $sql_jwtv.='jwtv_status="-"';
              $sql_jwtv.=' where jwtv_categ0 like "%'.trim(substr($categ,3,100)).'%"';
              $sql_jwtv.=' and jwtv_lang="'.$lang.'"';
              $res_jwtv=msql_query($sql_jwtv,$link_vcrx);
//               echo '<br>'.$sql_jwtv;
            }
					}
				}
			}
		}
		echo '<br>';
		echo '***************************************';
		echo '<br>';
	}	

  get_jwtv2($lang,'Audio');
  
//   $jworg='https://mediator.jw.org/v1/categories/'.$lang.'/';
// 	$jworg1=$jworg.'Audio?detailed=1';
//   $page = file_get_contents($jworg1);
// 	$json1=json_decode($page,true,512,JSON_BIGINT_AS_STRING);
//   if($fecha1=='')
//   {$fecha1=date("Y-m-d",strtotime('today -240 month'));}

//   $ccat=0;
// 	foreach ($json1 as $key1=>$val1)
// 	{
// 		foreach($val1 as $key2=>$val2)
// 		{
// 			if(gettype($val2)=="array")
// 			{
// 				foreach($val2 as $key3=>$val3)
// 				{
// 					if(gettype($val3)=="array")
// 					{
// 						foreach($val3 as $key4=>$val4)
// 						{
//               if($key4=='key'){$categ=$val4;}
//               if($key4=='name'){$categdes=$val4;}
//               if($key4=='key')
//               {
//               $ccat++;
//               $categ=$val4;
// 							echo '<br>'.str_repeat('=',20);
//               echo 'CATEGORIA:';
//               echo $key4.'==> '.$val4;
//               get_jwtv2($lang,$categ);
// //               get_jwtv2($lang,'VODChildren');
//               }
//  							if(gettype($val4)=="array")
// 							{
// 								foreach($val4 as $key5=>$val5)
// 								{
// 									if(gettype($val5)=="array")
// 									{
// 										foreach($val5 as $key6=>$val6)
// 										{
//                       if(mb_stristr('"wsr"."lsr"',$key5))
//                       {  
//                         if($key6=='md'){$imgmd=$val6;}
//                         if($key6=='lg'){$imglg=$val6;}
//                         if($key6=='xl'){$imgxl=$val6;}
//                         $imagen=$imgmd;
//                         if($imglg!=''){$imagen=$imglg;}
//                         if($imgxl!=''){$imagen=$imgxl;}
//                       }
// // 											if(gettype($val6)=="array")
// // 											{
// //                         if($key6=='files' or $key6=='images')
// //                         {
// // 												foreach($val6 as $key7=>$val7)
// // 												{
// // 													if(gettype($val7)=="array")
// // 													{	
// // 														foreach($val7 as $key8=>$val8)
// // 														{
// // 															if(gettype($val8)=="array")
// // 															{
// // 																foreach($val8 as $key9=>$val9)
// // 																{
// // // 																	echo '<br>9'.str_repeat('*',9);echo $key9.'==> '.$val9;
// // 																}	
// // 															}
// // 														}
// // 													}
// // 												}
// //                         }
// // 											}
// 										}	
// //                     {
// //                       if(substr($categ,0,3)=='VOD'){$categ=substr($categ,3);}
// //                       if(substr($categ1,0,3)=='VOD'){$categ1=substr($categ1,3);}
// //                       $codigo=$guia.$lang.$categ.$categ1;

// //                       $sql_jwtv='insert into jwtv0(jwtv_codigo,jwtv_idate)value("'.$codigo.'",now())';
// //                       $res_jwtv=msql_query($sql_jwtv,$link_vcrx);

// //                       $sql_jwtv ='update jwtv0 set ';
// //                       $sql_jwtv.='jwtv_descri="'.$titulo.'",';
// //                       $sql_jwtv.='jwtv_title="'.$titulo.'",';
// //                       $sql_jwtv.='jwtv_guia="'.$guia.'",';
// //                       $sql_jwtv.='jwtv_categ0="'.$categ.'",';
// // //                       $sql_jwtv.='jwtv_categ0n="'.$categn.'",';
// // //                       $sql_jwtv.='jwtv_categ0d="'.$categdes.'",';
// //                       $sql_jwtv.='jwtv_categ1="'.$categ0.'",';
// //                       $sql_jwtv.='jwtv_categ1d="'.$categ0des.'",';
// //                       $sql_jwtv.='jwtv_lang="'.$lang.'",';
// //                       $sql_jwtv.='jwtv_segundos="'.$segundos.'",';
// //                       $sql_jwtv.='jwtv_publi="'.$publicado.'",';
// //                       $sql_jwtv.='jwtv_modif="'.$modif.'",';
// //                       $sql_jwtv.='jwtv_mediamp3="'.$mediamp3.'",';
// //                       $sql_jwtv.='jwtv_media240="'.$media240.'",';
// //                       $sql_jwtv.='jwtv_media360="'.$media360.'",';
// //                       $sql_jwtv.='jwtv_media480="'.$media480.'",';
// //                       $sql_jwtv.='jwtv_media720="'.$media720.'",';
// //                       $sql_jwtv.='jwtv_media1080="'.$media1080.'",';
// //                       $sql_jwtv.='jwtv_media240s="'.$media240s.'",';
// //                       $sql_jwtv.='jwtv_media360s="'.$media360s.'",';
// //                       $sql_jwtv.='jwtv_media480s="'.$media480s.'",';
// //                       $sql_jwtv.='jwtv_media720s="'.$media720s.'",';
// //                       $sql_jwtv.='jwtv_media1080s="'.$media1080s.'",';
// //                       $sql_jwtv.='jwtv_image="'.$imagen.'",';
// //                       $sql_jwtv.='jwtv_status="-"';
// //                       $sql_jwtv.='where jwtv_codigo="'.$codigo.'"';
// //                       $res_jwtv=msql_query($sql_jwtv,$link_vcrx);

// //                       echo '<br>';
// //                       echo  '<br>*  '.$key6;
// //                       echo  '<br>**  '.$categ;
// //                       echo  '<br>****  '.$categ0;
// //                       echo  '<br>******  '.$categ0des;
// //                       echo  '<br>********  GUID:';
// //                       echo '<br>'.$categ1;
// //                       echo '<br>'.$guia;
// //                       echo '<br>'.$titulo;
// //                       echo '<br>'.$tipo;
// //                       echo '<br>'.$segundos;
// //                       echo '<br>'.$publicado;
// //                       echo '<br>'.$modif;
// //                       echo '<br>'.$exte;
// //                       echo '<br>'.$reso;
// //                       echo '<br>'.$imagen;
// //                       echo '<br>'.$mediamp3;
// //                       echo '<br>'.$media240;
// //                       echo '<br>'.$media360;
// //                       echo '<br>'.$media480;
// //                       echo '<br>'.$media720;
// //                       echo '<br>'.$media1080;
// //                       $tipo='';
// //                       $mediamp3='';
// //                       $media240='';
// //                       $media360='';
// //                       $media480='';
// //                       $media720='';
// //                       $media1080='';
// //                       $media240s='';
// //                       $media360s='';
// //                       $media480s='';
// //                       $media720s='';
// //                       $media1080s='';
// //                       $imagen='';
// //                       $imgmd='';
// //                       $imglg='';
// //                       $imgxl='';
// //                     }
                    
// 									}
// 								}	
// 							}
           
              
              
//             }
//             if($ccat>0)
//             {
//               $sql_jwtv ='update jwtv0 set ';
//               $sql_jwtv.='jwtv_categ0n="'.$ccat.'",';
// //               $sql_jwtv.='jwtv_categ0="'.$categ.'",';
//               $sql_jwtv.='jwtv_categ0d="'.$categdes.'",';
//               $sql_jwtv.='jwtv_image0="'.$imagen.'",';
//               $sql_jwtv.='jwtv_status="-"';
//               $sql_jwtv.=' where jwtv_categ0 like "%'.trim(substr($categ,3,100)).'%"';
//               $sql_jwtv.=' and jwtv_lang="'.$lang.'"';
//               $res_jwtv=msql_query($sql_jwtv,$link_vcrx);
//             }
// 					}
// 				}
// 			}
// 		}
// 		echo '<br>';
// 		echo '***************************************';
// 		echo '<br>';
// 	}	

  
  $sql_updt='update jwtv0 set jwtv_categ0=substring(jwtv_categ0,4) where jwtv_categ0 like "VOD%"';
  $res_updt=msql_query($sql_updt,$link_vcrx); 
    
  $sql_updt='update jwtv0 set jwtv_categ1=substring(jwtv_categ1,4) where jwtv_categ1 like "VOD%"';
  $res_updt=msql_query($sql_updt,$link_vcrx); 
  
  
  //relleno
//   $sql_updt='update jwtv0 set jwtv_categ1=substring(jwtv_categ1,4) where jwtv_categ1 like "VOD%"';
//   $res_updt=msql_query($sql_updt,$link_vcrx); 
  
    
  $sql_updt='update jwtv0 set jwtv_categ0="Audio", jwtv_categ0n=99 where jwtv_categ0 like "Audio%"';
  $res_updt=msql_query($sql_updt,$link_vcrx); 
    
  $sql_dele='delete from jwtv0 where jwtv_guia="" or jwtv_guia is null';
  $res_dele=msql_query($sql_dele,$link_vcrx); 
    
	echo '*** FIN ***';
  
  
}

function get_jwtv2($lang,$categ,$fecha1='') 
{
//   return;
	include('../_cfg/config.php');
// 	include('php_gene1.php'); 
  echo '<br>';
  echo date('H:i:s',strtotime('now')); 
  
  if($fecha1==''){$fecha1="2008-01-01";};

	$jworg='https://mediator.jw.org/v1/categories/'.$lang.'/';
	$jworg1=$jworg.$categ.'?detailed=1';
	$page = file_get_contents($jworg1);
	$json1=json_decode($page,true,512,JSON_BIGINT_AS_STRING);
     
//      if(!mb_stristr($jworg1,'rogramsEvents')){return;}
//      echo'<br>'.$jworg1;
//      echo'<br>'.$json1;
//      return;
{
	foreach ($json1 as $key1=>$val1)
	{
		foreach($val1 as $key2=>$val2)
		{
               if($key2=='name'){$categdes=$val2;}
			if(gettype($val2)=="array")
			{
				foreach($val2 as $key3=>$val3)
				{
					if(gettype($val3)=="array")
					{
						foreach($val3 as $key4=>$val4)
						{
              if($key4=='key'){$categ0=$val4;}
              if($key4=='name'){$categ0des=$val4;}
							if(gettype($val4)=="array")
							{
								foreach($val4 as $key5=>$val5)
								{
									if(gettype($val5)=="array")
									{
										foreach($val5 as $key6=>$val6)
										{
                      if($key6=='primaryCategory'){$categ1=$val6;}
                      if($key6=='guid'){$guia=$val6;}
                      if($key6=='type'){$tipo=$val6;}
                      if($key6=='title'){$titulo=$val6;}
                      if($key6=='duration'){$segundos=$val6;}
                      if($key6=='firstPublished'){$publi=$val6;}
											if(gettype($val6)=="array")
											{
                        if($key6=='files' or $key6=='images')
                        {
                          foreach($val6 as $key7=>$val7)
                          {
                            if(gettype($val7)=="array")
                            {	
                              foreach($val7 as $key8=>$val8)
                              {
                                if($key8=='progressiveDownloadURL')
                                {
                                  $media=$val8;
                                  if(mb_stristr($media,'.mp4'))
                                  {
                                    if(!mb_stristr($media,'-Sub_'))
                                    {
                                      if(mb_stristr($media,'240P')){$media240=$media;}
                                      if(mb_stristr($media,'360P')){$media360=$media;}
                                      if(mb_stristr($media,'480P')){$media480=$media;}
                                      if(mb_stristr($media,'720P')){$media720=$media;}
                                      if(mb_stristr($media,'1080P')){$media1080=$media;}
                                    }
                                    if(mb_stristr($media,'-Sub_'))
                                    {
                                      if(mb_stristr($media,'240P')){$media240s=$media;}
                                      if(mb_stristr($media,'360P')){$media360s=$media;}
                                      if(mb_stristr($media,'480P')){$media480s=$media;}
                                      if(mb_stristr($media,'720P')){$media720s=$media;}
                                      if(mb_stristr($media,'1080P')){$media1080s=$media;}
                                    }
                                    $mediamp3='';
                                  }
                                }
                                if($key8=='modifiedDatetime'){$modif=$val8;}
                                if($key8=='mimetype'){$exte=$val8;}
                                if($key8=='frameHeight'){$reso=$val8;}
                                if($tipo=='audio'){$mediamp3=$media;}

                                if(mb_stristr('"wsr"."lsr"',$key7))
                                {
                                  if($key8=='md'){$imgmd=$val8;}
                                  if($key8=='lg'){$imglg=$val8;}
                                  if($key8=='xl'){$imgxl=$val8;}
                                  $imagen=$imgmd;
                                  if($imglg!=''){$imagen=$imglg;}
                                  if($imgxl!=''){$imagen=$imgxl;}
                                }
  // 															if(gettype($val8)=="array")
  // 															{
  // 																foreach($val8 as $key9=>$val9)
  // 																{
  // 																	echo '<br>9'.str_repeat('*',9);echo $key9.'==> '.$val9;
  // 																}	
  // 															}
                              }
                            }
                          }
                        }
											}
										}	

                    {
                      if(substr($categ,0,3)=='VOD'){$categ=substr($categ,3,100);}
                      if(substr($categ1,0,3)=='VOD'){$categ1=substr($categ1,3,100);}
                      $codigo=$guia.$lang.$categ.$categ1;
                      
                      
//                       if($modif>=$fecha1)
                      {
                        {
                        if($media720==''){$media720=$media1080;}
                        if($media480==''){$media480=$media720;}
                        if($media360==''){$media360=$media480;}
                        if($media240==''){$media240=$media360;}

                        if($media360==''){$media360=$media240;}
                        if($media480==''){$media480=$media360;}
                        if($media720==''){$media720=$media480;}
                        if($media1080==''){$media1080=$media720;}
                        
                        if($media720s==''){$media720s=$media1080s;}
                        if($media480s==''){$media480s=$media720s;}
                        if($media360s==''){$media360s=$media480s;}
                        if($media240s==''){$media240s=$media360s;}

                        if($media360s==''){$media360s=$media240s;}
                        if($media480s==''){$media480s=$media360s;}
                        if($media720s==''){$media720s=$media480s;}
                        if($media1080s==''){$media1080s=$media720s;}
                        }
                        
                        $sql_jwtv='insert into jwtv0(jwtv_codigo,jwtv_idate)value("'.$codigo.'",now())';
                        $res_jwtv=msql_query($sql_jwtv,$link_vcrx);

                        $sql_jwtv ='update jwtv0 set ';
                        $sql_jwtv.='jwtv_descri="'.$titulo.'",';
                        $sql_jwtv.='jwtv_title="'.$titulo.'",';
                        $sql_jwtv.='jwtv_guia="'.$guia.'",';
                        $sql_jwtv.='jwtv_categ0="'.$categ.'",';
  //                       $sql_jwtv.='jwtv_categ0n="'.$categn.'",';
  //                       $sql_jwtv.='jwtv_categ0d="'.$categdes.'",';
                        $sql_jwtv.='jwtv_categ1="'.$categ0.'",';
                        $sql_jwtv.='jwtv_categ1d="'.$categ0des.'",';
                        $sql_jwtv.='jwtv_lang="'.$lang.'",';
                        $sql_jwtv.='jwtv_segundos="'.$segundos.'",';
                        $sql_jwtv.='jwtv_publi="'.$publi.'",';
                        $sql_jwtv.='jwtv_modif="'.$modif.'",';
                        $sql_jwtv.='jwtv_mediamp3="'.$mediamp3.'",';
                        $sql_jwtv.='jwtv_media240="'.$media240.'",';
                        $sql_jwtv.='jwtv_media360="'.$media360.'",';
                        $sql_jwtv.='jwtv_media480="'.$media480.'",';
                        $sql_jwtv.='jwtv_media720="'.$media720.'",';
                        $sql_jwtv.='jwtv_media1080="'.$media1080.'",';
                        $sql_jwtv.='jwtv_media240s="'.$media240s.'",';
                        $sql_jwtv.='jwtv_media360s="'.$media360s.'",';
                        $sql_jwtv.='jwtv_media480s="'.$media480s.'",';
                        $sql_jwtv.='jwtv_media720s="'.$media720s.'",';
                        $sql_jwtv.='jwtv_media1080s="'.$media1080s.'",';
                        $sql_jwtv.='jwtv_image="'.$imagen.'",';
                        $sql_jwtv.='jwtv_status="-"';
                        $sql_jwtv.='where jwtv_codigo="'.$codigo.'"';
                        $res_jwtv=msql_query($sql_jwtv,$link_vcrx);

                        echo '<br>';
                        echo  '<br>*  '.$key6;
                        echo  '<br>**  '.$categ;
                        echo  '<br>****  '.$categ0;
                        echo  '<br>******  '.$categ0des;
                        echo  '<br>********  GUID:';
                        echo '<br>'.$categ1;
                        echo '<br>'.$guia;
                        echo '<br>'.$titulo;
                        echo '<br>'.$tipo;
                        echo '<br>'.$segundos;
                        echo '<br>'.$publi;
                        echo '<br>'.$modif;
                        echo '<br>'.$exte;
                        echo '<br>'.$reso;
                        echo '<br>'.$imagen;
                        echo '<br>'.$mediamp3;
                        echo '<br>'.$media240;
                        echo '<br>'.$media360;
                        echo '<br>'.$media480;
                        echo '<br>'.$media720;
                        echo '<br>'.$media1080;
                        $tipo='';
                        $mediamp3='';
                        $media240='';
                        $media360='';
                        $media480='';
                        $media720='';
                        $media1080='';
                        $media240s='';
                        $media360s='';
                        $media480s='';
                        $media720s='';
                        $media1080s='';
                        $imagen='';
                        $imgmd='';
                        $imglg='';
                        $imgxl='';
                      }
                    }
                    
									}
								}	
							}
						}
					}
				}
			}
		}
		echo '<br>';
		echo '***************************************';
		echo '<br>';
	}	
  }
  echo '*** FIN ***';
  
  
  
  
}

function gen_xspf($lang)
{
 	include('../_cfg/config.php');

		$jwtv_sql='delete from jwtv0 where jwtv_guia="" or jwtv_guia is null';
		$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	
// 	$jwtv_sql='update jwtv0 set jwtv_categ1d=jwtv_categ1 where jwtv_categ1d="" or jwtv_categ1d is null';
// 	$jwtv_res=msql_query($jwtv_sql,$link_vcrx);

	
			$jwtv_sql='select distinct jwtv_categ1,jwtv_categ1d, jwtv_lang from jwtv0';
			$jwtv_sql.=' where jwtv_lang="'.$lang.'" and jwtv_categ1d!="" and jwtv_categ1d is not null';
			$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
			while($jwtv_aso=msql_fetch_assoc($jwtv_res))
			{
// 				$cat1_sql='update jwtv0 set jwtv_categ1d="'.$jwtv_aso['jwtv_categ1d'].'" ';
// 				$cat1_sql.='where jwtv_categ1="'.$jwtv_aso['jwtv_categ1'].'" and jwtv_lang="'.$jwtv_aso['jwtv_lang'].'" and (jwtv_categ1d="" or jwtv_categ1d is null)';
// 				$cat1_res=msql_query($cat1_sql,$link_vcrx);
			}
	
		$rutatv='../../iptv/jwtv/';
// 		$rutatv='../../iptv/jwtv/'.$lang.'/';
		if (!file_exists($rutatv)){mkdir($rutatv);}
		$main_date = date("Y-m-d\TH:i:s", time());
	
		$name= 	'jwtv'.'.xspf';
		$xml1 = $rutatv.$name ;

		$xml=new DomDocument("1.0","UTF-8");
		$xml->xmlStandalone = true;

		$playlist=$xml->createElement("playlist");	
		$playlist=$xml->appendChild($playlist);
		$playlist->setAttribute("xmlns","http://xspf.org/ns/0/");
		$playlist->setAttribute("version", "1");
		{
			$datetl=$xml->createElement("date",$main_date);	
			$datetl=$playlist->appendChild($datetl);

			$tracklist=$xml->createElement("trackList");	
			$tracklist=$playlist->appendChild($tracklist);
			{
				$jwtv_sql ='select distinct jwtv_lang, lang_descri from jwtv0 left join lang0 on jwtv_lang=lang_codigo ';
				$jwtv_sql.='where jwtv_guia!="" ';
// 				$jwtv_sql.='order by length(jwtv_lang), jwtv_lang';
				$jwtv_sql.='order by length(jwtv_lang), lang_descri ';
				$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
				
	
				while($jwtv_aso=msql_fetch_assoc($jwtv_res))
				{
					$resol='';
					{
						$title="JWTV ".$jwtv_aso['jwtv_lang'];
						$title="".$jwtv_aso['jwtv_lang'].'-'.ucfirst($jwtv_aso['lang_descri']);
						$title=ucfirst($jwtv_aso['lang_descri']);
						$media='http://iptv.vcrxol.com/jwtv/jwtv_'.$jwtv_aso['jwtv_lang'].'.xspf';
						$media='http://iptv.vcrxol.com/jwtv/'.$jwtv_aso['jwtv_lang'].'/jwtv_'.$jwtv_aso['jwtv_lang'].'.xspf';
						$image='http://iptv.vcrxol.com/icon/logo_'.$jwtv_aso['jwtv_lang'].'.jpg';
						{	
							$track=$xml->createElement("track");	
							$track=$tracklist->appendChild($track);
							$track->appendChild($xml->createElement("title",$title));
							$track->appendChild($xml->createElement("location",$media));
							$track->appendChild($xml->createElement("image",$image));
						}
					}	
				}	
			}
		}
		$xml->formatOut=true;
		$xml->save($xml1);
		echo '<br>*** '.$name;
	

	$jwtv_sql='select distinct jwtv_lang from jwtv0 ';
	if($lang!="")
	{$jwtv_sql.='where jwtv_lang="'.$lang.'" ';}
	$jwtv_sql.='order by length(jwtv_lang), jwtv_lang';
	$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	while($jwtv_aso=msql_fetch_assoc($jwtv_res))
	{gen_xspf0($jwtv_aso['jwtv_lang']);}
	
	
	$jwtv_sql='select distinct jwtv_lang from jwtv0 ';
	if($lang!="")
	{$jwtv_sql.='where jwtv_lang="'.$lang.'" ';}
	$jwtv_sql.='order by length(jwtv_lang), jwtv_lang';
	$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	while($jwtv_aso=msql_fetch_assoc($jwtv_res))
	{
// 		gen_xspf1($jwtv_aso['jwtv_lang'],'1080');
		gen_xspf1($jwtv_aso['jwtv_lang'],'720');
		gen_xspf1($jwtv_aso['jwtv_lang'],'480');
		gen_xspf1($jwtv_aso['jwtv_lang'],'360');
		gen_xspf1($jwtv_aso['jwtv_lang'],'240');
		gen_xspf1($jwtv_aso['jwtv_lang'],'mp3');
	}

	if(1==1)
	{
	$jwtv_sql='select distinct jwtv_categ0,jwtv_lang,jwtv_categ0n from jwtv0 ';
	if($lang!="")
	{$jwtv_sql.='where jwtv_lang="'.$lang.'" ';}
	$jwtv_sql.='order by length(jwtv_lang), jwtv_lang, jwtv_categ0n, jwtv_categ1';
	$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	while($jwtv_aso=msql_fetch_assoc($jwtv_res) )
	{
// 		gen_xspf2('',$jwtv_aso['jwtv_categ0'],$jwtv_aso['jwtv_lang'],'0','1080');
		gen_xspf2('',$jwtv_aso['jwtv_categ0'],$jwtv_aso['jwtv_lang'],'0','720');
		gen_xspf2('',$jwtv_aso['jwtv_categ0'],$jwtv_aso['jwtv_lang'],'0','480');
		gen_xspf2('',$jwtv_aso['jwtv_categ0'],$jwtv_aso['jwtv_lang'],'0','360');
		gen_xspf2('',$jwtv_aso['jwtv_categ0'],$jwtv_aso['jwtv_lang'],'0','240');
	}
		gen_xspf2('','Audio',$jwtv_aso['jwtv_lang'],'0','mp3');
	}
// 	echo '<br>*****';
// 	die();

	if(1==1)
	{
// 	$jwtv_sql='select distinct jwtv_categ1, jwtv_lang from jwtv0 order by length(jwtv_lang), jwtv_lang, jwtv_categ0n, jwtv_categ1 ';
// 	$jwtv_sql='select distinct jwtv_categ1, jwtv_lang from jwtv0 ';
	$jwtv_sql='select distinct jwtv_categ0n,jwtv_categ1, jwtv_lang from jwtv0 ';
	if($lang!="")
	{$jwtv_sql.='where jwtv_lang="'.$lang.'" ';}
	$jwtv_sql.='order by length(jwtv_lang), jwtv_lang,  jwtv_categ0n, jwtv_categ1 ';
	$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	while($jwtv_aso=msql_fetch_assoc($jwtv_res))
	{
// // 		gen_xspf2($jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','1080');
// 		gen_xspf2($jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','720');
// 		gen_xspf2($jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','480');
// 		gen_xspf2($jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','360');
// 		gen_xspf2($jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','240');
// 		gen_xspf2($jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','mp3');

// 		gen_xspf2($jwtv_aso['jwtv_categ0n'],$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','1080');
		gen_xspf2($jwtv_aso['jwtv_categ0n'],$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','720');
		gen_xspf2($jwtv_aso['jwtv_categ0n'],$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','480');
		gen_xspf2($jwtv_aso['jwtv_categ0n'],$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','360');
		gen_xspf2($jwtv_aso['jwtv_categ0n'],$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','240');
		gen_xspf2($jwtv_aso['jwtv_categ0n'],$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','mp3');
	}
// 		gen_xspf2('',$jwtv_aso['jwtv_categ1'],$jwtv_aso['jwtv_lang'],'1','mp3');

	}
	
}



function gen_xspf0($lang)
{
include('../_cfg/config.php');

		$rutatv='../../iptv/jwtv/';
		$rutatv='../../iptv/jwtv/'.$lang.'/';
		if (!file_exists($rutatv)){mkdir($rutatv);}
		$main_date = date("Y-m-d\TH:i:s", time());

		$name= 	'jwtv_'.$lang.'.xspf';
		$xml1 = $rutatv.$name ;


		$xml=new DomDocument("1.0","UTF-8");
		$xml->xmlStandalone = true;

		$playlist=$xml->createElement("playlist");	
		$playlist=$xml->appendChild($playlist);
		$playlist->setAttribute("xmlns","http://xspf.org/ns/0/");
		$playlist->setAttribute("version", "1");
		{

			$tracklist=$xml->createElement("trackList");	
			$tracklist=$playlist->appendChild($tracklist);

			// CAT 0
			{

				$arr = array('720','480', '360', '240','mp3');
				foreach ($arr as &$resol) 
				{
// 				$media='http://iptv.vcrxol.com/jwtv/jwtv_'.$lang.$resol.'.xspf';
				$media='http://iptv.vcrxol.com/jwtv/'.$lang.'/jwtv_'.$lang.$resol.'.xspf';
				$title=$resol;
				$image='http://iptv.vcrxol.com/icon/logo'.$resol.'.jpg';
					
				$trackok=1;
				if ($trackok==1)
				{	
					$track=$xml->createElement("track");	
					$track=$tracklist->appendChild($track);
					$track->appendChild($xml->createElement("title",$title));
					$track->appendChild($xml->createElement("location",$media));
					$track->appendChild($xml->createElement("image",$image));
				}

				}	
		
			}
	
		}

		$xml->formatOut=true;
		// $strings_xml=$xml->saveXML();
		$xml->save($xml1);
		echo '<br>*** '.$name;
	
	
	
}

function gen_xspf1($lang,$resol)
{

// if($resol!=720){return;}
	
include('../_cfg/config.php');
	
		$xcolu=8;
		$ccolu=0;	

		$rutatv='../../iptv/jwtv/';
		$rutatv='../../iptv/jwtv/'.$lang.'/';
		if (!file_exists($rutatv)){mkdir($rutatv);}
		$main_date = date("Y-m-d\TH:i:s", time());

		$name= 	'jwtv_'.$lang.$resol.'.xspf';
		$xml1 = $rutatv.$name ;


		$xml=new DomDocument("1.0","UTF-8");
		$xml->xmlStandalone = true;

		$playlist=$xml->createElement("playlist");	
		$playlist=$xml->appendChild($playlist);
		$playlist->setAttribute("xmlns","http://xspf.org/ns/0/");
		$playlist->setAttribute("version", "1");
		{
// 			$datetl=$xml->createElement("date",$main_date);	
// 			$datetl=$playlist->appendChild($datetl);
			$tracklist=$xml->createElement("trackList");	
			$tracklist=$playlist->appendChild($tracklist);

			//ULTIMOS
			{
				$jwtv_sql ='select *  ';
				$jwtv_sql.='from jwtv0 where jwtv_lang="'.$lang.'" ';
				if($resol=='mp3')
				{$jwtv_sql.='and jwtv_categ0 like "Audio%" ';}
				else
				{$jwtv_sql.='and jwtv_categ0 not like "Audio%" ';}
				$jwtv_sql.='group by jwtv_guia ';
				$jwtv_sql.='order by jwtv_publi desc, jwtv_guia desc ';
				$jwtv_sql.='limit '.$xcolu;
				$jwtv_res=msql_query($jwtv_sql,$link_vcrx);

				while($jwtv_aso=msql_fetch_assoc($jwtv_res))
				{
          $media=$jwtv_aso['jwtv_media'.$resol];
          $title=$jwtv_aso['jwtv_title'];
          if($jwtv_aso['jwtv_categ0']=='Audio'){$title='♪ '.$title;	}
          if ($title==""){$title=$jwtv_aso['jwtv_categ1d'];}
          $image=$jwtv_aso['jwtv_image'];

          $trackok=1;
          if($trackok==1)
          {	
            $track=$xml->createElement("track");	
            $track=$tracklist->appendChild($track);
            $track->appendChild($xml->createElement("title",$title));
            $track->appendChild($xml->createElement("location",$media));
            $track->appendChild($xml->createElement("image",$image));
          }
				}	
			}

			// CAT 0
			{
	
				$jwtv_sql ='select jwtv0.*, jwtv_categ0 as categ,jwtv_categ0d as categd, 0 as numcat, max(jwtv_image0) as image0 ';
				$jwtv_sql.='from jwtv0 where jwtv_lang="'.$lang.'" ';
				if($resol=='mp3')
				{$jwtv_sql.='and jwtv_categ0 like "Audio%" ';}
// 				else
// 				{$jwtv_sql.='and jwtv_categ0 not like "Audio%" ';}
				$jwtv_sql.='group by jwtv_categ0d ';
				$jwtv_sql.='order by jwtv_categ0n, jwtv_categ1 ';
				$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
//         echo '/*/*/*/*/*/*/*/*/'.$jwtv_sql;

				while($jwtv_aso=msql_fetch_assoc($jwtv_res))
				{
          $media='http://iptv.vcrxol.com/jwtv/jwtv_'.$jwtv_aso['jwtv_lang'].$resol.'_'.$jwtv_aso['numcat'].$jwtv_aso['categ'].'.xspf';
          $media='http://iptv.vcrxol.com/jwtv/'.$jwtv_aso['jwtv_lang'].'/jwtv_'.$jwtv_aso['jwtv_lang'].$resol.'_'.$jwtv_aso['numcat'].$jwtv_aso['categ'].'.xspf';
  //         echo '<br>'.$jwtv_aso['categ'].'    -     '.$media;  
          $title=strtoupper2($jwtv_aso['categd']);
          if ($title==""){$title=$jwtv_aso['categ'];}
          $title='* '.$title;
          $image=$jwtv_aso['image0'];
          if($image=="" and $jwtv_aso['categ']=="Audio"){$image='http://iptv.vcrxol.com/icon/logomp3.jpg';}

          $trackok=1;
          if ($trackok==1)
          {	
            $track=$xml->createElement("track");	
            $track=$tracklist->appendChild($track);
            $track->appendChild($xml->createElement("title",$title));
            $track->appendChild($xml->createElement("location",$media));
            $track->appendChild($xml->createElement("image",$image));
            $ccolu++;
          }
				}	
		
			}

			// CAT 1
			{
				$jwtv_sql ='select jwtv0.*, ';
				$jwtv_sql.='jwtv_categ1 as categ, max(jwtv_categ1d) as categd, 1 as numcat,';
// 				$jwtv_sql.='count(jwtv_categ0n) as contador0,';
				$jwtv_sql.='max(jwtv_image0) as image0, ';
				$jwtv_sql.='max(jwtv_image) as image ';
				$jwtv_sql.='from jwtv0 where jwtv_lang="'.$lang.'" ';
				if($resol=='mp3')
				{$jwtv_sql.='and jwtv_categ0 like "Audio%" ';}
// 				else
// 				{$jwtv_sql.='and jwtv_categ0 not like "Audio%" ';}
				$jwtv_sql.='group by jwtv_categ0,jwtv_categ1 ';
// 				$jwtv_sql.='order by jwtv_categ0n, contador0 desc, jwtv_categ1d, jwtv_categ1';
				$jwtv_sql.='order by jwtv_categ0n, jwtv_categ1d, jwtv_categ1';
				$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	
				$lcateg0="";
				while($jwtv_aso=msql_fetch_assoc($jwtv_res))
				{
          $jwtv_sql2 ='select * from jwtv0 where jwtv_categ1d="'.$jwtv_aso['categd'].'" ';
          $jwtv_sql2.='and jwtv_categ0="'.$jwtv_aso['jwtv_categ0'].'"';
          $jwtv_sql2.='and jwtv_lang="'.$lang.'"';
          $jwtv_res2=msql_query($jwtv_sql2,$link_vcrx);
          $categ0=msql_result($jwtv_res2,0,'jwtv_categ0');
          $categ1=msql_result($jwtv_res2,0,'jwtv_categ1');

          $media='http://iptv.vcrxol.com/jwtv/jwtv_'.$jwtv_aso['jwtv_lang'].$resol.'_'.$jwtv_aso['numcat'].$jwtv_aso['categ'].'_'.$jwtv_aso['jwtv_categ0n'].'.xspf';
          $media='http://iptv.vcrxol.com/jwtv/'.$jwtv_aso['jwtv_lang'].'/jwtv_'.$jwtv_aso['jwtv_lang'].$resol.'_'.$jwtv_aso['numcat'].$jwtv_aso['categ'].'_'.$jwtv_aso['jwtv_categ0n'].'.xspf';
          $title=$jwtv_aso['categd'];
          if ($title==""){$title=categ1des($lang,$jwtv_aso['jwtv_categ1']);}
          if ($title==""){$title=$jwtv_aso['jwtv_categ1'];}
          $image=$jwtv_aso['image0'];
          if($image==""){$image=$jwtv_aso['image'];}
          if(mb_stristr($categ0,'Audio'))
          {
            if(mb_stristr($categ1,'Piano')){$title='♪ '.$title;}
            if(mb_stristr($categ1,'Orchestra')){$title='♫ '.$title;}
            if(mb_stristr($categ1,'Melodies')){$title='♪♫ '.$title;}
          }

          if($lcateg0!=$jwtv_aso['jwtv_categ0'] and 1==2)
          {
            while(($ccolu % $xcolu)>0)
            {
            $track=$xml->createElement("track");	
            $track=$tracklist->appendChild($track);
            $track->appendChild($xml->createElement("title","-"));
            $track->appendChild($xml->createElement("location",""));
            $track->appendChild($xml->createElement("image",""));
            $ccolu++;
            }
          }

          $trackok=1;
          if ($trackok==1)
          {	
            $track=$xml->createElement("track");	
            $track=$tracklist->appendChild($track);
            $track->appendChild($xml->createElement("title",$title));
            $track->appendChild($xml->createElement("location",$media));
            $track->appendChild($xml->createElement("image",$image));
            $ccolu++;
          }

          $lcateg0=$jwtv_aso['jwtv_categ0'];
					
				}	
		
			}
	
		}

		$xml->formatOut=true;
		// $strings_xml=$xml->saveXML();
		$xml->save($xml1);
		echo '<br>*** '.$name;
	
}

function gen_xspf2($cat0n,$categ,$lang,$numcat,$resol)
{
 	include('../_cfg/config.php');
	
$rutatv='../../iptv/jwtv/';
$rutatv='../../iptv/jwtv/'.$lang.'/';
if (!file_exists($rutatv)){mkdir($rutatv);}
	
if($categ==""){return;}  
if($resol=='mp3' and $cat0n!=99){return;}  
  
$name = 	'jwtv_'.$lang.$resol.'_'.$numcat.$categ;
if($numcat=='1')
{$name.= 	'_'.$cat0n;}
$name.='.xspf';


$xml1 = $rutatv.$name ;

echo '<br>'.$name;	

$main_date = date("Y-m-d\TH:i:s", time());
	
$xml=new DomDocument("1.0","UTF-8");
$xml->xmlStandalone = true;

$playlist=$xml->createElement("playlist");	
$playlist=$xml->appendChild($playlist);
$playlist->setAttribute("xmlns","http://xspf.org/ns/0/");
$playlist->setAttribute("version", "1");
{
	
	$datetl=$xml->createElement("date",$main_date);	
	$datetl=$playlist->appendChild($datetl);
	
	$tracklist=$xml->createElement("trackList");	
	$tracklist=$playlist->appendChild($tracklist);
	{
	
		$jwtv_sql ='select * from jwtv0 ';
		$jwtv_sql.='where jwtv_lang="'.$lang.'" ';
		$jwtv_sql.='and jwtv_categ'.$numcat.'="'.$categ.'" ';
		$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
		$categ0=msql_result($jwtv_res,0,'jwtv_categ0');
		$categ1=msql_result($jwtv_res,0,'jwtv_categ1');

		$jwtv_sql ='select * from jwtv0 ';
		$jwtv_sql.='where jwtv_lang="'.$lang.'" ';
		$jwtv_sql.='and jwtv_categ'.$numcat.'="'.$categ.'" ';
		if($numcat=='1')
		{$jwtv_sql.='and jwtv_categ0n="'.$cat0n.'"';
		}
		if($resol=='mp3')
		{$jwtv_sql.='and jwtv_categ0 like "Audio%" ';}		
// 		else 
// 		{$jwtv_sql.='and jwtv_categ0 not like "Audio%" ';}
		$jwtv_sql.=' group by jwtv_guia ';

		$orderby ='order by jwtv_modif desc';	
		if($categ0=='Audio' and mb_stristr('KNOPV',substr($categ1,0,1)))
		{$orderby ='order by jwtv_title ';}
		if($categ0=='Audio' and mb_stristr('D',substr($categ1,0,1)))
		{$orderby ='order by jwtv_publi ';}

// 		$jwtv_sql.='group by jwtv_title ';

		$jwtv_sql.=$orderby;
// 		 echo '<br>'.$jwtv_sql;
	
		$jwtv_res=msql_query($jwtv_sql,$link_vcrx);
	
		while($jwtv_aso=msql_fetch_assoc($jwtv_res))
		{
			$media=$jwtv_aso['jwtv_media'.$resol];
			$medias=$jwtv_aso['jwtv_media'.$resol.'s'];

			if(mb_stristr($jwtv_aso['jwtv_categ0'],'Music') or mb_stristr($jwtv_aso['jwtv_categ1'],'Song'))
			{$media=$medias;}
			
			$title=$jwtv_aso['jwtv_title'];
			$image=$jwtv_aso['jwtv_image'];
			{	
				$track=$xml->createElement("track");	
				$track=$tracklist->appendChild($track);

// 				$nodo=$xml->createElement("title",$title);	
// 				$nodo=$track->appendChild($nodo);
				$track->appendChild($xml->createElement("title",$title));

// 				$nodo=$xml->createElement("location",$media);	
// 				$nodo=$track->appendChild($nodo);
				$track->appendChild($xml->createElement("location",$media));

// 				$nodo=$xml->createElement("image",$image);	
// 				$nodo=$track->appendChild($nodo);
				$track->appendChild($xml->createElement("image",$image));

// 				$nodo=$xml->createElement("album",$jwtv_aso['jwtv_categ'.$numcat.'']);	
// 				$nodo=$track->appendChild($nodo);
				$track->appendChild($xml->createElement("album",$jwtv_aso['jwtv_categ'.$numcat.'']));

	// 			$nodo=$xml->createElement("artist",$jwtv_aso['jwtv_categ1d']);	
	// 			$nodo=$track->appendChild($nodo);

	// 			$nodo=$xml->createElement("date",$jwtv_aso['jwtv_modif']);	
	// 			$nodo=$track->appendChild($nodo);

// 				$nodo=$xml->createElement("identifier",$jwtv_aso['jwtv_guia']);	
// 				$nodo=$track->appendChild($nodo);
				$track->appendChild($xml->createElement("identifier",$jwtv_aso['jwtv_guia']));

// 				$nodo=$xml->createElement("info","https://tv.jw.org");	
// 				$nodo=$track->appendChild($nodo);
				$track->appendChild($xml->createElement("info","https://tv.jw.org"));

			}
		}
	}

}
	
$xml->formatOut=true;
// $strings_xml=$xml->saveXML();
$xml->save($xml1);
	
	
}


function categ1des($lang,$cat1des)
{
 	include('../_cfg/config.php');
		$cat1_sql='select distinct jwtv_categ1,jwtv_categ1d, jwtv_lang from jwtv0 where jwtv_categ1d!="" and jwtv_categ1d is not null ';
		$cat1_sql.='and jwtv_categ1="'.$cat1des.'" ';
		$cat1_sql.='and jwtv_lang="'.$lang.'" ';
		$cat1_res=msql_query($cat1_sql,$link_vcrx);
// 	echo '<br>'.$cat1_sql;
// 	echo '<br>'.msql_error();
		$cat1des='';
		if(msql_num_rows($cat1_res)!=0){$cat1des=msql_result($cat1_res,0,'jwtv_categ1d');}
		return $cat1des;
}


?>


