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 10KB

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