Example #1
0
    def pre(self):
        c.response_wrappers = []
        MinimalController.pre(self)

        set_cnameframe()

        # populate c.cookies unless we're on the unsafe media_domain
        if request.host != g.media_domain or g.media_domain == g.domain:
            try:
                for k, v in request.cookies.iteritems():
                    # minimalcontroller can still set cookies
                    if k not in c.cookies:
                        # we can unquote even if it's not quoted
                        c.cookies[k] = Cookie(value=unquote(v), dirty=False)
            except CookieError:
                # pylons or one of the associated retarded libraries
                # can't handle broken cookies
                request.environ["HTTP_COOKIE"] = ""

        c.firsttime = firsttime()

        # the user could have been logged in via one of the feeds
        maybe_admin = False

        # no logins for RSS feed unless valid_feed has already been called
        if not c.user:
            if c.extension != "rss":
                (c.user, maybe_admin) = valid_cookie(
                    c.cookies[g.login_cookie].value if g.login_cookie in c.cookies else ""
                )
                if c.user:
                    c.user_is_loggedin = True

            if not c.user:
                c.user = UnloggedUser(get_browser_langs())
                # patch for fixing mangled language preferences
                if not isinstance(c.user.pref_lang, basestring) or not all(
                    isinstance(x, basestring) for x in c.user.pref_content_langs
                ):
                    c.user.pref_lang = g.lang
                    c.user.pref_content_langs = [g.lang]
                    c.user._commit()
        if c.user_is_loggedin:
            if not c.user._loaded:
                c.user._load()
            c.modhash = c.user.modhash()
            if request.method.upper() == "GET":
                read_mod_cookie()
            if hasattr(c.user, "msgtime") and c.user.msgtime:
                c.have_messages = c.user.msgtime
            if hasattr(c.user, "modmsgtime"):
                c.show_mod_mail = True
                if c.user.modmsgtime:
                    c.have_mod_messages = c.user.modmsgtime
            else:
                c.show_mod_mail = Subsciteit.reverse_moderator_ids(c.user)
            c.user_is_admin = maybe_admin and c.user.name in g.admins
            c.user_special_distinguish = c.user.special_distinguish()
            c.user_is_sponsor = c.user_is_admin or c.user.name in g.sponsors
            if request.path != "/validuser" and not g.disallow_db_writes:
                c.user.update_last_visit(c.start_time)

        c.over18 = over18()

        # set_browser_langs()
        set_host_lang()
        set_iface_lang()
        set_content_lang()
        set_recent_clicks()
        # used for HTML-lite templates
        set_colors()

        # set some environmental variables in case we hit an abort
        if not isinstance(c.site, FakeSubsciteit):
            request.environ["SCITEIT_NAME"] = c.site.name

        # random sciteit trickery -- have to do this after the content lang is set
        if c.site == Random:
            c.site = Subsciteit.random_sciteit()
            redirect_to("/" + c.site.path.strip("/") + request.path)
        elif c.site == RandomNSFW:
            c.site = Subsciteit.random_sciteit(over18=True)
            redirect_to("/" + c.site.path.strip("/") + request.path)

        if not request.path.startswith("/api/login/"):
            # check that the site is available:
            if c.site.spammy() and not c.user_is_admin and not c.error_page:
                abort(404, "not found")

            # check if the user has access to this subsciteit
            if not c.site.can_view(c.user) and not c.error_page:
                abort(403, "forbidden")

            # check over 18
            if (
                c.site.over_18
                and not c.over18
                and request.path not in ("/frame", "/over18")
                and c.render_style == "html"
            ):
                return self.intermediate_redirect("/over18")

        # check whether to allow custom styles
        c.allow_styles = self.allow_stylesheets
        if g.css_killswitch:
            c.allow_styles = False
        # if the preference is set and we're not at a cname
        elif not c.user.pref_show_stylesheets and not c.cname:
            c.allow_styles = False
        # if the site has a cname, but we're not using it
        elif c.site.domain and c.site.css_on_cname and not c.cname:
            c.allow_styles = False