Files
CyberPanel/filemanager/views.py

132 lines
4.4 KiB
Python
Raw Normal View History

2017-12-09 22:30:10 +05:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
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
from django.http import HttpResponse,Http404
2018-02-16 00:57:46 +05:00
import json
from websiteFunctions.models import Websites
import subprocess
import shlex
import os
2018-04-18 15:57:49 +05:00
from plogical.virtualHostUtilities import virtualHostUtilities
2018-08-18 00:39:10 +05:00
from plogical.acl import ACLManager
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']
admin = Administrator.objects.get(pk=userID)
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)
if ACLManager.checkOwnership(domainName, admin, currentACL) == 1:
pass
else:
return ACLManager.loadErrorJson('permissionsChanged', 0)
2018-02-16 00:57:46 +05:00
website = Websites.objects.get(domain=domainName)
externalApp = website.externalApp
command = "sudo chown -R " + externalApp + ":" + externalApp +" /home/"+domainName
subprocess.call(shlex.split(command))
2018-10-12 18:18:10 +05:00
command = "sudo chown -R lscpd:lscpd /home/" + domainName+"/logs"
2018-02-16 00:57:46 +05:00
subprocess.call(shlex.split(command))
data_ret = {'permissionsChanged': 1, 'error_message': "None"}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
except BaseException, msg:
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)
def downloadFile(request):
2018-04-18 15:57:49 +05:00
try:
2018-04-18 15:57:49 +05:00
data = json.loads(request.body)
fileToDownload = data['fileToDownload']
response = ''
if os.path.isfile(fileToDownload):
try:
with open(fileToDownload, 'rb') as f:
response = HttpResponse(f.read(), content_type="application/octet-stream")
response['Content-Disposition'] = 'inline; filename=' + os.path.basename(fileToDownload)
except Exception as e:
raise Http404
return response
except KeyError:
return redirect(loadLoginPage)
def createTemporaryFile(request):
try:
2018-08-18 00:39:10 +05:00
userID = request.session['userID']
2018-04-18 15:57:49 +05:00
data = json.loads(request.body)
domainName = data['domainName']
2018-08-18 00:39:10 +05:00
admin = Administrator.objects.get(pk=userID)
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +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(domainName, admin, currentACL) == 1:
pass
else:
return ACLManager.loadErrorJson('createTemporaryFile', 0)
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +05:00
## Create file manager entry
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +05:00
if Websites.objects.filter(domain=domainName).exists():
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/filemanager.py"
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +05:00
execPath = execPath + " createTemporaryFile --domainName " + domainName
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +05:00
output = subprocess.check_output(shlex.split(execPath))
2018-04-18 15:57:49 +05:00
2018-08-21 13:10:40 +05:00
if output.find("0,") > -1:
data_ret = {'createTemporaryFile': 0, 'error_message': "None"}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
2018-04-18 15:57:49 +05:00
else:
2018-08-21 13:10:40 +05:00
domainRandomSeed = output.rstrip('\n')
data_ret = {'createTemporaryFile': 1, 'error_message': "None", 'domainRandomSeed': domainRandomSeed}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
2018-04-18 15:57:49 +05:00
except KeyError:
return redirect(loadLoginPage)