function demandReports()
{
    if (!STATS_ACTIVE || !isset($_POST["p_dr_p"]) || Server::$Operators[CALLER_SYSTEM_ID]->GetPermission(PERMISSION_REPORTS) == PERMISSION_NONE) {
        return;
    }
    if (!CacheManager::IsDataUpdate(POST_INTERN_DUT_REPORTS, DATA_UPDATE_KEY_REPORTS)) {
        return;
    }
    $limit = !empty($_POST["p_dr_p"]) && is_numeric($_POST["p_dr_p"]) && $_POST["p_dr_p"] > 1 ? ($_POST["p_dr_p"] - 1) * DATA_DEMAND_LOADS : 0;
    $type = !empty($_POST["p_dr_t"]) && in_array($_POST["p_dr_t"], array(STATISTIC_PERIOD_TYPE_DAY, STATISTIC_PERIOD_TYPE_MONTH, STATISTIC_PERIOD_TYPE_YEAR)) ? $_POST["p_dr_t"] : STATISTIC_PERIOD_TYPE_DAY;
    $xml = "";
    if ($type == STATISTIC_PERIOD_TYPE_DAY) {
        $type = "`day` > 0";
    } else {
        if ($type == STATISTIC_PERIOD_TYPE_MONTH) {
            $type = "`month` > 0 AND `day`=0";
        } else {
            if ($type == STATISTIC_PERIOD_TYPE_YEAR) {
                $type = "`year` > 0 AND `day`=0 AND `month`=0";
            }
        }
    }
    $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE " . $type . " ORDER BY `time` DESC,`mtime` DESC LIMIT " . $limit . "," . DBManager::RealEscape(DATA_DEMAND_LOADS) . ";");
    if ($result) {
        while ($row = DBManager::FetchArray($result)) {
            if ($row["month"] == 0 && $row["day"] == 0) {
                $report = new StatisticYear($row["year"], 0, 0, $row["aggregated"], $row["mtime"]);
            } else {
                if ($row["day"] == 0) {
                    $report = new StatisticMonth($row["year"], $row["month"], 0, $row["aggregated"], $row["mtime"]);
                } else {
                    $report = new StatisticDay($row["year"], $row["month"], $row["day"], $row["aggregated"], $row["mtime"]);
                }
            }
            $chats = 0;
            $qmonth = $report->Type == STATISTIC_PERIOD_TYPE_YEAR ? "" : " AND `month`='" . DBManager::RealEscape($row["month"]) . "'";
            $qday = $report->Type != STATISTIC_PERIOD_TYPE_DAY ? "" : " AND `day`='" . DBManager::RealEscape($row["day"]) . "'";
            if ($results = DBManager::Execute(true, "SELECT (SUM(`amount`)-SUM(`multi`)) AS `samount` FROM `" . DB_PREFIX . DATABASE_STATS_AGGS_CHATS . "` WHERE `year`='" . DBManager::RealEscape($row["year"]) . "'" . $qmonth . $qday . "")) {
                if (DBManager::GetRowCount($results) == 1) {
                    $rows = DBManager::FetchArray($results);
                    if (is_numeric($rows["samount"])) {
                        $chats = $rows["samount"];
                    }
                }
            }
            $convrate = $row["sessions"] > 0 ? round(100 * $row["conversions"] / $row["sessions"], StatisticProvider::$RoundPrecision) : 0;
            $xml .= "<r i=\"" . base64_encode($report->GetHash()) . "\" a=\"" . base64_encode($row["aggregated"]) . "\" ch=\"" . base64_encode($chats) . "\" c=\"" . base64_encode($convrate) . "\" r=\"" . base64_encode($report->Type) . "\" s=\"" . base64_encode($row["sessions"]) . "\" v=\"" . base64_encode($row["visitors_unique"]) . "\" t=\"" . base64_encode($row["time"]) . "\" mt=\"" . base64_encode($row["mtime"]) . "\" y=\"" . base64_encode($row["year"]) . "\" m=\"" . base64_encode($row["month"]) . "\" d=\"" . base64_encode($row["day"]) . "\"></r>\r\n";
        }
    }
    $result = DBManager::Execute(true, "SELECT count(`time`) AS `total` FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "`;");
    $trow = DBManager::FetchArray($result);
    $result = DBManager::Execute(true, "SELECT count(`time`) AS `ttotal` FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE " . $type . ";");
    $ttrow = DBManager::FetchArray($result);
    Server::$Response->Reports = "<dr dut=\"" . base64_encode(CacheManager::$DataUpdateTimes[DATA_UPDATE_KEY_REPORTS]) . "\" p=\"" . base64_encode(DATA_ITEM_LOADS) . "\" t=\"" . base64_encode($trow["total"]) . "\" q=\"" . base64_encode($ttrow["ttotal"]) . "\">\r\n" . $xml . "\r\n</dr>";
}
 static function GetReportFromHash($_hash, $_year, $_month, $_day, $_users)
 {
     if (is_numeric($_year) && is_numeric($_month) && is_numeric($_day)) {
         $result = DBManager::Execute(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE `day`='" . DBManager::RealEscape($_day) . "' AND `month`='" . DBManager::RealEscape($_month) . "' AND `year`='" . DBManager::RealEscape($_year) . "' LIMIT 1;");
         while ($row = DBManager::FetchArray($result)) {
             if ($row["month"] == 0) {
                 $report = new StatisticYear($row["year"], 0, 0, $row["aggregated"], $row["mtime"]);
             } else {
                 if ($row["day"] == 0) {
                     $report = new StatisticMonth($row["year"], $row["month"], 0, $row["aggregated"], $row["mtime"]);
                 } else {
                     $report = new StatisticDay($row["year"], $row["month"], $row["day"], $row["aggregated"], $row["mtime"]);
                 }
             }
             if ($report->GetHash() == $_hash && @file_exists($report->GetFilename(true, $_users))) {
                 $html = IOStruct::GetFile($report->GetFilename(true, $_users));
                 $html = str_replace("<!--body_part-->", $html, IOStruct::GetFile(TEMPLATE_HTML_STATS_BASE));
                 $html = str_replace("<!--server-->", LIVEZILLA_URL, $html);
                 return $html;
             }
         }
     }
     return false;
 }