diff options
Diffstat (limited to 'cgi/post.py')
-rw-r--r-- | cgi/post.py | 226 |
1 files changed, 118 insertions, 108 deletions
diff --git a/cgi/post.py b/cgi/post.py index 3b836bb..6f7ff03 100644 --- a/cgi/post.py +++ b/cgi/post.py @@ -4,8 +4,7 @@ import os import shutil import time import threading -import Queue -import _mysql +import queue import formatting import logging @@ -34,10 +33,16 @@ class Post(object): "thumb_width": 0, "thumb_height": 0, "ip": "", + "IS_DELETED": 0, "timestamp_formatted": "", "timestamp": 0, "bumped": 0, "locked": 0, + "last": 0, + "expires": 0, + "expires_formatted": "", + "expires_alert": 0, + "length": 0, } def __getitem__(self, key): @@ -51,20 +56,25 @@ class Post(object): def insert(self): logging.info("Insertando Post") + post_keys = [] + post_templates = [] post_values = [] - for key, value in self.post.iteritems(): + for key, value in self.post.items(): + post_keys.append(key) + if key == 'ip': - template = "INET6_ATON('%s')" + template = "INET6_ATON(%s)" else: - template = "'%s'" + template = "%s" - post_values.append(template % _mysql.escape_string(str(value))) + post_templates.append(template) + post_values.append(value) return InsertDb("INSERT INTO `posts` (`%s`) VALUES (%s)" % ( - "`, `".join(self.post.keys()), - ", ".join(post_values) - )) + "`, `".join(post_keys), + ", ".join(post_templates) + ), post_values) class RegenerateThread(threading.Thread): def __init__(self, threadid, request_queue): @@ -89,15 +99,17 @@ def threadNumReplies(post): """ board = Settings._.BOARD - num = FetchOne("SELECT COUNT(1) FROM `posts` WHERE `parentid` = '%s' AND `boardid` = '%s'" % (post, board['id']), 0) - return int(num[0])+1 + num = FetchOne("SELECT COUNT(1) AS 'count' FROM `posts` WHERE `parentid` = %s AND `boardid` = %s" % (post, board['id'])) + return int(num["count"]) + 1 def get_parent_post(post_id, board_id): - post = FetchOne("SELECT `id`, `email`, `message`, `locked`, `subject`, `timestamp`, `bumped`, `last`, `length` FROM `posts` WHERE `id` = %s AND `parentid` = 0 AND `IS_DELETED` = 0 AND `boardid` = %s LIMIT 1" % (post_id, board_id)) + post = FetchOne("SELECT `id`, `email`, `message`, `locked`, `subject`, `timestamp`, `bumped`, `last`, `length` FROM `posts` WHERE `id` = %s AND `parentid` = 0 AND `IS_DELETED` = 0 AND `boardid` = %s LIMIT 1", + (post_id, board_id)) + if post: return post else: - raise UserError, _("The ID of the parent post is invalid.") + raise UserError(_("The ID of the parent post is invalid.")) def getThread(postid=0, mobile=False, timestamp=0): board = Settings._.BOARD @@ -106,11 +118,10 @@ def getThread(postid=0, mobile=False, timestamp=0): database_lock.acquire() try: if timestamp: - cond = "`timestamp` = %s" % str(timestamp) + op_post = FetchOne("SELECT * FROM `posts` WHERE `timestamp` = %s AND `boardid` = %s AND parentid = 0 LIMIT 1", (timestamp, board["id"])) else: - cond = "`id` = %s" % str(postid) + op_post = FetchOne("SELECT * FROM `posts` WHERE `id` = %s AND `boardid` = %s AND parentid = 0 LIMIT 1", (postid, board["id"])) - op_post = FetchOne("SELECT IS_DELETED, email, file, file_size, id, image_height, image_width, message, name, subject, thumb, thumb_height, thumb_width, timestamp_formatted, tripcode, parentid, locked, expires, expires_alert, expires_formatted, timestamp FROM `posts` WHERE %s AND `boardid` = %s AND parentid = 0 LIMIT 1" % (cond, board["id"])) if op_post: op_post['num'] = 1 if mobile: @@ -119,7 +130,7 @@ def getThread(postid=0, mobile=False, timestamp=0): #thread = {"id": op_post["id"], "posts": [op_post], "omitted": 0, "omitted_img": 0} total_bytes += len(op_post["message"])+80 - replies = FetchAll("SELECT IS_DELETED, email, file, file_size, id, image_height, image_width, message, name, subject, thumb, thumb_height, thumb_width, timestamp_formatted, tripcode, parentid, locked, expires, expires_alert, expires_formatted, timestamp FROM `posts` WHERE `parentid` = %s AND `boardid` = %s ORDER BY `id` ASC" % (op_post["id"], board["id"])) + replies = FetchAll("SELECT IS_DELETED, email, file, file_size, id, image_height, image_width, message, name, subject, thumb, thumb_height, thumb_width, timestamp_formatted, tripcode, parentid, locked, expires, expires_alert, expires_formatted, timestamp FROM `posts` WHERE `parentid` = %s AND `boardid` = %s ORDER BY `id` ASC", (op_post["id"], board["id"])) thread["length"] = 1 if replies: for reply in replies: @@ -132,7 +143,7 @@ def getThread(postid=0, mobile=False, timestamp=0): total_bytes += len(reply["message"])+57 # An imageboard needs subject - if board["board_type"] in ['1', '5']: + if board["board_type"] in [1, 5]: thread["timestamp"] = op_post["timestamp"] thread["subject"] = op_post["subject"] thread["message"] = op_post["message"] @@ -141,6 +152,7 @@ def getThread(postid=0, mobile=False, timestamp=0): #threads = [thread] else: + raise Exception(postid) return None finally: database_lock.release() @@ -152,7 +164,7 @@ def getID(threadid, postnum): database_lock.acquire() try: - posts = FetchAll("SELECT id FROM `posts` WHERE `parentid`=%s AND `boardid`=%s ORDER BY `id` ASC" % (thread["id"], board["id"])) + posts = FetchAll("SELECT id FROM `posts` WHERE `parentid`= %s AND `boardid`= %s ORDER BY `id` ASC", (thread["id"], board["id"])) if posts: post = posts[int(postnum)-1] postid = post["id"] @@ -181,10 +193,6 @@ def shortenMsg(message, elid='0', elboard='0'): message_shortened += message_exploded[i] + '<br />' - #try: - message_shortened = message_shortened.decode('utf-8', 'replace') - #except: - if len(message_shortened) > limit: message_shortened = message_shortened[:limit] @@ -202,7 +210,7 @@ def threadUpdated(postid): """ # Use queues only if multithreading is enabled if Settings.USE_MULTITHREADING: - request_queue = Queue.Queue() + request_queue = queue.Queue() threads = [RegenerateThread(i, request_queue) for i in range(2)] for t in threads: t.start() @@ -226,7 +234,7 @@ def regenerateFrontPages(): """ board = Settings._.BOARD threads = [] - if board['board_type'] == '1': + if board['board_type'] == 1: threads_to_fetch = int(board['numthreads']) threads_to_limit = threads_to_fetch + 50 else: @@ -238,18 +246,16 @@ def regenerateFrontPages(): database_lock.acquire() try: # fetch necessary threads and calculate how many posts we need - allthreads_query = "SELECT id, timestamp, subject, locked, length FROM `posts` WHERE `boardid` = '%s' AND parentid = 0 AND IS_DELETED = 0 ORDER BY `bumped` DESC, `id` ASC LIMIT %d" % \ - (board["id"], threads_to_limit) - allthreads = FetchAll(allthreads_query) + allthreads_query = "SELECT id, timestamp, subject, locked, length FROM `posts` WHERE `boardid` = %s AND parentid = 0 AND IS_DELETED = 0 ORDER BY `bumped` DESC, `id` ASC LIMIT %s" + allthreads = FetchAll(allthreads_query, (board["id"], threads_to_limit)) posts_to_fetch = 0 for t in allthreads[:threads_to_fetch]: posts_to_fetch += int(t["length"]) more_threads = allthreads[threads_to_fetch:50] # get the needed posts for the front page and order them - posts_query = "SELECT * FROM `posts` WHERE `boardid` = '%s' ORDER BY `bumped` DESC, CASE parentid WHEN 0 THEN id ELSE parentid END ASC, `id` ASC LIMIT %d" % \ - (board["id"], posts_to_fetch) - posts = FetchAll(posts_query) + posts_query = "SELECT * FROM `posts` WHERE `boardid` = %s ORDER BY `bumped` DESC, CASE parentid WHEN 0 THEN id ELSE parentid END ASC, `id` ASC LIMIT %s" + posts = FetchAll(posts_query, (board["id"], posts_to_fetch)) threads = [] if posts: @@ -257,9 +263,9 @@ def regenerateFrontPages(): post_num = 0 for post in posts: - if post["parentid"] == '0': + if not post["parentid"]: skipThread = False - if post["IS_DELETED"] == '0': + if not post["IS_DELETED"]: # OP; Make new thread if thread is not None: thread["length"] = post_num @@ -284,7 +290,7 @@ def regenerateFrontPages(): is_omitted = False if len(threads) > 0: # Todo : Make this better - if board['board_type'] == '1': + if board['board_type'] == 1: page_count = 1 # Front page only threads_per_page = int(board['numthreads']) else: @@ -299,7 +305,7 @@ def regenerateFrontPages(): page_count = int(math.ceil(float(len(threads)) / float(int(board['numthreads'])))) threads_per_page = int(board['numthreads']) - for i in xrange(page_count): + for i in range(page_count): pages.append([]) start = i * threads_per_page end = start + threads_per_page @@ -351,7 +357,7 @@ def regeneratePage(page_num, page_count, threads, is_omitted=False, more_threads else: file_name = str(page_num) - if board['board_type'] == '1': + if board['board_type'] == 1: templatename = "txt_board.html" else: templatename = "board.html" @@ -384,7 +390,7 @@ def threadList(mode=0): maxthreads = 1000 cutFactor = 70 - if board['board_type'] == '1': + if board['board_type'] == 1: filename = "txt_threadlist.html" full_threads = FetchAll("SELECT id, timestamp, timestamp_formatted, subject, length, last FROM `posts` WHERE parentid = 0 AND boardid = %(board)s AND IS_DELETED = 0 ORDER BY `bumped` DESC LIMIT %(limit)s" \ % {'board': board["id"], 'limit': maxthreads}) @@ -397,7 +403,7 @@ def threadList(mode=0): # Generate threadlist timestamps = [] for thread in full_threads: - if board['board_type'] == '1': + if board['board_type'] == 1: thread["timestamp_formatted"] = thread["timestamp_formatted"].split(" ")[0] timestamps.append([thread["last"], formatTimestamp(thread["last"])]) if mobile: @@ -443,8 +449,8 @@ def threadList(mode=0): def catalog(sort=''): board = Settings._.BOARD - if board['board_type'] != '0': - raise UserError, "No hay catálogo disponible para esta sección." + if board['board_type'] != 0: + raise UserError("No hay catálogo disponible para esta sección.") cutFactor = 500 @@ -484,7 +490,7 @@ def regenerateThreadPage(postid): thread = getThread(postid) - if board['board_type'] in ['1', '5']: + if board['board_type'] in [1, 5]: template_filename = "txt_thread.html" outname = Settings.ROOT_DIR + board["dir"] + "/res/" + str(thread["timestamp"]) + ".html" title_matome = thread['subject'] @@ -510,14 +516,14 @@ def regenerateThreadPage(postid): def threadPage(postid, mobile=False, timestamp=0): board = Settings._.BOARD - if board['board_type'] in ['1', '5']: + if board['board_type'] in [1, 5]: template_filename = "txt_thread.html" else: template_filename = "board.html" thread = getThread(postid, mobile, timestamp) if not thread: - raise UserError, "El hilo no existe." + raise UserError("El hilo no existe.") return renderTemplate(template_filename, {"threads": [thread], "replythread": postid}, mobile) @@ -525,8 +531,8 @@ def dynamicRead(parentid, ranges, mobile=False): import re board = Settings._.BOARD - if board['board_type'] != '1': - raise UserError, "Esta sección no es un BBS y como tal no soporta lectura dinámica." + if board['board_type'] != 1: + raise UserError("Esta sección no es un BBS y como tal no soporta lectura dinámica.") # get entire thread template_fname = "txt_thread.html" @@ -539,10 +545,10 @@ def dynamicRead(parentid, ranges, mobile=False): import json with open(fname) as f: thread = json.load(f) - thread['posts'] = [dict(zip(thread['keys'], row)) for row in thread['posts']] + thread['posts'] = [dict(list(zip(thread['keys'], row))) for row in thread['posts']] template_fname = "txt_archive.html" else: - raise UserError, 'El hilo no existe.' + raise UserError('El hilo no existe.') filtered_thread = { "id": thread['id'], @@ -645,7 +651,7 @@ def dynamicRead(parentid, ranges, mobile=False): filtered_thread["posts"].insert(0, thread["posts"][0]) if not filtered_thread["posts"]: - raise UserError, "No hay posts que mostrar." + raise UserError("No hay posts que mostrar.") post_preview = cut_home_msg(filtered_thread["posts"][0]["message"], 0) @@ -663,7 +669,7 @@ def regenerateBoard(everything=False): # Use queues only if multithreading is enabled if Settings.USE_MULTITHREADING: - request_queue = Queue.Queue() + request_queue = queue.Queue() threads = [RegenerateThread(i, request_queue) for i in range(Settings.MAX_PROGRAM_THREADS)] for t in threads: t.start() @@ -694,61 +700,62 @@ def deletePost(postid, password, deltype='0', imageonly=False, quick=False): postid = int(postid) # get post - post = FetchOne("SELECT `id`, `timestamp`, `parentid`, `file`, `thumb`, `password`, `length` FROM `posts` WHERE `boardid` = %s AND `id` = %s LIMIT 1" % (board["id"], str(postid))) + post = FetchOne("SELECT `id`, `timestamp`, `parentid`, `file`, `thumb`, `password`, `length` FROM `posts` WHERE `boardid` = %s AND `id` = %s LIMIT 1", (board["id"], postid)) # abort if the post doesn't exist if not post: - raise UserError, _("There isn't a post with this ID. It was probably deleted.") + raise UserError(_("There isn't a post with this ID. It was probably deleted.")) if password: if password != post['password']: - raise UserError, "No tienes permiso para eliminar este mensaje." - elif post["parentid"] == '0' and int(post["length"]) >= Settings.DELETE_FORBID_LENGTH: - raise UserError, "No puedes eliminar un hilo con tantas respuestas." - elif (int(time.time()) - int(post["timestamp"])) > 86400: - raise UserError, "No puedes eliminar un post tan viejo." + raise UserError("No tienes permiso para eliminar este mensaje.") + elif not post["parentid"] and post["length"] >= Settings.DELETE_FORBID_LENGTH: + raise UserError("No puedes eliminar un hilo con tantas respuestas.") + elif (int(time.time()) - post["timestamp"]) > 86400: + raise UserError("No puedes eliminar un post tan viejo.") # just update the DB if deleting only the image, otherwise delete whole post if imageonly: if post["file"]: deleteFile(post) - UpdateDb("UPDATE `posts` SET `file` = '', `file_hex` = '', `thumb` = '', `thumb_width` = 0, `thumb_height` = 0 WHERE `boardid` = %s AND `id` = %s LIMIT 1" % (board["id"], str(post['id']))) + UpdateDb("UPDATE `posts` SET `file` = '', `file_hex` = '', `thumb` = '', `thumb_width` = 0, `thumb_height` = 0 WHERE `boardid` = %s AND `id` = %s LIMIT 1", (board["id"], post['id'])) else: - if int(post["parentid"]) == 0: + if not post["parentid"]: deleteReplies(post) logging.info("Deleting post " + str(postid)) - if deltype != '0' and post["parentid"] != '0': + if deltype != 0 and post["parentid"]: # Soft delete (recycle bin) - UpdateDb("UPDATE `posts` SET `IS_DELETED` = %s WHERE `boardid` = %s AND `id` = %s LIMIT 1" % (deltype, board["id"], post["id"])) + UpdateDb("UPDATE `posts` SET `IS_DELETED` = %s WHERE `boardid` = %s AND `id` = %s LIMIT 1", (deltype, board["id"], post["id"])) else: # Hard delete if post["file"]: deleteFile(post) - if post['parentid'] != '0': + if post['parentid']: numreplies = threadNumReplies(post["parentid"]) if numreplies > 2: - newlast = FetchOne('SELECT timestamp, email FROM posts WHERE boardid = %s AND parentid = %s AND timestamp < %d ORDER BY timestamp DESC LIMIT 1' % (board['id'], post['parentid'], int(post['timestamp']))) + newlast = FetchOne('SELECT timestamp, email FROM posts WHERE boardid = %s AND parentid = %s AND timestamp < %s ORDER BY timestamp DESC LIMIT 1', (board['id'], post['parentid'], post['timestamp'])) else: - newlast = FetchOne('SELECT timestamp FROM posts WHERE boardid = %s AND id = %s LIMIT 1' % (board['id'], post['parentid'])) + newlast = FetchOne('SELECT timestamp FROM posts WHERE boardid = %s AND id = %s LIMIT 1', (board['id'], post['parentid'])) - UpdateDb("DELETE FROM `posts` WHERE `boardid` = %s AND `id` = %s LIMIT 1" % (board["id"], post["id"])) + UpdateDb("DELETE FROM `posts` WHERE `boardid` = %s AND `id` = %s LIMIT 1", (board["id"], post["id"])) - if post['parentid'] != '0': - UpdateDb("UPDATE `posts` SET last = %s, length = %d WHERE `id` = '%s' AND `boardid` = '%s'" % (newlast["timestamp"], threadNumReplies(post["parentid"]), post["parentid"], board["id"])) + if post['parentid']: + UpdateDb("UPDATE `posts` SET last = %s, length = %s WHERE `id` = %s AND `boardid` = %s", + (newlast["timestamp"], threadNumReplies(post["parentid"]), post["parentid"], board["id"])) - if post['parentid'] == '0': - if board['board_type'] == '1': - os.unlink(Settings.ROOT_DIR + board["dir"] + "/res/" + post["timestamp"] + ".html") + if not post['parentid']: + if board['board_type'] == 1: + os.unlink(Settings.ROOT_DIR + str(board["dir"]) + "/res/" + str(post["timestamp"]) + ".html") else: - os.unlink(Settings.ROOT_DIR + board["dir"] + "/res/" + post["id"] + ".html") + os.unlink(Settings.ROOT_DIR + str(board["dir"]) + "/res/" + str(post["id"]) + ".html") regenerateHome() # rebuild thread and fronts if reply; rebuild only fronts if not - if post["parentid"] != '0': + if post["parentid"]: threadUpdated(post["parentid"]) else: regenerateFrontPages() @@ -757,12 +764,12 @@ def deleteReplies(thread): board = Settings._.BOARD # delete files first - replies = FetchAll("SELECT `parentid`, `file`, `thumb` FROM `posts` WHERE `boardid` = %s AND `parentid` = %s AND `file` != ''" % (board["id"], thread["id"])) + replies = FetchAll("SELECT `parentid`, `file`, `thumb` FROM `posts` WHERE `boardid` = %s AND `parentid` = %s AND `file` != ''", (board["id"], thread["id"])) for post in replies: deleteFile(post) # delete all replies from DB - UpdateDb("DELETE FROM `posts` WHERE `boardid` = %s AND `parentid` = %s" % (board["id"], thread["id"])) + UpdateDb("DELETE FROM `posts` WHERE `boardid` = %s AND `parentid` = %s", (board["id"], thread["id"])) def deleteFile(post): """ @@ -804,7 +811,7 @@ def trimThreads(): archived = False # Use limit of the board type - if board['board_type'] == '1': + if board['board_type'] == 1: limit = Settings.TXT_MAX_THREADS else: limit = Settings.MAX_THREADS @@ -815,36 +822,36 @@ def trimThreads(): alert_time = int(round(int(board['maxage']) * Settings.MAX_AGE_ALERT)) time_limit = t + (alert_time * 86400) - old_ops = FetchAll("SELECT `id`, `timestamp`, `expires`, `expires_alert`, `length` FROM `posts` WHERE `boardid` = %s AND `parentid` = 0 AND IS_DELETED = 0 AND `expires` > 0 AND `expires` < %s LIMIT 50" % (board['id'], time_limit)) + old_ops = FetchAll("SELECT `id`, `timestamp`, `expires`, `expires_alert`, `length` FROM `posts` WHERE `boardid` = %s AND `parentid` = 0 AND IS_DELETED = 0 AND `expires` > 0 AND `expires` < %s LIMIT 50", (board['id'], time_limit)) for op in old_ops: if t >= int(op['expires']): # Trim old threads - if board['archive'] == '1' and int(op["length"]) >= Settings.ARCHIVE_MIN_LENGTH: + if board['archive'] and op["length"] >= Settings.ARCHIVE_MIN_LENGTH: archiveThread(op["id"]) archived = True deletePost(op["id"], None) else: # Add alert to threads approaching deletion - UpdateDb("UPDATE `posts` SET expires_alert = 1 WHERE `boardid` = %s AND `id` = %s" % (board['id'], op['id'])) + UpdateDb("UPDATE `posts` SET expires_alert = 1 WHERE `boardid` = %s AND `id` = %s", (board['id'], op['id'])) # trim inactive threads next - if board['maxinactive'] != '0': + if board['maxinactive'] > 0: t = time.time() oldest_last = t - (int(board['maxinactive']) * 86400) - old_ops = FetchAll("SELECT `id`, `length` FROM `posts` WHERE `boardid` = %s AND `parentid` = 0 AND IS_DELETED = 0 AND `last` < %d LIMIT 50" % (board['id'], oldest_last)) + old_ops = FetchAll("SELECT `id`, `length` FROM `posts` WHERE `boardid` = %s AND `parentid` = 0 AND IS_DELETED = 0 AND `last` < %s LIMIT 50", (board['id'], oldest_last)) for op in old_ops: - if board['archive'] == '1' and int(op["length"]) >= Settings.ARCHIVE_MIN_LENGTH: + if board['archive'] and op["length"] >= Settings.ARCHIVE_MIN_LENGTH: archiveThread(op["id"]) archived = True deletePost(op["id"], None) # select trim type by board - if board['board_type'] == '1': + if board['board_type'] == 1: trim_method = Settings.TXT_TRIM_METHOD else: trim_method = Settings.TRIM_METHOD @@ -862,7 +869,7 @@ def trimThreads(): if len(op_posts) > limit: posts = op_posts[limit:] for post in posts: - if board['archive'] == '1' and int(op["length"]) >= Settings.ARCHIVE_MIN_LENGTH: + if board['archive'] and op["length"] >= Settings.ARCHIVE_MIN_LENGTH: archiveThread(post["id"]) archived = True @@ -879,7 +886,7 @@ def autoclose_thread(parentid, t, replies): board = Settings._.BOARD # decide the replylimit - if board['board_type'] == '1' and Settings.TXT_CLOSE_THREAD_ON_REPLIES > 0: + if board['board_type'] == 1 and Settings.TXT_CLOSE_THREAD_ON_REPLIES > 0: replylimit = Settings.TXT_CLOSE_THREAD_ON_REPLIES elif Settings.CLOSE_THREAD_ON_REPLIES > 0: replylimit = Settings.CLOSE_THREAD_ON_REPLIES @@ -929,7 +936,7 @@ def pageNavigator(page_num, page_count, is_omitted=False): pagenav += "</span><span>" - for i in xrange(page_count): + for i in range(page_count): if i == page_num: pagenav += "[<strong>%d</strong>]" % i else: @@ -965,18 +972,20 @@ def flood_check(t,post,boardid): #lastpost = FetchOne("SELECT COUNT(*) FROM `posts` WHERE `ip` = INET6_ATON('%s') and `parentid` = 0 and `boardid` = '%s' and IS_DELETED = 0 AND timestamp > %d" % (str(post["ip"]), boardid, int(maxtime)), 0) # NO MATTER THE IP - lastpost = FetchOne("SELECT COUNT(*) FROM `posts` WHERE `parentid` = 0 and `boardid` = '%s' and IS_DELETED = 0 AND timestamp > %d" % (boardid, int(maxtime)), 0) + lastpost = FetchOne("SELECT `timestamp` FROM `posts` WHERE `parentid` = 0 and `boardid` = %s and IS_DELETED = 0 AND timestamp > %s", + (boardid, maxtime)) else: maxtime = round(t - int(board['postsecs'])) - lastpost = FetchOne("SELECT COUNT(*) FROM `posts` WHERE `ip` = INET6_ATON('%s') and `parentid` != 0 and `boardid` = '%s' and IS_DELETED = 0 AND timestamp > %d" % (str(post["ip"]), boardid, int(maxtime)), 0) + lastpost = FetchOne("SELECT `timestamp` FROM `posts` WHERE `ip` = INET6_ATON(%s) and `parentid` != 0 and `boardid` = %s and IS_DELETED = 0 AND timestamp > %s", + (post["ip"], boardid, maxtime)) - if int(lastpost[0]): + if lastpost: if post["parentid"]: - raise UserError, _("Flood detected. Please wait a moment before posting again.") + raise UserError(_("Flood detected. Please wait a moment before posting again.")) else: - lastpost = FetchOne("SELECT `timestamp` FROM `posts` WHERE `parentid`=0 and `boardid`='%s' and IS_DELETED = 0 ORDER BY `timestamp` DESC" % (boardid), 0) - wait = int(int(board['threadsecs']) - (t - int(lastpost[0]))) - raise UserError, "Espera " + str(wait) + " segundos antes de crear otro hilo." + lastpost = FetchOne("SELECT `timestamp` FROM `posts` WHERE `parentid`= 0 and `boardid`= %s and IS_DELETED = 0 ORDER BY `timestamp` DESC", (boardid,)) + wait = int(int(board['threadsecs']) - (t - int(lastpost["timestamp"]))) + raise UserError("Espera " + str(wait) + " segundos antes de crear otro hilo.") def cut_home_msg(message, boardlength=0): short_message = message.replace("<br />", " ") @@ -985,17 +994,15 @@ def cut_home_msg(message, boardlength=0): limit = Settings.HOME_LASTPOSTS_LENGTH - boardlength if len(short_message) > limit: - if isinstance(short_message, unicode): - short_message = short_message[:limit].encode('utf-8') + "…" - else: - short_message = short_message.decode('utf-8')[:limit].encode('utf-8') + "…" + if isinstance(short_message, str): + short_message = short_message[:limit] + "…" short_message = re.compile(r"&(.(?!;))*$", re.DOTALL | re.IGNORECASE).sub("", short_message) # Removes incomplete HTML return short_message def getLastAge(board_type, limit): threads = [] - sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, bumped, last, length, thumb, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND posts.locked < 3 AND boards.board_type = %d ORDER BY bumped DESC LIMIT %d" % (board_type, limit) - threads = FetchAll(sql) + sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, bumped, last, length, thumb, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND posts.locked < 3 AND boards.board_type = %s ORDER BY bumped DESC LIMIT %s" + threads = FetchAll(sql, (board_type, limit)) for post in threads: post['id'] = int(post['id']) @@ -1015,8 +1022,8 @@ def getLastAge(board_type, limit): def getNewThreads(limit): threads = [] - sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, thumb, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND boards.id <> 34 AND boards.id <> 13 AND posts.locked = 0 ORDER BY timestamp DESC LIMIT %d" % (limit) - threads = FetchAll(sql) + sql = "SELECT posts.id, boards.name AS board_fulln, boards.subname AS board_name, board_type, boards.dir, timestamp, thumb, CASE WHEN posts.subject = boards.subject THEN posts.message ELSE posts.subject END AS content FROM posts INNER JOIN boards ON boardid = boards.id WHERE parentid = 0 AND IS_DELETED = 0 AND boards.secret = 0 AND boards.id <> 34 AND boards.id <> 13 AND posts.locked = 0 ORDER BY timestamp DESC LIMIT %s" + threads = FetchAll(sql, (limit,)) for post in threads: post['id'] = int(post['id']) @@ -1092,13 +1099,16 @@ def regenerateAccess(): if not Settings.HTACCESS_GEN: return False - bans = FetchAll("SELECT `ipstr`, `boards` FROM `bans` WHERE `blind` = '1' ORDER BY `ipstart` ASC") boards = FetchAll('SELECT `dir` FROM `boards`') global_boards = [board['dir'] for board in boards if board['dir'] not in Settings.EXCLUDE_GLOBAL_BANS] + bans = [] global_bans = [] board_bans = {} + + if Settings.ENABLE_BANS: + bans = FetchAll("SELECT `ipstr`, `boards` FROM `bans` WHERE `blind` = '1' ORDER BY `ipstart` ASC") for ban in bans: if ban["boards"]: @@ -1159,14 +1169,14 @@ def make_url(postid, post, parent_post, noko, mobile): if mobile: if not noko: url = Settings.CGI_URL + 'mobile/' + board["dir"] - elif board["board_type"] == '1': + elif board["board_type"] == 1: url = "%s/mobileread/%s/%s/l10#form" % (Settings.CGI_URL, board["dir"], parent_post['timestamp']) else: url = "%s/mobileread/%s/%s#%s" % (Settings.CGI_URL, board["dir"], parentid, postid) else: if not noko: url = Settings.BOARDS_URL + board["dir"] + "/" - elif board["board_type"] == '1': + elif board["board_type"] == 1: url = "%s/read/%s/l50#bottom" % (Settings.BOARDS_URL + board["dir"], str(parent_post['timestamp'])) else: url = "%s/res/%s.html#%s" % (Settings.BOARDS_URL + board["dir"], str(parentid), postid) @@ -1192,7 +1202,7 @@ def latestAdd(post, postnum, postid, parent_post): timestamp_formatted = datetime.datetime.fromtimestamp(post['timestamp']).strftime('%Y-%m-%dT%H:%M:%S%Z') parentid = parent_post['id'] if post['parentid'] else postid - if board['board_type'] == '1': + if board['board_type'] == 1: url = '/%s/read/%s/%d' % (board['dir'], (parent_post['timestamp'] if post['parentid'] else post['timestamp']), (postnum if postnum else 1)) else: url = '/%s/res/%s.html#%s' % (board['dir'], parentid, postid) @@ -1221,19 +1231,19 @@ def archiveThread(postid): with open(Settings.ROOT_DIR + board["dir"] + "/kako/" + str(thread['timestamp']) + ".json", "w") as f: json.dump(thread, f, indent=0) except: - raise UserError, "Can't archive: %s" % thread['timestamp'] + raise UserError("Can't archive: %s" % thread['timestamp']) UpdateDb("REPLACE INTO archive (id, boardid, timestamp, subject, length) VALUES ('%s', '%s', '%s', '%s', '%s')" % (thread['id'], board['id'], thread['timestamp'], _mysql.escape_string(thread['subject']), thread['length'])) def throw_dice(dice): qty = int(dice[0][1:]) if qty == 0: - raise UserError, "No tienes dados para lanzar." + raise UserError("No tienes dados para lanzar.") if qty > 100: qty = 100 sides = int(dice[1][1:]) if dice[1] else 6 if sides == 0: - raise UserError, "Tus dados no tienen caras." + raise UserError("Tus dados no tienen caras.") if sides > 100: sides = 100 @@ -1262,7 +1272,7 @@ def discord_hook(post, url): if not Settings.DISCORD_HOOK_URL: return - import urllib2 + import urllib.request, urllib.error, urllib.parse import json board = Settings._.BOARD @@ -1283,7 +1293,7 @@ def discord_hook(post, url): data = {"content": "test"} jsondata = json.dumps(data, separators=(',',':')) - opener = urllib2.build_opener() + opener = urllib.request.build_opener() #opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0')] response = opener.open(Settings.DISCORD_HOOK_URL, jsondata, 6) the_page = response.read() |