1
|
import re
|
2
|
import os
|
3
|
import pickle
|
4
|
|
5
|
from larpe import sessions
|
6
|
from mod_python import Cookie
|
7
|
|
8
|
def is_auth_ok(req):
|
9
|
""" Test if you are authenticate on the Larpe server """
|
10
|
cookies = Cookie.get_cookies(req, Cookie.MarshalCookie, secret='secret007')
|
11
|
sessions_dir = os.path.join("%(larpe_dir)s", "sessions")
|
12
|
for name, cookie in cookies.iteritems():
|
13
|
value = cookie.value.replace('"', '')
|
14
|
if "larpe-" in name and value in os.listdir(sessions_dir):
|
15
|
try:
|
16
|
file = open(os.path.join(sessions_dir, value), "rb")
|
17
|
session = pickle.load(file)
|
18
|
if not session.users or not session.id:
|
19
|
return False
|
20
|
return True
|
21
|
except Exception, err:
|
22
|
return False
|
23
|
return False
|
24
|
|
25
|
|
26
|
def filter_page(filter, page):
|
27
|
r = re.compile(r"""(<a.*?href=["']).*?(["'].*?>D.*?connexion</a>)""")
|
28
|
page = r.sub(r"""\1%(logout_url)s\2""", page)
|
29
|
return page
|
30
|
|
31
|
def outputfilter(filter):
|
32
|
""" Apache called this function by default """
|
33
|
if not re.search("^/liberty/.*", filter.req.uri) and not is_auth_ok(filter.req):
|
34
|
filter.write('<meta http-equiv="refresh" content="0; url=%(login_url)s" />')
|
35
|
filter.close()
|
36
|
return
|
37
|
|
38
|
if filter.req.content_type is not None:
|
39
|
is_html = re.search('text/html', filter.req.content_type)
|
40
|
if filter.req.content_type is None or not is_html:
|
41
|
filter.pass_on()
|
42
|
else:
|
43
|
if not hasattr(filter.req, 'temp_doc'):
|
44
|
# Create a new attribute to hold the document
|
45
|
filter.req.temp_doc = []
|
46
|
# If content-length ended up wrong, Gecko browsers truncated data
|
47
|
if 'Content-Length' in filter.req.headers_out:
|
48
|
del filter.req.headers_out['Content-Length']
|
49
|
|
50
|
temp_doc = filter.req.temp_doc
|
51
|
s = filter.read()
|
52
|
# Could get '' at any point, but only get None at end
|
53
|
while s:
|
54
|
temp_doc.append(s)
|
55
|
s = filter.read()
|
56
|
|
57
|
# The end
|
58
|
if s is None:
|
59
|
page = ''.join(temp_doc)
|
60
|
page = filter_page(filter, page)
|
61
|
filter.write(page)
|
62
|
filter.close()
|
63
|
|