# coding=utf-8 import _mysql from database import * from framework import * from template import * from img import * from post import * from settings import Settings d_thread = {} d_post = {} def anarkia(self, path_split): setBoard('anarkia') if len(path_split) <= 2: self.output = main() return raise UserError('Ya fue, baisano...') if path_split[2] == 'opt': self.output = boardoptions(self.formdata) elif path_split[2] == 'mod': self.output = mod(self.formdata) elif path_split[2] == 'bans': self.output = bans(self.formdata) elif path_split[2] == 'css': self.output = css(self.formdata) elif path_split[2] == 'type': self.output = type(self.formdata) elif path_split[2] == 'emojis': self.output = emojis(self.formdata) else: raise UserError('ke?') def main(): board = Settings._.BOARD logs = FetchAll( "SELECT * FROM `logs` WHERE `staff` = 'Anarko' ORDER BY `timestamp` DESC") for log in logs: log['timestamp_formatted'] = formatTimestamp(log['timestamp']) return renderTemplate('anarkia.html', {'mode': 0, 'logs': logs}) def type(formdata): board = Settings._.BOARD if board['board_type'] == '1': (type_now, type_do, do_num) = ('BBS', 'IB', '0') else: (type_now, type_do, do_num) = ('IB', 'BBS', '1') if formdata.get('transform') == 'do': t = 0 try: with open('anarkia_time') as f: t = int(f.read()) except IOError: pass dif = time.time() - t if dif > (10 * 60): # if True: import re t = time.time() board['board_type'] = do_num board['force_css'] = Settings.HOME_URL + \ 'anarkia/style_' + type_do.lower() + '.css' updateBoardSettings() # update posts fix_board() # regenerate setBoard('anarkia') regenerateBoard(True) tf = timeTaken(t, time.time()) with open('anarkia_time', 'w') as f: t = f.write(str(int(time.time()))) msg = 'Cambiada estructura de sección a %s. (%s)' % (type_do, tf) logAction(msg) return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg}) else: raise UserError('Esta acción sólo se puede realizar cada 10 minutos. Faltan: %d mins.' % (10-int(dif/60))) return renderTemplate('anarkia.html', {'mode': 7, 'type_now': type_now, 'type_do': type_do}) def fix_board(): board = Settings._.BOARD get_fix_dictionary() if board['board_type'] == '1': to_fix = FetchAll( "SELECT * FROM posts WHERE message LIKE '%%anarkia/res/%%' AND boardid = %s" % board['id']) else: to_fix = FetchAll( "SELECT * FROM posts WHERE message LIKE '%%anarkia/read/%%' AND boardid = %s" % board['id']) for p in to_fix: try: if board['board_type'] == '1': newmessage = re.sub( r'/anarkia/res/(\d+).html#(\d+)">>>(\d+)', fix_to_bbs, p['message']) else: newmessage = re.sub( r'/anarkia/read/(\d+)/(\d+)">>>(\d+)', fix_to_ib, p['message']) UpdateDb("UPDATE posts SET message = '%s' WHERE boardid = %s AND id = %s" % (_mysql.escape_string(newmessage), board['id'], p['id'])) except KeyError: pass return True def fix_to_bbs(matchobj): threadid = matchobj.group(1) pid = matchobj.group(2) new_thread = d_thread[threadid] new_post = d_post[new_thread][pid] return '/anarkia/read/%s/%s">>>%s' % (new_thread, new_post, new_post) def fix_to_ib(matchobj): threadid = matchobj.group(1) num = int(matchobj.group(2)) new_thread = d_thread[threadid] new_post = d_post[new_thread][num] return '/anarkia/res/%s.html#%s">>>%s' % (new_thread, new_post, new_post) def get_fix_dictionary(): global d_thread, d_post board = Settings._.BOARD res = FetchAll( "SELECT id, timestamp, parentid FROM posts WHERE boardid = %s ORDER BY CASE parentid WHEN 0 THEN id ELSE parentid END ASC, `id` ASC" % board['id']) num = 1 thread = 0 for p in res: pid = p['id'] if p['parentid'] == '0': num = 1 time = p['timestamp'] if board['board_type'] == '1': d_thread[pid] = time thread = time else: d_thread[time] = pid thread = pid d_post[thread] = {} if board['board_type'] == '1': d_post[thread][pid] = num else: d_post[thread][num] = pid num += 1 return def css(formdata): board = Settings._.BOARD if board['board_type'] == '1': basename = 'style_bbs.css' else: basename = 'style_ib.css' fname = '%sanarkia/%s' % (Settings.HOME_DIR, basename) if formdata.get('cssfile'): with open(fname, 'w') as f: cssfile = f.write(formdata['cssfile']) msg = 'CSS actualizado.' logAction(msg) return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg}) with open(fname) as f: cssfile = f.read() return renderTemplate('anarkia.html', {'mode': 6, 'basename': basename, 'cssfile': cssfile}) def bans(formdata): board = Settings._.BOARD if formdata.get('unban'): unban = int(formdata['unban']) boardpickle = pickle.dumps(['anarkia']) ban = FetchOne("SELECT * FROM `bans` WHERE id = %d" % unban) if not ban: raise UserError("Ban inválido.") if ban['boards'] != boardpickle: raise USerError("Ban inválido.") UpdateDb('DELETE FROM `bans` WHERE id = %s' % ban['id']) logAction("Usuario %s desbaneado." % ban['ip'][:4]) regenerateAccess() bans = FetchAll('SELECT * FROM `bans` WHERE staff = \'anarko\'') for ban in bans: ban['added'] = formatTimestamp(ban['added']) if ban['until'] == '0': ban['until'] = _('Does not expire') else: ban['until'] = formatTimestamp(ban['until']) return renderTemplate('anarkia.html', {'mode': 5, 'bans': bans}) def mod(formdata): board = Settings._.BOARD if formdata.get('thread'): parentid = int(formdata['thread']) posts = FetchAll('SELECT * FROM `posts` WHERE (parentid = %d OR id = %d) AND boardid = %s ORDER BY `id` ASC' % (parentid, parentid, board['id'])) return renderTemplate('anarkia.html', {'mode': 3, 'posts': posts}) elif formdata.get('lock'): postid = int(formdata['lock']) post = FetchOne('SELECT id, locked FROM posts WHERE boardid = %s AND id = %d AND parentid = 0 LIMIT 1' % ( board['id'], postid)) if post['locked'] == '0': setLocked = 1 msg = "Hilo %s cerrado." % post['id'] else: setLocked = 0 msg = "Hilo %s abierto." % post['id'] UpdateDb("UPDATE `posts` SET `locked` = %d WHERE `boardid` = '%s' AND `id` = '%s' LIMIT 1" % ( setLocked, board["id"], post["id"])) threadUpdated(post['id']) logAction(msg) return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg}) elif formdata.get('del'): postid = int(formdata['del']) post = FetchOne('SELECT id, parentid FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % ( board['id'], postid)) if post['parentid'] != '0': deletePost(post['id'], None, '3', False) msg = "Mensaje %s eliminado." % post['id'] logAction(msg) return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg}) else: raise UserError("jaj no") elif formdata.get('restore'): postid = int(formdata['restore']) post = FetchOne('SELECT id, parentid FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % ( board['id'], postid)) UpdateDb('UPDATE `posts` SET `IS_DELETED` = 0 WHERE `boardid` = %s AND `id` = %s LIMIT 1' % ( board['id'], post['id'])) if post['parentid'] != '0': threadUpdated(post['parentid']) else: regenerateFrontPages() msg = "Mensaje %s recuperado." % post['id'] logAction(msg) return renderTemplate('anarkia.html', {'mode': 99, 'msg': msg}) elif formdata.get('ban'): postid = int(formdata['ban']) post = FetchOne('SELECT id, ip FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % ( board['id'], postid)) return renderTemplate('anarkia.html', {'mode': 4, 'post': post}) elif formdata.get('banto'): postid = int(formdata['banto']) post = FetchOne('SELECT id, message, parentid, ip FROM posts WHERE boardid = %s AND id = %d LIMIT 1' % ( board['id'], postid)) reason = formdata.get('reason').replace( 'script', '').replace('meta', '') if reason is not None: if formdata['seconds'] != '0': until = str(timestamp() + int(formdata['seconds'])) else: until = '0' where = pickle.dumps(['anarkia']) ban = FetchOne("SELECT `id` FROM `bans` WHERE `ip` = '" + post['ip'] + "' AND `boards` = '" + _mysql.escape_string(where) + "' LIMIT 1") if ban: raise UserError("Este usuario ya esta baneado.") # Blind mode if formdata.get('blind') == '1': blind = '1' else: blind = '0' InsertDb("INSERT INTO `bans` (`ip`, `netmask`, `boards`, `added`, `until`, `staff`, `reason`, `blind`) VALUES ('" + post['ip'] + "', INET_ATON('255.255.255.255'), '" + _mysql.escape_string( where) + "', " + str(timestamp()) + ", " + until + ", 'anarko', '" + _mysql.escape_string(formdata['reason']) + "', '"+blind+"')") newmessage = post['message'] + \ '