plugins/spamx/functions.inc
author Dirk Haun <dirk@haun-online.de>
Sat, 07 Nov 2009 11:35:44 +0100
branchHEAD
changeset 7445 b0971977fd28
parent 6958 0774a19f037c
child 7690 e48c1d426d72
permissions -rw-r--r--
When a plugin returns 0 items for the Admins Block, don't display that as 'N/A' (bug #0001025)
     1 <?php
     2 
     3 /**
     4  * File: functions.inc
     5  * This is the functions.inc for the Geeklog Spam-X plugin
     6  *
     7  * Copyright (C) 2004-2009 by the following authors:
     8  * Authors      Tom Willett     tomw AT pigstye DOT net
     9  *              Dirk Haun       dirk AT haun-online DOT de
    10  *
    11  * Licensed under GNU General Public License
    12  *
    13  * @package Spam-X
    14  */
    15 
    16 if (strpos(strtolower($_SERVER['PHP_SELF']), 'functions.inc') !== false) {
    17     die ('This file can not be used on its own.');
    18 }
    19 
    20 /**
    21  * Language file include
    22  */
    23 $plugin_path = $_CONF['path'] . 'plugins/spamx/';
    24 $langfile = $plugin_path . 'language/' . $_CONF['language'] . '.php';
    25 
    26 if (file_exists($langfile)) {
    27     include_once $langfile;
    28 } else {
    29     include_once $plugin_path . 'language/english.php';
    30 }
    31 
    32 /**
    33 * Check and see if we need to load the plugin configuration
    34 */
    35 if (!isset($_SPX_CONF['timeout'])) {
    36     require_once $_CONF['path_system'] . 'classes/config.class.php';
    37 
    38     $spx_config = config::get_instance();
    39     $_SPX_CONF = $spx_config->get_config('spamx');
    40 }
    41 
    42 
    43 // +---------------------------------------------------------------------------+
    44 // | Geeklog Plugin API Implementations                                        |
    45 // +---------------------------------------------------------------------------+
    46 
    47 /**
    48 * Shows the statistics for the plugin on stats.php.  If $showsitestats is 1
    49 * then we are to only print the overall stats in the 'site statistics' box
    50 * otherwise we show the detailed stats for the plugin
    51 *
    52 * @param    int     $showsitestats  Flag to let us know which stats to get
    53 * @return   string  returns formatted HTML to insert in stats page
    54 * @see      plugin_statssummary_spamx
    55 *
    56 */
    57 function plugin_showstats_spamx($showsitestats)
    58 {
    59     global $_CONF, $_TABLES, $LANG_SX00;
    60 
    61     $retval = '';
    62 
    63     if (SEC_hasRights('spamx.admin')) {
    64         // detailed stats are only visible to Spam-X admins
    65         require_once( $_CONF['path_system'] . 'lib-admin.php' );
    66         $header_arr = array(
    67             array('text' => $LANG_SX00['stats_page_title'], 'field' => 'label', 'header_class' => 'stats-header-title'),
    68             array('text' => $LANG_SX00['stats_entries'], 'field' => 'stats', 'header_class' => 'stats-header-count', 'field_class' => 'stats-list-count'),
    69         );
    70         $data_arr = array();
    71         $text_arr = array('has_menu'     => false,
    72                           'title'        => $LANG_SX00['stats_headline'],
    73         );
    74         $data_arr = array(
    75             array('label' => $LANG_SX00['stats_pblacklist'],
    76                 'stats' => COM_numberFormat (DB_count ($_TABLES['spamx'], 'name', 'Personal'))),
    77             array('label' => $LANG_SX00['stats_ip'],
    78                 'stats' => COM_numberFormat (DB_count ($_TABLES['spamx'], 'name', 'IP'))),
    79             array('label' => $LANG_SX00['stats_ipofurl'],
    80                 'stats' => COM_numberFormat (DB_count ($_TABLES['spamx'], 'name', 'IPofUrl'))),
    81             array('label' => $LANG_SX00['stats_header'],
    82                 'stats' => COM_numberFormat (DB_count ($_TABLES['spamx'], 'name', 'HTTPHeader'))),
    83             array('label' => $LANG_SX00['slvwhitelist'],
    84                 'stats' => COM_numberFormat (DB_count ($_TABLES['spamx'], 'name', 'SLVwhitelist')))
    85         );
    86         $retval .= ADMIN_simpleList("", $header_arr, $text_arr, $data_arr);
    87     }
    88 
    89     return $retval;
    90 }
    91 
    92 /**
    93 * New stats plugin API function for proper integration with the site stats
    94 *
    95 * @return   array(item text, item count);
    96 * @see      plugin_showstats_spamx
    97 *
    98 */
    99 function plugin_statssummary_spamx ()
   100 {
   101     global $_TABLES, $LANG_SX00;
   102 
   103     $counter = DB_getItem ($_TABLES['vars'], 'value', "name = 'spamx.counter'");
   104 
   105     return array ($LANG_SX00['stats_deleted'], COM_numberFormat ($counter));
   106 }
   107 
   108 /**
   109  * This will put an option for the plugin in the command and control block on moderation.php
   110  *
   111  * Add the plugin name, icon and link to the command and control block in moderation.php
   112  *
   113  * @return array Array containing (plugin name, admin url, url of plugin icon)
   114  */
   115 function plugin_cclabel_spamx()
   116 {
   117     global $_CONF, $LANG_SX00;
   118 
   119     $retval = array();
   120     if (SEC_hasRights('spamx.admin')) {
   121         $retval = array($LANG_SX00['plugin_name'],
   122             $_CONF['site_admin_url'] . '/plugins/spamx/index.php',
   123             plugin_geticon_spamx ());
   124     }
   125 
   126     return $retval;
   127 }
   128 
   129 /**
   130  * Returns the administrative option for this plugin
   131  *
   132  * Adds the plugin to the Admin menu
   133  *
   134  * @return array Array containing (plugin name, plugin admin url, # of items in plugin or '')
   135  */
   136 function plugin_getadminoption_spamx()
   137 {
   138     global $_CONF, $LANG_SX00;
   139 
   140     if (SEC_hasRights('spamx.admin')) {
   141         return array($LANG_SX00['plugin_name'],
   142             $_CONF['site_admin_url'] . '/plugins/spamx/index.php', '');
   143     }
   144 }
   145 
   146 /**
   147  * Returns the current plugin code version
   148  *
   149  * @return string    plugin version
   150  */
   151 function plugin_chkVersion_spamx()
   152 {
   153     global $_CONF;
   154 
   155     require_once $_CONF['path'] . 'plugins/spamx/autoinstall.php';          
   156 
   157     $inst_parms = plugin_autoinstall_spamx('spamx'); 
   158 
   159     return $inst_parms['info']['pi_version'];
   160 }
   161 
   162 /**
   163 * Update the Spam-X plugin
   164 *
   165 * @return   int     Number of message to display
   166 *
   167 */
   168 function plugin_upgrade_spamx()
   169 {
   170     global $_CONF, $_TABLES;
   171 
   172     $installed_version = DB_getItem($_TABLES['plugins'], 'pi_version',
   173                                     "pi_name = 'spamx'");
   174     $code_version = plugin_chkVersion_spamx();
   175     if ($installed_version == $code_version) {
   176         // nothing to do
   177         return true;
   178     }
   179 
   180     require_once $_CONF['path'] . 'plugins/spamx/autoinstall.php';
   181 
   182     if (! plugin_compatible_with_this_version_spamx('spamx')) {
   183         return 3002;
   184     }
   185 
   186     $inst_parms = plugin_autoinstall_spamx('spamx');
   187     $pi_gl_version = $inst_parms['info']['pi_gl_version'];
   188 
   189     DB_query("UPDATE {$_TABLES['plugins']} SET pi_version = '$code_version', pi_gl_version = '$pi_gl_version' WHERE pi_name = 'spamx'");
   190 
   191     return true;
   192 }
   193 
   194 /**
   195 * Called during site migration - handle changed URLs or paths
   196 *
   197 * @param    array   $old_conf   contents of the $_CONF array on the old site
   198 * @param    boolean             true on success, otherwise false
   199 *
   200 */
   201 function plugin_migrate_spamx($old_conf)
   202 {
   203     global $_CONF, $_TABLES;
   204 
   205     // only update the SLV whitelist, so we don't use INST_updateSiteUrl
   206     $old_url = addslashes($old_conf['site_url']);
   207     $result = DB_query("SELECT name, value FROM {$_TABLES['spamx']} WHERE name = 'SLVwhitelist' AND value LIKE '{$old_url}%'");
   208     $num = DB_numRows($result);
   209 
   210     for ($i = 0; $i < $num; $i++) {
   211         list($name, $value) = DB_fetchArray($result);
   212         $new_value = addslashes(str_replace($old_conf['site_url'],
   213                                             $_CONF['site_url'], $value));
   214         $old_value = addslashes($value);
   215 
   216         DB_query("UPDATE {$_TABLES['spamx']} SET value = '$new_value' WHERE name = 'SLVwhitelist' AND value = '$old_value'");
   217     }
   218 
   219     return true;
   220 }
   221 
   222 /**
   223  * Actual Plugin Functions here.
   224  */
   225 
   226 /**
   227  * Check a post for spam
   228  *
   229  * @param   string  $comment    comment text
   230  * @param   int     $action     (former spam action - not used any more)
   231  * @return  int                 > 0: spam detected, == 0: no spam
   232  *
   233  */
   234 function plugin_checkforSpam_spamx ($comment, $action = -1)
   235 {
   236     global $_CONF, $_SPX_CONF;
   237 
   238     // skip spam check for members of the 'spamx Admin' group, if enabled
   239     if (isset ($_SPX_CONF['admin_override']) && $_SPX_CONF['admin_override']) {
   240         if (SEC_inGroup ('spamx Admin')) {
   241             return 0;
   242         }
   243     }
   244 
   245     $spamx_path = $_CONF['path'] . 'plugins/spamx/';
   246 
   247     // Set up Spamx_Examine array
   248     $Spamx_Examine = array ();
   249     if ($dir = @opendir ($spamx_path)) {
   250         while (($file = readdir ($dir)) !== false) {
   251             if (is_file ($spamx_path . $file)) {
   252                 if (substr ($file, -18) == '.Examine.class.php') {
   253                     $sfile = str_replace ('.Examine.class.php', '', $file);
   254                     $Spamx_Examine[] = $sfile;
   255                 }
   256             }
   257         }
   258         closedir ($dir);
   259     }
   260 
   261     $res = 0;
   262     foreach ($Spamx_Examine as $Examine) {
   263         $filename = $Examine . '.Examine.class.php';
   264         require_once ($spamx_path . $filename);
   265         $EX = new $Examine;
   266         $res = $EX->execute ($comment);
   267         if ($res == 1) {
   268             break;
   269         }
   270     }
   271 
   272     return $res;
   273 }
   274 
   275 /**
   276  * Perform action after spam has been detected
   277  *
   278  * @param   string  $comment    comment text
   279  * @param   int     $action     which action modules to call (sum of module numbers)
   280  * @return  int                 number of message to display to the spammer
   281  *
   282  */
   283 function plugin_spamaction_spamx ($comment, $action)
   284 {
   285     global $_CONF, $_SPX_CONF;
   286 
   287     $res = 0;
   288 
   289     $spamx_path = $_CONF['path'] . 'plugins/spamx/';
   290 
   291     if (($action == -1) || ($action == '')) {
   292         $action = $_SPX_CONF['action'];
   293     }
   294 
   295     // Set up Spamx_Action array
   296     $Spamx_Action = array ();
   297     if ($dir = @opendir ($spamx_path)) {
   298         while (($file = readdir ($dir)) !== false) {
   299             if (is_file ($spamx_path . $file)) {
   300                 if (substr ($file, -17) == '.Action.class.php') {
   301                     $sfile = str_replace ('.Action.class.php', '', $file);
   302                     require_once ($spamx_path . $file);
   303                     $CM = new $sfile;
   304                     $Spamx_Action[$sfile] = $CM->number ();
   305                 }
   306             }
   307         }
   308         closedir ($dir);
   309     }
   310 
   311     foreach ($Spamx_Action as $Act => $Num) {
   312         if (($action & $Num) == $Num) {
   313             $AC = new $Act;
   314             $AC->execute ($comment);
   315 
   316             $res = max ($res, $AC->result ());
   317         }
   318     }
   319 
   320     return $res;
   321 }
   322 
   323 /**
   324  * Logs message to spamx.log
   325  *
   326  * This will print a message to the spamx log
   327  *
   328  * @param   string  $logentry   Message to write to log
   329  */
   330 function SPAMX_log ($logentry)
   331 {
   332     global $_CONF, $LANG01, $_SPX_CONF;
   333 
   334     if ((!isset ($_SPX_CONF['logging']) || ($_SPX_CONF['logging'] === true)) &&
   335             !empty ($logentry)) {
   336         $logentry = str_replace( array( '<?', '?>' ), array( '(@', '@)' ),
   337                                  $logentry );
   338 
   339         $timestamp = strftime ('%c');
   340         $logfile = $_CONF['path_log'] . 'spamx.log';
   341 
   342         if (!$file = fopen ($logfile, 'a')) {
   343             COM_errorLog ($LANG01[33] . $logfile . ' (' . $timestamp . ')', 1);
   344         }
   345 
   346         fputs ($file, "$timestamp - $logentry \n");
   347     }
   348 }
   349 
   350 /**
   351 * Returns the URL of the plugin's icon
   352 *
   353 * @return   string      URL of the icon
   354 *
   355 */
   356 function plugin_geticon_spamx ()
   357 {
   358     global $_CONF;
   359 
   360     return $_CONF['site_admin_url'] . '/plugins/spamx/images/spamx.png';
   361 }
   362 
   363 /**
   364 * Automatic uninstall function for plugins
   365 *
   366 * This code is automatically uninstalling the plugin.
   367 * It passes an array to the core code function that removes
   368 * tables, groups, features and php blocks from the tables.
   369 * Additionally, this code can perform special actions that cannot be
   370 * foreseen by the core code (interactions with other plugins for example)
   371 *
   372 * @return   array   Plugin information
   373 *
   374 */
   375 function plugin_autouninstall_spamx ()
   376 {
   377     $out = array (
   378         /* give the name of the tables, without $_TABLES[] */
   379         'tables' => array('spamx'),
   380         /* give the full name of the group, as in the db */
   381         'groups' => array('spamx Admin'),
   382         /* give the full name of the feature, as in the db */
   383         'features' => array('spamx.admin', 'spamx.view'),
   384         /* give the full name of the block, including 'phpblock_', etc */
   385         'php_blocks' => array(),
   386         /* give all vars with their name */
   387         'vars' => array('spamx_gid', 'spamx.counter')
   388     );
   389 
   390     return $out;
   391 }
   392 
   393 /**
   394 * Provide URL of a documentation file
   395 *
   396 * @param    string  $file   documentation file being requested, e.g. 'config'
   397 * @return   mixed           URL or false when not available
   398 *
   399 */
   400 function plugin_getdocumentationurl_spamx($file)
   401 {
   402     global $_CONF;
   403 
   404     static $docurl;
   405 
   406     switch ($file) {
   407     case 'index':
   408     case 'config':
   409         if (isset($docurl)) {
   410             $retval = $docurl;
   411         } else {
   412             $doclang = COM_getLanguageName();
   413             $docs = 'docs/' . $doclang . '/spamx.html';
   414             if (file_exists($_CONF['path_html'] . $docs)) {
   415                 $retval = $_CONF['site_url'] . '/' . $docs;
   416             } else {
   417                 $retval = $_CONF['site_url'] . '/docs/english/spamx.html';
   418             }
   419             $docurl = $retval;
   420         }
   421         break;
   422 
   423     default:
   424         $retval = false;
   425         break;
   426     }
   427 
   428     return $retval;
   429 }
   430 
   431 ?>