def pre(self): g.cache.caches = (LocalCache(),) + g.cache.caches[1:] #check if user-agent needs a dose of rate-limiting ratelimit_agents() c.domain = g.domain c.response_wrappers = [] c.errors = ErrorSet() c.firsttime = firsttime() (c.user, maybe_admin) = \ valid_cookie(request.cookies.get(g.login_cookie)) if c.user: c.user_is_loggedin = True else: c.user = UnloggedUser(get_browser_langs()) c.user._load() if c.user_is_loggedin: if not c.user._loaded: c.user._load() c.modhash = c.user.modhash() if request.method.lower() == 'get': read_click_cookie() read_mod_cookie() if hasattr(c.user, 'msgtime') and c.user.msgtime: c.have_messages = c.user.msgtime c.user_is_admin = maybe_admin and c.user.name in g.admins c.over18 = over18() #set_browser_langs() set_host_lang() set_subreddit() set_content_type() set_iface_lang() set_content_lang() # check if the user has access to this subreddit if not c.site.can_view(c.user): abort(403, "forbidden") #check over 18 if c.site.over_18 and not c.over18: d = dict(dest=reddit_link(request.path, url = True) + utils.query_string(request.GET)) return redirect_to("/over18" + utils.query_string(d)) #check content cache if not c.user_is_loggedin: r = cache.get(self.request_key()) if r and request.method == 'GET': response = c.response response.headers = r.headers response.content = r.content response.status_code = r.status_code request.environ['pylons.routes_dict']['action'] = 'cached_response' # make sure to carry over the content type c.response_content_type = r.headers['content-type'] if r.headers.has_key('access-control'): c.response_access_control = r.headers['access-control'] c.used_cache = True # response wrappers have already been applied before cache write c.response_wrappers = []
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": session_cookie = c.cookies.get(g.login_cookie) if session_cookie: c.user = valid_cookie(session_cookie.value) if c.user: c.user_is_loggedin = True admin_cookie = c.cookies.get(g.admin_cookie) if c.user_is_loggedin and admin_cookie: maybe_admin, first_login = valid_admin_cookie(admin_cookie.value) if maybe_admin: self.enable_admin_mode(c.user, first_login=first_login) else: self.disable_admin_mode(c.user) 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 c.show_mod_mail = Subreddit.reverse_moderator_ids(c.user) c.have_mod_messages = getattr(c.user, "modmsgtime", False) 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, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # random reddit trickery -- have to do this after the content lang is set if c.site == Random: c.site = Subreddit.random_reddit() redirect_to("/" + c.site.path.strip('/') + request.path) elif c.site == RandomNSFW: c.site = Subreddit.random_reddit(over18 = True) redirect_to("/" + c.site.path.strip('/') + request.path) if not request.path.startswith("/api/login/"): # is the subreddit banned? if c.site.spammy() and not c.user_is_admin and not c.error_page: ban_info = getattr(c.site, "ban_info", {}) if "message" in ban_info: message = ban_info['message'] else: sitelink = url_escape(add_sr("/")) subject = ("/r/%s has been incorrectly banned" % c.site.name) link = ("/r/redditrequest/submit?url=%s&title=%s" % (sitelink, subject)) message = strings.banned_subreddit_message % dict( link=link) errpage = pages.RedditError(strings.banned_subreddit_title, message, image="subreddit-banned.png") request.environ['usable_error_content'] = errpage.render() self.abort404() # check if the user has access to this subreddit if not c.site.can_view(c.user) and not c.error_page: errpage = pages.RedditError(strings.private_subreddit_title, strings.private_subreddit_message, image="subreddit-private.png") request.environ['usable_error_content'] = errpage.render() self.abort403() #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 = True c.can_apply_styles = self.allow_stylesheets if g.css_killswitch: c.can_apply_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.can_apply_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.can_apply_styles = False
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_is_loggedin: 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_is_loggedin: 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.lower() == "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 = Subreddit.reverse_moderator_ids(c.user) c.user_is_admin = maybe_admin and c.user.name in g.admins c.user_is_sponsor = c.user_is_admin or c.user.name in g.sponsors if 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_reddits() 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, FakeSubreddit): request.environ["REDDIT_NAME"] = c.site.name # random reddit trickery -- have to do this after the content lang is set if c.site == Random: c.site = Subreddit.random_reddit() redirect_to("/" + c.site.path.strip("/") + request.path) elif c.site == RandomNSFW: c.site = Subreddit.random_reddit(over18=True) redirect_to("/" + c.site.path.strip("/") + request.path) # check that the site is available: if c.site._spam and not c.user_is_admin and not c.error_page: abort(404, "not found") # check if the user has access to this subreddit 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
def pre(self): g.cache.caches = (LocalCache(),) + g.cache.caches[1:] #check if user-agent needs a dose of rate-limiting if not c.error_page: ratelimit_agents() # the domain has to be set before Cookies get initialized set_subreddit() set_cnameframe() # populate c.cookies c.cookies = Cookies() try: for k,v in request.cookies.iteritems(): # 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.response_wrappers = [] c.errors = ErrorSet() c.firsttime = firsttime() (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 else: c.user = UnloggedUser(get_browser_langs()) c.user._load() if c.user_is_loggedin: if not c.user._loaded: c.user._load() c.modhash = c.user.modhash() if request.method.lower() == 'get': read_click_cookie() read_mod_cookie() if hasattr(c.user, 'msgtime') and c.user.msgtime: c.have_messages = c.user.msgtime c.user_is_admin = maybe_admin and c.user.name in g.admins c.user_is_sponsor = c.user_is_admin or c.user.name in g.sponsors c.over18 = over18() #set_browser_langs() set_host_lang() set_content_type() set_iface_lang() set_content_lang() set_colors() set_recent_reddits() # set some environmental variables in case we hit an abort if not isinstance(c.site, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # check if the user has access to this subreddit 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 = True 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 not c.cname: c.allow_styles = False #check content cache if not c.user_is_loggedin: r = cache.get(self.request_key()) if r and request.method == 'GET': response = c.response response.headers = r.headers response.content = r.content for x in r.cookies.keys(): if x in cache_affecting_cookies: cookie = r.cookies[x] response.set_cookie(key = x, value = cookie.value, domain = cookie.get('domain',None), expires = cookie.get('expires',None), path = cookie.get('path',None)) response.status_code = r.status_code request.environ['pylons.routes_dict']['action'] = 'cached_response' # make sure to carry over the content type c.response_content_type = r.headers['content-type'] if r.headers.has_key('access-control'): c.response_access_control = r.headers['access-control'] c.used_cache = True # response wrappers have already been applied before cache write c.response_wrappers = []
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": session_cookie = c.cookies.get(g.login_cookie) if session_cookie: c.user = valid_cookie(session_cookie.value) if c.user: c.user_is_loggedin = True admin_cookie = c.cookies.get(g.admin_cookie) if c.user_is_loggedin and admin_cookie: maybe_admin, first_login = valid_admin_cookie( admin_cookie.value) if maybe_admin: self.enable_admin_mode(c.user, first_login=first_login) else: self.disable_admin_mode(c.user) 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 c.show_mod_mail = Subreddit.reverse_moderator_ids(c.user) c.have_mod_messages = getattr(c.user, "modmsgtime", False) 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, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # random reddit trickery -- have to do this after the content lang is set if c.site == Random: c.site = Subreddit.random_reddit() redirect_to("/" + c.site.path.strip('/') + request.path) elif c.site == RandomNSFW: c.site = Subreddit.random_reddit(over18=True) redirect_to("/" + c.site.path.strip('/') + request.path) if not request.path.startswith("/api/login/"): # is the subreddit banned? if c.site.spammy() and not c.user_is_admin and not c.error_page: ban_info = getattr(c.site, "ban_info", {}) if "message" in ban_info: message = ban_info['message'] else: sitelink = url_escape(add_sr("/")) subject = ("/r/%s has been incorrectly banned" % c.site.name) link = ("/r/redditrequest/submit?url=%s&title=%s" % (sitelink, subject)) message = strings.banned_subreddit_message % dict( link=link) errpage = pages.RedditError(strings.banned_subreddit_title, message, image="subreddit-banned.png") request.environ['usable_error_content'] = errpage.render() self.abort404() # check if the user has access to this subreddit if not c.site.can_view(c.user) and not c.error_page: errpage = pages.RedditError(strings.private_subreddit_title, strings.private_subreddit_message, image="subreddit-private.png") request.environ['usable_error_content'] = errpage.render() self.abort403() #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 = True c.can_apply_styles = self.allow_stylesheets if g.css_killswitch: c.can_apply_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.can_apply_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.can_apply_styles = False
def pre(self): g.cache.caches = (LocalCache(), ) + g.cache.caches[1:] #check if user-agent needs a dose of rate-limiting if not c.error_page: ratelimit_agents() # the domain has to be set before Cookies get initialized set_subreddit() set_cnameframe() # populate c.cookies c.cookies = Cookies() try: for k, v in request.cookies.iteritems(): # 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.response_wrappers = [] c.errors = ErrorSet() c.firsttime = firsttime() (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 else: c.user = UnloggedUser(get_browser_langs()) c.user._load() if c.user_is_loggedin: if not c.user._loaded: c.user._load() c.modhash = c.user.modhash() if request.method.lower() == 'get': read_click_cookie() read_mod_cookie() if hasattr(c.user, 'msgtime') and c.user.msgtime: c.have_messages = c.user.msgtime c.user_is_admin = maybe_admin and c.user.name in g.admins c.user_is_sponsor = c.user_is_admin or c.user.name in g.sponsors c.over18 = over18() #set_browser_langs() set_host_lang() set_content_type() set_iface_lang() set_content_lang() set_colors() set_recent_reddits() # set some environmental variables in case we hit an abort if not isinstance(c.site, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # check if the user has access to this subreddit 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 = True 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 not c.cname: c.allow_styles = False #check content cache if not c.user_is_loggedin: r = cache.get(self.request_key()) if r and request.method == 'GET': response = c.response response.headers = r.headers response.content = r.content for x in r.cookies.keys(): if x in cache_affecting_cookies: cookie = r.cookies[x] response.set_cookie(key=x, value=cookie.value, domain=cookie.get('domain', None), expires=cookie.get( 'expires', None), path=cookie.get('path', None)) response.status_code = r.status_code request.environ['pylons.routes_dict'][ 'action'] = 'cached_response' # make sure to carry over the content type c.response_content_type = r.headers['content-type'] if r.headers.has_key('access-control'): c.response_access_control = r.headers['access-control'] c.used_cache = True # response wrappers have already been applied before cache write c.response_wrappers = []
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 = Subreddit.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, FakeSubreddit): request.environ['REDDIT_NAME'] = c.site.name # random reddit trickery -- have to do this after the content lang is set if c.site == Random: c.site = Subreddit.random_reddit() redirect_to("/" + c.site.path.strip('/') + request.path) elif c.site == RandomNSFW: c.site = Subreddit.random_reddit(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 subreddit 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