static function GetMatches($_list, $_question, $_language, $_chat, $_internal, $lmsi = false, $lmse = false, $lpi = null, $lpe = null)
 {
     $answers = array();
     $usedResIds = array();
     foreach ($_list as $reply) {
         if (!isset($reply->SearchType)) {
             continue;
         }
         if ($_chat != null) {
             $reply->Answer = $_chat->TextReplace($reply->Answer);
         }
         if ($_internal != null) {
             $reply->Answer = $_internal->TextReplace($reply->Answer);
         }
         if ($reply->SearchType != 5) {
             $reply->Tags = str_replace(array("!", ".", "?", "=", ")", "(", "-", "_", ":", "#", "~", "?"), "", strtolower($reply->Tags));
         }
         if (!$reply->MatchesLanguage($_language)) {
             continue;
         }
         if (empty($_chat->AllocatedTime) && !$reply->Waiting) {
             if (!($_internal != null && $_internal->IsBot)) {
                 continue;
             }
         }
         $tags = explode(",", $reply->Tags);
         $count = 0;
         if (!empty($_chat)) {
             if ($lmsi === false && ($reply->SendInactivityTimeInternal > -1 || $reply->SendInactivityTimeExternal > -1)) {
                 $lpi = Chat::GetLastPost($_chat->ChatId, true);
                 $lpe = Chat::GetLastPost($_chat->ChatId, false);
                 $lmsi = $lpi != null ? $lpi->Created : 0;
                 $lmse = $lpe != null ? $lpe->Created : 0;
             }
             $lm = max($lmsi, $lmse);
             $lastMessageExternal = $lmse > $lmsi && !empty($lm);
             $lastMessageInternal = $lmsi >= $lmse;
             if (empty($lm)) {
                 $lm = $_chat->AllocatedTime;
             }
             if (!empty($lm)) {
                 if ($reply->SendInactivityTimeInternal > -1 && $lastMessageExternal && $lmsi > 0) {
                     if (time() - $lm > $reply->SendInactivityTimeInternal) {
                         $answers[$count . "-" . count($answers)] = $reply;
                     }
                 }
                 if ($reply->SendInactivityTimeExternal > -1 && $lastMessageInternal) {
                     if (time() - $lm > $reply->SendInactivityTimeExternal) {
                         if (!($lpi != null && $reply->Answer == $lpi->Text)) {
                             $answers[$count . "-" . count($answers)] = $reply;
                         }
                     }
                 }
                 if ($reply->CloseChat && !empty($_chat) && !empty($_internal)) {
                     if (count($answers) > 0 && isset($answers["0-0"]) && $answers["0-0"] == $reply) {
                         $_chat->InternalClose($_internal->SystemId);
                     }
                 }
             }
         }
         if ($reply->SendInactivityTimeInternal == -1 && $reply->SendInactivityTimeExternal == -1) {
             foreach ($tags as $tag) {
                 if ($reply->SearchType == 5) {
                     if (@preg_match($reply->Tags, $_question) === 1) {
                         $count++;
                     }
                 } else {
                     if ($reply->SearchType < 4 && strpos($_question, $tag) !== false || jokerCompare($tag, $_question)) {
                         $count++;
                     }
                 }
             }
             if ($reply->SearchType == 0 && $count == substr_count($reply->Tags, ",") + 1 || $reply->SearchType > 0 && $count >= $reply->SearchType || $reply->SearchType >= 4 && $count > 0) {
                 if (empty($reply->Answer)) {
                     if (KnowledgeBaseEntry::GetById($reply->ResourceId) !== null && !isset($usedResIds[$reply->ResourceId])) {
                         $answers[$count . "-" . count($answers)] = $reply;
                         $usedResIds[$reply->ResourceId] = true;
                     }
                 } else {
                     $answers = array();
                     $answers[$count . "-" . count($answers)] = $reply;
                     break;
                 }
             }
         }
     }
     return $answers;
 }
function createSPAMFilter()
{
    global $CONFIG;
    if (!empty($CONFIG["gl_sfa"])) {
        $filterkeys = array(0 => $CONFIG["gl_sfv"]);
        if (strpos($CONFIG["gl_sfv"], ",") !== -1) {
            $filterkeys = explode(",", $CONFIG["gl_sfv"]);
        }
        foreach ($filterkeys as $fvalue) {
            foreach ($_GET as $gvalue) {
                if (jokerCompare($fvalue, base64UrlDecode($gvalue))) {
                    Filter::Create(getIP(), null, "AUTO SPAM Filter: " . $fvalue);
                    return true;
                }
            }
            foreach ($_POST as $pvalue) {
                if (jokerCompare($fvalue, base64UrlDecode($pvalue))) {
                    Filter::Create(getIP(), null, "AUTO SPAM Filter: " . $fvalue);
                    return true;
                }
            }
        }
    }
    return false;
}
 function SetQuery($_referrer, $issearchengine = false, $parammatch = false, $encoding = "")
 {
     $parts = parse_url(strtolower($_referrer));
     $uparts = explode("&", @$parts["query"]);
     foreach (HistoryUrl::$SearchEngines as $sparam => $engines) {
         foreach ($uparts as $param) {
             $kv = explode("=", $param);
             $parammatch = $kv[0] == $sparam && !empty($kv[1]);
             foreach ($engines as $engine) {
                 if (jokerCompare($engine, $parts["host"])) {
                     $issearchengine = true;
                 }
                 if ($issearchengine && $parammatch) {
                     if (empty($encoding)) {
                         foreach (HistoryUrl::$SearchEngineEncodings as $enc => $eengines) {
                             foreach ($eengines as $eengine) {
                                 if ($eengine == $engine) {
                                     $encoding = $enc;
                                 }
                             }
                         }
                     }
                     $this->Query = empty($encoding) ? urldecode(trim($kv[1])) : html_entity_decode(@iconv($encoding, "UTF-8", urldecode(trim($kv[1]))), ENT_QUOTES, 'UTF-8');
                     if (!empty($this->Query) && isnull(getCookieValue("sp"))) {
                         setCookieValue("sp", $this->Query);
                     }
                     queryDB(true, "UPDATE `" . DB_PREFIX . DATABASE_VISITOR_BROWSERS . "` SET `query`='" . DBManager::RealEscape($this->GetQueryId($this->Query, $_referrer)) . "' WHERE `id`='" . DBManager::RealEscape($this->BrowserId) . "' LIMIT 1;");
                     return true;
                 }
             }
         }
     }
     return $issearchengine;
 }
 static function GetMatches($_list, $_question, $_language)
 {
     $answers = array();
     foreach ($_list as $reply) {
         if ($reply->SearchType != 5) {
             $reply->Tags = str_replace(array("!", ".", "?", "=", ")", "(", "-", "_", ":", "#", "~", "?"), "", strtolower($reply->Tags));
         }
         if (!$reply->MatchesLanguage($_language)) {
             continue;
         }
         $tags = explode(",", $reply->Tags);
         $count = 0;
         foreach ($tags as $tag) {
             if ($reply->SearchType == 5) {
                 if (@preg_match($reply->Tags, $_question) === 1) {
                     $count++;
                 }
             } else {
                 if ($reply->SearchType < 4 && strpos($_question, $tag) !== false || jokerCompare($tag, $_question)) {
                     $count++;
                 }
             }
         }
         if ($reply->SearchType == 0 && $count == substr_count($reply->Tags, ",") + 1 || $reply->SearchType > 0 && $count >= $reply->SearchType || $reply->SearchType >= 4 && $count > 0) {
             if (empty($reply->Answer)) {
                 if (getResource($reply->ResourceId) !== null) {
                     $answers[$count . "-" . count($answers)] = $reply;
                 }
             } else {
                 $answers = array();
                 $answers[$count . "-" . count($answers)] = $reply;
                 break;
             }
         }
     }
     krsort($answers);
     return $answers;
 }