diff options
Diffstat (limited to 'classes')
-rw-r--r-- | classes/feedenclosure.php | 7 | ||||
-rw-r--r-- | classes/feeditem.php | 15 | ||||
-rw-r--r-- | classes/feeditem/atom.php | 109 | ||||
-rw-r--r-- | classes/feeditem/common.php | 51 | ||||
-rw-r--r-- | classes/feeditem/rss.php | 107 | ||||
-rw-r--r-- | classes/feedparser.php | 180 | ||||
-rw-r--r-- | classes/feeds.php | 9 | ||||
-rw-r--r-- | classes/sanitizedummy.php | 7 |
8 files changed, 474 insertions, 11 deletions
diff --git a/classes/feedenclosure.php b/classes/feedenclosure.php new file mode 100644 index 000000000..d610dd7c8 --- /dev/null +++ b/classes/feedenclosure.php @@ -0,0 +1,7 @@ +<?php +class FeedEnclosure { + public $link; + public $type; + public $length; +} +?> diff --git a/classes/feeditem.php b/classes/feeditem.php new file mode 100644 index 000000000..e64a13fdc --- /dev/null +++ b/classes/feeditem.php @@ -0,0 +1,15 @@ +<?php +abstract class FeedItem { + abstract function get_id(); + abstract function get_date(); + abstract function get_link(); + abstract function get_title(); + abstract function get_description(); + abstract function get_content(); + abstract function get_comments_url(); + abstract function get_comments_count(); + abstract function get_categories(); + abstract function get_enclosures(); + abstract function get_author(); +} +?> diff --git a/classes/feeditem/atom.php b/classes/feeditem/atom.php new file mode 100644 index 000000000..b981dc319 --- /dev/null +++ b/classes/feeditem/atom.php @@ -0,0 +1,109 @@ +<?php +class FeedItem_Atom extends FeedItem_Common { + function get_id() { + $id = $this->elem->getElementsByTagName("id")->item(0); + + if ($id) { + return $id->nodeValue; + } else { + return $this->get_link(); + } + } + + function get_date() { + $updated = $this->elem->getElementsByTagName("updated")->item(0); + + if ($updated) { + return strtotime($updated->nodeValue); + } + } + + function get_link() { + $links = $this->elem->getElementsByTagName("link"); + + foreach ($links as $link) { + if ($link && $link->hasAttribute("href") && (!$link->hasAttribute("rel") + || $link->getAttribute("rel") == "alternate")) { + return $link->getAttribute("href"); + } + } + } + + function get_title() { + $title = $this->elem->getElementsByTagName("title")->item(0); + + if ($title) { + return $title->nodeValue; + } + } + + function get_content() { + $content = $this->elem->getElementsByTagName("content")->item(0); + + if ($content) { + return $content->nodeValue; + } + } + + function get_description() { + $summary = $this->elem->getElementsByTagName("summary")->item(0); + + if ($summary) { + return $summary->nodeValue; + } + } + + function get_categories() { + $categories = $this->elem->getElementsByTagName("category"); + $cats = array(); + + foreach ($categories as $cat) { + if ($cat->hasAttribute("term")) + array_push($cats, $cat->getAttribute("term")); + } + + $categories = $this->xpath->query("dc:subject", $this->elem); + + foreach ($categories as $cat) { + array_push($cats, $cat->nodeValue); + } + + return $cats; + } + + function get_enclosures() { + $links = $this->elem->getElementsByTagName("link"); + + $encs = array(); + + foreach ($links as $link) { + if ($link && $link->hasAttribute("href") && $link->hasAttribute("rel")) { + if ($link->getAttribute("rel") == "enclosure") { + $enc = new FeedEnclosure(); + + $enc->type = $link->getAttribute("type"); + $enc->link = $link->getAttribute("href"); + $enc->length = $link->getAttribute("length"); + + array_push($encs, $enc); + } + } + } + + $enclosures = $this->xpath->query("media:content", $this->elem); + + foreach ($enclosures as $enclosure) { + $enc = new FeedEnclosure(); + + $enc->type = $enclosure->getAttribute("type"); + $enc->link = $enclosure->getAttribute("url"); + $enc->length = $enclosure->getAttribute("length"); + + array_push($encs, $enc); + } + + return $encs; + } + +} +?> diff --git a/classes/feeditem/common.php b/classes/feeditem/common.php new file mode 100644 index 000000000..0787a42cb --- /dev/null +++ b/classes/feeditem/common.php @@ -0,0 +1,51 @@ +<?php +abstract class FeedItem_Common extends FeedItem { + protected $elem; + protected $xpath; + protected $doc; + + function __construct($elem, $doc, $xpath) { + $this->elem = $elem; + $this->xpath = $xpath; + $this->doc = $doc; + } + + function get_author() { + $author = $this->elem->getElementsByTagName("author")->item(0); + + if ($author) { + $name = $author->getElementsByTagName("name")->item(0); + + if ($name) return $name->nodeValue; + + $email = $author->getElementsByTagName("email")->item(0); + + if ($email) return $email->nodeValue; + + if ($author->nodeValue) + return $author->nodeValue; + } + + $author = $this->xpath->query("dc:creator", $this->elem)->item(0); + + if ($author) { + return $author->nodeValue; + } + } + + // todo + function get_comments_url() { + + } + + function get_comments_count() { + $comments = $this->xpath->query("slash:comments", $this->elem)->item(0); + + if ($comments) { + return $comments->nodeValue; + } + } + + +} +?> diff --git a/classes/feeditem/rss.php b/classes/feeditem/rss.php new file mode 100644 index 000000000..2f363b4f6 --- /dev/null +++ b/classes/feeditem/rss.php @@ -0,0 +1,107 @@ +<?php +class FeedItem_RSS extends FeedItem_Common { + function get_id() { + $id = $this->elem->getElementsByTagName("guid")->item(0); + + if ($id) { + return $id->nodeValue; + } else { + return $this->get_link(); + } + } + + function get_date() { + $pubDate = $this->elem->getElementsByTagName("pubDate")->item(0); + + if ($pubDate) { + return strtotime($pubDate->nodeValue); + } + } + + function get_link() { + $link = $this->elem->getElementsByTagName("link")->item(0); + + if ($link) { + return $link->nodeValue; + } + } + + function get_title() { + $title = $this->elem->getElementsByTagName("title")->item(0); + + if ($title) { + return $title->nodeValue; + } + } + + function get_content() { + $content = $this->xpath->query("content:encoded", $this->elem)->item(0); + + if ($content) { + return $content->nodeValue; + } + + $content = $this->elem->getElementsByTagName("description")->item(0); + + if ($content) { + return $content->nodeValue; + } + } + + function get_description() { + $summary = $this->elem->getElementsByTagName("description")->item(0); + + if ($summary) { + return $summary->nodeValue; + } + } + + function get_categories() { + $categories = $this->elem->getElementsByTagName("category"); + $cats = array(); + + foreach ($categories as $cat) { + array_push($cats, $cat->nodeValue); + } + + $categories = $this->xpath->query("dc:subject", $this->elem); + + foreach ($categories as $cat) { + array_push($cats, $cat->nodeValue); + } + + return $cats; + } + + function get_enclosures() { + $enclosures = $this->elem->getElementsByTagName("enclosure"); + + $encs = array(); + + foreach ($enclosures as $enclosure) { + $enc = new FeedEnclosure(); + + $enc->type = $enclosure->getAttribute("type"); + $enc->link = $enclosure->getAttribute("url"); + $enc->length = $enclosure->getAttribute("length"); + + array_push($encs, $enc); + } + + $enclosures = $this->xpath->query("media:content", $this->elem); + + foreach ($enclosures as $enclosure) { + $enc = new FeedEnclosure(); + + $enc->type = $enclosure->getAttribute("type"); + $enc->link = $enclosure->getAttribute("url"); + $enc->length = $enclosure->getAttribute("length"); + + array_push($encs, $enc); + } + + return $encs; + } + +} +?> diff --git a/classes/feedparser.php b/classes/feedparser.php new file mode 100644 index 000000000..d60db8a28 --- /dev/null +++ b/classes/feedparser.php @@ -0,0 +1,180 @@ +<?php +class FeedParser { + private $doc; + private $error; + private $items; + private $link; + private $title; + private $type; + private $xpath; + + const FEED_RDF = 0; + const FEED_RSS = 1; + const FEED_ATOM = 2; + + function __construct($data) { + libxml_use_internal_errors(true); + libxml_clear_errors(); + $this->doc = new DOMDocument(); + $this->doc->loadXML($data); + $this->error = $this->format_error(libxml_get_last_error()); + libxml_clear_errors(); + + $this->items = array(); + } + + function init() { + $root = $this->doc->firstChild; + $xpath = new DOMXPath($this->doc); + $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); + $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); + $xpath->registerNamespace('rdf', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'); + $xpath->registerNamespace('slash', 'http://purl.org/rss/1.0/modules/slash/'); + $xpath->registerNamespace('dc', 'http://purl.org/dc/elements/1.1/'); + $xpath->registerNamespace('content', 'http://purl.org/rss/1.0/modules/content/'); + + $this->xpath = $xpath; + + $root = $xpath->query("(//atom:feed|//channel|//rdf:rdf|//rdf:RDF)")->item(0); + + if ($root) { + switch (mb_strtolower($root->tagName)) { + case "rdf:rdf": + $this->type = $this::FEED_RDF; + break; + case "channel": + $this->type = $this::FEED_RSS; + break; + case "feed": + $this->type = $this::FEED_ATOM; + break; + default: + $this->error = "Unknown/unsupported feed type"; + return; + } + + switch ($this->type) { + case $this::FEED_ATOM: + + $title = $xpath->query("//atom:feed/atom:title")->item(0); + + if ($title) { + $this->title = $title->nodeValue; + } + + $link = $xpath->query("//atom:feed/atom:link[not(@rel)]")->item(0); + + if ($link && $link->hasAttributes()) { + $this->link = $link->getAttribute("href"); + } + + $articles = $xpath->query("//atom:entry"); + + foreach ($articles as $article) { + array_push($this->items, new FeedItem_Atom($article, $this->doc, $this->xpath)); + } + + break; + case $this::FEED_RSS: + + $title = $xpath->query("//channel/title")->item(0); + + if ($title) { + $this->title = $title->nodeValue; + } + + $link = $xpath->query("//channel/link")->item(0); + + if ($link && $link->hasAttributes()) { + $this->link = $link->getAttribute("href"); + } + + $articles = $xpath->query("//channel/item"); + + foreach ($articles as $article) { + array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); + } + + break; + case $this::FEED_RDF: + $xpath->registerNamespace('rssfake', 'http://purl.org/rss/1.0/'); + + $title = $xpath->query("//rssfake:channel/rssfake:title")->item(0); + + if ($title) { + $this->title = $title->nodeValue; + } + + $link = $xpath->query("//rssfake:channel/rssfake:link")->item(0); + + if ($link) { + $this->link = $link->nodeValue; + } + + $articles = $xpath->query("//rssfake:item"); + + foreach ($articles as $article) { + array_push($this->items, new FeedItem_RSS($article, $this->doc, $this->xpath)); + } + + break; + + } + } else { + $this->error = "Unknown/unsupported feed type"; + return; + } + } + + function format_error($error) { + if ($error) { + return sprintf("LibXML error %s at line %d (column %d): %s", + $error->code, $error->line, $error->column, + $error->message); + } else { + return ""; + } + } + + function error() { + return $this->error; + } + + function get_link() { + return $this->link; + } + + function get_title() { + return $this->title; + } + + function get_items() { + return $this->items; + } + + function get_links($rel) { + $rv = array(); + + switch ($this->type) { + case $this::FEED_ATOM: + $links = $this->xpath->query("//atom:feed/atom:link"); + + foreach ($links as $link) { + if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { + array_push($rv, $link->getAttribute('href')); + } + } + break; + case $this::FEED_RSS: + $links = $this->xpath->query("//channel/link"); + foreach ($links as $link) { + if (!$rel || $link->hasAttribute('rel') && $link->getAttribute('rel') == $rel) { + array_push($rv, $link->getAttribute('href')); + } + } + break; + } + + return $rv; + } +} ?> diff --git a/classes/feeds.php b/classes/feeds.php index f21bb3ce5..1a1798151 100644 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -514,8 +514,10 @@ class Feeds extends Handler_Protected { } else { - $line["tags"] = get_article_tags($id, $_SESSION["uid"], $line["tag_cache"]); - unset($line["tag_cache"]); + if ($line["tag_cache"]) + $tags = explode(",", $line["tag_cache"]); + else + $tags = false; $line["content"] = sanitize($line["content_preview"], sql_bool_to_bool($line['hide_images']), false, $entry_site_url); @@ -627,7 +629,6 @@ class Feeds extends Handler_Protected { } $reply['content'] .= "</div>"; - $reply['content'] .= "<div class=\"cdmContentInner\">"; if ($line["orig_feed_id"]) { @@ -682,7 +683,7 @@ class Feeds extends Handler_Protected { $reply['content'] .= $p->hook_article_left_button($line); } - $tags_str = format_tags_string($line["tags"], $id); + $tags_str = format_tags_string($tags, $id); $reply['content'] .= "<img src='images/tag.png' alt='Tags' title='Tags'> <span id=\"ATSTR-$id\">$tags_str</span> diff --git a/classes/sanitizedummy.php b/classes/sanitizedummy.php deleted file mode 100644 index 7182fb7f5..000000000 --- a/classes/sanitizedummy.php +++ /dev/null @@ -1,7 +0,0 @@ -<?php -class SanitizeDummy extends SimplePie_Sanitize { - function sanitize($data, $type, $base = '') { - return $data; - } -} -?> |