diff options
Diffstat (limited to 'cgi/framework.py')
-rw-r--r-- | cgi/framework.py | 96 |
1 files changed, 32 insertions, 64 deletions
diff --git a/cgi/framework.py b/cgi/framework.py index faea8d9..5277df0 100644 --- a/cgi/framework.py +++ b/cgi/framework.py @@ -6,11 +6,11 @@ import time import hashlib import pickle import socket -import _mysql -import urllib +import urllib.request, urllib.parse, urllib.error import re import logging -from Cookie import SimpleCookie +import base64 +from http.cookies import SimpleCookie from settings import Settings from database import * @@ -22,14 +22,14 @@ def setBoard(dir): with the data from the db. """ if not dir: - raise UserError, _("The specified board is invalid.") + raise UserError(_("The specified board is invalid.")) logging.debug("Seteando el board " + dir) - board = FetchOne("SELECT * FROM `boards` WHERE `dir` = '%s' LIMIT 1" % _mysql.escape_string(dir)) + board = FetchOne("SELECT * FROM `boards` WHERE `dir` = %s LIMIT 1", (dir,)) if not board: - raise UserError, _("The specified board is invalid.") + raise UserError(_("The specified board is invalid.")) board["filetypes"] = FetchAll( - "SELECT * FROM `boards_filetypes` INNER JOIN `filetypes` ON filetypes.id = boards_filetypes.filetypeid WHERE `boardid` = %s ORDER BY `ext` ASC" % _mysql.escape_string(board['id'])) + "SELECT * FROM `boards_filetypes` INNER JOIN `filetypes` ON filetypes.id = boards_filetypes.filetypeid WHERE `boardid` = %s ORDER BY `ext` ASC", (board['id'],)) board["filetypes_ext"] = [filetype['ext'] for filetype in board['filetypes']] @@ -145,7 +145,7 @@ def updateBoardSettings(): del board["filetypes"] del board["filetypes_ext"] post_values = ["`" + _mysql.escape_string(str(key)) + "` = '" + _mysql.escape_string( - str(value)) + "'" for key, value in board.iteritems()] + str(value)) + "'" for key, value in board.items()] UpdateDb("UPDATE `boards` SET %s WHERE `id` = '%s' LIMIT 1" % (", ".join(post_values), board["id"])) @@ -242,20 +242,11 @@ def getFormData(self): self.environ["wsgi.post_form"] = post_form self.environ["wsgi.input"] = new_input - try: - formdata = {} - for key in dict(fs): - try: - formdata.update({key: fs[key].value}) - if key == "file": - formdata.update( - {"file_original": secure_filename(fs[key].filename)}) - except AttributeError: - formdata.update({key: fs[key]}) + formdata = {} + for key in dict(fs): + formdata.update({key: fs[key].value}) - return formdata - except TypeError: - return fs + return formdata class InputProcessed(object): @@ -273,45 +264,22 @@ def secure_filename(path): ''.join([os.path.sep, os.path.altsep or '']))) return cgi.escape(split.sub('', path)) - -def getMD5(data): +def getMD5b(data): m = hashlib.md5() m.update(data) - + return m.hexdigest() -def nullstr(len): return "\0" * len - +def getMD5(data): + m = hashlib.md5() + m.update(bytes(data, 'utf-8')) -def hide_data(data, length, key, secret): - """ - Encrypts data, useful for tripcodes and IDs - """ - crypt = rc4(nullstr(length), rc4( - nullstr(32), key + secret) + data).encode('base64') - return crypt.rstrip('\n') + return m.hexdigest() -def rc4(data, key): - """ - rc4 implementation - """ - x = 0 - box = range(256) - for i in range(256): - x = (x + box[i] + ord(key[i % len(key)])) % 256 - box[i], box[x] = box[x], box[i] - x = 0 - y = 0 - out = [] - for char in data: - x = (x + 1) % 256 - y = (y + box[x]) % 256 - box[x], box[y] = box[y], box[x] - out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256])) - - return ''.join(out) +def getb64(data): + return base64.b64encode(bytes(data, 'utf-8')).decode('utf-8') def getRandomLine(filename): @@ -337,7 +305,7 @@ def N_(message): return message def getCookie(self, value=""): try: - return urllib.unquote_plus(self._cookies[value].value) + return urllib.parse.unquote_plus(self._cookies[value].value) except KeyError: return None @@ -353,16 +321,16 @@ def setCookie(self, key, value="", max_age=None, expires=None, path="/", domain= """ if self._newcookies is None: self._newcookies = SimpleCookie() - self._newcookies[key] = urllib.quote_plus(value) + self._newcookies[key] = urllib.parse.quote_plus(value) if not max_age is None: self._newcookies[key]["max-age"] = max_age if not expires is None: - if isinstance(expires, basestring): + if isinstance(expires, str): self._newcookies[key]["expires"] = expires expires = None elif isinstance(expires, datetime): expires = expires.utctimetuple() - elif not isinstance(expires, (int, long)): + elif not isinstance(expires, int): expires = datetime.datetime.gmtime(expires) else: raise ValueError("Se requiere de un entero o un datetime") @@ -448,20 +416,20 @@ def inet_ntoa(packed_ip): def is_bad_proxy(pip): - import urllib2 + import urllib.request, urllib.error, urllib.parse import socket socket.setdefaulttimeout(3) try: - proxy_handler = urllib2.ProxyHandler({'http': pip}) - opener = urllib2.build_opener(proxy_handler) + proxy_handler = urllib.request.ProxyHandler({'http': pip}) + opener = urllib.request.build_opener(proxy_handler) opener.addheaders = [('User-agent', 'Mozilla/5.0')] - urllib2.install_opener(opener) - req = urllib2.Request('http://bienvenidoainternet.org') - sock = urllib2.urlopen(req) - except urllib2.HTTPError, e: + urllib.request.install_opener(opener) + req = urllib.request.Request('http://bienvenidoainternet.org') + sock = urllib.request.urlopen(req) + except urllib.error.HTTPError as e: return e.code - except Exception, detail: + except Exception as detail: return True return False |