From 63f8af1fc3049527a4a4b53a1bbfde90cb4b2eb3 Mon Sep 17 00:00:00 2001 From: RaNaN Date: Sat, 19 Feb 2011 21:58:08 +0100 Subject: encoding fixes --- module/Utils.py | 9 ++++++++ module/database/UserDatabase.py | 34 +++++++++++++++++++++------- module/plugins/hoster/BasePlugin.py | 7 +++++- module/web/json_app.py | 13 ++++------- module/web/pyload_app.py | 14 ++++++++---- module/web/templates/jinja/default/base.html | 8 +++---- module/web/utils.py | 23 +++++++++++++++---- module/web/webinterface.py | 5 ++-- 8 files changed, 80 insertions(+), 33 deletions(-) (limited to 'module') diff --git a/module/Utils.py b/module/Utils.py index b023a981d..6f889118d 100644 --- a/module/Utils.py +++ b/module/Utils.py @@ -13,6 +13,13 @@ def chmod(*args): except: pass +def decode(string): + """ decode string with utf if possible """ + try: + return string.decode("utf8", "ignore") + except: + return string + def save_join(*args): """ joins a path, encoding aware """ paths = [] @@ -21,6 +28,8 @@ def save_join(*args): if i: path = path.replace(":","") + path = decode(path) + tmp = path.encode(sys.getfilesystemencoding(), "replace") paths.append(tmp) return join(*paths) diff --git a/module/database/UserDatabase.py b/module/database/UserDatabase.py index a69dfff0e..6137581ed 100644 --- a/module/database/UserDatabase.py +++ b/module/database/UserDatabase.py @@ -22,20 +22,36 @@ from DatabaseBackend import style from hashlib import sha1 import random +class PERMS: + ADD = 1 # can add packages + DELETE = 2 # can delete packages + STATUS = 4 # see and change server status + SEE_DOWNLOADS = 16 # see queue and collector + DOWNLOAD = 32 # can download from webinterface + SETTINGS = 64 # can access settings + +class ROLE: + ADMIN = 0 #admin has all permissions implicit + USER = 1 + +def has_permission(current, perms): + # bytewise or perms before if needed + return current == (current & perms) + class UserMethods(): @style.queue def checkAuth(db, user, password): c = db.c - c.execute('SELECT name, password, role, permission, template FROM "users" WHERE name=?', (user, )) + c.execute('SELECT id, name, password, role, permission, template FROM "users" WHERE name=?', (user, )) r = c.fetchone() if not r: return {} - salt = r[1][:5] - pw = r[1][5:] + salt = r[2][:5] + pw = r[2][5:] h = sha1(salt + password) if h.hexdigest() == pw: - return {"name": r[0], "role": r[2], "permission": r[3], "template": r[4]} + return {"id": r[0], "name": r[1], "role": r[3], "permission": r[4], "template": r[5]} else: return {} @@ -51,6 +67,11 @@ class UserMethods(): c.execute('UPDATE users SET password=? WHERE name=?', (password, user)) else: c.execute('INSERT INTO users (name, password) VALUES (?, ?)', (user, password)) + + + @style.queue + def setPermission(db, userid, perms): + db.c.execute("UPDATE users SET permission=? WHERE id=?", (perms, userid)) @style.queue def listUsers(db): @@ -63,10 +84,7 @@ class UserMethods(): @style.queue def removeUser(db, user): - c = db.c - c.execute('SELECT name FROM users WHERE name=?', (user, )) - if c.fetchone() is not None: - c.execute('DELETE FROM users WHERE name=?', (user, )) + c.execute('DELETE FROM users WHERE name=?', (user, )) DatabaseBackend.registerSub(UserMethods) diff --git a/module/plugins/hoster/BasePlugin.py b/module/plugins/hoster/BasePlugin.py index b1ae630fd..0248ca624 100644 --- a/module/plugins/hoster/BasePlugin.py +++ b/module/plugins/hoster/BasePlugin.py @@ -22,9 +22,14 @@ class BasePlugin(Hoster): # self.__name__ = "NetloadIn" # pyfile.name = "test" -# self.load("http://localhost:9000/short") +# self.html = self.load("http://localhost:9000/short") # self.download("http://localhost:9000/short") +# self.api = self.load("http://localhost:9000/short") # self.decryptCaptcha("http://localhost:9000/captcha") +# +# if pyfile.url == "79": +# self.core.server_methods.add_package("test", [str(i) for i in range(80)], 1) +# # return if pyfile.url.startswith("http"): diff --git a/module/web/json_app.py b/module/web/json_app.py index f10c09498..8eb2dbbfe 100644 --- a/module/web/json_app.py +++ b/module/web/json_app.py @@ -210,7 +210,7 @@ def link_order(ids): @route("/json/add_package", method="POST") @login_required('can_add') def add_package(): - name = request.forms['add_name'] + name = request.forms.get("add_name", "New Package") queue = int(request.forms['add_dest']) links = request.forms['add_links'].decode("utf8", "ignore") links = links.split("\n") @@ -219,7 +219,7 @@ def add_package(): try: f = request.files['add_file'] - if name is None or name == "": + if name == "New Package": name = f.name fpath = join(PYLOAD.get_conf_val("general", "download_folder"), "tmp_" + f.filename) @@ -230,9 +230,6 @@ def add_package(): except: pass - if name is None or name == "": - return HTTPError() - name = name.decode("utf8", "ignore") links = map(lambda x: x.strip(), links) @@ -283,10 +280,10 @@ def move_package(dest, id): def edit_package(): try: id = int(request.forms.get("pack_id")) - data = {"name": request.forms.get("pack_name"), - "folder": request.forms.get("pack_folder"), + data = {"name": request.forms.get("pack_name").decode("utf8", "ignore"), + "folder": request.forms.get("pack_folder").decode("utf8", "ignore"), "priority": request.forms.get("pack_prio"), - "password": request.forms.get("pack_pws")} + "password": request.forms.get("pack_pws").decode("utf8", "ignore")} PYLOAD.set_package_data(id, data) return "success" diff --git a/module/web/pyload_app.py b/module/web/pyload_app.py index 82701e755..643e1e75c 100644 --- a/module/web/pyload_app.py +++ b/module/web/pyload_app.py @@ -38,7 +38,7 @@ from webinterface import PYLOAD, PROJECT_DIR, SETUP from utils import render_to_response, parse_permissions, parse_userdata, login_required from filters import relpath, unquotepath -from module.utils import formatSize +from module.utils import formatSize, decode # Helper @@ -69,6 +69,9 @@ def base(messages): ## Views @error(500) def error500(error): + if request.header.get('X-Requested-With') == 'XMLHttpRequest': + return HTTPError(500, error.traceback) + return base(["An Error occured, please enable debug mode to get more details.", error, error.traceback.replace("\n", "
") if error.traceback else "No Traceback"]) @@ -103,6 +106,7 @@ def login_post(): s = request.environ.get('beaker.session') s["authenticated"] = True + s["id"] = info["id"] s["name"] = info["name"] s["role"] = info["role"] s["perms"] = info["permission"] @@ -171,14 +175,14 @@ def downloads(): for item in sorted(listdir(root)): if isdir(join(root, item)): folder = { - 'name': item, - 'path': item, + 'name': decode(item), + 'path': decode(item), 'files': [] } for file in sorted(listdir(join(root, item))): try: if isfile(join(root, item, file)): - folder['files'].append(file) + folder['files'].append(decode(file)) except: pass @@ -466,7 +470,7 @@ def logs(item=-1): if counter >= item: try: - date, time, level, message = l.split(" ", 3) + date, time, level, message = l.decode("utf8", "ignore").split(" ", 3) dtime = datetime.strptime(date + ' ' + time, '%d.%m.%Y %H:%M:%S') except: dtime = None diff --git a/module/web/templates/jinja/default/base.html b/module/web/templates/jinja/default/base.html index f5e2b9634..0931291b7 100644 --- a/module/web/templates/jinja/default/base.html +++ b/module/web/templates/jinja/default/base.html @@ -30,9 +30,9 @@ document.addEvent("domready", function(){ $('add_form').onsubmit=function() { $('add_form').target = 'upload_target'; - if ($('add_name').value == "" && $('add_file').value != " "){ + if ($('add_name').value == "" && $('add_file').value == ""){ alert("{{_("Please Enter a packagename.")}}"); - return false + return false; }else{ out(); } @@ -259,7 +259,7 @@ function AddBox()
-{% if perms.can_change_status %} +{% if perms.status %} {% endif %} -{% if perms.can_see_dl %} +{% if perms.see_downloads %}