1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Andreas Gohr <gohr@cosmocode.de>
5 */
6// must be run within Dokuwiki
7if(!defined('DOKU_INC')) die();
8
9if(!defined('BLOGTNG_DIR')) define('BLOGTNG_DIR',DOKU_PLUGIN.'blogtng/');
10
11/**
12 * Class helper_plugin_blogtng_linkback
13 */
14class helper_plugin_blogtng_linkback extends DokuWiki_Plugin {
15    /**
16     * @return bool
17     */
18    public function linkbackAllowed() {
19        $entry = $this->getPost();
20        return !plugin_isdisabled('blogtng') &&
21               $this->getConf('receive_linkbacks') &&
22               $entry['blog'] !== '' && $entry['blog'] !== null &&
23               $entry['commentstatus'] === 'enabled';
24    }
25
26    /**
27     * @return array|null
28     */
29    private function getPost() {
30        global $ID;
31        /** @var helper_plugin_blogtng_entry $ehelper */
32        $ehelper = plugin_load('helper', 'blogtng_entry');
33        $ehelper->load_by_pid(md5($ID));
34        return $ehelper->entry;
35    }
36
37    /**
38     * @param $type
39     * @param $title
40     * @param $sourceUri
41     * @param $excerpt
42     * @param $id
43     * @return bool
44     */
45    public function saveLinkback($type, $title, $sourceUri, $excerpt, $id) {
46        /** @var helper_plugin_blogtng_sqlite $sqlitehelper */
47        $sqlitehelper = plugin_load('helper', 'blogtng_sqlite');
48        if (!$sqlitehelper->ready()) return false;
49
50        $comment = array('source' => $type,
51                         'name'   => $title,
52                         'web'    => $sourceUri,
53                         'text'   => $excerpt,
54                         'pid'    => md5($id),
55                         'page'   => $id,
56                         'subscribe' => null,
57                         'status' => 'hidden',
58                         'ip' => clientIP(true));
59
60        $query = 'SELECT web, source FROM comments WHERE pid = ?';
61
62        $resid = $sqlitehelper->getDB()->query($query, $comment['pid']);
63        if ($resid === false) {
64            return false;
65        }
66
67        $comments = $sqlitehelper->getDB()->res2arr($resid);
68
69        foreach($comments as $c) {
70            if ($c['web'] === $comment['web'] && $c['source'] === $comment['source']) {
71                return false;
72            }
73        }
74
75        /** @var helper_plugin_blogtng_comments $chelper */
76        $chelper = plugin_load('helper', 'blogtng_comments');
77        $chelper->save($comment);
78        return true;
79    }
80}
81