2020-06-17 23:50:08 +02:00
|
|
|
<?php
|
|
|
|
|
2022-11-18 18:06:46 +01:00
|
|
|
namespace Core\API;
|
2020-06-17 23:50:08 +02:00
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
use Core\Driver\SQL\Expression\Count;
|
|
|
|
use Core\Driver\SQL\Expression\Distinct;
|
2023-01-16 21:47:23 +01:00
|
|
|
use Core\Driver\SQL\Query\Insert;
|
|
|
|
use Core\Objects\DatabaseEntity\Group;
|
2023-02-09 23:55:30 +01:00
|
|
|
use Core\Objects\DatabaseEntity\Route;
|
|
|
|
use Core\Objects\DatabaseEntity\User;
|
2020-07-18 12:51:36 +02:00
|
|
|
use DateTime;
|
2022-11-18 18:06:46 +01:00
|
|
|
use Core\Driver\SQL\Condition\Compare;
|
|
|
|
use Core\Driver\SQL\Condition\CondBool;
|
|
|
|
use Core\Objects\Context;
|
2020-06-17 23:50:08 +02:00
|
|
|
|
|
|
|
class Stats extends Request {
|
|
|
|
|
2020-06-27 22:47:12 +02:00
|
|
|
private bool $mailConfigured;
|
|
|
|
private bool $recaptchaConfigured;
|
|
|
|
|
2022-06-20 19:52:31 +02:00
|
|
|
public function __construct(Context $context, $externalCall = false) {
|
|
|
|
parent::__construct($context, $externalCall, array());
|
2020-06-17 23:50:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-17 20:53:35 +02:00
|
|
|
private function checkSettings(): bool {
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Settings\Get($this->context);
|
2020-06-27 22:47:12 +02:00
|
|
|
$this->success = $req->execute(array("key" => "^(mail_enabled|recaptcha_enabled)$"));
|
|
|
|
$this->lastError = $req->getLastError();
|
2020-06-25 16:54:58 +02:00
|
|
|
|
|
|
|
if ($this->success) {
|
2020-06-27 22:47:12 +02:00
|
|
|
$settings = $req->getResult()["settings"];
|
2024-04-11 20:41:03 +02:00
|
|
|
$this->mailConfigured = $settings["mail_enabled"];
|
|
|
|
$this->recaptchaConfigured = $settings["recaptcha_enabled"];
|
2020-06-25 16:54:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
2020-07-18 12:51:36 +02:00
|
|
|
private function getVisitorCount() {
|
2022-06-20 19:52:31 +02:00
|
|
|
$sql = $this->context->getSQL();
|
2020-07-18 12:51:36 +02:00
|
|
|
$date = new DateTime();
|
|
|
|
$monthStart = $date->format("Ym00");
|
|
|
|
$monthEnd = $date->modify("+1 month")->format("Ym00");
|
2023-01-05 22:47:17 +01:00
|
|
|
$res = $sql->select(new Count(new Distinct("cookie")))
|
2020-07-18 12:51:36 +02:00
|
|
|
->from("Visitor")
|
|
|
|
->where(new Compare("day", $monthStart, ">="))
|
|
|
|
->where(new Compare("day", $monthEnd, "<"))
|
2020-07-18 13:00:49 +02:00
|
|
|
->where(new Compare("count", 2, ">="))
|
2020-07-18 12:51:36 +02:00
|
|
|
->execute();
|
|
|
|
|
|
|
|
$this->success = ($res !== false);
|
|
|
|
$this->lastError = $sql->getLastError();
|
|
|
|
return ($this->success ? $res[0]["count"] : $this->success);
|
|
|
|
}
|
|
|
|
|
2022-02-21 13:01:03 +01:00
|
|
|
public function _execute(): bool {
|
2023-02-09 23:55:30 +01:00
|
|
|
$sql = $this->context->getSQL();
|
|
|
|
$userCount = User::count($sql);
|
|
|
|
$pageCount = Route::count($sql, new CondBool("active"));
|
2024-03-30 11:22:59 +01:00
|
|
|
$groupCount = Group::count($sql);
|
2022-11-18 18:06:46 +01:00
|
|
|
$req = new \Core\API\Visitors\Stats($this->context);
|
2020-07-01 21:10:25 +02:00
|
|
|
$this->success = $req->execute(array("type"=>"monthly"));
|
|
|
|
$this->lastError = $req->getLastError();
|
2020-06-25 16:54:58 +02:00
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-17 23:50:08 +02:00
|
|
|
|
2020-07-01 21:10:25 +02:00
|
|
|
$visitorStatistics = $req->getResult()["visitors"];
|
2020-07-18 12:51:36 +02:00
|
|
|
$visitorCount = $this->getVisitorCount();
|
|
|
|
if (!$this->success) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-04-05 17:14:36 +02:00
|
|
|
$req = new \Core\API\Logs\Get($this->context, false);
|
|
|
|
$success = $req->execute([
|
|
|
|
"since" => (new \DateTime())->modify("-48 hours"),
|
|
|
|
"severity" => "error"
|
|
|
|
]);
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
$errorCount = $req->getResult()["pagination"]["total"];
|
|
|
|
} else {
|
|
|
|
$errorCount = "Unknown";
|
|
|
|
}
|
|
|
|
|
2020-06-24 16:13:54 +02:00
|
|
|
$loadAvg = "Unknown";
|
|
|
|
if (function_exists("sys_getloadavg")) {
|
|
|
|
$loadAvg = sys_getloadavg();
|
|
|
|
}
|
|
|
|
|
2020-06-27 22:47:12 +02:00
|
|
|
if (!$this->checkSettings()) {
|
2020-06-25 16:54:58 +02:00
|
|
|
return false;
|
2020-06-17 23:50:08 +02:00
|
|
|
}
|
|
|
|
|
2023-01-05 22:47:17 +01:00
|
|
|
$this->result["data"] = [
|
|
|
|
"userCount" => $userCount,
|
|
|
|
"pageCount" => $pageCount,
|
2024-03-30 11:22:59 +01:00
|
|
|
"groupCount" => $groupCount,
|
2023-01-05 22:47:17 +01:00
|
|
|
"visitors" => $visitorStatistics,
|
|
|
|
"visitorsTotal" => $visitorCount,
|
2024-04-05 17:14:36 +02:00
|
|
|
"errorCount" => $errorCount,
|
2023-01-05 22:47:17 +01:00
|
|
|
"server" => [
|
|
|
|
"version" => WEBBASE_VERSION,
|
|
|
|
"server" => $_SERVER["SERVER_SOFTWARE"] ?? "Unknown",
|
|
|
|
"memory_usage" => memory_get_usage(),
|
|
|
|
"load_avg" => $loadAvg,
|
|
|
|
"database" => $this->context->getSQL()->getStatus(),
|
|
|
|
"mail" => $this->mailConfigured,
|
|
|
|
"reCaptcha" => $this->recaptchaConfigured
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-06-17 23:50:08 +02:00
|
|
|
return $this->success;
|
|
|
|
}
|
|
|
|
|
2023-01-16 21:47:23 +01:00
|
|
|
public static function getDefaultACL(Insert $insert): void {
|
2024-03-27 14:12:01 +01:00
|
|
|
$insert->addRow(self::getEndpoint(), [Group::ADMIN, Group::SUPPORT], "Allows users to view site statistics", true);
|
2023-01-16 21:47:23 +01:00
|
|
|
}
|
2020-06-17 23:50:08 +02:00
|
|
|
}
|