summaryrefslogtreecommitdiff
path: root/providers/Rss.php
blob: a6145d94aaa10d5bffbcab1570abce546a953202 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

namespace Providers;

require_once('XmlFeed.php');
require_once('Item.php');

class Rss extends \Providers\XmlFeed {

    protected function _getFeedUrl($feed) {
        $config = json_decode(file_get_contents('../config/rss.json'), TRUE);
        if (isset($config[$feed])) {
            return $config[$feed];
        }
        return NULL;
    }

    protected function _getCachePath() {
        return '../cache/rss.%s';
    }

    protected function _parseFeedContent($feed) {
        $feedItems = $feed->channel->item ?: $feed->entry;
        $items = [];
        foreach ($feedItems as $item) {
            $items[] = $item->asXML();
        }
        return $items;
    }

    protected function _parseRssItem($contentString) {
        $itemString = str_replace(['content:encoded>', '<yt:', '</yt:', '<dc:', '</dc:', '<media:', '</media:', '<wfw:', '</wfw:'], ['content>', '<', '</', '<', '</', '<', '</', '<', '</'], $contentString);
        $item = new \SimpleXMLElement($itemString);
        return $item;
    }

    protected function _getDescriptionLink($itemObj) {
        $linkConfig = explode(':', $this->_options['descriptionlink']);
        $linkDomain = NULL;
        if (!is_numeric($linkConfig[0])) {
            $linkDomain = array_shift($linkConfig);
            if (!$linkConfig) {
                $linkConfig = ['1'];
            }
        }
        $linkIndex = intval($linkConfig[0]) - 1;
        $textLinks = [];
        if (preg_match_all('#(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-&?=%.]+#', $itemObj->Text, $textLinks)) {
            $textLinks = $textLinks[0];
            if ($linkDomain) {
                $textLinks = array_values(array_filter($textLinks, function($link) use($linkDomain) {
                    if($linkParts = parse_url($link)) {
                        return $linkParts['host'] == $linkDomain;
                    }
                }));
            }
            if (count($textLinks) > $linkIndex) {
                return $textLinks[$linkIndex];
            }
        }
        return $itemObj->Link;
    }

    protected function _mapRssItem($item) {
        $itemObject = new Item();
        $itemObject->ID = strval($item->id ?: $item->guid) ?: ltrim(parse_url(strval($item->link))['path'], '/');
        $itemObject->Title = strval($item->title);
        $itemObject->Time = strval($item->published ?: $item->pubDate ?: $item->updated);
        $itemObject->Text = strval($item->summary ?: $item->description ?: $item->content ?: $item->group->description);
        $itemObject->Link = strval(isset($item->link['href']) ? $item->link->attributes()['href'] : $item->link);
        $itemObject->Author = strval($item->creator ? $item->creator : (is_string($item->author) ? $item->author : $item->author->name));
        if (array_key_exists('descriptionlink', $this->_options)) {
            $itemObject->Link = $this->_getDescriptionLink($itemObject);
        }
        return $itemObject;
    }

    protected function _mapItems($content) {
        $items = [];
        foreach ($content as $contentString) {
            $item = $this->_parseRssItem($contentString);
            $items[] = $this->_mapRssItem($item);
        }
        return $items;
    }

    public function title() {
        $cacheFile = sprintf($this->_getCachePath() . '.title', $this->_feed);
        if (!file_exists($cacheFile)) {
            $this->_fetchItems();
        }
        if ($this->_feedXml) {
            $title = strval($this->_feedXml->title ?: $this->_feedXml->channel->title);
            file_put_contents($cacheFile, $title);
            return $title;
        } else {
            return file_get_contents($cacheFile);
        }
    }

}

?>