summaryrefslogtreecommitdiff
path: root/init.php
blob: a00c3d52a0862bb8e84f46a639c19926fa22fd33 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php

class Af_Feedmod extends Plugin {

    private $link;
    private $host;

    function about()
    {
        return array(
            0.9,   // version
            'Replace feed contents by contents from the linked page',   // description
            'mbirth',   // author
            false,   // is_system
        );
    }

    function init($host)
    {
        $this->link = $host->get_link();
        $this->host = $host;

        $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
        $host->add_hook($host::HOOK_PREFS_TAB, $this);
    }

    function hook_article_filter($article)
    {
        $sometext = $this->host->get($this, "sometext");
        
        $owner_uid = $article['owner_uid'];

        if (strpos($article['link'], 'heise.de') !== FALSE) {   // only process heise.de articles
            if (strpos($article['plugin_data'], "feedmod,$owner_uid:") === FALSE) {   // do not process an article more than once
                $doc = new DOMDocument();
                @$doc->loadHTML(fetch_file_contents($article['link']));

                $basenode = false;

                if ($doc) {
                    $xpath = new DOMXPath($doc);
                    $entries = $xpath->query('(//div.meldung_wrapper)');   // find main DIV

                    $matches = array();
                    foreach ($entries as $entry) {
                        $basenode = $entry;
                        break;
                    }

                    if ($basenode) {
                        $article['content'] = $doc->saveXML($basenode);
                        $article['plugin_data'] = "feedmod,$owner_uid:" . $article['plugin_data'];
                    }
                }
            } else if (isset($article['stored']['content'])) {
                $article['content'] = $article['stored']['content'];
            }
        }
        return $article;
    }

    function hook_prefs_tab($args)
    {
        if ($args != "prefPrefs") return;

        print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__("FeedMod Plugin")."\">";

        print "<br/>";

        $sometext = $this->host->get($this, "sometext");

        print "<form dojoType=\"dijit.form.Form\">";

        print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
            evt.preventDefault();
            if (this.validate()) {
                new Ajax.Request('backend.php', {
                    parameters: dojo.objectToQuery(this.getValues()),
                    onComplete: function(transport) {
                        notify_info(transport.responseText);
                    }
                });
                //this.reset();
            }
            </script>";
            
            print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
            print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
            print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"feedmod\">";

            print "<table width=\"100%\" class=\"prefPrefsList\">";

            print "<tr><td width=\"40%\">".__("Some text")."</td>";
            print "<td class=\"prefValue\"><input dojoType=\"dijit.form.ValidationTextBox\" required=\"1\" name=\"sometext\" value=\"$sometext\"></td></tr>";

            print "</table>";

            print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
                __("Save")."</button>";

            print "</form>";

            print "</div>"; #pane
    }
            
    function save()
    {
        $sometext = explode(",", db_escape_string($this->link, $_POST["sometext"]));
        $sometext = array_map("trim", $sometext);
        $sometext = array_map("mb_strtolower", $sometext);
        $sometext = join(", ", $sometext);

        $this->host->set($this, "sometext", $sometext);

        echo __("Configuration saved.");
    }

}