<?php

include('../global.php');

$SourceURL = 'http://dviproduction.cz/video/playlist.xml';
$StartText = '<item>';
$TitleSeparator = '<title>';
$DescriptionSeparator = '<description>';
$StartLink = 'url="';
$Category = 13;
$Author = 'Automat';

//$Content = addslashes(ToUTF8(file_get_contents($SourceURL), 'win1250'));
$Content = file_get_contents($SourceURL);
while(strpos($Content, $StartText) !== false)
{
  $Content = substr($Content, strpos($Content, $StartText) + strlen($StartText));
  $Title = substr($Content, strpos($Content, $TitleSeparator) + strlen($TitleSeparator));
  $Title = substr($Title, 0, strpos($Title, '<'));

  $Description = substr($Content, strpos($Content, $DescriptionSeparator) + strlen($DescriptionSeparator));
  $Description = trim(substr($Description, 0, strpos($Description, '<')));

  $Content = substr($Content, strpos($Content, $StartLink) + strlen($StartLink));
  $Link = substr($Content, 0, strpos($Content, '"'));
//  $Link = substr($Link, 0, strpos($Link, '?')); // Remove URL parameters
//  $Link = substr($Link, 0, strrpos($Link, '/')).substr($Link, strrpos($Link, '/')); // Insert month between folder and filename

  //echo('Title: '.$Title.'<br>');
  //echo('Description: '.$Description.'<br>');
  //echo('Link: '.$Link.'<br><hr>');
  
  $DbResult = $Database->select('News', 'Id', '`Title`="'.$Title.'" AND `Category`='.$Category);
  //echo($Database->LastQuery);
  if($DbResult->num_rows == 0)
  {
    $Database->insert('News', array('Title' => $Title, 'Date' => 'NOW()', 'Author' => $Author, 'Category' => $Category, 'Content' => $Description, 'Link' => $Link));
    echo($Database->insert_id.',');
  }
}

?>
