<?php

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

$SourceURL = 'http://www.obec-hovezi.cz/web/rss.xml';
$StartText = '<item>';
$TitleSeparator = '<title>';
$DescriptionSeparator = '<description>';
$StartLink = '<link>';
$Category = 16;
$Author = 'Automat';

$doc = new DOMDocument();
$doc->load($SourceURL);
foreach($doc->getElementsByTagName('item') as $node) 
{
  $Title = $node->getElementsByTagName('title')->item(0)->nodeValue;
  $Description = $node->getElementsByTagName('description')->item(0)->nodeValue;
  $Link = $node->getElementsByTagName('link')->item(0)->nodeValue;  
  $Date = $node->getElementsByTagName('pubDate')->item(0)->nodeValue;
  
  $Title = $Database->real_escape_string($Title);
  $Description = strip_tags($Description);
  $Description = str_replace("\r", '', $Description);
  $Description = str_replace("\n", '<br>', $Description);
  $Description = $Database->real_escape_string($Description);
  $Link = $Database->real_escape_string($Link);
  //if(($CommaPos = strpos($Date, ',')) !== FALSE)
  //  $Date = substr($Date, $CommaPos + 1);
  $Date = TimeToMysqlDateTime(strtotime($Date));
  
  
  $DbResult = $Database->select('News', 'Id', '(`Title`="'.$Title.'") AND (`Category`='.$Category.') AND (`Content` = "'.$Description.'") AND (`Link` = "'.$Link.'")');
  //echo($Database->LastQuery);
  if($DbResult->num_rows == 0)
  {
    $Database->insert('News', array('Title' => $Title, 'Date' => $Date, 'Author' => $Author, 'Category' => $Category, 'Content' => $Description, 'Link' => $Link));
    echo($Database->insert_id.',');
  }
}

/*
//$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
//  $Description .= '<br><a href="'.$Link.'">Video</a>';

  //echo('Title: '.$Title.'<br>');
  //echo('Description: '.$Description.'<br>');
  //echo('Link: '.$Link.'<br><hr>');
  
  $Link = $Database->real_escape_string($Link);
  $Title = $Database->real_escape_string($Title);
  $Description = strip_cdata($Description);
  $Description = strip_tags($Description);
  
  $Description = str_replace("\n", '<br>', $Description);
  $Description = str_replace("\r", '', $Description);
  $Description = $Database->real_escape_string($Description);
  
  $DbResult = $Database->select('News', 'Id', '(`Title`="'.$Title.'") AND (`Category`='.$Category.') AND (`Content` = "'.$Description.'") AND (`Link` = "'.$Link.'")');
  //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.',');
  }
} 
*/

?>
