Dotclear


Ignore:
Timestamp:
03/08/18 17:58:39 (7 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Code formatting (PSR-2)

Location:
plugins/antispam/inc
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • plugins/antispam/inc/class.dc.spamfilter.php

    r3396 r3730  
    1010# 
    1111# -- END LICENSE BLOCK ----------------------------------------- 
    12 if (!defined('DC_RC_PATH')) { return; } 
     12if (!defined('DC_RC_PATH')) {return;} 
    1313 
    1414class dcSpamFilter 
    1515{ 
    16      public $name; 
    17      public $description; 
    18      public $active = true; 
    19      public $order = 100; 
    20      public $auto_delete = false; 
    21      public $help = null; 
     16    public $name; 
     17    public $description; 
     18    public $active      = true; 
     19    public $order      = 100; 
     20    public $auto_delete = false; 
     21    public $help        = null; 
    2222 
    23      protected $has_gui = false; 
    24      protected $gui_url = null; 
     23    protected $has_gui = false; 
     24    protected $gui_url = null; 
    2525 
    26      protected $core; 
     26    protected $core; 
    2727 
    28      /** 
    29      Object constructor 
     28    /** 
     29    Object constructor 
    3030 
    31      @param    core      <b>dcCore</b>       Dotclear core object 
    32      */ 
    33      public function __construct($core) 
    34      { 
    35           $this->core =& $core; 
    36           $this->setInfo(); 
     31    @param    core        <b>dcCore</b>        Dotclear core object 
     32     */ 
     33    public function __construct($core) 
     34    { 
     35        $this->core = &$core; 
     36        $this->setInfo(); 
    3737 
    38           if (!$this->name) { 
    39                $this->name = get_class($this); 
    40           } 
     38        if (!$this->name) { 
     39            $this->name = get_class($this); 
     40        } 
    4141 
    42           if ($core && isset($core->adminurl)) { 
    43              $this->gui_url = $core->adminurl->get('admin.plugin.antispam',array('f' => get_class($this)),'&'); 
    44           } 
    45      } 
     42        if ($core && isset($core->adminurl)) { 
     43            $this->gui_url = $core->adminurl->get('admin.plugin.antispam', array('f' => get_class($this)), '&'); 
     44        } 
     45    } 
    4646 
    47      /** 
    48      This method is called by the constructor and allows you to change some 
    49      object properties without overloading object constructor. 
    50      */ 
    51      protected function setInfo() 
    52      { 
    53           $this->description = __('No description'); 
    54      } 
     47    /** 
     48    This method is called by the constructor and allows you to change some 
     49    object properties without overloading object constructor. 
     50     */ 
     51    protected function setInfo() 
     52    { 
     53        $this->description = __('No description'); 
     54    } 
    5555 
    56      /** 
    57      This method should return if a comment is a spam or not. If it returns true 
    58      or false, execution of next filters will be stoped. If should return nothing 
    59      to let next filters apply. 
     56    /** 
     57    This method should return if a comment is a spam or not. If it returns true 
     58    or false, execution of next filters will be stoped. If should return nothing 
     59    to let next filters apply. 
    6060 
    61      Your filter should also fill $status variable with its own information if 
    62      comment is a spam. 
     61    Your filter should also fill $status variable with its own information if 
     62    comment is a spam. 
    6363 
    64      @param         type      <b>string</b>       Comment type (comment or trackback) 
    65      @param         author    <b>string</b>       Comment author 
    66      @param         email     <b>string</b>       Comment author email 
    67      @param         site      <b>string</b>       Comment author website 
    68      @param         ip        <b>string</b>       Comment author IP address 
    69      @param         content   <b>string</b>       Comment content 
    70      @param         post_id   <b>integer</b>      Comment post_id 
    71      @param[out]    status    <b>integer</b>      Comment status 
    72      @return   <b>boolean</b> 
    73      */ 
    74      public function isSpam($type,$author,$email,$site,$ip,$content,$post_id,&$status) 
    75      { 
    76      } 
     64    @param        type        <b>string</b>        Comment type (comment or trackback) 
     65    @param        author    <b>string</b>        Comment author 
     66    @param        email    <b>string</b>        Comment author email 
     67    @param        site        <b>string</b>        Comment author website 
     68    @param        ip        <b>string</b>        Comment author IP address 
     69    @param        content    <b>string</b>        Comment content 
     70    @param        post_id    <b>integer</b>        Comment post_id 
     71    @param[out]    status    <b>integer</b>        Comment status 
     72    @return    <b>boolean</b> 
     73     */ 
     74    public function isSpam($type, $author, $email, $site, $ip, $content, $post_id, &$status) 
     75    { 
     76    } 
    7777 
    78      /** 
    79      This method is called when a non-spam (ham) comment becomes spam or when a 
    80      spam becomes a ham. 
     78    /** 
     79    This method is called when a non-spam (ham) comment becomes spam or when a 
     80    spam becomes a ham. 
    8181 
    82      @param    type      <b>string</b>       Comment type (comment or trackback) 
    83      @param    filter    <b>string</b>       Filter name 
    84      @param    author    <b>string</b>       Comment author 
    85      @param    email     <b>string</b>       Comment author email 
    86      @param    site      <b>string</b>       Comment author website 
    87      @param    ip        <b>string</b>       Comment author IP address 
    88      @param    content   <b>string</b>       Comment content 
    89      @param    post_url  <b>string</b>       Post URL 
    90      @param    rs        <b>record</b>       Comment record 
    91      @return   <b>boolean</b> 
    92      */ 
    93      public function trainFilter($status,$filter,$type,$author,$email,$site,$ip,$content,$rs) 
    94      { 
    95      } 
     82    @param    type        <b>string</b>        Comment type (comment or trackback) 
     83    @param    filter    <b>string</b>        Filter name 
     84    @param    author    <b>string</b>        Comment author 
     85    @param    email    <b>string</b>        Comment author email 
     86    @param    site        <b>string</b>        Comment author website 
     87    @param    ip        <b>string</b>        Comment author IP address 
     88    @param    content    <b>string</b>        Comment content 
     89    @param    post_url    <b>string</b>        Post URL 
     90    @param    rs        <b>record</b>        Comment record 
     91    @return    <b>boolean</b> 
     92     */ 
     93    public function trainFilter($status, $filter, $type, $author, $email, $site, $ip, $content, $rs) 
     94    { 
     95    } 
    9696 
    97      /** 
    98      This method returns filter status message. You can overload this method to 
    99      return a custom message. Message is shown in comment details and in 
    100      comments list. 
     97    /** 
     98    This method returns filter status message. You can overload this method to 
     99    return a custom message. Message is shown in comment details and in 
     100    comments list. 
    101101 
    102      @param    status         <b>string</b>       Filter status. 
    103      @param    comment_id     <b>record</b>       Comment record 
    104      @return   <b>string</b> 
    105      */ 
    106      public function getStatusMessage($status,$comment_id) 
    107      { 
    108           return sprintf(__('Filtered by %1$s (%2$s)'),$this->guiLink(),$status); 
    109      } 
     102    @param    status        <b>string</b>        Filter status. 
     103    @param    comment_id    <b>record</b>        Comment record 
     104    @return    <b>string</b> 
     105     */ 
     106    public function getStatusMessage($status, $comment_id) 
     107    { 
     108        return sprintf(__('Filtered by %1$s (%2$s)'), $this->guiLink(), $status); 
     109    } 
    110110 
    111      /** 
    112      This method is called when you enter filter configuration. Your class should 
    113      have $has_gui property set to "true" to enable GUI. 
     111    /** 
     112    This method is called when you enter filter configuration. Your class should 
     113    have $has_gui property set to "true" to enable GUI. 
    114114 
    115      In this method you should put everything related to filter configuration. 
    116      $url variable is the URL of GUI <i>unescaped</i>. 
     115    In this method you should put everything related to filter configuration. 
     116    $url variable is the URL of GUI <i>unescaped</i>. 
    117117 
    118      @param    url       <b>string</b>       GUI URL. 
    119      */ 
    120      public function gui($url) 
    121      { 
    122      } 
     118    @param    url        <b>string</b>        GUI URL. 
     119     */ 
     120    public function gui($url) 
     121    { 
     122    } 
    123123 
    124      public function hasGUI() 
    125      { 
    126           if (!$this->core->auth->check('admin',$this->core->blog->id)) { 
    127                return false; 
    128           } 
     124    public function hasGUI() 
     125    { 
     126        if (!$this->core->auth->check('admin', $this->core->blog->id)) { 
     127            return false; 
     128        } 
    129129 
    130           if (!$this->has_gui) { 
    131                return false; 
    132           } 
     130        if (!$this->has_gui) { 
     131            return false; 
     132        } 
    133133 
    134           return true; 
    135      } 
     134        return true; 
     135    } 
    136136 
    137      public function guiURL() 
    138      { 
    139           if (!$this->hasGui()) { 
    140                return false; 
    141           } 
     137    public function guiURL() 
     138    { 
     139        if (!$this->hasGui()) { 
     140            return false; 
     141        } 
    142142 
    143           return $this->gui_url; 
    144      } 
     143        return $this->gui_url; 
     144    } 
    145145 
    146      /** 
    147      Returns a link to filter GUI if exists or only filter name if has_gui 
    148      property is false. 
     146    /** 
     147    Returns a link to filter GUI if exists or only filter name if has_gui 
     148    property is false. 
    149149 
    150      @return   <b>string</b> 
    151      */ 
    152      public function guiLink() 
    153      { 
    154           if (($url = $this->guiURL()) !== false) { 
    155                $url = html::escapeHTML($url); 
    156                $link = '<a href="%2$s">%1$s</a>'; 
    157           } else { 
    158                $link = '%1$s'; 
    159           } 
     150    @return    <b>string</b> 
     151     */ 
     152    public function guiLink() 
     153    { 
     154        if (($url = $this->guiURL()) !== false) { 
     155            $url = html::escapeHTML($url); 
     156            $link = '<a href="%2$s">%1$s</a>'; 
     157        } else { 
     158            $link = '%1$s'; 
     159        } 
    160160 
    161           return sprintf($link,$this->name,$url); 
    162      } 
     161        return sprintf($link, $this->name, $url); 
     162    } 
    163163 
    164      public function help() 
    165      { 
    166      } 
     164    public function help() 
     165    { 
     166    } 
    167167} 
  • plugins/antispam/inc/class.dc.spamfilters.php

    r3159 r3730  
    1010# 
    1111# -- END LICENSE BLOCK ----------------------------------------- 
    12 if (!defined('DC_RC_PATH')) { return; } 
     12if (!defined('DC_RC_PATH')) {return;} 
    1313 
    1414class dcSpamFilters 
    1515{ 
    16      private $filters = array(); 
    17      private $filters_opt = array(); 
    18      private $core; 
     16    private $filters    = array(); 
     17    private $filters_opt = array(); 
     18    private $core; 
    1919 
    20      public function __construct($core) 
    21      { 
    22           $this->core =& $core; 
    23      } 
     20    public function __construct($core) 
     21    { 
     22        $this->core = &$core; 
     23    } 
    2424 
    25      public function init($filters) 
    26      { 
    27           foreach ($filters as $f) 
    28           { 
    29                if (!class_exists($f)) { 
    30                     continue; 
    31                } 
     25    public function init($filters) 
     26    { 
     27        foreach ($filters as $f) { 
     28            if (!class_exists($f)) { 
     29                continue; 
     30            } 
    3231 
    33                $r = new ReflectionClass($f); 
    34                $p = $r->getParentClass(); 
     32            $r = new ReflectionClass($f); 
     33            $p = $r->getParentClass(); 
    3534 
    36                if (!$p || $p->name != 'dcSpamFilter') { 
    37                     continue; 
    38                } 
     35            if (!$p || $p->name != 'dcSpamFilter') { 
     36                continue; 
     37            } 
    3938 
    40                $this->filters[$f] = new $f($this->core); 
    41           } 
     39            $this->filters[$f] = new $f($this->core); 
     40        } 
    4241 
    43           $this->setFilterOpts(); 
    44           if (!empty($this->filters_opt)) { 
    45                uasort($this->filters,array($this,'orderCallBack')); 
    46           } 
    47      } 
     42        $this->setFilterOpts(); 
     43        if (!empty($this->filters_opt)) { 
     44            uasort($this->filters, array($this, 'orderCallBack')); 
     45        } 
     46    } 
    4847 
    49      public function getFilters() 
    50      { 
    51           return $this->filters; 
    52      } 
     48    public function getFilters() 
     49    { 
     50        return $this->filters; 
     51    } 
    5352 
    54      public function isSpam($cur) 
    55      { 
    56           foreach ($this->filters as $fid => $f) 
    57           { 
    58                if (!$f->active) { 
    59                     continue; 
    60                } 
     53    public function isSpam($cur) 
     54    { 
     55        foreach ($this->filters as $fid => $f) { 
     56            if (!$f->active) { 
     57                continue; 
     58            } 
    6159 
    62                $type = $cur->comment_trackback ? 'trackback' : 'comment'; 
    63                $author = $cur->comment_author; 
    64                $email = $cur->comment_email; 
    65                $site = $cur->comment_site; 
    66                $ip = $cur->comment_ip; 
    67                $content = $cur->comment_content; 
    68                $post_id = $cur->post_id; 
     60            $type    = $cur->comment_trackback ? 'trackback' : 'comment'; 
     61            $author = $cur->comment_author; 
     62            $email  = $cur->comment_email; 
     63            $site    = $cur->comment_site; 
     64            $ip      = $cur->comment_ip; 
     65            $content = $cur->comment_content; 
     66            $post_id = $cur->post_id; 
    6967 
    70                $is_spam = $f->isSpam($type,$author,$email,$site,$ip,$content,$post_id,$status); 
     68            $is_spam = $f->isSpam($type, $author, $email, $site, $ip, $content, $post_id, $status); 
    7169 
    72                if ($is_spam === true) { 
    73                     if ($f->auto_delete) { 
    74                          $cur->clean(); 
    75                     } else { 
    76                          $cur->comment_status = -2; 
    77                          $cur->comment_spam_status = $status; 
    78                          $cur->comment_spam_filter = $fid; 
    79                     } 
    80                     return true; 
    81                } elseif ($is_spam === false) { 
    82                     return false; 
    83                } 
    84           } 
     70            if ($is_spam === true) { 
     71                if ($f->auto_delete) { 
     72                    $cur->clean(); 
     73                } else { 
     74                    $cur->comment_status      = -2; 
     75                    $cur->comment_spam_status = $status; 
     76                    $cur->comment_spam_filter = $fid; 
     77                } 
     78                return true; 
     79            } elseif ($is_spam === false) { 
     80                return false; 
     81            } 
     82        } 
    8583 
    86           return false; 
    87      } 
     84        return false; 
     85    } 
    8886 
    89      public function trainFilters($rs,$status,$filter_name) 
    90      { 
    91           foreach ($this->filters as $fid => $f) 
    92           { 
    93                if (!$f->active) { 
    94                     continue; 
    95                } 
     87    public function trainFilters($rs, $status, $filter_name) 
     88    { 
     89        foreach ($this->filters as $fid => $f) { 
     90            if (!$f->active) { 
     91                continue; 
     92            } 
    9693 
    97                $type = $rs->comment_trackback ? 'trackback' : 'comment'; 
    98                $author = $rs->comment_author; 
    99                $email = $rs->comment_email; 
    100                $site = $rs->comment_site; 
    101                $ip = $rs->comment_ip; 
    102                $content = $rs->comment_content; 
     94            $type    = $rs->comment_trackback ? 'trackback' : 'comment'; 
     95            $author = $rs->comment_author; 
     96            $email  = $rs->comment_email; 
     97            $site    = $rs->comment_site; 
     98            $ip      = $rs->comment_ip; 
     99            $content = $rs->comment_content; 
    103100 
    104                $f->trainFilter($status,$filter_name,$type,$author,$email,$site,$ip,$content,$rs); 
    105           } 
    106      } 
     101            $f->trainFilter($status, $filter_name, $type, $author, $email, $site, $ip, $content, $rs); 
     102        } 
     103    } 
    107104 
    108      public function statusMessage($rs,$filter_name) 
    109      { 
    110           $f = isset($this->filters[$filter_name]) ? $this->filters[$filter_name] : null; 
     105    public function statusMessage($rs, $filter_name) 
     106    { 
     107        $f = isset($this->filters[$filter_name]) ? $this->filters[$filter_name] : null; 
    111108 
    112           if ($f === null) 
    113           { 
    114                return __('Unknown filter.'); 
    115           } 
    116           else 
    117           { 
    118                $status = $rs->exists('comment_spam_status') ? $rs->comment_spam_status : null; 
     109        if ($f === null) { 
     110            return __('Unknown filter.'); 
     111        } else { 
     112            $status = $rs->exists('comment_spam_status') ? $rs->comment_spam_status : null; 
    119113 
    120                return $f->getStatusMessage($status,$rs->comment_id); 
    121           } 
    122      } 
     114            return $f->getStatusMessage($status, $rs->comment_id); 
     115        } 
     116    } 
    123117 
    124      public function saveFilterOpts($opts,$global=false) 
    125      { 
    126           $this->core->blog->settings->addNamespace('antispam'); 
    127           if ($global) { 
    128                $this->core->blog->settings->antispam->drop('antispam_filters'); 
    129           } 
    130           $this->core->blog->settings->antispam->put('antispam_filters',$opts,'array','Antispam Filters',true,$global); 
    131      } 
     118    public function saveFilterOpts($opts, $global = false) 
     119    { 
     120        $this->core->blog->settings->addNamespace('antispam'); 
     121        if ($global) { 
     122            $this->core->blog->settings->antispam->drop('antispam_filters'); 
     123        } 
     124        $this->core->blog->settings->antispam->put('antispam_filters', $opts, 'array', 'Antispam Filters', true, $global); 
     125    } 
    132126 
    133      private function setFilterOpts() 
    134      { 
    135           if ($this->core->blog->settings->antispam->antispam_filters !== null) { 
    136                $this->filters_opt = $this->core->blog->settings->antispam->antispam_filters; 
    137           } 
     127    private function setFilterOpts() 
     128    { 
     129        if ($this->core->blog->settings->antispam->antispam_filters !== null) { 
     130            $this->filters_opt = $this->core->blog->settings->antispam->antispam_filters; 
     131        } 
    138132 
    139           # Create default options if needed 
    140           if (!is_array($this->filters_opt)) { 
    141                $this->saveFilterOpts(array(),true); 
    142                $this->filters_opt = array(); 
    143           } 
     133        # Create default options if needed 
     134        if (!is_array($this->filters_opt)) { 
     135            $this->saveFilterOpts(array(), true); 
     136            $this->filters_opt = array(); 
     137        } 
    144138 
    145           foreach ($this->filters_opt as $k => $o) 
    146           { 
    147                if (isset($this->filters[$k]) && is_array($o)) { 
    148                     $this->filters[$k]->active = isset($o[0])?$o[0]:false; 
    149                     $this->filters[$k]->order = isset($o[1])?$o[1]:0; 
    150                     $this->filters[$k]->auto_delete = isset($o[2])?$o[2]:false; 
    151                } 
    152           } 
    153      } 
     139        foreach ($this->filters_opt as $k => $o) { 
     140            if (isset($this->filters[$k]) && is_array($o)) { 
     141                $this->filters[$k]->active      = isset($o[0]) ? $o[0] : false; 
     142                $this->filters[$k]->order       = isset($o[1]) ? $o[1] : 0; 
     143                $this->filters[$k]->auto_delete = isset($o[2]) ? $o[2] : false; 
     144            } 
     145        } 
     146    } 
    154147 
    155      private function orderCallBack($a,$b) 
    156      { 
    157           if ($a->order == $b->order) { 
    158                return 0; 
    159           } 
     148    private function orderCallBack($a, $b) 
     149    { 
     150        if ($a->order == $b->order) { 
     151            return 0; 
     152        } 
    160153 
    161           return $a->order > $b->order ? 1 : -1; 
    162      } 
     154        return $a->order > $b->order ? 1 : -1; 
     155    } 
    163156} 
  • plugins/antispam/inc/lib.dc.antispam.php

    r3627 r3730  
    1010# 
    1111# -- END LICENSE BLOCK ----------------------------------------- 
    12 if (!defined('DC_RC_PATH')) { return; } 
     12if (!defined('DC_RC_PATH')) {return;} 
    1313 
    1414class dcAntispam 
    1515{ 
    16      public static $filters; 
    17  
    18      public static function initFilters() 
    19      { 
    20           global $core; 
    21  
    22           if (!isset($core->spamfilters)) { 
    23                return; 
    24           } 
    25  
    26           self::$filters = new dcSpamFilters($core); 
    27           self::$filters->init($core->spamfilters); 
    28      } 
    29  
    30      public static function isSpam($cur) 
    31      { 
    32           self::initFilters(); 
    33           self::$filters->isSpam($cur); 
    34      } 
    35  
    36      public static function trainFilters($blog,$cur,$rs) 
    37      { 
    38           $status = null; 
    39           # From ham to spam 
    40           if ($rs->comment_status != -2 && $cur->comment_status == -2) { 
    41                $status = 'spam'; 
    42           } 
    43  
    44           # From spam to ham 
    45           if ($rs->comment_status == -2 && $cur->comment_status == 1) { 
    46                $status = 'ham'; 
    47           } 
    48  
    49           # the status of this comment has changed 
    50           if ($status) 
    51           { 
    52                $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null; 
    53  
    54                self::initFilters(); 
    55                self::$filters->trainFilters($rs,$status,$filter_name); 
    56           } 
    57      } 
    58  
    59      public static function statusMessage($rs) 
    60      { 
    61           if ($rs->exists('comment_status') && $rs->comment_status == -2) 
    62           { 
    63                $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null; 
    64  
    65                self::initFilters(); 
    66  
    67                return 
    68                '<p><strong>'.__('This comment is a spam:').'</strong> '. 
    69                self::$filters->statusMessage($rs,$filter_name).'</p>'; 
    70           } 
    71      } 
    72  
    73      public static function dashboardIcon($core, $icons) 
    74      { 
    75           if (($count = self::countSpam($core)) > 0) { 
    76                $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)'); 
    77                $icons['comments'][0] .= '</span></a> <br /><a href="'.$core->adminurl->get('admin.comments',array('status' => '-2')).'"><span>'. 
    78                     sprintf($str,$count); 
    79           } 
    80      } 
    81  
    82      public static function dashboardIconTitle($core) 
    83      { 
    84           if (($count = self::countSpam($core)) > 0) { 
    85                $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)'); 
    86                return '</span></a> <br /><a href="'.$core->adminurl->get('admin.comments',array('status' => '-2')).'"><span>'. 
    87                     sprintf($str,$count); 
    88           } else { 
    89                return ''; 
    90           } 
    91      } 
    92  
    93      public static function countSpam($core) 
    94      { 
    95           return $core->blog->getComments(array('comment_status'=>-2),true)->f(0); 
    96      } 
    97  
    98      public static function countPublishedComments($core) 
    99      { 
    100           return $core->blog->getComments(array('comment_status'=>1),true)->f(0); 
    101      } 
    102  
    103      public static function delAllSpam($core, $beforeDate = null) 
    104      { 
    105           $strReq = 
    106           'SELECT comment_id '. 
    107           'FROM '.$core->prefix.'comment C '. 
    108           'JOIN '.$core->prefix.'post P ON P.post_id = C.post_id '. 
    109           "WHERE blog_id = '".$core->con->escape($core->blog->id)."' ". 
    110           'AND comment_status = -2 '; 
    111           if ($beforeDate) { 
    112                $strReq .= 'AND comment_dt < \''.$beforeDate.'\' '; 
    113           } 
    114  
    115           $rs = $core->con->select($strReq); 
    116           $r = array(); 
    117           while ($rs->fetch()) { 
    118                $r[] = (integer) $rs->comment_id; 
    119           } 
    120  
    121           if (empty($r)) { 
    122                return; 
    123           } 
    124  
    125           $strReq = 
    126           'DELETE FROM '.$core->prefix.'comment '. 
    127           'WHERE comment_id '.$core->con->in($r).' '; 
    128  
    129           $core->con->execute($strReq); 
    130      } 
    131  
    132      public static function getUserCode($core) 
    133      { 
    134           $code = 
    135           pack('a32',$core->auth->userID()). 
    136           pack('H*',$core->auth->cryptLegacy($core->auth->getInfo('user_pwd'))); 
    137           return bin2hex($code); 
    138      } 
    139  
    140      public static function checkUserCode($core,$code) 
    141      { 
    142           $code = pack('H*',$code); 
    143  
    144           $user_id = trim(@pack('a32',substr($code,0,32))); 
    145           $pwd = @unpack('H*hex',substr($code,32)); 
    146  
    147           if ($user_id === false || $pwd === false) { 
    148                return false; 
    149           } 
    150  
    151           $pwd = $pwd['hex']; 
    152  
    153           $strReq = 'SELECT user_id, user_pwd '. 
    154                     'FROM '.$core->prefix.'user '. 
    155                     "WHERE user_id = '".$core->con->escape($user_id)."' "; 
    156  
    157           $rs = $core->con->select($strReq); 
    158  
    159           if ($rs->isEmpty()) { 
    160                return false; 
    161           } 
    162  
    163           if ($core->auth->cryptLegacy($rs->user_pwd) != $pwd) { 
    164                return false; 
    165           } 
    166  
    167           $permissions = $core->getBlogPermissions($core->blog->id); 
    168  
    169           if ( empty($permissions[$rs->user_id]) ) { 
    170                return false; 
    171           } 
    172  
    173           return $rs->user_id; 
    174      } 
    175  
    176      public static function purgeOldSpam($core) 
    177      { 
    178           $defaultDateLastPurge = time(); 
    179           $defaultModerationTTL = '7'; 
    180           $init = false; 
    181  
    182           // settings 
    183           $core->blog->settings->addNamespace('antispam'); 
    184  
    185           $dateLastPurge = $core->blog->settings->antispam->antispam_date_last_purge; 
    186           if ($dateLastPurge === null) { 
    187                $init = true; 
    188                $core->blog->settings->antispam->put('antispam_date_last_purge',$defaultDateLastPurge,'integer','Antispam Date Last Purge (unix timestamp)',true,false); 
    189                $dateLastPurge = $defaultDateLastPurge; 
    190           } 
    191           $moderationTTL = $core->blog->settings->antispam->antispam_moderation_ttl; 
    192           if ($moderationTTL === null) { 
    193                $core->blog->settings->antispam->put('antispam_moderation_ttl',$defaultModerationTTL,'integer','Antispam Moderation TTL (days)',true,false); 
    194                $moderationTTL = $defaultModerationTTL; 
    195           } 
    196  
    197           if ($moderationTTL < 0) { 
    198                // disabled 
    199                return; 
    200           } 
    201  
    202           // we call the purge every day 
    203           if ((time()-$dateLastPurge) > (86400)) { 
    204                // update dateLastPurge 
    205                if (!$init) { 
    206                     $core->blog->settings->antispam->put('antispam_date_last_purge',time(),null,null,true,false); 
    207                } 
    208                $date = date('Y-m-d H:i:s', time() - $moderationTTL*86400); 
    209                dcAntispam::delAllSpam($core, $date); 
    210           } 
    211      } 
     16    public static $filters; 
     17 
     18    public static function initFilters() 
     19    { 
     20        global $core; 
     21 
     22        if (!isset($core->spamfilters)) { 
     23            return; 
     24        } 
     25 
     26        self::$filters = new dcSpamFilters($core); 
     27        self::$filters->init($core->spamfilters); 
     28    } 
     29 
     30    public static function isSpam($cur) 
     31    { 
     32        self::initFilters(); 
     33        self::$filters->isSpam($cur); 
     34    } 
     35 
     36    public static function trainFilters($blog, $cur, $rs) 
     37    { 
     38        $status = null; 
     39        # From ham to spam 
     40        if ($rs->comment_status != -2 && $cur->comment_status == -2) { 
     41            $status = 'spam'; 
     42        } 
     43 
     44        # From spam to ham 
     45        if ($rs->comment_status == -2 && $cur->comment_status == 1) { 
     46            $status = 'ham'; 
     47        } 
     48 
     49        # the status of this comment has changed 
     50        if ($status) { 
     51            $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null; 
     52 
     53            self::initFilters(); 
     54            self::$filters->trainFilters($rs, $status, $filter_name); 
     55        } 
     56    } 
     57 
     58    public static function statusMessage($rs) 
     59    { 
     60        if ($rs->exists('comment_status') && $rs->comment_status == -2) { 
     61            $filter_name = $rs->exists('comment_spam_filter') ? $rs->comment_spam_filter : null; 
     62 
     63            self::initFilters(); 
     64 
     65            return 
     66            '<p><strong>' . __('This comment is a spam:') . '</strong> ' . 
     67            self::$filters->statusMessage($rs, $filter_name) . '</p>'; 
     68        } 
     69    } 
     70 
     71    public static function dashboardIcon($core, $icons) 
     72    { 
     73        if (($count = self::countSpam($core)) > 0) { 
     74            $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)'); 
     75            $icons['comments'][0] .= '</span></a> <br /><a href="' . $core->adminurl->get('admin.comments', array('status' => '-2')) . '"><span>' . 
     76            sprintf($str, $count); 
     77        } 
     78    } 
     79 
     80    public static function dashboardIconTitle($core) 
     81    { 
     82        if (($count = self::countSpam($core)) > 0) { 
     83            $str = ($count > 1) ? __('(including %d spam comments)') : __('(including %d spam comment)'); 
     84            return '</span></a> <br /><a href="' . $core->adminurl->get('admin.comments', array('status' => '-2')) . '"><span>' . 
     85            sprintf($str, $count); 
     86        } else { 
     87            return ''; 
     88        } 
     89    } 
     90 
     91    public static function countSpam($core) 
     92    { 
     93        return $core->blog->getComments(array('comment_status' => -2), true)->f(0); 
     94    } 
     95 
     96    public static function countPublishedComments($core) 
     97    { 
     98        return $core->blog->getComments(array('comment_status' => 1), true)->f(0); 
     99    } 
     100 
     101    public static function delAllSpam($core, $beforeDate = null) 
     102    { 
     103        $strReq = 
     104        'SELECT comment_id ' . 
     105        'FROM ' . $core->prefix . 'comment C ' . 
     106        'JOIN ' . $core->prefix . 'post P ON P.post_id = C.post_id ' . 
     107        "WHERE blog_id = '" . $core->con->escape($core->blog->id) . "' " . 
     108            'AND comment_status = -2 '; 
     109        if ($beforeDate) { 
     110            $strReq .= 'AND comment_dt < \'' . $beforeDate . '\' '; 
     111        } 
     112 
     113        $rs = $core->con->select($strReq); 
     114        $r  = array(); 
     115        while ($rs->fetch()) { 
     116            $r[] = (integer) $rs->comment_id; 
     117        } 
     118 
     119        if (empty($r)) { 
     120            return; 
     121        } 
     122 
     123        $strReq = 
     124        'DELETE FROM ' . $core->prefix . 'comment ' . 
     125        'WHERE comment_id ' . $core->con->in($r) . ' '; 
     126 
     127        $core->con->execute($strReq); 
     128    } 
     129 
     130    public static function getUserCode($core) 
     131    { 
     132        $code = 
     133        pack('a32', $core->auth->userID()) . 
     134        pack('H*', $core->auth->cryptLegacy($core->auth->getInfo('user_pwd'))); 
     135        return bin2hex($code); 
     136    } 
     137 
     138    public static function checkUserCode($core, $code) 
     139    { 
     140        $code = pack('H*', $code); 
     141 
     142        $user_id = trim(@pack('a32', substr($code, 0, 32))); 
     143        $pwd     = @unpack('H*hex', substr($code, 32)); 
     144 
     145        if ($user_id === false || $pwd === false) { 
     146            return false; 
     147        } 
     148 
     149        $pwd = $pwd['hex']; 
     150 
     151        $strReq = 'SELECT user_id, user_pwd ' . 
     152        'FROM ' . $core->prefix . 'user ' . 
     153        "WHERE user_id = '" . $core->con->escape($user_id) . "' "; 
     154 
     155        $rs = $core->con->select($strReq); 
     156 
     157        if ($rs->isEmpty()) { 
     158            return false; 
     159        } 
     160 
     161        if ($core->auth->cryptLegacy($rs->user_pwd) != $pwd) { 
     162            return false; 
     163        } 
     164 
     165        $permissions = $core->getBlogPermissions($core->blog->id); 
     166 
     167        if (empty($permissions[$rs->user_id])) { 
     168            return false; 
     169        } 
     170 
     171        return $rs->user_id; 
     172    } 
     173 
     174    public static function purgeOldSpam($core) 
     175    { 
     176        $defaultDateLastPurge = time(); 
     177        $defaultModerationTTL = '7'; 
     178        $init                 = false; 
     179 
     180        // settings 
     181        $core->blog->settings->addNamespace('antispam'); 
     182 
     183        $dateLastPurge = $core->blog->settings->antispam->antispam_date_last_purge; 
     184        if ($dateLastPurge === null) { 
     185            $init = true; 
     186            $core->blog->settings->antispam->put('antispam_date_last_purge', $defaultDateLastPurge, 'integer', 'Antispam Date Last Purge (unix timestamp)', true, false); 
     187            $dateLastPurge = $defaultDateLastPurge; 
     188        } 
     189        $moderationTTL = $core->blog->settings->antispam->antispam_moderation_ttl; 
     190        if ($moderationTTL === null) { 
     191            $core->blog->settings->antispam->put('antispam_moderation_ttl', $defaultModerationTTL, 'integer', 'Antispam Moderation TTL (days)', true, false); 
     192            $moderationTTL = $defaultModerationTTL; 
     193        } 
     194 
     195        if ($moderationTTL < 0) { 
     196            // disabled 
     197            return; 
     198        } 
     199 
     200        // we call the purge every day 
     201        if ((time() - $dateLastPurge) > (86400)) { 
     202            // update dateLastPurge 
     203            if (!$init) { 
     204                $core->blog->settings->antispam->put('antispam_date_last_purge', time(), null, null, true, false); 
     205            } 
     206            $date = date('Y-m-d H:i:s', time() - $moderationTTL * 86400); 
     207            dcAntispam::delAllSpam($core, $date); 
     208        } 
     209    } 
    212210} 
  • plugins/antispam/inc/lib.dc.antispam.url.php

    r2566 r3730  
    1010# 
    1111# -- END LICENSE BLOCK ----------------------------------------- 
    12 if (!defined('DC_RC_PATH')) { return; } 
     12if (!defined('DC_RC_PATH')) {return;} 
    1313 
    1414class dcAntispamURL extends dcUrlHandlers 
    1515{ 
    16      public static function hamFeed($args) 
    17      { 
    18           self::genFeed('ham',$args); 
    19      } 
     16    public static function hamFeed($args) 
     17    { 
     18        self::genFeed('ham', $args); 
     19    } 
    2020 
    21      public static function spamFeed($args) 
    22      { 
    23           self::genFeed('spam',$args); 
    24      } 
     21    public static function spamFeed($args) 
     22    { 
     23        self::genFeed('spam', $args); 
     24    } 
    2525 
    26      private static function genFeed($type,$args) 
    27      { 
    28           global $core; 
    29           $user_id = dcAntispam::checkUserCode($core,$args); 
     26    private static function genFeed($type, $args) 
     27    { 
     28        global $core; 
     29        $user_id = dcAntispam::checkUserCode($core, $args); 
    3030 
    31           if ($user_id === false) { 
    32                self::p404(); 
    33                return; 
    34           } 
     31        if ($user_id === false) { 
     32            self::p404(); 
     33            return; 
     34        } 
    3535 
    36           $core->auth->checkUser($user_id,null,null); 
     36        $core->auth->checkUser($user_id, null, null); 
    3737 
    38           header('Content-Type: application/xml; charset=UTF-8'); 
     38        header('Content-Type: application/xml; charset=UTF-8'); 
    3939 
    40           $title = $core->blog->name.' - '.__('Spam moderation'). ' - '; 
    41           $params = array(); 
    42           $end_url = ''; 
    43           if ($type == 'spam') { 
    44                $title .= __('Spam'); 
    45                $params['comment_status'] = -2; 
    46                $end_url = '?status=-2'; 
    47           } else { 
    48                $title .= __('Ham'); 
    49                $params['sql'] = ' AND comment_status IN (1,-1) '; 
    50           } 
     40        $title   = $core->blog->name . ' - ' . __('Spam moderation') . ' - '; 
     41        $params = array(); 
     42        $end_url = ''; 
     43        if ($type == 'spam') { 
     44            $title .= __('Spam'); 
     45            $params['comment_status'] = -2; 
     46            $end_url                  = '?status=-2'; 
     47        } else { 
     48            $title .= __('Ham'); 
     49            $params['sql'] = ' AND comment_status IN (1,-1) '; 
     50        } 
    5151 
    52           echo 
    53           '<?xml version="1.0" encoding="utf-8"?>'."\n". 
    54           '<rss version="2.0"'."\n". 
    55           'xmlns:dc="http://purl.org/dc/elements/1.1/"'."\n". 
    56           'xmlns:content="http://purl.org/rss/1.0/modules/content/">'."\n". 
    57           '<channel>'."\n". 
    58           '<title>'.html::escapeHTML($title).'</title>'."\n". 
    59           '<link>'.(DC_ADMIN_URL ? DC_ADMIN_URL.'comments.php'.$end_url : 'about:blank').'</link>'."\n". 
    60           '<description></description>'."\n"; 
     52        echo 
     53        '<?xml version="1.0" encoding="utf-8"?>' . "\n" . 
     54        '<rss version="2.0"' . "\n" . 
     55        'xmlns:dc="http://purl.org/dc/elements/1.1/"' . "\n" . 
     56        'xmlns:content="http://purl.org/rss/1.0/modules/content/">' . "\n" . 
     57        '<channel>' . "\n" . 
     58        '<title>' . html::escapeHTML($title) . '</title>' . "\n" . 
     59            '<link>' . (DC_ADMIN_URL ? DC_ADMIN_URL . 'comments.php' . $end_url : 'about:blank') . '</link>' . "\n" . 
     60            '<description></description>' . "\n"; 
    6161 
    62           $rs = $core->blog->getComments($params); 
    63           $maxitems = 20; 
    64           $nbitems = 0; 
     62        $rs      = $core->blog->getComments($params); 
     63        $maxitems = 20; 
     64        $nbitems = 0; 
    6565 
    66           while ($rs->fetch() && ($nbitems < $maxitems)) 
    67           { 
    68                $nbitems++; 
    69                $uri = DC_ADMIN_URL ? DC_ADMIN_URL.'comment.php?id='.$rs->comment_id : 'about:blank'; 
    70                $author = $rs->comment_author; 
    71                $title = $rs->post_title.' - '.$author; 
    72                if ($type == 'spam') { 
    73                     $title .= '('.$rs->comment_spam_filter.')'; 
    74                } 
    75                $id = $rs->getFeedID(); 
     66        while ($rs->fetch() && ($nbitems < $maxitems)) { 
     67            $nbitems++; 
     68            $uri    = DC_ADMIN_URL ? DC_ADMIN_URL . 'comment.php?id=' . $rs->comment_id : 'about:blank'; 
     69            $author = $rs->comment_author; 
     70            $title  = $rs->post_title . ' - ' . $author; 
     71            if ($type == 'spam') { 
     72                $title .= '(' . $rs->comment_spam_filter . ')'; 
     73            } 
     74            $id = $rs->getFeedID(); 
    7675 
    77                $content = '<p>IP: '.$rs->comment_ip; 
     76            $content = '<p>IP: ' . $rs->comment_ip; 
    7877 
    79                if (trim($rs->comment_site)) { 
    80                     $content .= '<br />URL: <a href="'.$rs->comment_site.'">'.$rs->comment_site.'</a>'; 
    81                } 
    82                $content .= "</p><hr />\n"; 
    83                $content .= $rs->comment_content; 
     78            if (trim($rs->comment_site)) { 
     79                $content .= '<br />URL: <a href="' . $rs->comment_site . '">' . $rs->comment_site . '</a>'; 
     80            } 
     81            $content .= "</p><hr />\n"; 
     82            $content .= $rs->comment_content; 
    8483 
    85                echo 
    86                '<item>'."\n". 
    87                '  <title>'.html::escapeHTML($title).'</title>'."\n". 
    88                '  <link>'.$uri.'</link>'."\n". 
    89                '  <guid>'.$id.'</guid>'."\n". 
    90                '  <pubDate>'.$rs->getRFC822Date().'</pubDate>'."\n". 
    91                '  <dc:creator>'.html::escapeHTML($author).'</dc:creator>'."\n". 
    92                '  <description>'.html::escapeHTML($content).'</description>'."\n". 
    93                '</item>'; 
    94           } 
     84            echo 
     85            '<item>' . "\n" . 
     86            '  <title>' . html::escapeHTML($title) . '</title>' . "\n" . 
     87            '  <link>' . $uri . '</link>' . "\n" . 
     88            '  <guid>' . $id . '</guid>' . "\n" . 
     89            '  <pubDate>' . $rs->getRFC822Date() . '</pubDate>' . "\n" . 
     90            '  <dc:creator>' . html::escapeHTML($author) . '</dc:creator>' . "\n" . 
     91            '  <description>' . html::escapeHTML($content) . '</description>' . "\n" . 
     92                '</item>'; 
     93        } 
    9594 
    96           echo "</channel>\n</rss>"; 
    97      } 
     95        echo "</channel>\n</rss>"; 
     96    } 
    9897} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map