1<?php
2/**
3 * Webcomics Plugin
4 *
5 * (The previous author was Christoph Lang <calbity@gmx.de>)
6 *
7 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
8 * @author LarsDW223
9 */
10
11// must be run within Dokuwiki
12if (! defined('DOKU_INC')) die();
13
14if (! defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
15require_once (DOKU_PLUGIN . 'syntax.php');
16
17/**
18 * All DokuWiki plugins to extend the parser/rendering mechanism
19 * need to inherit from this class
20 */
21class syntax_plugin_webcomics extends DokuWiki_Syntax_Plugin
22{
23    private $comics = array();
24
25    public function __construct() {
26        $feeds = explode(';', $this->getConf('feeds'));
27        foreach ($feeds as $feed) {
28            if (preg_match('/(.*?)="(.*?)"/', $feed, $matches) == 1) {
29                if (!empty($matches[1]) && !empty($matches[2])) {
30                    $key = strtoupper($matches[1]);
31                    $this->comics[$key] = $matches[2];
32                }
33            }
34        }
35    }
36
37    public function connectTo ($mode)
38    {
39        $this->Lexer->addSpecialPattern('<comic=".*?">', $mode, 'plugin_webcomics');
40    }
41
42    public function getType ()
43    {
44        return 'substition';
45    }
46
47    public function getSort ()
48    {
49        return 667;
50    }
51
52    public function handle($match, $state, $pos, Doku_Handler $handler)
53    {
54        $match = substr($match, 8, -2);
55        $match = strtoupper($match);
56
57        $url = $this->comics[$match];
58        $message = '';
59        if (!empty($url)) {
60            // Download feed
61            $ch = new DokuHTTPClient();
62            $piece = $ch->get($url);
63            $xml = simplexml_load_string($piece);
64
65            // Get data
66            preg_match('/src="(.*?)"/', (string) $xml->channel->item->description, $matches);
67            $url = (string) $xml->channel->item->link;
68            $src = $matches[1];
69            $alt = 'Comic';
70            $title = 'Comic';
71            if (preg_match('/title="(.*?)"/', (string) $xml->channel->item->description, $matches) == 1) {
72                $title = $matches[1];
73            }
74            if (preg_match('/alt="(.*?)"/', (string) $xml->channel->item->description, $matches) == 1) {
75                $alt = $matches[1];
76            }
77
78            // Build link
79            $link = '<a href="'.$url .'" alt=""><img src="'.$src.'" title="'.$title.'" alt="'.$alt.'"/></a>'."\n";
80
81            return array($link);
82        }
83
84        $message = str_replace('%COMIC%', $match, $this->getLang('unknown'));
85        return array('', $message);
86    }
87
88    public function render($mode, Doku_Renderer $renderer, $data)
89    {
90        if ($mode == 'xhtml')
91        {
92            if (!empty($data[0])) {
93                $renderer->doc .= $data[0];
94            } else {
95                msg($data[1], -1);
96            }
97            return true;
98        }
99        return false;
100    }
101}
102