b2evolution PHP Cross Reference Blogging Systems

Source: /inc/antispam/model/_iprangecache.class.php - 113 lines - 3719 bytes - Summary - Text - Print

Description: This file implements the IPRangeCache class. This file is part of the evoCore framework - {@link http://evocore.net/} See also {@link http://sourceforge.net/projects/evocms/}.

   1  <?php
   2  /**

   3   * This file implements the IPRangeCache class.

   4   *

   5   * This file is part of the evoCore framework - {@link http://evocore.net/}

   6   * See also {@link http://sourceforge.net/projects/evocms/}.

   7   *

   8   * @copyright (c)2003-2014 by Francois Planque - {@link http://fplanque.com/}

   9   * Parts of this file are copyright (c)2004-2006 by Daniel HAHLER - {@link http://thequod.de/contact}.

  10   *

  11   * {@internal License choice

  12   * - If you have received this file as part of a package, please find the license.txt file in

  13   *   the same folder or the closest folder above for complete license terms.

  14   * - If you have received this file individually (e-g: from http://evocms.cvs.sourceforge.net/)

  15   *   then you must choose one of the following licenses before using the file:

  16   *   - GNU General Public License 2 (GPL) - http://www.opensource.org/licenses/gpl-license.php

  17   *   - Mozilla Public License 1.1 (MPL) - http://www.opensource.org/licenses/mozilla1.1.php

  18   * }}

  19   *

  20   * {@internal Open Source relicensing agreement:

  21   * Daniel HAHLER grants Francois PLANQUE the right to license

  22   * Daniel HAHLER's contributions to this file and the b2evolution project

  23   * under any OSI approved OSS license (http://www.opensource.org/licenses/).

  24   * }}

  25   *

  26   * @package evocore

  27   *

  28   * {@internal Below is a list of authors who have contributed to design/coding of this file: }}

  29   * @author fplanque: Francois PLANQUE.

  30   *

  31   * @version $Id: _iprangecache.class.php 849 2012-02-16 09:09:09Z yura $

  32   */
  33  if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  34  
  35  load_class( '_core/model/dataobjects/_dataobjectcache.class.php', 'DataObjectCache' );
  36  
  37  load_class( 'antispam/model/_iprange.class.php', 'IPRange' );
  38  
  39  /**

  40   * IP Range Cache Class

  41   *

  42   * @package evocore

  43   */
  44  class IPRangeCache extends DataObjectCache
  45  {
  46      /**

  47       * Lazy filled index of IP addresses

  48       */
  49      var $ip_index = array();
  50  
  51      /**

  52       * Constructor

  53       *

  54       * @param string object type of elements in Cache

  55       * @param string Name of the DB table

  56       * @param string Prefix of fields in the table

  57       * @param string Name of the ID field (including prefix)

  58       */
  59  	function IPRangeCache( $objType = 'IPRange', $dbtablename = 'T_antispam__iprange', $dbprefix = 'aipr_', $dbIDname = 'aipr_ID' )
  60      {
  61          parent::DataObjectCache( $objType, false, $dbtablename, $dbprefix, $dbIDname );
  62      }
  63  
  64  
  65      /**

  66       * Get an object from cache by IP address

  67       *

  68       * Load into cache if necessary

  69       *

  70       * @param string IP address

  71       * @param boolean false if you want to return false on error

  72       * @param boolean true if function should die on empty/null

  73       */
  74      function & get_by_ip( $req_ip, $halt_on_error = false, $halt_on_empty = false )
  75      {
  76          global $DB, $Debuglog;
  77  
  78          if( !isset( $this->ip_index[ $req_ip ] ) )
  79          {    // not yet in cache:
  80  
  81              $IP = ip2int( $req_ip );
  82  
  83              $SQL = new SQL( 'Get ID of IP range by IP address' );
  84              $SQL->SELECT( 'aipr_ID' );
  85              $SQL->FROM( 'T_antispam__iprange' );
  86              $SQL->WHERE( 'aipr_IPv4start <= '.$DB->quote( $IP ) );
  87              $SQL->WHERE_and( 'aipr_IPv4end >= '.$DB->quote( $IP ) );
  88              $IPRange_ID = $DB->get_var( $SQL->get() );
  89  
  90              // Get object from IPRangeCache bi ID

  91              $IPRange =  $this->get_by_ID( $IPRange_ID, $halt_on_error, $halt_on_empty );
  92  
  93              if( $IPRange )
  94              {    // It is in IPRangeCache
  95                  $this->ip_index[ $req_ip ] = $IPRange;
  96              }
  97              else
  98              {    // not in the IPRangeCache
  99                  if( $halt_on_error ) debug_die( "Requested $this->objtype does not exist!" );
 100                  $this->ip_index[ $req_ip ] = false;
 101              }
 102          }
 103          else
 104          {
 105              $Debuglog->add( "Retrieving <strong>$this->objtype($req_ip)</strong> from cache" );
 106          }
 107  
 108          return $this->ip_index[ $req_ip ];
 109      }
 110  
 111  }
 112  
 113  ?>

title

Description

title

Description

title

Description

title

title

Body