Dotclear

source: plugins/dcFilterDuplicate/inc/class.filter.duplicate.php @ 3262

Revision 3262, 3.7 KB checked in by JcDenis, 10 years ago (diff)

switch to DC 2.6, clean up code

Line 
1<?php
2# -- BEGIN LICENSE BLOCK ----------------------------------
3#
4# This file is part of dcFilterDuplicate, a plugin for Dotclear 2.
5#
6# Copyright (c) 2009-2013 Jean-Christian Denis and contributors
7# contact@jcdenis.fr http://jcd.lv
8#
9# Licensed under the GPL version 2.0 license.
10# A copy of this license is available in LICENSE file or at
11# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
12#
13# -- END LICENSE BLOCK ------------------------------------
14
15if (!defined('DC_RC_PATH')) {
16
17     return null;
18}
19
20/**
21 * @ingroup DC_PLUGIN_DCFILTERDUPLICATE
22 * @brief Filter duplicate comments on multiblogs.
23 * @since 2.6
24 */
25class dcFilterDuplicate extends dcSpamFilter
26{
27     public $name = 'Duplicate comment filter';
28     public $has_gui = true;
29
30     protected function setInfo()
31     {
32          $this->name = __('Duplicate comment filter');
33          $this->description = __('Same comments on others blogs of a multiblog.');
34     }
35     
36     public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status)
37     {
38          if ($type != 'comment') {
39
40               return null;
41          }
42
43          $minlen = abs((integer) $this->core->blog->settings->dcFilterDuplicate->dcfilterduplicate_minlen);
44          if (strlen($content) < $minlen) {
45
46               return null;
47          }
48
49          try {
50               if ($this->isDuplicate($content, $ip)) {
51                    $this->markDuplicate($content, $ip);
52                    $status = 'Duplicate on other blog';
53
54                    return true;
55               }
56               else {
57
58                    return null;
59               }
60          }
61          catch (Exception $e) {
62               throw new Exception($e->getMessage());
63          }
64     }
65
66     public function isDuplicate($content, $ip)
67     {
68          $rs = $this->core->con->select(
69               'SELECT C.comment_id '.
70               'FROM '.$this->core->prefix.'comment C '.
71               'LEFT JOIN '.$this->core->prefix.'post P ON C.post_id=P.post_id '.
72               "WHERE P.blog_id != '".$this->core->blog->id."' ".
73               "AND C.comment_content='".$this->core->con->escape($content)."' ".
74               "AND C.comment_ip='".$ip."' "
75          );
76
77          return !$rs->isEmpty();
78     }
79     
80     public function markDuplicate($content, $ip)
81     {
82          $cur = $this->core->con->openCursor($this->core->prefix.'comment');
83          $this->core->con->writeLock($this->core->prefix.'comment');
84
85          $cur->comment_status = -2;
86          $cur->comment_spam_status = 'Duplicate on other blog';
87          $cur->comment_spam_filter = 'dcFilterDuplicate';
88
89          $cur->update(
90               "WHERE comment_content='".$this->core->con->escape($content)."' ".
91               "AND comment_ip='".$ip."' "
92          );
93          $this->core->con->unlock();
94
95          $this->triggerOtherBlogs($content, $ip);
96     }
97
98     public function gui($url)
99     {
100          $minlen = abs((integer) $this->core->blog->settings->dcFilterDuplicate->dcfilterduplicate_minlen);
101          if (isset($_POST['dcfilterduplicate_minlen'])) {
102               $minlen = abs((integer) $_POST['dcfilterduplicate_minlen']);
103               $this->core->blog->settings->dcFilterDuplicate->put(
104                    'dcfilterduplicate_minlen',
105                    $minlen,
106                    'integer'
107               );
108          }
109
110          $res =
111          '<form action="'.html::escapeURL($url).'" method="post">'.
112          '<p><label class="classic">'.__('Minimum content length before check for duplicate:').'<br />'.
113          form::field(array('dcfilterduplicate_minlen'), 65, 255, $minlen).
114          '</label></p>'.
115          '<p><input type="submit" name="save" value="'.__('Save').'" />'.
116          $this->core->formNonce().'</p>'.
117          '</form>';
118
119          return $res;
120     }
121
122     public function triggerOtherBlogs($content, $ip)
123     {
124          $rs = $this->core->con->select(
125               'SELECT P.blog_id '.
126               'FROM '.$this->core->prefix.'comment C '.
127               'LEFT JOIN '.$this->core->prefix.'post P ON C.post_id=P.post_id '.
128               "WHERE C.comment_content='".$this->core->con->escape($content)."' ".
129               "AND C.comment_ip='".$ip."' "
130          );
131
132          while ($rs->fetch()) {
133               $b = new dcBlog($this, $rs->blog_id);
134               $b->triggerBlog();
135               unset($b);
136          }
137     }
138}
Note: See TracBrowser for help on using the repository browser.

Sites map