web-base/Core/API/Stats.class.php

112 lines
3.4 KiB
PHP
Raw Normal View History

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
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;
use Core\Objects\DatabaseEntity\User;
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
}
private function getUserCount() {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-17 23:50:08 +02:00
$res = $sql->select($sql->count())->from("User")->execute();
2020-06-19 13:13:13 +02:00
$this->success = $this->success && ($res !== FALSE);
2020-06-17 23:50:08 +02:00
$this->lastError = $sql->getLastError();
return ($this->success ? $res[0]["count"] : 0);
}
private function getPageCount() {
2022-06-20 19:52:31 +02:00
$sql = $this->context->getSQL();
2020-06-19 13:13:13 +02:00
$res = $sql->select($sql->count())->from("Route")
->where(new CondBool("active"))
->execute();
$this->success = $this->success && ($res !== FALSE);
$this->lastError = $sql->getLastError();
return ($this->success ? $res[0]["count"] : 0);
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"];
$this->mailConfigured = ($settings["mail_enabled"] ?? "0") === "1";
$this->recaptchaConfigured = ($settings["recaptcha_enabled"] ?? "0") === "1";
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");
$res = $sql->select($sql->count($sql->distinct("cookie")))
->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 {
2020-06-17 23:50:08 +02:00
$userCount = $this->getUserCount();
$pageCount = $this->getPageCount();
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;
}
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
}
2020-06-25 16:54:58 +02:00
$this->result["userCount"] = $userCount;
$this->result["pageCount"] = $pageCount;
$this->result["visitors"] = $visitorStatistics;
2020-07-18 12:51:36 +02:00
$this->result["visitorsTotal"] = $visitorCount;
2020-06-25 16:54:58 +02:00
$this->result["server"] = array(
"version" => WEBBASE_VERSION,
"server" => $_SERVER["SERVER_SOFTWARE"] ?? "Unknown",
"memory_usage" => memory_get_usage(),
"load_avg" => $loadAvg,
2022-06-20 19:52:31 +02:00
"database" => $this->context->getSQL()->getStatus(),
2020-06-27 22:47:12 +02:00
"mail" => $this->mailConfigured,
"reCaptcha" => $this->recaptchaConfigured
2020-06-25 16:54:58 +02:00
);
2020-06-17 23:50:08 +02:00
return $this->success;
}
}