Article Listing 1 mar2006.tar

Listing 1 Sample PHP code snippet

/**
 * @param  string - IP address
 * @return int    - score based on values in database
 */
function _isSpamIpAddr($ip)
{
  $ini   = $this->_parseKeywordFile($this->ipDb);
  $score = 0;

  foreach($ini as $sec => $item){
    if(is_array($item)){
      $regex = "/";
      for($x = 0; $x < sizeof($item); $x++){
        $tmp = trim($item[$x]);
        if($x > 0 && strlen($tmp) > 0){
          $regex .= "|";
        }
      $regex .= $tmp;
      }
      $regex .= "/i";
      if(preg_match_all($regex, $ip, $matches, PREG_SET_ORDER)){
        foreach ($matches as $val) {
          $score += $sec;
        }
      }
    }
  }
  return $score;
} 

/**
 * @param  string filename
 * @return string array
 * @access private
 */
function _parseKeywordFile($file)
{
  $lines = @file($file);
  $arr = array();
  $sec = "";
  for($x = 0; $x < sizeof($lines); $x++){
    $tmp = trim($lines[$x]);
    if(ereg("^\s*#(.*)", $tmp, $heading)){
      continue;
    }
    $tmp = trim(ereg_replace('#.*$', '', $tmp));
    if(ereg("^\[.*\]$", $tmp, $heading)){
      $sec = ereg_replace("(\[|\])", "", $heading[0]);
      $arr[$sec] = array();
    } else {
      if(strlen($sec) > 0 && strlen($tmp) > 0){
        array_push($arr[$sec], $tmp);
      }
    }
  }
  return $arr;
}