Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/Commands/RegisterCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function execute() : ServerResponse
'text' => "*error:* server '$uid' not found",
'parse_mode' => 'Markdown'
]);
}
else if ($id_server == -1)
{
return Request::sendMessage([
'chat_id' => $chat_id,
'text' => "*error:* '$uid' is not in a valid format.",
'parse_mode' => 'Markdown'
]);
}

echo "here now\n";
Expand Down
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 @@ -127,6 +139,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 Expand Up @@ -194,6 +211,11 @@ public function udpate_db()

public function id_server($uid)
{
if (!$this->is_valid_uuid($uid))
{
echo "Invalid UUID format rejected: " . $uid;
return -1;
}
$pdo = $this->pdo();
$sql = "SELECT * FROM `ob_online` where uid=:uid order by id DESC limit 1";
$statement = $pdo->prepare($sql);
Expand Down