Exemplo n.º 1
0
def is_valid_email(e):    
    ##if not hasattr(RegularExpressions, "emailMatcher"):
    if not rx.exists("emailMatcher"):
        # See RFC 2821, RFC 2822, RFC 3696
        
        # lots of legitimate chars in localname, but can't start or end with a .
        # Not all emailers support all of these characters
        localnameChars = r"[-.a-zA-Z0-9!#$%*/?|^{}`~'""+=_]"
        localnameTerminalChars = r"[-a-zA-Z0-9!#$%*/?|^{}`~'""+=_]"
        localnameRE = r"(%s|%s%s{0,62}%s)" % (localnameTerminalChars, localnameTerminalChars, localnameChars, localnameTerminalChars)
        
        # Domain name can't start or end with a . or -
        domainChars = r"[-.a-zA-Z0-9]"
        domainTerminalChars = r"[a-zA-Z0-9]"
        hostnameRE = r"(%s|%s%s*%s)" % (domainTerminalChars, domainTerminalChars, domainChars, domainTerminalChars)
        
        tldChars = r"[a-zA-Z]"
        tldIntlChars = r"[a-zA-Z0-9]"
        tldRE = r"(%s{2,10}|\.xn--%s+)" % (tldChars, tldIntlChars)
        
        emailRE = r"^(?P<localname>%s)@(?P<hostname>%s)\.(?P<tld>%s)$" % (localnameRE, hostnameRE, tldRE)
        
        rx.define("emailMatcher", emailRE)
    
    matches = rx.match("emailMatcher", e)
    if matches is None:
        return False
    
    localname = matches.group("localname")
    hostname = matches.group("hostname")
    
    if localname.find("..") != -1 or hostname.find("..") != -1:
        return False

    return True
Exemplo n.º 2
0
def default_handler(request):
    """Default handler for Django and html pages"""

    if not rx.exists(kiwiPathMatcher):
        rx.define(kiwiPathMatcher, r"^(/)?(?P<name>.+?)(?P<suffix>(%s)%s)$" % (kiwioptions.WEB_ALLOWED_SUFFIXES, "?" if kiwioptions.WEB_PREFERRED_SUFFIX == "" else ""))

    # Separate the path into a name and suffix
    path = request.path.replace("\\", "/")
    m = rx.match(kiwiPathMatcher, path)
    if m is None:
        return error404(request)

    templateName = m.group("name")
    givenSuffix = m.group("suffix")

    # For GET requests when there is a preferred suffix, do a 301 to it if necessary
    if kiwioptions.WEB_PREFERRED_SUFFIX is not None and (request.method == "GET") and (givenSuffix != kiwioptions.WEB_PREFERRED_SUFFIX):
        newPath = "/" + templateName + kiwioptions.WEB_PREFERRED_SUFFIX
        return HttpResponsePermanentRedirect(newPath)
    
    # The urlpatterns should have filtered out any URLs with .'s, but make sure
    if templateName.find(".") > 0:
        return error404(request)
    
    return render_template_to_response(request, templateName)
Exemplo n.º 3
0
def isMobileBrowser(req, iPhone=False, wantAgent=False):
    """Detects the type of browser in use.
    
    Set iPhone to the desired value to be returned for iPhones.
    The default is that the iPhone is considered to be a non-mobile browser.
    
    Set wantAgent to True if you want the mobile agent string that was matched to be returned.
    Setting wantAgent to be true is less performant and the agent string is not always reliable anyway.
    For unknown browsers that are believed to be mobile (because the support WAP), True is returned.
    
    Returns a non-false value if the browser is a mobile browser.
    Returns False for a non-mobile browser."""
    
    meta = req.META
    
    if "HTTP_USER_AGENT" not in meta:
        return isWAPBrowser(meta)
        
    userAgent = meta["HTTP_USER_AGENT"].lower()
    
    # Special case iPhone
    if userAgent.find("iphone") != -1:
        return iPhone
    
    if (not wantAgent) and isWAPBrowser(meta):
        return True
    
    # It might seem that this would be faster with a big regular expression,
    # but it turns out that the code to find substrings is so optimized that
    # the regular expression compiler can't compete with it.
    for s in __mobileBrowserAgentStrings:
        if userAgent.find(s) != -1:
            return s

    # But we do use regular expressions for agent strings that require them
    ##if not hasattr(RegularExpressions, "mobileMatcher"):
    ##    RegularExpressions.mobileMatcher = re.compile("(?P<agent>%s)" % __mobileBrowserAgentRegExp)
    if not rx.exists("mobileMatcher"):
        rx.define("mobileMatcher", "(?P<agent>%s)" % __mobileBrowserAgentRegExp)
    
    ##m = RegularExpressions.mobileMatcher.search(userAgent)
    matches = rx.search("mobileMatcher", userAgent)
    if matches: return matches.group("agent")

    if wantAgent and isWAPBrowser(meta):
        return True

    return False