1<?php
2
3/**
4 * DokuWiki Plugin feedaggregator (Action Component)
5 *
6 * @license GPL 3 http://www.gnu.org/licenses/gpl-3.0.html
7 * @author  Sam Wilson <sam@samwilson.id.au>
8 */
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class action_plugin_feedaggregator extends DokuWiki_Action_Plugin {
15
16    /**
17     * Registers a callback function for the PREPROCESS event.
18     *
19     * @param Doku_Event_Handler $controller DokuWiki's event controller object
20     * @return void
21     */
22    public function register(Doku_Event_Handler $controller) {
23        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, 'handle');
24    }
25
26    /**
27     *
28     *
29     * @param Doku_Event $event  Not used
30     * @param mixed      $param  Not used
31     * @return void
32     */
33    public function handle(Doku_Event &$event, $param) {
34        global $conf;
35        if ($event->data != 'feedaggregator') {
36            return;
37        }
38        $event->preventDefault();
39
40        // See if we need a token and whether it matches.
41        $requiredToken = $this->getConf('token');
42        $suppliedToken = (isset($_GET['token'])) ? $_GET['token'] : false;
43        if (!empty($requiredToken) && $suppliedToken !== $requiredToken) {
44            msg("Token doesn't match for feedaggregator");
45            return true;
46        }
47
48        // Get the feed list.
49        $feeds = file(fullpath($conf['tmpdir'].'/feedaggregator.csv'));
50
51        // Set up SimplePie and merge all the feeds together.
52        $simplepie = new FeedParser();
53        $ua = 'Mozilla/4.0 (compatible; DokuWiki feedaggregator plugin '.wl('', '', true).')';
54        $simplepie->set_useragent($ua);
55        $simplepie->force_feed($this->getConf('force_feed'));
56        $simplepie->force_fsockopen($this->getConf('force_fsockopen'));
57        $simplepie->set_feed_url($feeds);
58
59        // Set up caching.
60        $cacheDir = fullpath($conf['cachedir'].'/feedaggregator');
61        io_mkdir_p($cacheDir);
62        $simplepie->enable_cache();
63        $simplepie->set_cache_location($cacheDir);
64
65        // Run the actual feed aggregation.
66        $simplepie->init();
67
68        // Check for errors.
69        if ($simplepie->error()) {
70            header("Content-type:text/plain");
71            echo join("\n", $simplepie->error());
72        }
73
74        // Create the output HTML and cache it for use by the syntax component.
75        $html = '';
76        foreach ($simplepie->get_items() as $item) {
77            $html .= "<div class='feedaggregator_item'>\n"
78                    ."<h2>".$item->get_title()."</h2>\n"
79                    .$item->get_content()."\n"
80                    ."<p>"
81                    ."  <a href='".$item->get_permalink()."'>Published ".$item->get_date('j M Y')."</a> "
82                    ."  in <a href='".$item->get_feed()->get_permalink()."'>".$item->get_feed()->get_title()."</a>"
83                    ."</p>\n"
84                    ."</div>\n\n";
85        }
86        io_saveFile($cacheDir.'/output.html', $html);
87
88        // Output nothing, as this should be run from cron and we don't want to
89        // flood the logs with success.
90        exit(0);
91    }
92
93}
94