function CheckPattern($name, $gpid, $type)
{
    if (!is_numeric($gpid)) {
        return;
    }
    if (!is_numeric($type)) {
        return;
    }
    if (!isset($GLOBALS["CACHE_CLEAN_MYSQL"])) {
        $GLOBALS["CACHE_CLEAN_MYSQL"] = time();
    }
    if ($type == 3) {
        if (preg_match("#^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\$#", $name)) {
            return 0;
        }
    }
    try {
        if (!class_exists("mysql_squid_builder")) {
            include_once dirname(__FILE__) . "/ressources/class.mysql.squid.builder.php";
        }
        $q = new mysql_squid_builder();
        $sql = "SELECT ID,`value`  FROM webfilter_aclsdynamic WHERE gpid={$gpid} AND `type`={$type}";
        $results = $q->QUERY_SQL($sql);
        if (!$q->ok) {
            WLOG("CheckPattern()::ERROR SQL: {$sql}");
            WLOG("CheckPattern()::ERROR SQL: `{$q->mysql_error}`");
            return 0;
        }
        if (mysql_num_rows($results) == 0) {
            return 0;
        }
        while ($ligne = mysql_fetch_assoc($results)) {
            $value = $ligne["value"];
            if (preg_match("#re:(.+)#", $value, $re)) {
                if (preg_match("#{$re[1]}#i", $name)) {
                    return $ligne["ID"];
                }
                continue;
            }
            $value = string_to_regex($value);
            if ($GLOBALS["DEBUG_LEVEL"] > 3) {
                WLOG("CheckPattern()::Checks `{$value}` with `{$name}`");
            }
            if (preg_match("#{$value}#i", $name)) {
                if ($GLOBALS["DEBUG_LEVEL"] > 3) {
                    WLOG("CheckPattern()::Match `{$value}` with `{$name}` = {$ligne["ID"]}");
                }
                return $ligne["ID"];
            }
        }
        if (DistanceInMns($GLOBALS["CACHE_CLEAN_MYSQL"]) > 5) {
            $GLOBALS["CACHE_CLEAN_MYSQL"] = time();
            if ($GLOBALS["DEBUG_LEVEL"] > 3) {
                WLOG("CheckPattern():: Clean old rules...");
            }
            $sql = "DELETE FROM `webfilter_aclsdynamic` WHERE `duration`>0 AND `maxtime`>" . time();
            $q->QUERY_SQL($sql);
            if (!$q->ok) {
                WLOG("CheckPattern()::{$q->mysql_error},{$sql}");
            }
        }
    } catch (Exception $e) {
        WLOG($e->getMessage());
        return 0;
    }
    return 0;
}
function CheckPattern($name, $gpid, $type)
{
    if (!is_numeric($gpid)) {
        return;
    }
    if (!is_numeric($type)) {
        return;
    }
    if (!isset($GLOBALS["CACHE_CLEAN_MYSQL"])) {
        $GLOBALS["CACHE_CLEAN_MYSQL"] = time();
    }
    if ($type == 3) {
        if (preg_match("#^[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+\$#", $name)) {
            return 0;
        }
    }
    try {
        $sql = "SELECT ID,`value`  FROM webfilter_aclsdynamic WHERE gpid={$gpid} AND `type`={$type}";
        $results = api_QUERY_SQL($sql);
        if (mysql_num_rows($results) == 0) {
            if ($GLOBALS["DEBUG_LEVEL"] > 1) {
                WLOG("CheckPattern()::{$sql} -> NO ROW");
            }
            return 0;
        }
        while ($ligne = mysql_fetch_assoc($results)) {
            $value = $ligne["value"];
            if (preg_match("#re:(.+)#", $value, $re)) {
                if (preg_match("#{$re[1]}#i", $name)) {
                    return $ligne["ID"];
                }
                continue;
            }
            $value = string_to_regex($value);
            if ($GLOBALS["DEBUG_LEVEL"] > 3) {
                WLOG("CheckPattern()::Checks `{$value}` with `{$name}`");
            }
            if (preg_match("#{$value}#i", $name)) {
                if ($GLOBALS["DEBUG_LEVEL"] > 3) {
                    WLOG("CheckPattern()::Match `{$value}` with `{$name}` = {$ligne["ID"]}");
                }
                return $ligne["ID"];
            }
        }
        if (DistanceInMns($GLOBALS["CACHE_CLEAN_MYSQL"]) > 5) {
            $GLOBALS["CACHE_CLEAN_MYSQL"] = time();
            if ($GLOBALS["DEBUG_LEVEL"] > 3) {
                WLOG("CheckPattern():: Clean old rules...");
            }
            $sql = "DELETE FROM `webfilter_aclsdynamic` WHERE `duration`>0 AND `maxtime`>" . time();
            api_QUERY_SQL($sql);
        }
    } catch (Exception $e) {
        WLOG($e->getMessage());
        return 0;
    }
    return 0;
}