load("uutiset.xml") ) { print "xml-virhe: uutiset.xml"; exit; } $channel = $_REQUEST["channel"]; /* 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(' SELECT (lastBuildDate + ttl) - :aika AS aika FROM Channel WHERE FeedURL = :channel '); $aika = time(); $sql->bindParam(":aika", $aika); $sql->bindParam(":channel", $channel); $ok = $sql->execute(); $aika = $sql->fetch(PDO::FETCH_ASSOC); if ( $aika["aika"] > 0 ) { // print $aika["aika"]; load_from_cache($channel, $doc, $cachedir); } // print "foo"; // print $aika["aika"]; $feed = new DOMDocument(); if ( !$feed->load($channel) ) { $li = $doc->createElement("li"); $txt = $doc->createTextNode( "xml-virhe: $channel"); $li->appendChild($txt); $doc->documentElement->appendChild($li); print $doc->saveXML(); exit; } $lastBuildDate = $feed->getElementsByTagName("lastBuildDate")->item(0)->textContent; if ( $lastBuildDate == "" ) $lastBuildDate = time(); else $lastBuildDate = strtotime( $lastBuildDate ); // $lastBuildDate = date("Y-m-d G:i:s", $lastBuildDate); $sql = $dbh->prepare(' UPDATE Channel SET lastBuildDate = :aika WHERE FeedURL = :channel '); $sql->bindParam(":channel", $channel); $sql->bindParam(":aika", $lastBuildDate); $ok = $sql->execute(); $sql2 = $dbh->prepare(' SELECT link FROM Read Where UserID = :user AND channel = :channel '); $sql2->bindParam(":channel", $channel); $sql2->bindParam(":user", $hlo); $ok = $sql2->execute(); $luetut = $sql2->fetchAll(PDO::FETCH_COLUMN, 0); $titlet = $feed->getElementsByTagName("title"); if ( $titlet->item(0)->parentNode->nodeName == "channel" ) $title = $titlet->item(0)->textContent; // print "$title"; $linkit = $feed->getElementsByTagName("link"); if ( $linkit->item(0)->parentNode->nodeName == "channel" ) $link = $linkit->item(0)->textContent; // print $link; $desc = $feed->getElementsByTagName("description"); if ( $linkit->item(0)->parentNode->nodeName == "channel" ) $description = $desc->item(0)->textContent; // print $description; $items = $feed->getElementsByTagName("item"); $otsikko = $doc->getElementById("otsikko"); $desc = $doc->getElementById("desc"); $item = $doc->getElementById("items"); $otsikko->setAttribute("href", $link); $txt = $doc->createTextNode($title); $otsikko->appendChild($txt); $txt = $doc->createTextNode($description); $desc->appendChild($txt); for($i=0; $i<$items->length; $i++) { $link = $items->item($i)->getElementsByTagName("link")->item(0)->textContent; if ( array_search($link, $luetut) == false ) { $title = $items->item($i)->getElementsByTagName("title")->item(0)->textContent; $desc = $items->item($i)->getElementsByTagName("description")->item(0)->textContent; // $title = $title . " foo " . array_search($link, $luetut); $li = $doc->createElement("li"); $h3 = $doc->createElement("h3"); $a = $doc->createElement("a"); $h3->appendChild($a); $txt = $doc->createTextNode($title); $a->appendChild($txt); $a->setAttribute("href", $link); $li->appendChild($h3); $txt = $doc->createTextNode($desc); $li->appendChild($txt); $item->appendChild($li); } } print $doc->saveXML(); $channel = str_replace("http://", "", $channel); $channel = str_replace("/", "_", $channel); $doc->save($cachedir . $channel); function load_from_cache($channel, $doc, $cachedir) { $channel = str_replace("http://", "", $channel); $channel = str_replace("/", "_", $channel); if ( !$doc->load($cachedir . $channel) ) { return; } print $doc->saveXML(); exit; }