home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / modules / wiki / lib / rate.php < prev    next >
PHP Script  |  2004-03-08  |  3KB  |  114 lines

  1. <?php
  2. // $Id: rate.php,v 1.1 2004/01/12 22:14:05 comsubvie Exp $
  3.  
  4. // Perform a lookup on an IP addresses edit-rate.
  5. function rateCheck($db, $type)
  6. {
  7.   global $RatePeriod, $RateView, $RateSearch, $RateEdit, $REMOTE_ADDR, $RtTbl;
  8.   
  9.   $fields = explode(".", $REMOTE_ADDR);
  10.   if($RatePeriod == 0)
  11.     { return; }
  12.  
  13.   $db->query("LOCK TABLES $RtTbl WRITE");
  14.  
  15.   // Make sure this IP address hasn't been excluded.
  16.  
  17.   $qid = $db->query("SELECT * FROM $RtTbl WHERE ip='$fields[0].*'");
  18.   if($db->result($qid))
  19.     { die(LIB_ErrorDeniedAccess); }
  20.   $qid = $db->query("SELECT * FROM $RtTbl WHERE ip='$fields[0].$fields[1].*'");
  21.   if($db->result($qid))
  22.     { die(LIB_ErrorDeniedAccess); }
  23.   $qid = $db->query("SELECT * FROM $RtTbl " .
  24.                     "WHERE ip='$fields[0].$fields[1].$fields[2].*'");
  25.   if($db->result($qid))
  26.     { die(LIB_ErrorDeniedAccess); }
  27.  
  28.   // Now check how many more actions we can perform.
  29.  
  30.   $qid = $db->query("SELECT TIME_TO_SEC(NOW()) - TIME_TO_SEC(time), " .
  31.                     "viewLimit, searchLimit, editLimit FROM $RtTbl " .
  32.                     "WHERE ip='$REMOTE_ADDR'");
  33.   if(!($result = $db->result($qid)))
  34.     { $result = array(-1, $RateView, $RateSearch, $RateEdit); }
  35.   else
  36.   {
  37.     if($result[0] < 0)
  38.       { $result[0] = $RatePeriod; }
  39.     $result[1] = min($result[1] + $result[0] * $RateView / $RatePeriod,
  40.                      $RateView);
  41.     $result[2] = min($result[2] + $result[0] * $RateSearch / $RatePeriod,
  42.                      $RateSearch);
  43.     $result[3] = min($result[3] + $result[0] * $RateEdit / $RatePeriod,
  44.                      $RateEdit);
  45.   }
  46.  
  47.   if($type == 'view')
  48.     { $result[1]--; }
  49.   else if($type == 'search')
  50.     { $result[2]--; }
  51.   else if($type == 'edit')
  52.     { $result[3]--; }
  53.  
  54.   if($result[1] < 0 || $result[2] < 0 || $result[3] < 0)
  55.     { die(LIB_ErrorRateExceeded); }
  56.  
  57.   // Record this action.
  58.  
  59.   if($result[0] == -1)
  60.   {
  61.     $db->query("INSERT INTO $RtTbl VALUES('$REMOTE_ADDR', " .
  62.                "NULL, $result[1], $result[2], $result[3])");
  63.   }
  64.   else
  65.   {
  66.     $db->query("UPDATE $RtTbl SET viewLimit=$result[1], " .
  67.                "searchLimit=$result[2], editLimit=$result[3] " .
  68.                "WHERE ip='$REMOTE_ADDR'");
  69.   }
  70.  
  71.   $db->query("UNLOCK TABLES");
  72. }
  73.  
  74. // Return a list of blocked address ranges.
  75. function rateBlockList($db)
  76. {
  77.   global $RatePeriod, $RtTbl;
  78.  
  79.   $list = array();
  80.  
  81.   if($RatePeriod == 0)
  82.     { return $list; }
  83.  
  84.   $qid = $db->query("SELECT ip FROM $RtTbl");
  85.   while(($result = $db->result($qid)))
  86.   {
  87.     if(preg_match('/^\\d+\\.(\\d+\\.(\\d+\\.)?)?\\*$/', $result[0]))
  88.       { $list[] = $result[0]; }
  89.   }
  90.  
  91.   return $list;
  92. }
  93.  
  94. // Block an address range.
  95. function rateBlockAdd($db, $address)
  96. {
  97.   global $RtTbl;
  98.  
  99.   if(!preg_match('/^\\d+\\.(\\d+\\.(\\d+\\.)?)?\\*$/', $address))
  100.     { return; }
  101.   $qid = $db->query("SELECT * FROM $RtTbl WHERE ip='$address'");
  102.   if($db->result($qid))
  103.     { return; }
  104.   $db->query("INSERT INTO $RtTbl(ip) VALUES('$address')");
  105. }
  106.  
  107. function rateBlockRemove($db, $address)
  108. {
  109.   global $RtTbl;
  110.  
  111.   $db->query("DELETE FROM $RtTbl WHERE ip='$address'");
  112. }
  113. ?>
  114.