Skip to content
Merged
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ composer require utopia-php/abuse
**Time Limit Abuse**

The time limit abuse allow each key (action) to be performed [X] times in given time frame.
This adapter uses a MySQL / MariaDB to store usage attempts. Before using it create the table schema as documented ate this repository (./data/schema.sql)
This adapter uses a MySQL / MariaDB to store usage attempts. Before using it create the table schema as documented in this repository (./data/schema.sql)

```php
<?php
Expand Down
27 changes: 27 additions & 0 deletions src/Abuse/Abuse.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,31 @@ public function check()
{
return $this->adapter->check();
}

/**
* Get abuse logs
*
* Returns logs with an offset and limit
*
* @param $offset
* @param $limit
*
* @return array
*/
public function getLogs(int $offset, int $limit): array
{
return $this->adapter->getLogs($offset, $limit);
}

/**
* Delete all logs older than $seconds seconds
*
* @param int $seconds
*
* @return bool
*/
public function cleanup(int $seconds): bool
{
return $this->adapter->cleanup($seconds);
}
}
22 changes: 22 additions & 0 deletions src/Abuse/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,26 @@ interface Adapter
* @return bool
*/
public function check();

/**
* Get abuse logs
*
* Returns logs with an offset and limit
*
* @param $offset
* @param $limit
*
* @return array
*/
public function getLogs(int $offset, int $limit): array;


/**
* Delete all logs older than $seconds seconds
*
* @param int $seconds
*
* @return bool
*/
public function cleanup(int $seconds): bool;
}
29 changes: 29 additions & 0 deletions src/Abuse/Adapters/ReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Utopia\Abuse\Adapters;

use Exception;
use Utopia\Abuse\Adapter;

class ReCaptcha implements Adapter
Expand Down Expand Up @@ -79,4 +80,32 @@ public function check()

return $result['success'];
}

/**
* Delete logs older than $seconds seconds
*
* @param int $seconds
*
* @throws Exception
* @return bool
*/
public function cleanup(int $seconds):bool
{
throw new Exception('Method not supported');
}

/**
* Get abuse logs
*
* Returns logs with an offset and limit
*
* @param $offset
* @param $limit
*
* @return array
*/
public function getLogs(int $offset, int $limit): array
{
throw new Exception('Method not supported');
}
}
44 changes: 44 additions & 0 deletions src/Abuse/Adapters/TimeLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,50 @@ protected function hit(string $key, int $time): void
$this->count++;
}

/**
* Get abuse logs
*
* Returns logs with an offset and limit
*
* @param $offset
* @param $limit
*
* @return array
*/
public function getLogs(int $offset, int $limit): array {

$st = $this->getPDO()->prepare('SELECT * FROM `' . $this->getNamespace() . '.abuse.abuse`;
LIMIT :offset, :limit
');
$st->bindValue(':offset', $offset, PDO::PARAM_INT);
$st->bindValue(':limit', $limit, PDO::PARAM_INT);
$st->execute();

$result = $st->fetchAll();

return $result;
}


/**
* Delete logs older than $seconds seconds
*
* @param int $seconds
*
* @return bool
*/
public function cleanup(int $seconds):bool
{
$st = $this->getPDO()->prepare('DELETE
FROM `'.$this->getNamespace().'.abuse.abuse`
WHERE (UNIX_TIMESTAMP(NOW()) - CAST(`_time` AS SIGNED)) > :seconds');

$st->bindValue(':seconds', $seconds, PDO::PARAM_INT);
$st->execute();

return ('00000' == $st->errorCode()) ? true : false;
}

/**
* Check
*
Expand Down
16 changes: 16 additions & 0 deletions tests/Abuse/AbuseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,20 @@ public function testIsValid()
$this->assertEquals($this->abuse->check(), false);
$this->assertEquals($this->abuse->check(), true);
}

public function testCleanup() {

// Check that there is only one log
$logs = $this->abuse->getLogs(0, 10);
$this->assertEquals(1, \count($logs));

sleep(5);
// Delete the log
$status = $this->abuse->cleanup(1);
$this->assertEquals($status, true);

// Check that there are no logs in the DB
$logs = $this->abuse->getLogs(0, 10);
$this->assertEquals(0, \count($logs));
}
}