add Feed reader class
authorStricted <info@nexus-irc.de>
Thu, 7 Nov 2013 02:41:13 +0000 (03:41 +0100)
committerStricted <info@nexus-irc.de>
Thu, 7 Nov 2013 02:41:41 +0000 (03:41 +0100)
Feed.class.php [new file with mode: 0644]

diff --git a/Feed.class.php b/Feed.class.php
new file mode 100644 (file)
index 0000000..7f5f1e6
--- /dev/null
@@ -0,0 +1,75 @@
+<?php
+/* Feed.class.php
+ * Copyright (C) 2013  Jan Altensen (Stricted)
+ * http://stricted.de/
+ * 
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License 
+ * along with this program. If not, see <http://www.gnu.org/licenses/>. 
+ */
+class FeedClass {
+       public function __construct($url) {
+        $file = file_get_contents($url);
+        $xml = simplexml_load_string($file);
+        $type = $this->getFeedType($xml);
+        $data = null;
+        switch($type) {
+            case 'rss':
+                $data = $this->parseRSSFeed($xml);
+                break;
+            case 'atom':
+                $data = $this->parseAtomFeed($xml);
+                break;
+            default:
+                return null;
+                break;
+        }
+        return $data;  
+    }
+
+    private function parseRSSFeed($xml) {
+               foreach ($xml->channel->item as $item) {
+                       $item->pubDate = $this->getTime($item->pubDate);
+                       $item->description = html_entity_decode($item->description);
+                       $data[] = $item;
+               } 
+               return $data;
+    }
+
+    private function parseAtomFeed($xml) {
+               $entries = $xml->children()->entry;
+               foreach ($entries as $entry) {
+                       $entry->children()->author = $entry->children()->author->name;
+                       $entry->children()->link = $entry->link->attributes()->href;
+                       $entry->children()->updated = $this->getTimeStamp($entry->children()->updated);
+                       $data[] = $entry->children();
+               }
+               return $data;
+    }
+
+    private function getTime($time) {
+               return date("d.m.Y H:i:s",intval(strtotime($time)));;
+       }
+       
+    private function getTimeStamp($time) {
+               return intval(strtotime($time));;
+       }
+
+    public static function getFeedType($xml) {
+        $type = false;
+        if (isset($xml->channel->item)) $type = 'rss';
+        if (isset($xml->entry)) $type = 'atom';
+
+        return $type;
+    }
+}
+?>
\ No newline at end of file