function processUpdateReport()
{
    global $CM, $STATS;
    $count = 0;
    if (STATS_ACTIVE) {
        if (!empty($CM)) {
            $CM->UnsetData(117);
            $STATS = new StatisticProvider();
        }
        while (isset($_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_va_" . $count])) {
            $parts = explode("_", $_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_va_" . $count]);
            if ($parts[1] == 0) {
                $report = new StatisticYear($parts[0], 0, 0);
            } else {
                if ($parts[2] == 0) {
                    $report = new StatisticMonth($parts[0], $parts[1], 0);
                } else {
                    $report = new StatisticDay($parts[0], $parts[1], $parts[2]);
                }
            }
            $report->Update(!empty($_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_vb_" . $count]));
            $count++;
        }
    }
}
function processUpdateReport()
{
    $count = 0;
    while (isset($_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_va_" . $count])) {
        $parts = explode("_", $_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_va_" . $count]);
        if ($parts[1] == 0) {
            $report = new StatisticYear($parts[0], 0, 0);
        } else {
            if ($parts[2] == 0) {
                $report = new StatisticMonth($parts[0], $parts[1], 0);
            } else {
                $report = new StatisticDay($parts[0], $parts[1], $parts[2]);
            }
        }
        $report->Update(!empty($_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_vb_" . $count]));
        $count++;
    }
}
function processUpdateReport()
{
    $count = 0;
    if (STATS_ACTIVE) {
        CacheManager::FlushKey(DATA_CACHE_KEY_STATS);
        Server::$Statistic = new StatisticProvider();
        while (isset($_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_va_" . $count])) {
            $parts = explode("_", $_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_va_" . $count]);
            if ($parts[1] == 0) {
                $report = new StatisticYear($parts[0], 0, 0, 0, 0);
            } else {
                if ($parts[2] == 0) {
                    $report = new StatisticMonth($parts[0], $parts[1], 0, 0, 0);
                } else {
                    $report = new StatisticDay($parts[0], $parts[1], $parts[2], 0, 0);
                }
            }
            $report->Update(!empty($_POST[POST_INTERN_PROCESS_UPDATE_REPORT . "_vb_" . $count]));
            $count++;
        }
    }
}
function buildReports($xml = "")
{
    global $RESPONSE, $STATS, $INTERNAL;
    if (empty($_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME])) {
        return;
    }
    if (empty($STATS->CurrentDay) || $INTERNAL[CALLER_SYSTEM_ID]->GetPermission(PERMISSION_REPORTS) == PERMISSION_NONE) {
        return;
    }
    if ($_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME] == XML_CLIP_NULL) {
        $_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME] = "0_0";
    }
    $parts = explode("_", $_POST[POST_INTERN_XMLCLIP_REPORTS_END_TIME]);
    if ($result = queryDB(true, "SELECT *,(SELECT MAX(`time`) FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "`) AS `maxtime`,(SELECT MAX(`mtime`) FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE `maxtime`=`time`) AS `maxmtime` FROM `" . DB_PREFIX . DATABASE_STATS_AGGS . "` WHERE (`time` = " . DBManager::RealEscape($parts[0]) . " AND `mtime` > " . DBManager::RealEscape($parts[1]) . ") OR (`time` > " . DBManager::RealEscape($parts[0]) . ") ORDER BY `time` ASC,`mtime` ASC LIMIT 1")) {
        while ($row = DBManager::FetchArray($result)) {
            if ($row["month"] == 0) {
                $report = new StatisticYear($row["year"], 0, 0);
            } else {
                if ($row["day"] == 0) {
                    $report = new StatisticMonth($row["year"], $row["month"], 0);
                } else {
                    $report = new StatisticDay($row["year"], $row["month"], $row["day"]);
                }
            }
            $type = -1;
            $update = false;
            $value = "";
            if ($report->Type == STATISTIC_PERIOD_TYPE_DAY) {
                if ($_POST[POST_INTERN_PROCESS_UPDATE_REPORT_TYPE] == 1) {
                    if ($STATS->CurrentDay->CreateVisitorList) {
                        if (empty($row["aggregated"]) && (!@file_exists($report->GetFilename(true, true)) || $row["time"] < time() - StatisticProvider::$AutoUpdateTime)) {
                            $report->SaveVisitorListToFile();
                        }
                        if (@file_exists($report->GetFilename(true, true))) {
                            $value = getFile($report->GetFilename(true, true));
                        }
                    }
                    $type = 1;
                } else {
                    if ($_POST[POST_INTERN_PROCESS_UPDATE_REPORT_TYPE] == 0) {
                        if ($STATS->CurrentDay->CreateReport) {
                            if (empty($row["aggregated"]) && (!@file_exists($report->GetFilename(true, false)) || $row["time"] < time() - StatisticProvider::$AutoUpdateTime)) {
                                $update = true;
                                $report->SaveReportToFile();
                            } else {
                                if (@file_exists($report->GetFilename(true, false))) {
                                    $value = getFile($report->GetFilename(true, false));
                                }
                            }
                        }
                        $type = 0;
                    }
                }
            } else {
                if (empty($row["aggregated"]) && (!@file_exists($report->GetFilename(true, false)) || $row["time"] < time() - StatisticProvider::$AutoUpdateTime)) {
                    $report->SaveReportToFile();
                }
                if (@file_exists($report->GetFilename(true, false))) {
                    $value = getFile($report->GetFilename(true, false));
                }
                $type = $report->Type == STATISTIC_PERIOD_TYPE_MONTH ? 2 : 3;
            }
            if ($type > -1) {
                $convrate = $row["sessions"] > 0 ? round(100 * $row["conversions"] / $row["sessions"], StatisticProvider::$RoundPrecision) : 0;
                $chats = $chatsd = 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 = queryDB(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"];
                        }
                    }
                }
                $xml .= "<r cid=\"" . base64_encode(getId(3)) . "\" ragg=\"" . base64_encode(empty($row["aggregated"]) ? 0 : 1) . "\" rtype=\"" . base64_encode($type) . "\" convrate=\"" . base64_encode($convrate) . "\" chats=\"" . base64_encode($chats) . "\" update=\"" . base64_encode($update ? 1 : 0) . "\" visitors=\"" . base64_encode($row["sessions"]) . "\" time=\"" . base64_encode($row["time"]) . "\" mtime=\"" . base64_encode($row["mtime"]) . "\" year=\"" . base64_encode($row["year"]) . "\" month=\"" . base64_encode($row["month"]) . "\" day=\"" . base64_encode($row["day"]) . "\">" . base64_encode($value) . "</r>\r\n";
            }
            $xml .= "<ri maxtime=\"" . base64_encode($row["maxtime"]) . "\" maxmtime=\"" . base64_encode($row["maxmtime"]) . "\" />";
        }
    }
    $RESPONSE->SetStandardResponse(1, $xml);
}
 function AggregateDay($_year, $_month, $_day)
 {
     $day = new StatisticDay($_year, $_month, $_day);
     $day->Close();
 }
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;
 }