mirror of
https://github.com/PrivateBin/PrivateBin.git
synced 2025-08-02 11:36:08 -04:00
Refactoring of code base - modularized code, introduced configuration, started working on a PDO based DB connector
This commit is contained in:
parent
241c75a5d5
commit
ba90d0cae2
10 changed files with 1170 additions and 388 deletions
111
lib/traffic_limiter.php
Normal file
111
lib/traffic_limiter.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
/**
|
||||
* ZeroBin
|
||||
*
|
||||
* a zero-knowledge paste bin
|
||||
*
|
||||
* @link http://sebsauvage.net/wiki/doku.php?id=php:zerobin
|
||||
* @copyright 2012 Sébastien SAUVAGE (sebsauvage.net)
|
||||
* @license http://www.opensource.org/licenses/zlib-license.php The zlib/libpng License
|
||||
* @version 0.15
|
||||
*/
|
||||
|
||||
/**
|
||||
* traffic_limiter
|
||||
*
|
||||
* Handles traffic limiting, so no user does more than one call per 10 seconds.
|
||||
*/
|
||||
class traffic_limiter
|
||||
{
|
||||
/**
|
||||
* @access private
|
||||
* @static
|
||||
* @var int
|
||||
*/
|
||||
private static $_limit = 10;
|
||||
|
||||
/**
|
||||
* @access private
|
||||
* @static
|
||||
* @var string
|
||||
*/
|
||||
private static $_path = 'data';
|
||||
|
||||
/**
|
||||
* set the time limit in seconds
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param int $limit
|
||||
* @return void
|
||||
*/
|
||||
public static function setLimit($limit)
|
||||
{
|
||||
self::$_limit = $limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* set the path
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public static function setPath($path)
|
||||
{
|
||||
self::$_path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* traffic limiter
|
||||
*
|
||||
* Make sure the IP address makes at most 1 request every 10 seconds.
|
||||
*
|
||||
* @access public
|
||||
* @static
|
||||
* @param string $ip
|
||||
* @return bool
|
||||
*/
|
||||
public static function canPass($ip)
|
||||
{
|
||||
if (!is_dir(self::$_path)) mkdir(self::$_path, 0705, true);
|
||||
$file = self::$_path . '/traffic_limiter.php';
|
||||
if (!is_file($file))
|
||||
{
|
||||
file_put_contents(
|
||||
$file,
|
||||
'<?php' . PHP_EOL .
|
||||
'$GLOBALS[\'traffic_limiter\'] = array();' . PHP_EOL
|
||||
);
|
||||
chmod($file, 0705);
|
||||
}
|
||||
|
||||
require $file;
|
||||
$tl = $GLOBALS['traffic_limiter'];
|
||||
|
||||
// purge file of expired IPs to keep it small
|
||||
foreach($tl as $key => $time)
|
||||
{
|
||||
if ($time + 10 < time())
|
||||
{
|
||||
unset($tl[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if (array_key_exists($ip, $tl) && ($tl[$ip] + 10 >= time()))
|
||||
{
|
||||
$result = false;
|
||||
} else {
|
||||
$tl[$ip] = time();
|
||||
$result = true;
|
||||
}
|
||||
file_put_contents(
|
||||
$file,
|
||||
'<?php' . PHP_EOL .
|
||||
'$GLOBALS[\'traffic_limiter\'] = ' .
|
||||
var_export($tl, true) . ';' . PHP_EOL
|
||||
);
|
||||
return $result;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue