plugins/spamx/Import.Admin.class.php
author Dirk Haun <dirk@haun-online.de>
Thu, 29 Oct 2009 13:00:11 +0100
branchHEAD
changeset 7397 c27e9026f22a
parent 6838 cb1ba8d99085
permissions -rw-r--r--
Fixed inclusion protection
     1 <?php
     2 
     3 /**
     4  * file:  Import.Admin.class.php
     5  * MTBlacklist refresh module
     6  *
     7  * Updates Sites MT Blacklist via Master MT Blacklist rss feed
     8  *
     9  * Copyright (C) 2004-2009 by the following authors:
    10  * Author      Tom Willett     tomw AT pigstye DOT net
    11  * Author      Dirk Haun       dirk AT haun-online DOT de
    12  *
    13  * Licensed under GNU General Public License
    14  *
    15  * Based on MT-Blacklist Updater by
    16  * Cheah Chu Yeow (http://blog.codefront.net/)
    17  *
    18  * @package Spam-X
    19  * @subpackage Modules
    20  */
    21 
    22 if (strpos(strtolower($_SERVER['PHP_SELF']), 'import.admin.class.php') !== false) {
    23     die('This file can not be used on its own!');
    24 }
    25 
    26 /**
    27 * Include Abstract Base Class
    28 */
    29 require_once $_CONF['path'] . 'plugins/spamx/BaseAdmin.class.php';
    30 
    31 /**
    32 * MTBlacklist Import
    33 *
    34 * @package Spam-X
    35 *
    36 */
    37 class Import extends BaseAdmin {
    38     /**
    39      * Constructor
    40      */
    41     function display()
    42     {
    43         global $_TABLES;
    44 
    45         if (DB_count($_TABLES['spamx'], 'name', 'MTBlacklist') > 0) {
    46             $display = $this->_update_blacklist();
    47         } else {
    48             $display = $this->_initial_import();
    49         }
    50 
    51         return $display;
    52     }
    53 
    54     function link()
    55     {
    56         global $_TABLES, $LANG_SX00;
    57 
    58         if (DB_count($_TABLES['spamx'], 'name', 'MTBlacklist') > 0) {
    59             $display = $LANG_SX00['uMTlist'];
    60         } else {
    61             $display = $LANG_SX00['initial_import'];
    62         }
    63 
    64         return $display;
    65     }
    66 
    67     /**
    68      * Update MT Blacklist from RSS feed
    69      */
    70     function _update_blacklist()
    71     {
    72         global $_CONF, $_TABLES, $LANG_SX00, $_SPX_CONF;
    73 
    74         require_once $_CONF['path'] . 'plugins/spamx/magpierss/rss_fetch.inc';
    75         require_once $_CONF['path'] . 'plugins/spamx/magpierss/rss_utils.inc';
    76 
    77         if (!defined('MAGPIE_USER_AGENT')) {
    78             define('MAGPIE_USER_AGENT', 'Geeklog/' . VERSION);
    79         }
    80         $rss = fetch_rss($_SPX_CONF['rss_url']);
    81         if ($rss === false) {
    82             if (strpos($_SPX_CONF['rss_url'], 'jayallen.org') === false) {
    83                 return '<p>An error occured when updating MT Blacklist</p>';
    84             } else {
    85                 $url = COM_createLink('discontinued', 'http://www.geeklog.net/article.php/mt-blacklist-discontinued');
    86                 return '<p>Please note that MT-Blacklist has been $url and will not be updated any more.</p>';
    87             }
    88         }
    89 
    90         // entries to add and delete, according to the blacklist changes feed
    91         $to_add = array();
    92         $to_delete = array();
    93 
    94         foreach ($rss->items as $item) {
    95             // time this entry was published (currently unused)
    96             // $published_time = parse_w3cdtf( $item['dc']['date'] );
    97             $entry = substr($item['description'], 0, -3); // blacklist entry
    98             $subject = $item['dc']['subject']; // indicates addition or deletion
    99 
   100             // is this an addition or a deletion?
   101             $dbentry = addslashes($entry);
   102             if (strpos($subject, 'addition') !== false) {
   103                 // save it to database
   104                 $result = DB_query("SELECT name FROM {$_TABLES['spamx']} WHERE name = 'MTBlacklist' AND value = '$dbentry'");
   105                 $nrows = DB_numRows($result);
   106                 if ($nrows < 1) {
   107                     DB_query("INSERT INTO {$_TABLES['spamx']} VALUES ('MTBlacklist', '$dbentry')");
   108                     $to_add[] = $entry;
   109                 }
   110             } else if (strpos($subject, 'deletion') !== false) {
   111                 // delete it from database
   112                 $result = DB_query("SELECT name FROM {$_TABLES['spamx']} WHERE name = 'MTBlacklist' AND value = '$dbentry'");
   113                 $nrows = DB_numRows($result);
   114                 if ($nrows >= 1) {
   115                     DB_delete($_TABLES['spamx'], array('name', 'value'),
   116                                                  array('MTBlacklist', $dbentry));
   117                     $to_delete[] = $entry;
   118                 }
   119             }
   120         }
   121         $display = '<hr' . XHTML . '><p><b>' . $LANG_SX00['entriesadded'] . '</b></p><ul>';
   122         foreach ($to_add as $e) {
   123             $display .= "<li>$e</li>";
   124         }
   125         $display .= '</ul><p><b>' . $LANG_SX00['entriesdeleted'] . '</b></p><ul>';
   126         foreach ($to_delete as $e) {
   127             $display .= "<li>$e</li>";
   128         }
   129         $display .= '</ul>';
   130         SPAMX_log($LANG_SX00['uMTlist'] . $LANG_SX00['uMTlist2'] . count($to_add) . $LANG_SX00['uMTlist3'] . count($to_delete) . $LANG_SX00['entries']);
   131 
   132         return $display;
   133     }
   134 
   135     /**
   136      * Initial import of the MT Blacklist
   137      */
   138     function _initial_import()
   139     {
   140         global $_CONF, $_TABLES, $LANG_SX00, $_SPX_CONF;
   141 
   142         if (ini_get('allow_url_fopen')) {
   143             $blacklist = file($_SPX_CONF['mtblacklist_url']);
   144             $count = $this->_do_import($blacklist);
   145 
   146             if ($count > 0) {
   147                 $display = sprintf($LANG_SX00['import_success'], $count);
   148                 SPAMX_log($LANG_SX00['uMTlist'] . $LANG_SX00['uMTlist2']
   149                           . $count . $LANG_SX00['uMTlist3'] . '0'
   150                           . $LANG_SX00['entries']);
   151             } else {
   152                 $display = $LANG_SX00['import_failure'];
   153             }
   154         } else { // read blacklist from local file
   155             $fromfile = $_CONF['path_data'] . 'blacklist.txt';
   156 
   157             if (file_exists($fromfile)) {
   158                 $blacklist = file($fromfile);
   159                 $count = $this->_do_import($blacklist);
   160 
   161                 if ($count > 0) {
   162                     $display = sprintf($LANG_SX00['import_success'], $count);
   163                     SPAMX_log($LANG_SX00['uMTlist'] . $LANG_SX00['uMTlist2']
   164                               . $count . $LANG_SX00['uMTlist3'] . '0'
   165                               . $LANG_SX00['entries']);
   166                 } else {
   167                     $display = $LANG_SX00['import_failure'];
   168                 }
   169             } else {
   170                 $display = sprintf($LANG_SX00['allow_url_fopen'],
   171                         $_CONF['path_data']);
   172                 $display .= '<p>'. COM_createLink($_SPX_CONF['mtblacklist_url'],
   173                         $_SPX_CONF['mtblacklist_url']);
   174             }
   175         }
   176 
   177         // Import Personal Blacklist for existing users.
   178         $fromfile = $_CONF['path_html'] . 'spamx/blacklist.php';
   179         if (file_exists($fromfile)) {
   180             require_once $fromfile;
   181             $count = $this->_do_importp($SPAMX_BLACKLIST);
   182             $display .= $LANG_SX00['initial_Pimport'];
   183             if ($count > 0) {
   184                 $display .= sprintf($LANG_SX00['import_success'], $count);
   185                 SPAMX_log($LANG_SX00['uPlist'] . $LANG_SX00['uMTlist2']
   186                           . $count . $LANG_SX00['uMTlist3'] . '0'
   187                           . $LANG_SX00['entries']);
   188             } else {
   189                 $display .= $LANG_SX00['import_failure'];
   190             }
   191         }
   192 
   193         return $display;
   194     }
   195 
   196     /**
   197      * Import the blacklist
   198      *
   199      * @param array $lines The blacklist
   200      * @return int number of lines imported
   201      */
   202     function _do_import($lines)
   203     {
   204         global $_TABLES;
   205 
   206         $count = 0;
   207         foreach ($lines as $line) {
   208             $l = explode('#', $line);
   209             $entry = addslashes(trim($l[0]));
   210             if (!empty($entry)) {
   211                 DB_query("INSERT INTO {$_TABLES['spamx']} VALUE ('MTBlacklist', '$entry')");
   212                 $count++;
   213             }
   214         }
   215 
   216         return $count;
   217     }
   218 
   219     /**
   220      * Import personal blacklist
   221      *
   222      * @param array $lines The blacklist
   223      * @return int number of lines imported
   224      */
   225     function _do_importp($lines)
   226     {
   227         global $_TABLES;
   228 
   229         $count = 0;
   230         foreach ($lines as $entry) {
   231             if (!empty($entry)) {
   232                 $entry = addslashes($entry);
   233                 DB_query("INSERT INTO {$_TABLES['spamx']} VALUES ('Personal', '$entry')");
   234                 $count++;
   235             }
   236         }
   237 
   238         return $count;
   239     }
   240 }
   241 
   242 ?>