A Python content management system designed for kirsle.net featuring a blog, comments and photo albums. https://rophako.kirsle.net/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals, print_function, absolute_import
  3. from flask import (g, session, request, render_template, flash, redirect,
  4. url_for, current_app)
  5. from functools import wraps
  6. import codecs
  7. import uuid
  8. import datetime
  9. import time
  10. import re
  11. import importlib
  12. import smtplib
  13. import markdown
  14. import json
  15. try:
  16. import urlparse
  17. except ImportError:
  18. from urllib import parse as urlparse
  19. import traceback
  20. from email.mime.multipart import MIMEMultipart
  21. from email.mime.text import MIMEText
  22. from rophako.log import logger
  23. from rophako.settings import Config
  24. def login_required(f):
  25. """Wrapper for pages that require a logged-in user."""
  26. @wraps(f)
  27. def decorated_function(*args, **kwargs):
  28. if not g.info["session"]["login"]:
  29. session["redirect_url"] = request.url
  30. flash("You must be logged in to do that!")
  31. return redirect(url_for("account.login"))
  32. return f(*args, **kwargs)
  33. return decorated_function
  34. def admin_required(f):
  35. """Wrapper for admin-only pages. Implies login_required."""
  36. @wraps(f)
  37. def decorated_function(*args, **kwargs):
  38. if not g.info["session"]["login"]:
  39. # Not even logged in?
  40. session["redirect_url"] = request.url
  41. flash("You must be logged in to do that!")
  42. return redirect(url_for("account.login"))
  43. if g.info["session"]["role"] != "admin":
  44. logger.warning("User tried to access an Admin page, but wasn't allowed!")
  45. return redirect(url_for("index"))
  46. return f(*args, **kwargs)
  47. return decorated_function
  48. def ajax_response(status, msg):
  49. """Return a standard JSON response."""
  50. status = "ok" if status else "error"
  51. return json.dumps(dict(
  52. status=status,
  53. msg=msg,
  54. ))
  55. def template(name, **kwargs):
  56. """Render a template to the browser."""
  57. html = render_template(name, **kwargs)
  58. # Get the elapsed time for the request.
  59. time_elapsed = "%.03f" % (time.time() - g.info["time"])
  60. html = re.sub(r'\%time_elapsed\%', time_elapsed, html)
  61. return html
  62. def markdown_template(path):
  63. """Render a Markdown page to the browser.
  64. The first line in the Markdown page should be an H1 header beginning with
  65. the # sign. This will set the page's <title> to match the header value.
  66. Pages can include lines that begin with the keyword `:meta` to apply
  67. meta information to control the Markdown parser. Supported meta lines
  68. and examples:
  69. To 'blacklist' extensions, i.e. to turn off line breaks inside a paragraph
  70. getting translated into a <br> tag (the key is the minus sign):
  71. :meta extensions -nl2br
  72. To add an extension, i.e. the abbreviations from PHP Markdown Extra:
  73. :meta extensions abbr"""
  74. # The path is the absolute path to the Markdown file, so open it directly.
  75. fh = codecs.open(path, "r", "utf-8")
  76. body = fh.read()
  77. fh.close()
  78. # Look for meta information in the file.
  79. lines = body.split("\n")
  80. content = list() # New set of lines, without meta info.
  81. extensions = set()
  82. blacklist = set() # Blacklisted extensions
  83. for line in lines:
  84. if line.startswith(":meta"):
  85. parts = line.split(" ")
  86. if len(parts) >= 3:
  87. # Supported meta commands.
  88. if parts[1] == "extensions":
  89. # Extension toggles.
  90. for extension in parts[2:]:
  91. if extension.startswith("-"):
  92. extension = extension[1:]
  93. blacklist.add(extension)
  94. else:
  95. extensions.add(extension)
  96. else:
  97. content.append(line)
  98. # Extract a title from the first line.
  99. first = content[0]
  100. if first.startswith("#"):
  101. first = first[1:].strip()
  102. rendered = render_markdown("\n".join(content),
  103. extensions=extensions,
  104. blacklist=blacklist,
  105. )
  106. return template("markdown.inc.html",
  107. title=first,
  108. markdown=rendered,
  109. )
  110. def render_markdown(body, html_escape=True, extensions=None, blacklist=None):
  111. """Render a block of Markdown text.
  112. This will default to escaping literal HTML characters. Set
  113. `html_escape=False` to trust HTML.
  114. * extensions should be a set() of extensions to add.
  115. * blacklist should be a set() of extensions to blacklist."""
  116. args = dict(
  117. lazy_ol=False, # If a numbered list starts at e.g. 4, show the <ol> there
  118. extensions=[
  119. "fenced_code", # GitHub style code blocks
  120. "tables", # http://michelf.ca/projects/php-markdown/extra/#table
  121. "smart_strong", # Handles double__underscore better.
  122. "codehilite", # Code highlighting with Pygment!
  123. "nl2br", # Line breaks inside a paragraph become <br>
  124. "sane_lists", # Make lists less surprising
  125. ],
  126. extension_configs={
  127. "codehilite": {
  128. "linenums": False,
  129. }
  130. }
  131. )
  132. if html_escape:
  133. args["safe_mode"] = "escape"
  134. # Additional extensions?
  135. if extensions is not None:
  136. for ext in extensions:
  137. args["extensions"].append(ext)
  138. if blacklist is not None:
  139. for ext in blacklist:
  140. args["extensions"].remove(str(ext))
  141. return u'<div class="markdown">{}</div>'.format(
  142. markdown.markdown(body, **args)
  143. )
  144. def send_email(to, subject, message, sender=None, reply_to=None):
  145. """Send an e-mail out."""
  146. if sender is None:
  147. sender = Config.mail.sender
  148. if type(to) != list:
  149. to = [to]
  150. logger.info("Send email to {}".format(to))
  151. if Config.mail.method == "smtp":
  152. # Send mail with SMTP.
  153. for email in to:
  154. msg = MIMEMultipart("alternative")
  155. msg.set_charset("utf-8")
  156. msg["Subject"] = subject
  157. msg["From"] = sender
  158. msg["To"] = email
  159. if reply_to is not None:
  160. msg["Reply-To"] = reply_to
  161. text = MIMEText(message, "plain", "utf-8")
  162. msg.attach(text)
  163. # Send the e-mail.
  164. try:
  165. server = smtplib.SMTP(Config.mail.server, Config.mail.port)
  166. server.sendmail(sender, [email], msg.as_string())
  167. except:
  168. pass
  169. def handle_exception(error):
  170. """Send an e-mail to the site admin when an exception occurs."""
  171. if current_app.config.get("DEBUG"):
  172. print(traceback.format_exc())
  173. raise
  174. import rophako.jsondb as JsonDB
  175. # Don't spam too many e-mails in a short time frame.
  176. cache = JsonDB.get_cache("exception_catcher")
  177. if cache:
  178. last_exception = int(cache)
  179. if int(time.time()) - last_exception < 120:
  180. # Only one e-mail per 2 minutes, minimum
  181. logger.error("RAPID EXCEPTIONS, DROPPING")
  182. return
  183. JsonDB.set_cache("exception_catcher", int(time.time()))
  184. username = "anonymous"
  185. try:
  186. if hasattr(g, "info") and "session" in g.info and "username" in g.info["session"]:
  187. username = g.info["session"]["username"]
  188. except:
  189. pass
  190. # Get the timestamp.
  191. timestamp = time.ctime(time.time())
  192. # Exception's traceback.
  193. error = str(error.__class__.__name__) + ": " + str(error)
  194. stacktrace = error + "\n\n" \
  195. + "==== Start Traceback ====\n" \
  196. + traceback.format_exc() \
  197. + "==== End Traceback ====\n\n" \
  198. + "Request Information\n" \
  199. + "-------------------\n" \
  200. + "Address: " + remote_addr() + "\n" \
  201. + "User Agent: " + request.user_agent.string + "\n" \
  202. + "Referrer: " + request.referrer
  203. # Construct the subject and message
  204. subject = "Internal Server Error on {} - {} - {}".format(
  205. Config.site.site_name,
  206. username,
  207. timestamp,
  208. )
  209. message = "{} has experienced an exception on the route: {}".format(
  210. username,
  211. request.path,
  212. )
  213. message += "\n\n" + stacktrace
  214. # Send the e-mail.
  215. send_email(
  216. to=Config.site.notify_address,
  217. subject=subject,
  218. message=message,
  219. )
  220. def generate_csrf_token():
  221. """Generator for CSRF tokens."""
  222. if "_csrf" not in session:
  223. session["_csrf"] = str(uuid.uuid4())
  224. return session["_csrf"]
  225. def include(endpoint, *args, **kwargs):
  226. """Include another sub-page inside a template."""
  227. # The 'endpoint' should be in the format 'module.function', i.e. 'blog.index'.
  228. module, function = endpoint.split(".")
  229. # Dynamically import the module and call its function.
  230. m = importlib.import_module("rophako.modules.{}".format(module))
  231. html = getattr(m, function)(*args, **kwargs)
  232. return html
  233. def remote_addr():
  234. """Retrieve the end user's remote IP address. If the site is configured
  235. to honor X-Forwarded-For and this header is present, it's returned."""
  236. if Config.security.use_forwarded_for:
  237. return request.access_route[0]
  238. return request.remote_addr
  239. def server_name():
  240. """Get the server's hostname."""
  241. urlparts = list(urlparse.urlparse(request.url_root))
  242. return urlparts[1]
  243. def pretty_time(time_format, unix):
  244. """Pretty-print a time stamp."""
  245. date = datetime.datetime.fromtimestamp(unix)
  246. return date.strftime(time_format)
  247. def sanitize_name(name):
  248. """Sanitize a name that may be used in the filesystem.
  249. Only allows numbers, letters, and some symbols."""
  250. return re.sub(r'[^A-Za-z0-9 .\-_]+', '', name)