1<?php
2/**
3 * @package Horde
4 */
5class Horde_Block_Feed extends Horde_Core_Block
6{
7    /**
8     */
9    private $_feed = null;
10
11    /**
12     */
13    public function __construct($app, $params = array())
14    {
15        parent::__construct($app, $params);
16
17        $this->enabled = class_exists('Horde_Feed');
18        $this->_name = _("Syndicated Feed");
19    }
20
21    /**
22     */
23    protected function _params()
24    {
25        return array(
26            'uri' => array(
27                'type' => 'text',
28                'name' => _("Feed Address")
29            ),
30            'limit' => array(
31                'name' => _("Number of articles to display"),
32                'type' => 'int',
33                'default' => 10
34            ),
35            'interval' => array(
36                'name' => _("How many seconds before we check for new articles?"),
37                'type' => 'int',
38                'default' => 86400
39            ),
40            'details' => array(
41                'name' => _("Show extra detail?"),
42                'type' => 'boolean',
43                'default' => 20
44            )
45        );
46    }
47
48    /**
49     */
50    protected function _title()
51    {
52        $this->_read();
53
54        return ($this->_feed instanceof Horde_Feed_Base)
55            ? $this->_feed->title()
56            : _("Feed");
57    }
58
59    /**
60     */
61    protected function _content()
62    {
63        $this->_read();
64
65        if ($this->_feed instanceof Horde_Feed_Base) {
66            $html = '';
67            $count = 0;
68            foreach ($this->_feed as $entry) {
69                if (++$count > $this->_params['limit']) {
70                    break;
71                }
72                $html .= '<a href="' . $entry->link. '"';
73                if (empty($this->_params['details'])) {
74                    $html .= ' title="' . htmlspecialchars(strip_tags($entry->description())) . '"';
75                }
76                $html .= '>' . htmlspecialchars($entry->title) . '</a>';
77                if (!empty($this->_params['details'])) {
78                    $html .= '<br />' .  htmlspecialchars(strip_tags($entry->description())). "<br />\n";
79                }
80                $html .= '<br />';
81            }
82            return $html;
83        }
84
85        return is_string($this->_feed)
86            ? $this->_feed
87            : '';
88    }
89
90    /**
91     */
92    private function _read()
93    {
94        if (empty($this->_params['uri'])) {
95            return;
96        }
97
98        $key = md5($this->_params['uri']);
99        $cache = $GLOBALS['injector']->getInstance('Horde_Cache');
100        $feed = $cache->get($key, $this->_params['interval']);
101        if (!empty($feed)) {
102            $this->_feed = unserialize($feed);
103        }
104
105        try {
106            $client = $GLOBALS['injector']
107              ->getInstance('Horde_Core_Factory_HttpClient')
108              ->create();
109            $feed = Horde_Feed::readUri($this->_params['uri'], $client);
110            $cache->set($key, serialize($feed));
111            $this->_feed = $feed;
112        } catch (Exception $e) {
113            $this->_feed = $e->getMessage();
114        }
115    }
116
117}
118