function UpdateAction($Arr, $Id = false)
{
    global $Db, $Logs, $nsProduct, $HostsArr, $SiteId, $CpId, $Site, $Lang;
    $TPath = "";
    $ActionPath = "";
    $CodeAction = 0;
    $SiteHostId = 0;
    extract($Arr);
    if (!ValidVar($Name)) {
        $Logs->Err($Lang['MustFillName']);
        return;
    }
    if (!ValidVar($Templ) && ValidVar($RedirOnly) != 2) {
        $Logs->Err($Lang['WhatIsAction']);
        return;
    }
    if (ValidVar($RedirOnly) == 1 && !ValidVar($RedirUrl) && !ValidVar($Dynamic)) {
        $Logs->Err($Lang['RedirUrlRequired']);
        return;
    }
    if (!ValidVar($Active)) {
        $Active = 0;
    }
    if (ValidVar($RedirOnly) == 2) {
        $CodeAction = 1;
    }
    if (!$CodeAction) {
        $TmpArr = explode("{a}", $Templ);
        if (ValidArr($TmpArr) && count($TmpArr) > 2) {
            $Logs->Err($Lang['OnlyOneActionTarget']);
            return;
        }
        $NoPage = false;
        $TArr = @parse_url($Templ);
        if (!$TArr) {
            $Logs->Err($Lang['UnableParseTemplate']);
            return;
        }
        if (isset($TArr['path']) && !isset($TArr['host'])) {
            //$Path=ToLower($TArr['path']);
            for ($i = 0; $i < count($HostsArr); $i++) {
                if (strpos($Path, $HostsArr[$i]) !== false) {
                    $Path = str_replace($HostsArr[$i], "", $Path);
                    $TArr['host'] = $HostsArr[$i];
                    $TArr['path'] = $Path;
                    break;
                }
            }
        }
        if (count($TArr) == 1 && substr(ValidVar($TArr['path']), 0, 1) != "/") {
            if (isset($TArr['path']) && !isset($TArr['query'])) {
                $TArr['query'] = $TArr['path'];
            }
            unset($TArr['path']);
            $NoPage = 1;
        }
        if (isset($TArr['fragment'])) {
            unset($TArr['fragment']);
        }
        if (!$NoPage) {
            if (!isset($TArr['host'])) {
                $TArr['host'] = $Site->HOST;
            }
            $TArr['host'] = ToLower($TArr['host']);
            //if (isset($TArr['path'])&&isset($TArr['query'])&&!isset($TArr['host'])) $TArr['host']=$Site->HOST;
            if (isset($TArr['path']) && substr($TArr['path'], 0, 1) != "/") {
                $TArr['path'] = "/" . $TArr['path'];
            }
            if (!ValidVar($TArr['host']) || !in_array($TArr['host'], $HostsArr)) {
                //$Logs->Err($Lang['InvalidDomain']);return;
                $Query = "INSERT INTO " . PFX . "_tracker_site_host (HOST, SITE_ID) VALUES (?, " . $Site->ID . ")";
                $Db->Query($Query, $TArr['host']);
                $NewHostId = $Db->LastInsertId;
                $HostsArr[] = $TArr['host'];
                $Site->Hosts[$NewHostId] = $TArr['host'];
            }
            if (!ValidVar($TArr['path'])) {
                $TArr['path'] = "/";
            }
            if (strpos($TArr['path'], "*") === false && strpos($TArr['path'], "{a}") === false && strpos($TArr['path'], ".") === false && $TArr['path'] != "/" && substr($TArr['path'], -1) != "/") {
                $TArr['path'] .= "/";
            }
            extract($TArr);
            foreach ($Site->Hosts as $SHId => $SHost) {
                if ($host == $SHost) {
                    $SiteHostId = $SHId;
                }
            }
            if (strpos($path, "*") === false && strpos($path, "{a}") === false) {
                $Query = "SELECT ID FROM " . PFX . "_tracker_site_page WHERE SITE_ID={$SiteId} AND PATH='{$path}'";
                $PageId = $Db->ReturnValue($Query);
                if (!ValidId($PageId)) {
                    $Query = "INSERT INTO " . PFX . "_tracker_site_page (SITE_ID, PATH) VALUES ({$SiteId}, '{$path}')";
                    $Db->Query($Query);
                    $PageId = $Db->LastInsertId;
                }
                $ActionPath = $path;
                if (!ValidId($PageId)) {
                    $Logs->Err($Lang['UnableCreatePage']);
                    return;
                }
            } else {
                if (ValidVar($RedirOnly)) {
                    $Logs->Err($Lang['NoRedirWithPageTemp']);
                    return;
                }
                $PageId = 0;
                $TPath = $path;
            }
            if (ValidVar($query)) {
                $query = "?" . $query;
            }
        } else {
            $PageId = 0;
            $query = $Templ;
            if (strpos($query, "?") === false || strpos($query, "?") != 0) {
                $query = "?" . $query;
            }
        }
        if (ValidVar($RedirOnly) != 1 && ValidVar($RedirOnly) != 2) {
            $RedirOnly = 0;
        }
        if (!ValidVar($RedirUrl)) {
            $RedirUrl = "";
        }
        if (!ValidVar($ItemVar)) {
            $ItemVar = "";
        }
        $RedirUrl = urlencode($RedirUrl);
        $LogInfo = "";
    } else {
        $PageId = 0;
        $query = "";
        $RedirUrl = "";
        $TPath = "";
        $ItemVar = "";
        $RedirOnly = 0;
    }
    if (!$Id) {
        $Query = "INSERT INTO " . PFX . "_tracker_visitor_action (SITE_ID, PAGE_ID, NAME, QUERY, PATH, REDIRECT_URL, REDIRECT_CATCH, ITEM_VAR, ACTIVE, CODE_ACTION, SITE_HOST_ID) VALUES ({$SiteId}, {$PageId}, '{$Name}', '{$query}', '{$TPath}', '{$RedirUrl}', '{$RedirOnly}', '{$ItemVar}', '{$Active}', '{$CodeAction}', '{$SiteHostId}')";
        $Db->Query($Query);
        $EditId = $Db->LastInsertId;
        $LogInfo = "&RCrt=1";
    } else {
        $Query = "UPDATE " . PFX . "_tracker_visitor_action SET PAGE_ID={$PageId}, NAME='{$Name}', QUERY='{$query}', PATH='{$TPath}', REDIRECT_URL='{$RedirUrl}', REDIRECT_CATCH='{$RedirOnly}', ITEM_VAR='{$ItemVar}', ACTIVE='{$Active}', CODE_ACTION='{$CodeAction}', SITE_HOST_ID='{$SiteHostId}' WHERE ID = {$Id} AND SITE_ID={$SiteId}";
        $Db->Query($Query);
        $EditId = $Id;
        $LogInfo = "&RUpd=1";
    }
    $WrRes = true;
    $WrErr = false;
    if (!$CodeAction) {
        $UseStore = $Db->ReturnValue("SELECT USE_STORE FROM " . PFX . "_tracker_config WHERE COMPANY_ID=0");
        if ($RedirOnly && $UseStore) {
            $ActionArr['ID'] = $EditId;
            $ActionArr['PAGE_ID'] = $PageId;
            $ActionArr['SITE_ID'] = $SiteId;
            $ActionArr['REDIRECT_URL'] = $RedirUrl;
            $ActionArr['PATH'] = $ActionPath;
            $WrRes = false;
            $WrRes = SaveActionToFile($ActionArr, "redir_action.nodb");
            //if (!$WrRes) $WrErr="&WrErr=1";
        }
    }
    $nsProduct->Redir("actions", "CpId={$CpId}&SiteId={$SiteId}&EditId={$EditId}" . $WrErr . $LogInfo);
}
function RebuildStoreFiles()
{
    $ActionFile = "redir_action.nodb";
    $f = @fopen(SELF . "/store/{$ActionFile}", "a+");
    @flock($f, LOCK_EX);
    @ftruncate($f, 0);
    @flock($f, LOCK_UN);
    @fclose($f);
    $SplitFile = "split_test.nodb";
    $f = @fopen(SELF . "/store/{$SplitFile}", "a+");
    @flock($f, LOCK_EX);
    @ftruncate($f, 0);
    @flock($f, LOCK_UN);
    @fclose($f);
    $Query = "SELECT * FROM " . PFX . "_tracker_visitor_action WHERE REDIRECT_CATCH='1' ";
    $Sql = new Query($Query);
    while ($Row = $Sql->Row()) {
        $ActionArr['ID'] = $Row->ID;
        $ActionArr['PAGE_ID'] = $Row->PAGE_ID;
        $ActionArr['SITE_ID'] = $Row->SITE_ID;
        $ActionArr['REDIRECT_URL'] = $Row->REDIRECT_URL;
        $ActionArr['PATH'] = $Row->PATH;
        SaveActionToFile($ActionArr, $ActionFile);
    }
    $Query = "SELECT ID FROM " . PFX . "_tracker_split_test";
    $Sql = new Query($Query);
    while ($Row = $Sql->Row()) {
        SaveSplitToFile(false, $SplitFile, $Row->ID);
    }
}