<?php
header('Content-type: text/plain; charset=ISO-8859-1');

require('kanta.php');



$channel = $_REQUEST["channel"];

$doc = new DOMDocument();
if ( !$doc->load($channel) ) {
 print "Syötettä ei löydy! "  . $channel;
 exit;
}

$titlet = $doc->getElementsByTagName("title");
if ( $titlet->item(0)->parentNode->nodeName == "channel" ) $title = $titlet->item(0)->textContent;
// print $title;	

$linkit = $doc->getElementsByTagName("link");
if ( $linkit->item(0)->parentNode->nodeName == "channel" ) $link = $linkit->item(0)->textContent;
// print $link;	

$desc = $doc->getElementsByTagName("description");
if ( $linkit->item(0)->parentNode->nodeName == "channel" ) $description = $desc->item(0)->textContent;
// print $description;	

$ttl = $doc->getElementsByTagName("ttl");
if ( $ttl->item(0) ) $ttl = $ttl->item(0)->textContent;
else $ttl = 5;
$ttl = $ttl * 60;
// $ttl = $ttl . " minutes";
// print $ttl;	

$date = $doc->getElementsByTagName("lastBuildDate");
$lastBuildDate = strtotime( $date->item(0)->textContent );
// $lastBuildDate = date("Y-m-d G:i:s", $lastBuildDate);
// print "\n$lastBuildDate\n";	

/*
CREATE TABLE Channel (
FeedURL VARCHAR(1024) NOT NULL PRIMARY KEY,
title VARCHAR(1024) NOT NULL,
link VARCHAR(1024) NOT NULL,
description VARCHAR(16384) NOT NULL,
ttl INTERVAL DEFAULT '5 minutes',
lastBuildDate TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
;
*/
	

$sql = $dbh->prepare('INSERT INTO Channel
(FeedURL, title, link, description, ttl, lastBuildDate)
VALUES (:feedurl, :title, :link, :description, :ttl, :lastbuilddate)');

if ( !$sql ) {
  print_r( $dbh->errorInfo() );
  print("tietokantavirhe");
}

// pitäisi lisätä myös käyttäjän henkilökohtaiseen feed-listaan!

$sql->bindParam(":feedurl", $channel); 
$sql->bindParam(":title", $title); 
$sql->bindParam(":link", $link); 
$sql->bindParam(":description", $description); 
$sql->bindParam(":ttl", $ttl); 
$sql->bindParam(":lastbuilddate", $lastBuildDate); 

$ok = $sql->execute();

if ( !$ok ) {
  print_r( $sql->errorInfo() );
  print("SQL-suorituksessa tapahtui virhe.");
} 
else {
  echo "ok";
}

?>
