function runPeriodicJobs()
{
    global $CONFIG, $VISITOR, $STATS;
    if (rand(0, 100) == 1) {
        $timeouts = array($CONFIG["poll_frequency_clients"] * 10, 86400, 86400 * 7, DATA_LIFETIME);
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE (`html` = '0' OR `html` = '') AND `time` < " . @mysql_real_escape_string(time() - $timeouts[3]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `time` < " . @mysql_real_escape_string(time() - $timeouts[3]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `persistent` = '0' AND `time` < " . @mysql_real_escape_string(time() - $timeouts[1]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_OPERATOR_LOGINS . "` WHERE `time` < " . @mysql_real_escape_string(time() - $timeouts[1]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "` WHERE `created` < " . @mysql_real_escape_string(time() - $timeouts[0]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_PROFILE_PICTURES . "` WHERE `webcam`=1 AND `time` < " . @mysql_real_escape_string(time() - $timeouts[0]));
        if (!STATS_ACTIVE) {
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `last_active`<'" . @mysql_real_escape_string(time() - $timeouts[1]) . "';");
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_OPERATOR_STATUS . "` WHERE `" . DB_PREFIX . DATABASE_OPERATOR_STATUS . "`.`confirmed`<'" . @mysql_real_escape_string(time() - $timeouts[1]) . "';");
        } else {
            StatisticProvider::DeleteHTMLReports();
        }
        if (!empty($CONFIG["gl_rm_chats"])) {
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `time` < " . @mysql_real_escape_string(time() - $CONFIG["gl_rm_chats_time"]));
        }
        if (!empty($CONFIG["gl_rm_rt"])) {
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_RATINGS . "` WHERE `time` < " . @mysql_real_escape_string(time() - $CONFIG["gl_rm_rt_time"]));
        }
        if (!empty($CONFIG["gl_rm_om"])) {
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_EDITORS . "` WHERE `time` < " . @mysql_real_escape_string(time() - $CONFIG["gl_rm_om_time"]));
            queryDB(true, "DELETE `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`,`" . DB_PREFIX . DATABASE_TICKETS . "` FROM `" . DB_PREFIX . DATABASE_TICKETS . "` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE `" . DB_PREFIX . DATABASE_TICKETS . "`.`id` = `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`ticket_id` AND `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`time` < " . @mysql_real_escape_string(time() - $CONFIG["gl_rm_om_time"]));
        }
        if ($result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_RESOURCES . "` WHERE `discarded`=1 AND `type` > 2 AND `edited` < " . @mysql_real_escape_string(time() - $timeouts[3]))) {
        }
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            $resultb = queryDB(true, "SELECT count(value) as linked FROM `" . DB_PREFIX . DATABASE_RESOURCES . "` WHERE `value`='" . @mysql_real_escape_string($row["value"]) . "';");
            $rowb = mysql_fetch_array($resultb, MYSQL_BOTH);
            if ($rowb["linked"] == 1) {
                @unlink(PATH_UPLOADS . $row["value"]);
            }
        }
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_RESOURCES . "` WHERE `discarded`='1' AND `edited` < " . @mysql_real_escape_string(time() - $timeouts[3]));
    } else {
        if (rand(0, 10) == 1) {
            sendChatTranscripts();
        }
    }
}
function processChatActions()
{
    global $INTERNAL, $RVISITOR;
    $count = 0;
    while (isset($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"])) {
        $type = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vd"];
        if ($type == "OperatorSignOff") {
            $op = $INTERNAL[$_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]];
            $op->SignOff();
        } else {
            if ($type == "SendChatTranscriptTo") {
                $value = 1;
                while (!empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_" . $value])) {
                    queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `transcript_sent`=0,`transcript_receiver`='" . DBManager::RealEscape($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]) . "' WHERE `transcript_sent`=1 AND `chat_id`='" . DBManager::RealEscape($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_" . $value]) . "' LIMIT 1;");
                    $value++;
                }
                sendChatTranscripts(true);
            } else {
                if ($type == "CreatePublicGroup") {
                    $room = new UserGroup();
                    $room->IsDynamic = true;
                    $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                    $room->Descriptions["EN"] = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"];
                    $room->Owner = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"];
                    $room->Save();
                    $room->AddMember($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"], !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_3"]));
                } else {
                    if ($type == "DeletePublicGroup") {
                        $room = new UserGroup();
                        $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                        $room->Destroy();
                    } else {
                        if ($type == "JoinPublicGroup") {
                            $room = new UserGroup();
                            $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                            $room->AddMember($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"], !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_3"]));
                        } else {
                            if ($type == "QuitPublicGroup") {
                                $room = new UserGroup();
                                $room->Id = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                                $room->RemoveMember($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                            } else {
                                if ($type == "StartOverlayChat") {
                                    $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]);
                                    $chat->RequestInitChat(CALLER_SYSTEM_ID);
                                } else {
                                    if ($type == "AddVisitorComment") {
                                        $visitor = new Visitor($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                        $visitor->SaveComment(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                                    } else {
                                        if ($type == "DownloadRecentHistory") {
                                            $RVISITOR = new Visitor($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                            $RVISITOR->LoadRecentVisits(true, $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                                        } else {
                                            if ($type == "SetTranslation") {
                                                $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]);
                                                $chat->ChatId = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"];
                                                $chat->SetTranslation($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]);
                                            } else {
                                                if ($type == "SetChatTicketParam") {
                                                    $ticket = new CommercialChatVoucher("", $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                    $ticket->Load();
                                                    $ticket->SetVoucherParams(!empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_1"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_2"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_3"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_4"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_5"]), !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_6"]));
                                                } else {
                                                    if (strlen($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]) > 0 && strlen($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"]) > 0) {
                                                        $chat = new VisitorChat($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_va"], $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vb"]);
                                                        $chat->ChatId = $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_vc"];
                                                        $chat->Load();
                                                        if ($type == "SetCallMeBackStatus") {
                                                            $chat->SetCallMeBackStatus($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                        } else {
                                                            if ($type == "JoinChatInvisible") {
                                                                $chat->JoinChat(CALLER_SYSTEM_ID, true, !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]));
                                                            } else {
                                                                if ($type == "JoinChat") {
                                                                    $chat->JoinChat(CALLER_SYSTEM_ID, false, !empty($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]));
                                                                } else {
                                                                    if ($type == "SetPriority") {
                                                                        $chat->SetPriority($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                    } else {
                                                                        if ($type == "SetTargetOperator") {
                                                                            $chat->SetTargetOperator($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                        } else {
                                                                            if ($type == "SetTargetGroup") {
                                                                                $chat->SetTargetGroup($_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                            } else {
                                                                                if ($type == "AcceptChat") {
                                                                                    $chat->InternalActivate();
                                                                                } else {
                                                                                    if ($type == "CloseChat") {
                                                                                        $chat->InternalClose(CALLER_SYSTEM_ID);
                                                                                    } else {
                                                                                        if ($type == "TakeChat") {
                                                                                            $chat->TakeChat(CALLER_SYSTEM_ID, $_POST[POST_INTERN_PROCESS_CHAT_ACTION . "_" . $count . "_ve_0"]);
                                                                                        } else {
                                                                                            if ($type == "DeclineChat") {
                                                                                                $chat->InternalDecline(CALLER_SYSTEM_ID);
                                                                                            } else {
                                                                                                if ($type == "LeaveChat") {
                                                                                                    $chat->LeaveChat(CALLER_SYSTEM_ID);
                                                                                                }
                                                                                            }
                                                                                        }
                                                                                    }
                                                                                }
                                                                            }
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        $count++;
    }
}
function cronJobs($_asCronJob)
{
    global $CONFIG, $INTERNAL;
    $timeouts = array($CONFIG["poll_frequency_clients"] * 10, 86400, 86400 * 7, DATA_LIFETIME);
    $randoms = $_asCronJob ? array(0 => 10, 1 => 10, 2 => 1) : array(0 => 300, 1 => 300, 2 => 5);
    if (rand(1, $randoms[0]) == 1) {
        if (!STATS_ACTIVE) {
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `last_active`<" . DBManager::RealEscape(time() - $CONFIG["gl_dvhd"] * 86400) . ";");
            queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_OPERATOR_STATUS . "` WHERE `" . DB_PREFIX . DATABASE_OPERATOR_STATUS . "`.`confirmed`<" . DBManager::RealEscape(time() - $CONFIG["gl_dvhd"] * 86400) . ";");
        } else {
            StatisticProvider::DeleteHTMLReports();
        }
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_EVENT_TRIGGERS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_EVENT_TRIGGERS . "`.`receiver_user_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE (`html` = '0' OR `html` = '') AND `time` < " . DBManager::RealEscape(time() - $timeouts[3]));
        queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` SET `closed`='" . time() . "' WHERE `chat_type`<>1 AND `closed`<`endtime` AND `endtime`<" . (time() - 1800) . ";");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `closed`=0 AND `chat_type`=1 AND NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE `chat_id` = `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "`.`chat_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `time` < " . DBManager::RealEscape(time() - $timeouts[3]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `persistent` = '0' AND `time` < " . DBManager::RealEscape(time() - $timeouts[1]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_POSTS . "` WHERE `repost` = '1' AND `time` < " . DBManager::RealEscape(time() - $timeouts[0]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_OPERATOR_LOGINS . "` WHERE `time` < " . DBManager::RealEscape(time() - $timeouts[1]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_EVENT_ACTION_INTERNALS . "` WHERE `created` < " . DBManager::RealEscape(time() - $timeouts[0]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_PROFILE_PICTURES . "` WHERE `webcam`=1 AND `time` < " . DBManager::RealEscape(time() - $timeouts[0]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_ALERTS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_ALERTS . "`.`receiver_user_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_FILES . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_CHAT_FILES . "`.`visitor_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_FORWARDS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE `chat_id` = `" . DB_PREFIX . DATABASE_CHAT_FORWARDS . "`.`chat_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_REQUESTS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_CHAT_REQUESTS . "`.`receiver_browser_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_STATS_AGGS_GOALS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_GOALS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_STATS_AGGS_GOALS . "`.`goal`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_STATS_AGGS_PAGES . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_DATA_PAGES . "` WHERE `id` = `" . DB_PREFIX . DATABASE_STATS_AGGS_PAGES . "`.`url`)");
        queryDB(true, "DELETE `" . DB_PREFIX . DATABASE_TICKETS . "` FROM `" . DB_PREFIX . DATABASE_TICKETS . "` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` ON `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`=`" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`ticket_id` WHERE `deleted`=1 AND `time` < " . DBManager::RealEscape(time() - $timeouts[3]) . ";");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_CUSTOMS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKETS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_TICKET_CUSTOMS . "`.`ticket_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_EDITORS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKETS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_TICKET_EDITORS . "`.`ticket_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_LOGS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKETS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_TICKET_LOGS . "`.`ticket_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_COMMENTS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKETS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_TICKET_COMMENTS . "`.`ticket_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKETS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`ticket_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKETS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE `ticket_id` = `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_ATTACHMENTS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` WHERE `id` = `" . DB_PREFIX . DATABASE_TICKET_ATTACHMENTS . "`.`parent_id`) AND NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_TICKET_EMAILS . "` WHERE `email_id` = `" . DB_PREFIX . DATABASE_TICKET_ATTACHMENTS . "`.`parent_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `visit_id` = `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "`.`visit_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSER_URLS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_VISITOR_BROWSER_URLS . "`.`browser_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "`.`visitor_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_CHATS . "` WHERE `chat_id` = `" . DB_PREFIX . DATABASE_VISITOR_CHAT_OPERATORS . "`.`chat_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITOR_GOALS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITORS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_VISITOR_GOALS . "`.`visitor_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_WEBSITE_PUSHS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_WEBSITE_PUSHS . "`.`receiver_browser_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_OVERLAY_BOXES . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_OVERLAY_BOXES . "`.`receiver_browser_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_GROUP_MEMBERS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_GROUPS . "` WHERE `id` = `" . DB_PREFIX . DATABASE_GROUP_MEMBERS . "`.`group_id`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_LOCALIZATIONS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_TYPES . "` WHERE `id` = `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_LOCALIZATIONS . "`.`tid`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "` WHERE NOT EXISTS (SELECT * FROM `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_TYPES . "` WHERE `id` = `" . DB_PREFIX . DATABASE_COMMERCIAL_CHAT_VOUCHERS . "`.`tid`)");
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_TICKET_EMAILS . "` WHERE `deleted`=1 AND `edited` < " . DBManager::RealEscape(time() - $timeouts[3]));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_PUSH_MESSAGES . "` WHERE `sent`=1 AND `created` < " . DBManager::RealEscape(time() - 600));
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_VISITOR_COMMENTS . "` WHERE `created` < " . DBManager::RealEscape(time() - max(1, $CONFIG["gl_colt"]) * 86400));
        if ($CONFIG["gl_adct"] != 1) {
            if (!empty($CONFIG["gl_rm_chats"])) {
                queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_chats_time"]));
            }
            if (!empty($CONFIG["gl_rm_oc"])) {
                queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=0 AND `time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_oc_time"]));
            }
            if (!empty($CONFIG["gl_rm_gc"])) {
                queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=2 AND `time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_gc_time"]));
            }
            if (!empty($CONFIG["gl_rm_rt"])) {
                queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_RATINGS . "` WHERE `time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_rt_time"]));
            }
            if (!empty($CONFIG["gl_rm_om"])) {
                queryDB(true, $d = "DELETE `" . DB_PREFIX . DATABASE_TICKETS . "` FROM `" . DB_PREFIX . DATABASE_TICKETS . "` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` ON `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`=`" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`ticket_id` WHERE `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_om_time"]));
            }
            if (!empty($CONFIG["gl_rm_tid"])) {
                queryDB(true, "DELETE `" . DB_PREFIX . DATABASE_TICKETS . "` FROM `" . DB_PREFIX . DATABASE_TICKETS . "` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "` ON `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`=`" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`ticket_id` INNER JOIN `" . DB_PREFIX . DATABASE_TICKET_EDITORS . "` ON `" . DB_PREFIX . DATABASE_TICKETS . "`.`id`=`" . DB_PREFIX . DATABASE_TICKET_EDITORS . "`.`ticket_id` WHERE `" . DB_PREFIX . DATABASE_TICKET_EDITORS . "`.`status`=3 AND `" . DB_PREFIX . DATABASE_TICKET_MESSAGES . "`.`time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_tid_time"]));
            }
            if (!empty($INTERNAL) && !empty($CONFIG["gl_rm_bc"])) {
                foreach ($INTERNAL as $sid => $operator) {
                    if ($operator->IsBot) {
                        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_CHAT_ARCHIVE . "` WHERE `chat_type`=1 AND `internal_id`='" . DBManager::RealEscape($sid) . "' AND `time` < " . DBManager::RealEscape(time() - $CONFIG["gl_rm_bc_time"]));
                    }
                }
            }
        }
        if ($result = queryDB(true, "SELECT * FROM `" . DB_PREFIX . DATABASE_RESOURCES . "` WHERE `discarded`=1 AND `type` > 2 AND `edited` < " . DBManager::RealEscape(time() - $timeouts[3]))) {
        }
        while ($result && ($row = DBManager::FetchArray($result))) {
            $resultb = queryDB(true, "SELECT count(value) as `linked` FROM `" . DB_PREFIX . DATABASE_RESOURCES . "` WHERE `value`='" . DBManager::RealEscape($row["value"]) . "';");
            $rowb = DBManager::FetchArray($resultb);
            if ($rowb["linked"] == 1) {
                @unlink(PATH_UPLOADS . $row["value"]);
            }
        }
        queryDB(true, "DELETE FROM `" . DB_PREFIX . DATABASE_RESOURCES . "` WHERE `discarded`='1' AND `edited` < " . DBManager::RealEscape(time() - $timeouts[3]));
    }
    if (rand(1, $randoms[2]) == 1) {
        if (empty($CONFIG["gl_rm_chats"]) || !empty($CONFIG["gl_rm_chats_time"])) {
            sendChatTranscripts();
        }
        downloadEmails($_asCronJob);
    }
}