diff --git a/larpe/trunk/conf/filters/output_ciril_net_rh.py b/larpe/trunk/conf/filters/output_ciril_net_rh.py new file mode 100644 index 0000000..e61fe32 --- /dev/null +++ b/larpe/trunk/conf/filters/output_ciril_net_rh.py @@ -0,0 +1,63 @@ +import re +import os +import pickle + +from larpe import sessions +from mod_python import Cookie + +def is_auth_ok(req): + """ Test if you are authenticate on the Larpe server """ + cookies = Cookie.get_cookies(req, Cookie.MarshalCookie, secret='secret007') + sessions_dir = os.path.join("%(larpe_dir)s", "sessions") + for name, cookie in cookies.iteritems(): + value = cookie.value.replace('"', '') + if "larpe-" in name and value in os.listdir(sessions_dir): + try: + file = open(os.path.join(sessions_dir, value), "rb") + session = pickle.load(file) + if not session.users or not session.id: + return False + return True + except Exception, err: + return False + return False + + +def filter_page(filter, page): + r = re.compile(r"""(D.*?connexion)""") + page = r.sub(r"""\1%(logout_url)s\2""", page) + return page + +def outputfilter(filter): + """ Apache called this function by default """ + if not re.search("^/liberty/.*", filter.req.uri) and not is_auth_ok(filter.req): + filter.write('') + filter.close() + return + + if filter.req.content_type is not None: + is_html = re.search('text/html', filter.req.content_type) + if filter.req.content_type is None or not is_html: + filter.pass_on() + else: + if not hasattr(filter.req, 'temp_doc'): + # Create a new attribute to hold the document + filter.req.temp_doc = [] + # If content-length ended up wrong, Gecko browsers truncated data + if 'Content-Length' in filter.req.headers_out: + del filter.req.headers_out['Content-Length'] + + temp_doc = filter.req.temp_doc + s = filter.read() + # Could get '' at any point, but only get None at end + while s: + temp_doc.append(s) + s = filter.read() + + # The end + if s is None: + page = ''.join(temp_doc) + page = filter_page(filter, page) + filter.write(page) + filter.close() + diff --git a/larpe/trunk/conf/output_filter_base.py b/larpe/trunk/conf/filters/output_replace_form.py similarity index 77% rename from larpe/trunk/conf/output_filter_base.py rename to larpe/trunk/conf/filters/output_replace_form.py index aff82da..d8964a5 100644 --- a/larpe/trunk/conf/output_filter_base.py +++ b/larpe/trunk/conf/filters/output_replace_form.py @@ -1,5 +1,10 @@ import re +def filter_page(filter, page): + current_form = re.compile('
]*?action="%(auth_form_action)s".*?>.*?
', re.DOTALL) + page = current_form.sub('
', page) + return page + def outputfilter(filter): # Only filter html code if filter.req.content_type is not None: diff --git a/larpe/trunk/larpe/Defaults.py b/larpe/trunk/larpe/Defaults.py index b19e629..ca723a0 100644 --- a/larpe/trunk/larpe/Defaults.py +++ b/larpe/trunk/larpe/Defaults.py @@ -7,4 +7,3 @@ WEB_ROOT = 'larpe' APACHE_MAIN_VHOST = '/etc/apache2/sites-available/apache2-vhost-larpe' APACHE_VHOST_COMMON = '/etc/larpe/apache2-vhost-larpe-common' APACHE_RELOAD = '/usr/sbin/larpe-reload-apache2' -OUTPUT_FILTER_BASE = '/usr/share/larpe/output_filter_base.py' diff --git a/larpe/trunk/larpe/admin/hosts.ptl b/larpe/trunk/larpe/admin/hosts.ptl index 72a07ce..61ce9ac 100644 --- a/larpe/trunk/larpe/admin/hosts.ptl +++ b/larpe/trunk/larpe/admin/hosts.ptl @@ -16,11 +16,11 @@ from larpe import site_authentication from larpe import errors from larpe import misc from larpe.hosts import Host +from larpe.admin.apache import Location from larpe.admin.liberty_utils import * -#from larpe.filter import filter_misc from larpe.admin.apache import write_apache2_vhosts from larpe.admin.forms_prefill import FormsDirectory -from larpe.Defaults import OUTPUT_FILTER_BASE +from larpe.Defaults import DATA_DIR from larpe.plugins import site_authentication_plugins def check_basic_configuration(form): @@ -795,32 +795,33 @@ POST request. You can desactivate some or all of them, or change their value.''' self.host.apache_python_paths = [] self.host.apache_output_python_filters = [] site_auth = site_authentication.get_site_authentication(self.host) - filters = site_auth.get_filters() + output_filters = site_auth.output_filters replace_login_form = self.host.auth_form_places == 'form_everywhere' and \ self.host.auth_form_action - if filters or replace_login_form: + if replace_login_form and not 'output_replace_form' in output_filters: + output_filters.append('output_replace_form') + if output_filters: + print output_filters + location = Location(self.host) + conf = { 'auth_form_action': self.host.auth_form_action, + 'name': self.host.name, + 'larpe_dir': get_publisher().app_dir, + 'logout_url': location.new_logout_url, + 'login_url': location.new_auth_url } # Set Python filter path for Apache configuration python_path = os.path.join(self.host.site_dir, 'filters') if python_path not in self.host.apache_python_paths: self.host.apache_python_paths.append(python_path) - # Write Python filter - python_file = open(os.path.join(self.host.site_dir, 'filters', 'output_replace_form.py'), 'w') - python_file.write(open(OUTPUT_FILTER_BASE).read()) - python_file.write("def filter_page(filter, page):\n") - if replace_login_form: - python_file.write('''\ - current_form = re.compile('
]*?action="%(auth_form_action)s".*?>.*?
', re.DOTALL) - page = current_form.sub('
', page)''' - % { 'auth_form_action': self.host.auth_form_action, 'name': self.host.name }) - for filter in filters: - python_file.write(" r = re.compile(r\"\"\"%s\"\"\")\n" % filter["re"]) - python_file.write(" page = r.sub(r\"\"\"%s\"\"\", page)\n" % filter["sub"]) - python_file.write(" return page\n") - python_file.close() - # Set Python filter for Apache configuration - if not 'output_replace_form' in self.host.apache_output_python_filters: - self.host.apache_output_python_filters.append('output_replace_form') + for filter in output_filters: + python_file = open( + os.path.join(self.host.site_dir, 'filters', filter + ".py"), + 'w') + python_file.write( + open(os.path.join(DATA_DIR, "filters", filter + ".py")).read() % conf + ) + if not filter in self.host.apache_output_python_filters: + self.host.apache_output_python_filters.append(filter) def sso_init_link [html] (self): form = self.form_sso_init_link() diff --git a/larpe/trunk/larpe/plugins/site_authentication/ciril_net_rh.py b/larpe/trunk/larpe/plugins/site_authentication/ciril_net_rh.py index 8a00a22..8e42186 100644 --- a/larpe/trunk/larpe/plugins/site_authentication/ciril_net_rh.py +++ b/larpe/trunk/larpe/plugins/site_authentication/ciril_net_rh.py @@ -8,6 +8,7 @@ from larpe.site_authentication import SiteAuthentication class CirilSiteAuthentication(SiteAuthentication): plugin_name = 'ciril' + output_filters = ['output_ciril_net_rh'] def auto_detect_site(cls, html_doc): if re.search( @@ -36,19 +37,6 @@ class CirilSiteAuthentication(SiteAuthentication): return success, return_content - def get_filters(self): - """ Rewrite the logout link """ - filters = [] - logout = {} - if not hasattr(self.host, 'base_url'): - return None - base_url_tokens = self.host.base_url.split('/') - base_url_tokens[-1] = 'logout' - new_logout_url = '/'.join(base_url_tokens) - logout["re"] = """(D.*?connexion)""" - logout["sub"] = r"\1%s\2" % new_logout_url - filters.append(logout) - return filters site_authentication_plugins.register(CirilSiteAuthentication) diff --git a/larpe/trunk/larpe/site_authentication.ptl b/larpe/trunk/larpe/site_authentication.ptl index 638a53a..cbea782 100644 --- a/larpe/trunk/larpe/site_authentication.ptl +++ b/larpe/trunk/larpe/site_authentication.ptl @@ -26,8 +26,10 @@ from users import User from federations import Federation class SiteAuthentication: + def __init__(self, host): self.host = host + self.output_filters = [] def federate(self, username, password, provider_id, cookies, select): user = get_session().get_user(provider_id) @@ -105,18 +107,6 @@ class SiteAuthentication: value = values[0], options = options) return form - def get_filters(self): - """ Allows to filters the page - Example with a logout link: - filters = [] - filter = {} - filer["re"] = "Logout" - filer["sub"] = "Logout" % logout_url - filters.append(filter) - return filters - """ - return list() - def submit_local_auth_form(self, form): username = form.get_widget('username').parse() password = form.get_widget('password').parse() diff --git a/larpe/trunk/setup.py b/larpe/trunk/setup.py index f95db53..d2778ca 100644 --- a/larpe/trunk/setup.py +++ b/larpe/trunk/setup.py @@ -1,4 +1,4 @@ -#! /usr/bin/env python +#!/usr/bin/env python import os import distutils.core