def __init__(self, login): global ldapcon global basedn if basedn == '': basedn = cfg.get('ldap', 'basedn') if ldapcon is None: ldapuri = cfg.get('ldap', 'uri') try: ldapcon = self.ldap_obj = LDAP(ldapuri) except Exception as e: raise Exception('Failed to bind to Ldap: %s' % e) try: self._data = [] search = 'uid=%s' % str(login).strip() self._data = ldapcon.search_s(basedn, ldap.SCOPE_SUBTREE, search) except Exception as e: raise self.LDAPException("Got error in search: '%s' '%s'<br/>%s" % (basedn, search, e)) if not self._data: raise self.LDAPException("%s not known" % login) self.index = 0 self.iterator = False self.unique = len(self._data) == 1
def linklint_handler(req, query, messages): path = req.filename req.filename = content_dir + "validator/linklint.pyl" """new version of py_handler() to work with PSP templates""" # get string from our customized handler() fakereq = DummyPage(req) f = open(req.filename, "r") filepath, filename = os.path.split(req.filename) oldpath = sys.path sys.path.append(filepath) #Allow files to import from their directory - bad? module = imp.load_source(os.path.splitext(filename)[0], req.filename, f) sys.path = oldpath module.handler(fakereq, query, path) user_content = fakereq.get_page() title = ("%s | Linklint Check - " % cfg.get('core', 'rg_abbrev').upper()) + path breadcrumbs = get_breadcrumbs(req) data = dict(cfg=cfg, psp=psp, breadcrumbs=breadcrumbs, title=title, user_content=user_content, nav_urls=nav_urls, messages=messages) template = psp.PSP(req, filename=cfg.get('core', 'template_file')) template.run(data) f.close() return apache.OK
def edit_handler(req, query, messages): req.content_type = "text/html" if not os.path.isfile(req.filename) and \ not (req.filename.endswith('_') and os.path.isfile(req.filename[:-1])): req.send_http_header() return apache.HTTP_NOT_FOUND if req.filename.endswith('_'): req.send_http_header() return psp.handler(req) user_content = '' if not os.path.isfile(req.filename) or os.path.islink(req.filename) or \ not (req.filename.endswith('.pml') or req.filename.endswith('.html')): sorry_text = """ <html><head><title>Can't edit this file | %s </title></head><body><h1>Can't edit this content</h1> <p>To edit underlay or python code, ssh to the site</p> </body></html> """ % cfg.get('core', 'rg_abbrev').upper() req.write(sorry_text) return apache.HTTP_FORBIDDEN f = open(req.filename, 'r') title_re = re.search(r"\<title\>(.*)\<\/title\>", f.read()) f.close() if title_re: title = "Edit %s | %s " % (title_re.groups()[0], cfg.get('core', 'rg_abbrev').upper()) else: title = "Edit %s | %s " % (req.filename, cfg.get('core', 'rg_abbrev').upper()) # See if page is requesting custom banner banner_img = None f = open(req.filename, 'r') banner_re = re.search(r"\<banner\>(.*)\<\/banner\>", f.read()) f.close() if banner_re: banner_img = banner_re.groups()[0] breadcrumbs = get_breadcrumbs(req) f = open(req.filename, "r") user_content = f.read() f.close() data = dict(psp=psp, breadcrumbs=breadcrumbs, title=title, banner_img=banner_img, user_head_content='', user_content=user_content, cfg=cfg, nav_urls=nav_urls, messages=messages) template_file = cfg.get('core', 'template_file_edit') template = psp.PSP(req, filename=template_file) template.run(data) return apache.OK
def get_news_elements(item_count=6, max_title_len=80, max_story_len=350): # Get news file, surround it by parent tags, and then join together basedir = cfg.get('core', "base_dir") news = ["<xml>"] news.extend(readfile(os.path.join(basedir, "content/news/home.pml"))) news.append("</xml>") news = "\n".join(news) # Preprocess string for ultra-conservative parser news = re.sub(r"&\S*;", " ", news) news = news.replace("<br>", "<br />") parser = etree.HTMLParser() tree = etree.parse(StringIO(news), parser) news_items = [] for entry in tree.iter('tr'): # Go through each row for title in entry.iter('th'): # Titles txt = "".join(list(title.itertext())) txt = txt[:max_title_len-3] + "..." if len(txt) > max_title_len else txt for body in entry.iter('td'): # Stories txt = "".join(list(body.itertext())) txt = txt[:max_story_len-3] + "..." if len(txt) > max_story_len else txt news_items.append(txt.strip().replace('\n', ' ')) if len(news_items) >= item_count * 2: break news_elements = [news_items[i:i+2] for i in range(0, len(news_items), 2)] return news_elements
def save_handler(req, query, messages): path = req.filename content_dir = cfg.get('core', 'content_dir') length = int(req.headers_in['content-length']) postvars = parse_qs(req.read(length), keep_blank_values=1) if b'content' not in postvars: return apache.HTTP_NOT_FOUND # Editor adds <p> </p> if file starts with a blank line or a comment user_content = postvars[b'content'].pop().decode('utf-8').replace(r'<p> </p>', '', 1) tidy = Tidy() OPTIONS = { 'wrap': 72, 'hide-comments' : 'no', 'preserve-entities': 'yes', 'char-encoding' : 'utf8', 'indent-attributes' : 'yes', } doc = '<!DOCTYPE html><head><title>foo</title></head><body>' + user_content + '</body></html>' doc, errs = tidy.tidy_document(doc, options=OPTIONS) if errs is not '': req.write("<strong>Errors found</strong><br/><pre>") req.write(cgi.escape(errs)) req.write("</pre>") return apache.OK doc = re.sub(r'^.*<body>(.*)</body>.*', r'\1', doc, flags=re.DOTALL) doc = re.sub(r'^ ', '', doc, flags=re.MULTILINE) if path.startswith(content_dir): filename = path + ".new" f = open(filename, "w") f.write(doc) f.close() os.rename(filename, path) os.chmod(path, 0o664) else: req.write("<strong>Attempt to update a file not in the web directory</strong>") return apache.HTTP_FORBIDDEN if 'save' in query: del query['save'] req.args = req.args.replace('?save', '') return psp_handler(req, query, messages)
def req_to_title(req): basedir = cfg.get('core', "base_dir") # Convert a request to a guess at what the title should be parse_filename = req.filename[len(basedir):-4] if parse_filename.endswith("home"): parse_filename = parse_filename[:-5] parts = parse_filename.split("/") title = "" if len(parts) > 1: title = "%s - %s -" % (parts[-2], parts[-1]) elif len(parts) > 0: title = "%s -" % parts[0] if title == " -": title = "" if title == " - -": title = "" return title
def search_handler(req, query, messages): path = os.path.dirname(req.filename) fakereq = DummyPage(req) f = open(req.filename, "r") filepath, filename = os.path.split(req.filename) oldpath = sys.path sys.path.append(filepath) #Allow files to import from their directory - bad? module = imp.load_source(os.path.splitext(filename)[0], req.filename, f) sys.path = oldpath module.handler(fakereq, query, path) user_content = fakereq.get_page() title = module.get_title(req, query, path) breadcrumbs = get_breadcrumbs(req) data = dict(cfg=cfg, psp=psp, breadcrumbs=breadcrumbs, title=title, user_content=user_content, nav_urls=nav_urls, messages=messages) template = psp.PSP(req, filename=cfg.get('core', 'template_file')) template.run(data) return apache.OK
# Run a link checker over the live website. # Send email on error # from ertos_config import cfg import smtplib import os from email.mime.text import MIMEText from subprocess import run import sys sys.path.append( os.path.join(os.path.dirname(os.path.abspath(__file__)), "../code")) cfg.read('config.cfg') mailhost = cfg.get('hosts', 'mailhost') RECIPIENTS = ['webmaster@%s' % cfg.get('hosts', 'maildomain')] CONTENTROOT = cfg.get('core', 'content_dir') OUTPUTDIR = '%s/linklint' % CONTENTROOT URL = "https://%s/" % cfg.get('hosts', 'live') URL1 = "https://docs.%s/" % cfg.get('hosts', 'live') linklint = run([ '/usr/bin/linkchecker', '-F', 'html/%s/index.html' % OUTPUTDIR, '--no-status', '--ignore-url="print$"' '--ignore-url="/blob/"' '--check-extern', URL, URL1 ], encoding='utf-8', capture_output=True)
def handler(req): import time start = time.time() if not os.path.isfile(req.filename): return apache.HTTP_NOT_FOUND # reads config and set some global variables global base_dir, content_dir, nav_urls #cfg.read(req.get_options()['site_config']) base_dir = cfg.get('core', 'base_dir') + "/" content_dir = cfg.get('core', 'content_dir') + "/" in_production = cfg.getboolean('core', 'production') d = os.path.dirname(req.filename) nav_urls = dir2list(content_dir) req.content_type = "text/html" if req.args is not None: query = cgi.parse_qs(req.args) else: query = {} # TODO: Make "messages" append to an error log instead messages = [] try: res = apache.OK if not in_production and req.args is not None: if 'linklint' in req.args: return linklint_handler(req, query, messages) if 'validate' in req.args: return validate_handler(req, query, messages) if 'save' in req.args: return save_handler(req, query, messages) if 'edit' in req.args: return edit_handler(req, query, messages) if req.filename.endswith('search/home.pyl'): res = search_handler(req, query, messages) elif req.filename.endswith(".pml") or req.filename.endswith('.psp'): res = psp_handler(req, query, messages) elif req.filename.endswith(".pyl"): res = pyl_handler(req, query, messages) elif req.filename.endswith(".pyx"): res = pyx_handler(req, query, messages) else: res = apache.HTTP_NOT_FOUND if req.content_type == "text/html": req.write("<!-- page generated in %s seconds -->" % str(time.time() - start)) return res except Exception as e: # cgitb should NEVER be used on the live public site # it can leak internal data like usernames and passwords formatted_lines = traceback.format_exc().splitlines() tb = "" for line in formatted_lines: tb += line +"\n" if not in_production: import cgitb #cgitb.enable() cgitb.Hook(file=req).handle() sorry_text = """<html><head><title>404 Not Found | %s | Data61</title></head><body><h1>404 Not Found</h1> <p>Some content in this page is currently unavailable or no longer exists.<br/> The webmaster has been notified. Please try again later.</p></body></html> """ % cfg.get('core', 'rg_abbrev').upper() req.write(sorry_text) if not in_production: req.write("<p>Traceback:</p>") req.write("<p>"+tb.replace("\n", "<br />")+"</p>") if in_production: # send webmasters a mail with the trace import smtplib, time from email.mime.text import MIMEText text = """ Problem with %s webpage at: %s:%s. Please check... Trace follows: %s Arguments (if any): %s mod-python hard working at %s """ % (cfg.get('core', 'rg_abbrev').upper(), req.server.server_hostname, req.filename, tb, str(query), time.strftime('%x %X %Z')) msg = MIMEText(text) md = cfg.get('hosts', 'maildomain') mx = cfg.get('hosts', 'mailhost') webmasters = ['webmaster@' + md] msg['Subject'] = '<%s Web> - Exception caught' % cfg.get('core', 'rg_abbrev').upper() msg['From'] = 'webmaster@' + md msg['To'] = 'webmaster@' + md s = smtplib.SMTP() s.connect(mx) s.sendmail(webmasters[0], webmasters, msg.as_string()) s.close() return apache.OK
def psp_handler(req, query, messages): req.content_type = "text/html; charset=utf-8" if not os.path.isfile(req.filename) and \ not (req.filename.endswith('_') and os.path.isfile(req.filename[:-1])): req.send_http_header() return apache.HTTP_NOT_FOUND # Debugging output. Needs PythonDebug in the apache conf. if req.filename.endswith('_'): req.send_http_header() return psp.handler(req) user_content = '' # trick PSP to give us a string for the user page # and pass it to the template fakereq = DummyPage(req) user_page = psp.PSP(fakereq, filename=req.filename) data = dict(cfg=cfg) try: user_page.run(data) except apache.SERVER_RETURN: req.page = fakereq.page req.status = fakereq.status return apache.OK user_content = fakereq.get_page() # some variables needed in our templates breadcrumbs = get_breadcrumbs(req) f = open(req.filename, "r") title_re = re.search(r"\<title\>(.*)\<\/title\>", f.read()) f.close() # If the user content was a tuple, it means we specified extra data for the <head> user_head_content = "" if type(user_content) is tuple: user_head_content = user_content[1] user_content = user_content[0] if cfg.has_option('core', 'titletemplate'): title = cfg.get('core', 'titletemplate') else: title = "%s | CSIRO's Data61" % cfg.get('core', 'rg_abbrev').upper() if title_re: title_end = title_re.end() title = "%s | %s" % (title_re.groups()[0], title) else: # Default to the ugly URL cur_url = req.filename[len(cfg.get('core', 'content_dir')):].strip('/') live_url = '%s' % cfg.get('hosts', 'url')+'/'+cur_url live_url = cur_url # Set the title title = "%s | %s" % (title, live_url) # See if page is requesting custom banner banner_img = None f = open(req.filename, "r") banner_re = re.search(r"\<banner\>(.*)\<\/banner\>", f.read()) f.close() if banner_re: banner_img = banner_re.groups()[0] data = dict(psp=psp, breadcrumbs=breadcrumbs, title=title, banner_img=banner_img, user_head_content=user_head_content, user_content=user_content, cfg=cfg, nav_urls=nav_urls, messages=messages) template_file = cfg.get('core', 'template_file') if 'media' in query: if query['media'][0] == 'print': template_file = cfg.get('core', 'template_file_print') try: template = psp.PSP(req, filename=template_file) except Exception as e: req.send_http_header() req.write("Template prep failed, $s\n", str(e)) traceback.print_exc() return apache.OK req.send_http_header() try: template.run(data) except Exception as e: req.write("template run failed, %s\n" % str(e)) traceback.print_exc() return apache.OK
def pyl_handler(req, query, messages): """new version of py_handler() to work with PSP templates""" # get string from our customized handler() fakereq = DummyPage(req) fakereq.user = req.user try: f = open(req.filename, "r") filepath, filename = os.path.split(req.filename) oldpath = sys.path sys.path.append(filepath) #Allow files to import from their directory - bad? module = imp.load_source(os.path.splitext(filename)[0], req.filename, f) except: req.status = apache.HTTP_NOT_FOUND return apache.OK sys.path = oldpath status = module.handler(fakereq, query) user_content = fakereq.get_page() # if this is a file download, then don't spit out template if "Content-Disposition" in req.headers_out: return apache.OK if cfg.has_option('core', 'titletemplate'): title = cfg.get('core', 'titletemplate') else: title = "%s | seL4" % cfg.get('core', 'rg_abbrev').upper() if "get_title" in dir(module): title = module.get_title(req) + " | " + title usr_head_content = "" if "get_head" in dir(module): usr_head_content = module.get_head(req) breadcrumbs = get_breadcrumbs(req) data = dict(cfg=cfg, psp=psp, breadcrumbs=breadcrumbs, title=title, user_content=user_content, user_head_content=usr_head_content, nav_urls=nav_urls, messages=messages) template_file = cfg.get('core', 'template_file') if 'media' in query: if query['media'][0] == 'print': template_file = cfg.get('core', 'template_file_print') if status != 0: req.status = status try: template = psp.PSP(req, filename=template_file) except Exception as e: req.write("psp failed: %s" % str(e)) return apache.OK req.content_type = "text/html; charset=utf-8" try: template.run(data) except apache.SERVER_RETURN: req.page = fakereq.page req.status = fakereq.status return apache.OK except Exception as e: req.write("template run failed: %s" % str(e)) return apache.OK