Skip to content

Commit

Permalink
Changed format of logAttempts, and rised time buffer to 2 seconds
Browse files Browse the repository at this point in the history
  • Loading branch information
jbogdani committed Sep 17, 2021
1 parent 8fc720a commit 5acc9d4
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
29 changes: 15 additions & 14 deletions lib/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,33 +559,34 @@ public static function update_htaccess()
* if any record is found for present IP address. Returns true is attempt is valid
* (no log available, or max time is greater than $time) or false if atempts is not valid
* @param string|false $logfile full path to log file, if false default (admin) log file wil be used
* @param integer $time Minimum time in millisecond between two attemps; default 1000 (1sec)
* @param integer $time Minimum time in seconds between two attemps; default 1000 (1sec)
* @return boolean true if is a valid attempt, false if attemmpt is not allowed
*/
public static function checkAttemptTime($logfile = false, $time = 1000)
public static function checkAttemptTime($logfile = false, $time = 2)
{
$separator= '::::';
if (!$logfile) {
$logfile = MAIN_DIR . 'logs/logAttempts.log';
}

$ip = $_SERVER['REMOTE_ADDR'];

$now = microtime(true);
$now = round(microtime(true));

if (file_exists($logfile)) {
$lastAttempt = file($logfile);
$lastIP = trim(str_replace(array("\n", "\r\n"), '', $lastAttempt[0]));
$lastTime = floatval(
trim(
str_replace(["\n", "\r\n"], '', $lastAttempt[1])
)
);

if ($lastIP === $ip) {
return ($now >= ($lastTime + $time));
list ($lastIP, $lastTime) = explode($separator, file_get_contents($logfile));
// Old format vs. new format
if ($lastIP && $lastTime){
$lastTime = (int) trim($lastTime);
$lastIP = trim($lastIP);

if ($lastIP === $ip) {
return ($now >= ($lastTime + $time));
}
}

}
return utils::write_in_file($logfile, $ip . "\n" . $now);
return utils::write_in_file($logfile, $ip . $separator . $now);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion modules/protectedtags/protectedtags.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public function login()
}

// Prevent serial attempts
if (!utils::checkAttemptTime(MAIN_DIR . 'logs/protectedTagsAttempts.log', 2000)) {
if (!utils::checkAttemptTime(MAIN_DIR . 'logs/protectedTagsAttempts.log')) {
throw new Exception('too_much_attempts');
}

Expand Down
1 change: 1 addition & 0 deletions version
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
5.7.0[] = "Changed format of logAttempts, and rised time buffer to 2 seconds"
5.6.2[] = "Optimized calculation of path of assets"
5.6.1[] = "Updated Twig to v. 3.3.2"
5.5.13[] = "Fixed bug with link to controller in protected tags public forms"
Expand Down

0 comments on commit 5acc9d4

Please sign in to comment.