Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ class PimOnlineBot
*/
private $base_url;

/**
* Minimum interval in seconds before updating the database for an online host.
* @var int
*/
const MIN_UPDATE_INTERVAL = 30;

/**
* Init a PimOnlineBot
*/
Expand Down Expand Up @@ -71,6 +77,12 @@ public function pdo()
return $this->pdo;
}

private function is_valid_uuid($uuid)
{
$pattern = '/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i';
return preg_match($pattern, $uuid) === 1;
}

private function init_db($mysql_credentials)
{
$dsn = 'mysql:host=' . $mysql_credentials['host'] . ';dbname=' . $mysql_credentials['database'];
Expand Down Expand Up @@ -99,6 +111,11 @@ private function init_telegram($pdo, $bot_api_key, $bot_username)

public function online($uid)
{
if (!$this->is_valid_uuid($uid))
{
echo "Invalid UUID format rejected: " . $uid;
return;
}
$pdo = $this->pdo();
$sql = "SELECT * FROM `ob_online` where uid=:uid order by id DESC limit 1";
$statement = $pdo->prepare($sql);
Expand Down Expand Up @@ -127,6 +144,11 @@ public function online($uid)
$id = $row['id'];
$past = $row['now'];
$alarm = $row['alarm'];
$currentTime = time();

if (!$alarm && ($currentTime - $past) < self::MIN_UPDATE_INTERVAL) {
return;
}

$sql = "UPDATE `ob_online` SET `now` = :now, `past` = :past, `alarm` = :alarm WHERE `id` = :id";
$statement = $pdo->prepare($sql);
Expand Down