2022-02-27 17:26:54 +00:00
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.models.server_permissions import Enum_Permissions_Server
|
2022-02-27 17:26:54 +00:00
|
|
|
from app.classes.shared.console import console
|
|
|
|
from app.classes.shared.helpers import helper
|
2022-03-01 01:02:30 +00:00
|
|
|
from app.classes.shared.file_helpers import file_helper
|
2022-02-27 17:26:54 +00:00
|
|
|
from app.classes.web.base_handler import BaseHandler
|
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
try:
|
|
|
|
import bleach
|
|
|
|
import tornado.web
|
|
|
|
import tornado.escape
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
|
|
|
helper.auto_installer_fix(e)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class FileHandler(BaseHandler):
|
2022-02-27 17:26:54 +00:00
|
|
|
def render_page(self, template, page_data):
|
|
|
|
self.render(
|
|
|
|
template,
|
|
|
|
data=page_data,
|
|
|
|
translate=self.translator.translate,
|
|
|
|
)
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def get(self, page):
|
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-02-27 17:26:54 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
permissions = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"Commands": Enum_Permissions_Server.Commands,
|
|
|
|
"Terminal": Enum_Permissions_Server.Terminal,
|
|
|
|
"Logs": Enum_Permissions_Server.Logs,
|
|
|
|
"Schedule": Enum_Permissions_Server.Schedule,
|
|
|
|
"Backup": Enum_Permissions_Server.Backup,
|
|
|
|
"Files": Enum_Permissions_Server.Files,
|
|
|
|
"Config": Enum_Permissions_Server.Config,
|
|
|
|
"Players": Enum_Permissions_Server.Players,
|
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
if page == "get_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
file_path = helper.get_os_understandable_path(
|
|
|
|
self.get_argument("file_path", None)
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "get_file"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
file_path,
|
|
|
|
) or not helper.check_file_exists(os.path.abspath(file_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid path in get_file file file ajax call ({file_path})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid path in get_file file file ajax call ({file_path})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
error = None
|
|
|
|
|
|
|
|
try:
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(file_path, encoding="utf-8") as file:
|
2022-02-27 17:26:54 +00:00
|
|
|
file_contents = file.read()
|
|
|
|
except UnicodeDecodeError:
|
2022-03-23 02:50:12 +00:00
|
|
|
file_contents = ""
|
|
|
|
error = "UnicodeDecodeError"
|
2022-02-27 17:26:54 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
self.write({"content": file_contents, "error": error})
|
2022-02-27 17:26:54 +00:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
elif page == "get_tree":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
path = self.get_argument("path", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "get_tree"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if helper.validate_traversal(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"], path
|
|
|
|
):
|
|
|
|
self.write(
|
|
|
|
helper.get_os_understandable_path(path)
|
|
|
|
+ "\n"
|
|
|
|
+ helper.generate_tree(path)
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
elif page == "get_dir":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
path = self.get_argument("path", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "get_tree"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if helper.validate_traversal(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"], path
|
|
|
|
):
|
|
|
|
self.write(
|
|
|
|
helper.get_os_understandable_path(path)
|
|
|
|
+ "\n"
|
|
|
|
+ helper.generate_dir(path)
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
self.finish()
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def post(self, page):
|
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-02-27 17:26:54 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
permissions = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"Commands": Enum_Permissions_Server.Commands,
|
|
|
|
"Terminal": Enum_Permissions_Server.Terminal,
|
|
|
|
"Logs": Enum_Permissions_Server.Logs,
|
|
|
|
"Schedule": Enum_Permissions_Server.Schedule,
|
|
|
|
"Backup": Enum_Permissions_Server.Backup,
|
|
|
|
"Files": Enum_Permissions_Server.Files,
|
|
|
|
"Config": Enum_Permissions_Server.Config,
|
|
|
|
"Players": Enum_Permissions_Server.Players,
|
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
if page == "create_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
file_parent = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("file_parent", default=None, strip=True)
|
|
|
|
)
|
|
|
|
file_name = self.get_body_argument("file_name", default=None, strip=True)
|
2022-02-27 17:26:54 +00:00
|
|
|
file_path = os.path.join(file_parent, file_name)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "create_file"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
file_path,
|
|
|
|
) or helper.check_file_exists(os.path.abspath(file_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid path in create_file file ajax call ({file_path})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid path in create_file file ajax call ({file_path})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Create the file by opening it
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(file_path, "w", encoding="utf-8") as file_object:
|
2022-02-27 17:26:54 +00:00
|
|
|
file_object.close()
|
|
|
|
|
|
|
|
elif page == "create_dir":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
dir_parent = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("dir_parent", default=None, strip=True)
|
|
|
|
)
|
|
|
|
dir_name = self.get_body_argument("dir_name", default=None, strip=True)
|
2022-02-27 17:26:54 +00:00
|
|
|
dir_path = os.path.join(dir_parent, dir_name)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "create_dir"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
dir_path,
|
|
|
|
) or helper.check_path_exists(os.path.abspath(dir_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid path in create_dir file ajax call ({dir_path})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid path in create_dir file ajax call ({dir_path})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
# Create the directory
|
|
|
|
os.mkdir(dir_path)
|
|
|
|
|
|
|
|
elif page == "unzip_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
path = helper.get_os_understandable_path(self.get_argument("path", None))
|
2022-02-27 17:26:54 +00:00
|
|
|
helper.unzipFile(path)
|
|
|
|
self.redirect(f"/panel/server_detail?id={server_id}&subpage=files")
|
|
|
|
return
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def delete(self, page):
|
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-02-27 17:26:54 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
permissions = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"Commands": Enum_Permissions_Server.Commands,
|
|
|
|
"Terminal": Enum_Permissions_Server.Terminal,
|
|
|
|
"Logs": Enum_Permissions_Server.Logs,
|
|
|
|
"Schedule": Enum_Permissions_Server.Schedule,
|
|
|
|
"Backup": Enum_Permissions_Server.Backup,
|
|
|
|
"Files": Enum_Permissions_Server.Files,
|
|
|
|
"Config": Enum_Permissions_Server.Config,
|
|
|
|
"Players": Enum_Permissions_Server.Players,
|
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
if page == "del_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
file_path = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("file_path", default=None, strip=True)
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
console.warning(f"Delete {file_path} for server {server_id}")
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "del_file"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
server_info = self.controller.servers.get_server_data_by_id(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
if not (
|
|
|
|
helper.in_path(
|
|
|
|
helper.get_os_understandable_path(server_info["path"]), file_path
|
|
|
|
)
|
|
|
|
or helper.in_path(
|
|
|
|
helper.get_os_understandable_path(server_info["backup_path"]),
|
|
|
|
file_path,
|
|
|
|
)
|
|
|
|
) or not helper.check_file_exists(os.path.abspath(file_path)):
|
2022-02-27 17:26:54 +00:00
|
|
|
logger.warning(f"Invalid path in del_file file ajax call ({file_path})")
|
2022-03-23 02:50:12 +00:00
|
|
|
console.warning(
|
|
|
|
f"Invalid path in del_file file ajax call ({file_path})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Delete the file
|
2022-03-01 01:02:30 +00:00
|
|
|
file_helper.del_file(file_path)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
elif page == "del_dir":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
dir_path = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("dir_path", default=None, strip=True)
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
console.warning(f"Delete {dir_path} for server {server_id}")
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not self.check_server_id(server_id, "del_dir"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
server_info = self.controller.servers.get_server_data_by_id(server_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(server_info["path"]), dir_path
|
|
|
|
) or not helper.check_path_exists(os.path.abspath(dir_path)):
|
2022-02-27 17:26:54 +00:00
|
|
|
logger.warning(f"Invalid path in del_file file ajax call ({dir_path})")
|
|
|
|
console.warning(f"Invalid path in del_file file ajax call ({dir_path})")
|
|
|
|
return
|
|
|
|
|
|
|
|
# Delete the directory
|
|
|
|
# os.rmdir(dir_path) # Would only remove empty directories
|
2022-03-23 02:50:12 +00:00
|
|
|
if helper.validate_traversal(
|
|
|
|
helper.get_os_understandable_path(server_info["path"]), dir_path
|
|
|
|
):
|
2022-03-01 01:02:30 +00:00
|
|
|
# Removes also when there are contents
|
|
|
|
file_helper.del_dirs(dir_path)
|
2022-02-27 17:26:54 +00:00
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def put(self, page):
|
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-02-27 17:26:54 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
permissions = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"Commands": Enum_Permissions_Server.Commands,
|
|
|
|
"Terminal": Enum_Permissions_Server.Terminal,
|
|
|
|
"Logs": Enum_Permissions_Server.Logs,
|
|
|
|
"Schedule": Enum_Permissions_Server.Schedule,
|
|
|
|
"Backup": Enum_Permissions_Server.Backup,
|
|
|
|
"Files": Enum_Permissions_Server.Files,
|
|
|
|
"Config": Enum_Permissions_Server.Config,
|
|
|
|
"Players": Enum_Permissions_Server.Players,
|
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
if page == "save_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
file_contents = self.get_body_argument(
|
|
|
|
"file_contents", default=None, strip=True
|
|
|
|
)
|
|
|
|
file_path = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("file_path", default=None, strip=True)
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self.check_server_id(server_id, "save_file"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
file_path,
|
|
|
|
) or not helper.check_file_exists(os.path.abspath(file_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid path in save_file file ajax call ({file_path})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid path in save_file file ajax call ({file_path})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# Open the file in write mode and store the content in file_object
|
2022-03-23 02:50:12 +00:00
|
|
|
with open(file_path, "w", encoding="utf-8") as file_object:
|
2022-02-27 17:26:54 +00:00
|
|
|
file_object.write(file_contents)
|
|
|
|
|
|
|
|
elif page == "rename_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
item_path = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("item_path", default=None, strip=True)
|
|
|
|
)
|
|
|
|
new_item_name = self.get_body_argument(
|
|
|
|
"new_item_name", default=None, strip=True
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self.check_server_id(server_id, "rename_file"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
if item_path is None or new_item_name is None:
|
|
|
|
logger.warning("Invalid path(s) in rename_file file ajax call")
|
|
|
|
console.warning("Invalid path(s) in rename_file file ajax call")
|
|
|
|
return
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
item_path,
|
|
|
|
) or not helper.check_path_exists(os.path.abspath(item_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid old name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid old name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
new_item_path = os.path.join(os.path.split(item_path)[0], new_item_name)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
new_item_path,
|
|
|
|
) or helper.check_path_exists(os.path.abspath(new_item_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid new name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid new name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# RENAME
|
|
|
|
os.rename(item_path, new_item_path)
|
|
|
|
|
|
|
|
@tornado.web.authenticated
|
|
|
|
def patch(self, page):
|
|
|
|
api_key, _, exec_user = self.current_user
|
2022-03-23 02:50:12 +00:00
|
|
|
superuser = exec_user["superuser"]
|
2022-02-27 17:26:54 +00:00
|
|
|
if api_key is not None:
|
|
|
|
superuser = superuser and api_key.superuser
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
server_id = self.get_argument("id", None)
|
2022-02-27 17:26:54 +00:00
|
|
|
permissions = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"Commands": Enum_Permissions_Server.Commands,
|
|
|
|
"Terminal": Enum_Permissions_Server.Terminal,
|
|
|
|
"Logs": Enum_Permissions_Server.Logs,
|
|
|
|
"Schedule": Enum_Permissions_Server.Schedule,
|
|
|
|
"Backup": Enum_Permissions_Server.Backup,
|
|
|
|
"Files": Enum_Permissions_Server.Files,
|
|
|
|
"Config": Enum_Permissions_Server.Config,
|
|
|
|
"Players": Enum_Permissions_Server.Players,
|
|
|
|
}
|
|
|
|
user_perms = self.controller.server_perms.get_user_id_permissions_list(
|
|
|
|
exec_user["user_id"], server_id
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
if page == "rename_file":
|
2022-03-23 02:50:12 +00:00
|
|
|
if not permissions["Files"] in user_perms:
|
2022-02-27 17:26:54 +00:00
|
|
|
if not superuser:
|
|
|
|
self.redirect("/panel/error?error=Unauthorized access to Files")
|
|
|
|
return
|
2022-03-23 02:50:12 +00:00
|
|
|
item_path = helper.get_os_understandable_path(
|
|
|
|
self.get_body_argument("item_path", default=None, strip=True)
|
|
|
|
)
|
|
|
|
new_item_name = self.get_body_argument(
|
|
|
|
"new_item_name", default=None, strip=True
|
|
|
|
)
|
|
|
|
|
|
|
|
if not self.check_server_id(server_id, "rename_file"):
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
if item_path is None or new_item_name is None:
|
|
|
|
logger.warning("Invalid path(s) in rename_file file ajax call")
|
|
|
|
console.warning("Invalid path(s) in rename_file file ajax call")
|
|
|
|
return
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
item_path,
|
|
|
|
) or not helper.check_path_exists(os.path.abspath(item_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid old name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid old name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
new_item_path = os.path.join(os.path.split(item_path)[0], new_item_name)
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if not helper.in_path(
|
|
|
|
helper.get_os_understandable_path(
|
|
|
|
self.controller.servers.get_server_data_by_id(server_id)["path"]
|
|
|
|
),
|
|
|
|
new_item_path,
|
|
|
|
) or helper.check_path_exists(os.path.abspath(new_item_path)):
|
|
|
|
logger.warning(
|
|
|
|
f"Invalid new name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Invalid new name path in rename_file file ajax call ({server_id})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
# RENAME
|
|
|
|
os.rename(item_path, new_item_path)
|
|
|
|
|
|
|
|
def check_server_id(self, server_id, page_name):
|
|
|
|
if server_id is None:
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
|
|
|
f"Server ID not defined in {page_name} file ajax call ({server_id})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Server ID not defined in {page_name} file ajax call ({server_id})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
else:
|
|
|
|
server_id = bleach.clean(server_id)
|
|
|
|
|
|
|
|
# does this server id exist?
|
|
|
|
if not self.controller.servers.server_id_exists(server_id):
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.warning(
|
|
|
|
f"Server ID not found in {page_name} file ajax call ({server_id})"
|
|
|
|
)
|
|
|
|
console.warning(
|
|
|
|
f"Server ID not found in {page_name} file ajax call ({server_id})"
|
|
|
|
)
|
2022-02-27 17:26:54 +00:00
|
|
|
return
|
|
|
|
return True
|