Files
CyberPanel/filemanager/views.py

298 lines
9.4 KiB
Python
Raw Normal View History

2017-12-09 22:30:10 +05:00
# -*- coding: utf-8 -*-
from django.shortcuts import render,redirect
from loginSystem.models import Administrator
from loginSystem.views import loadLoginPage
2018-02-16 00:57:46 +05:00
import plogical.CyberCPLogFileWriter as logging
2020-03-06 11:37:08 +05:00
from django.http import HttpResponse
2018-02-16 00:57:46 +05:00
import json
from websiteFunctions.models import Websites
2018-08-18 00:39:10 +05:00
from plogical.acl import ACLManager
2019-01-28 15:19:59 +05:00
from .filemanager import FileManager as FM
2017-12-09 22:30:10 +05:00
# Create your views here.
def loadFileManagerHome(request,domain):
try:
2018-08-18 00:39:10 +05:00
userID = request.session['userID']
2018-04-18 15:57:49 +05:00
if Websites.objects.filter(domain=domain).exists():
2018-08-21 13:10:40 +05:00
admin = Administrator.objects.get(pk=userID)
2018-08-18 00:39:10 +05:00
currentACL = ACLManager.loadedACL(userID)
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +05:00
if ACLManager.checkOwnership(domain, admin, currentACL) == 1:
2018-11-06 00:19:58 +05:00
return render(request, 'filemanager/index.html', {'domainName': domain})
2018-04-18 15:57:49 +05:00
else:
2018-08-21 13:10:40 +05:00
return ACLManager.loadError()
2018-04-18 15:57:49 +05:00
else:
return HttpResponse("Domain does not exists.")
2017-12-09 22:30:10 +05:00
2018-02-16 00:57:46 +05:00
except KeyError:
return redirect(loadLoginPage)
def changePermissions(request):
try:
2018-08-21 13:10:40 +05:00
userID = request.session['userID']
2022-04-20 16:41:25 +05:00
2018-02-16 00:57:46 +05:00
try:
data = json.loads(request.body)
domainName = data['domainName']
2018-08-21 13:10:40 +05:00
currentACL = ACLManager.loadedACL(userID)
2021-09-23 19:19:17 +05:00
if currentACL['admin'] == 1:
2018-08-21 13:10:40 +05:00
pass
else:
2021-09-23 19:19:17 +05:00
return ACLManager.loadError()
2018-08-21 13:10:40 +05:00
2020-03-07 19:01:54 +05:00
fm = FM(request, data)
fm.fixPermissions(domainName)
2020-02-21 17:36:09 +05:00
2018-02-16 00:57:46 +05:00
data_ret = {'permissionsChanged': 1, 'error_message': "None"}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2018-02-16 00:57:46 +05:00
logging.CyberCPLogFileWriter.writeToFile(str(msg))
data_ret = {'permissionsChanged': 0, 'error_message': str(msg)}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
2017-12-09 22:30:10 +05:00
except KeyError:
return redirect(loadLoginPage)
2019-01-28 15:19:59 +05:00
def controller(request):
2018-04-18 15:57:49 +05:00
try:
data = json.loads(request.body)
2022-04-04 16:01:36 +05:00
try:
domainName = data['domainName']
method = data['method']
2018-04-18 15:57:49 +05:00
2022-04-04 16:01:36 +05:00
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
currentACL = ACLManager.loadedACL(userID)
2019-01-28 15:19:59 +05:00
2022-04-20 16:31:24 +05:00
if domainName == '':
if currentACL['admin'] == 1:
pass
else:
return ACLManager.loadErrorJson('FilemanagerAdmin', 0)
2022-04-04 16:01:36 +05:00
else:
2022-04-20 16:31:24 +05:00
if ACLManager.checkOwnership(domainName, admin, currentACL) == 1:
pass
else:
return ACLManager.loadErrorJson()
2022-04-04 16:01:36 +05:00
except:
2022-04-20 16:31:24 +05:00
method = data['method']
userID = request.session['userID']
currentACL = ACLManager.loadedACL(userID)
if currentACL['admin'] == 1:
pass
else:
return ACLManager.loadErrorJson('FilemanagerAdmin', 0)
2019-01-28 15:19:59 +05:00
fm = FM(request, data)
if method == 'listForTable':
return fm.listForTable()
elif method == 'list':
return fm.list()
elif method == 'createNewFile':
return fm.createNewFile()
elif method == 'createNewFolder':
return fm.createNewFolder()
elif method == 'deleteFolderOrFile':
return fm.deleteFolderOrFile()
elif method == 'restore':
return fm.restore()
2019-01-28 15:19:59 +05:00
elif method == 'copy':
return fm.copy()
elif method == 'move':
return fm.move()
elif method == 'rename':
return fm.rename()
elif method == 'readFileContents':
return fm.readFileContents()
elif method == 'writeFileContents':
return fm.writeFileContents()
elif method == 'upload':
return fm.writeFileContents()
elif method == 'extract':
return fm.extract()
elif method == 'compress':
return fm.compress()
elif method == 'changePermissions':
return fm.changePermissions()
2019-01-28 15:19:59 +05:00
2019-12-10 15:09:10 +05:00
except BaseException as msg:
2019-01-28 15:19:59 +05:00
fm = FM(request, None)
return fm.ajaxPre(0, str(msg))
def upload(request):
try:
2018-04-18 15:57:49 +05:00
2019-01-28 15:19:59 +05:00
data = request.POST
2018-04-18 15:57:49 +05:00
2022-04-05 15:53:07 +05:00
try:
2018-04-18 15:57:49 +05:00
2022-04-05 15:53:07 +05:00
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
currentACL = ACLManager.loadedACL(userID)
if ACLManager.checkOwnership(data['domainName'], admin, currentACL) == 1:
pass
else:
return ACLManager.loadErrorJson()
except:
2019-01-28 15:19:59 +05:00
pass
2018-04-18 15:57:49 +05:00
2019-01-28 15:19:59 +05:00
fm = FM(request, data)
return fm.upload()
2018-04-18 15:57:49 +05:00
except KeyError:
return redirect(loadLoginPage)
2020-10-14 12:21:28 +05:00
def editFile(request):
try:
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
from urllib.parse import quote
from django.utils.encoding import iri_to_uri
domainName = request.GET.get('domainName')
fileName = request.GET.get('fileName')
try:
theme = request.GET.get('theme')
if theme == None:
theme = 'cobalt'
except:
theme = 'cobalt'
2020-10-14 12:21:28 +05:00
currentACL = ACLManager.loadedACL(userID)
if ACLManager.checkOwnership(domainName, admin, currentACL) == 1:
pass
else:
return ACLManager.loadError()
2020-10-18 12:29:14 +05:00
mode = FM.findMode(fileName)
modeFiles = FM.findModeFiles(mode)
2020-10-20 11:51:58 +05:00
additionalOptions = FM.findAdditionalOptions(mode)
themeFile = FM.findThemeFile(theme)
2020-10-14 12:21:28 +05:00
if ACLManager.checkOwnership(domainName, admin, currentACL) == 1:
return render(request, 'filemanager/editFile.html', {'domainName': domainName, 'fileName': fileName,
'mode': mode, 'modeFiles': modeFiles, 'theme': theme,
2020-10-20 11:51:58 +05:00
'themeFile': themeFile, 'additionalOptions': additionalOptions})
2020-10-14 12:21:28 +05:00
else:
return ACLManager.loadError()
except KeyError:
return redirect(loadLoginPage)
2022-04-20 16:41:25 +05:00
def FileManagerRoot(request):
### Load Custom CSS
try:
from baseTemplate.models import CyberPanelCosmetic
cosmetic = CyberPanelCosmetic.objects.get(pk=1)
except:
from baseTemplate.models import CyberPanelCosmetic
cosmetic = CyberPanelCosmetic()
cosmetic.save()
userID = request.session['userID']
currentACL = ACLManager.loadedACL(userID)
ipFile = "/etc/cyberpanel/machineIP"
f = open(ipFile)
ipData = f.read()
ipAddressLocal = ipData.split('\n', 1)[0]
try:
2022-04-24 04:29:48 +05:00
url = "https://platform.cyberpersons.com/CyberpanelAdOns/Adonpermission"
2022-04-20 16:41:25 +05:00
data = {
"name": "Filemanager",
"IP": ipAddressLocal
}
import requests
response = requests.post(url, data=json.dumps(data))
Status = response.json()['status']
if(Status == 1):
template = 'baseTemplate/FileManager.html'
else:
2022-04-24 22:01:54 +05:00
return redirect("https://cyberpanel.net/cyberpanel-addons")
2022-04-20 16:41:25 +05:00
except BaseException as msg:
template = 'baseTemplate/FileManager.html'
if currentACL['admin'] == 1:
pass
else:
return ACLManager.loadErrorJson('FilemanagerAdmin', 0)
from plogical.httpProc import httpProc
proc = httpProc(request, template)
return proc.render()
2022-04-20 21:37:29 +05:00
def downloadFile(request):
try:
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
from urllib.parse import quote
from django.utils.encoding import iri_to_uri
fileToDownload = request.build_absolute_uri().split('fileToDownload')[1][1:]
fileToDownload = iri_to_uri(fileToDownload)
domainName = request.GET.get('domainName')
currentACL = ACLManager.loadedACL(userID)
if ACLManager.checkOwnership(domainName, admin, currentACL) == 1:
pass
else:
return ACLManager.loadErrorJson('permissionsChanged', 0)
homePath = '/home/%s' % (domainName)
if fileToDownload.find('..') > -1 or fileToDownload.find(homePath) == -1:
return HttpResponse("Unauthorized access.")
response = HttpResponse(content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % (fileToDownload.split('/')[-1])
response['X-LiteSpeed-Location'] = '%s' % (fileToDownload)
return response
except KeyError:
return redirect(loadLoginPage)
def RootDownloadFile(request):
try:
userID = request.session['userID']
from urllib.parse import quote
from django.utils.encoding import iri_to_uri
fileToDownload = request.build_absolute_uri().split('fileToDownload')[1][1:]
fileToDownload = iri_to_uri(fileToDownload)
currentACL = ACLManager.loadedACL(userID)
if currentACL['admin'] == 1:
pass
else:
return ACLManager.loadError()
response = HttpResponse(content_type='application/force-download')
response['Content-Disposition'] = 'attachment; filename=%s' % (fileToDownload.split('/')[-1])
response['X-LiteSpeed-Location'] = '%s' % (fileToDownload)
return response
#return HttpResponse(response['X-LiteSpeed-Location'])
except KeyError:
return redirect(loadLoginPage)