Files
CyberPanel/filemanager/views.py

161 lines
5.6 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']
2017-12-09 22:30:10 +05:00
2018-08-18 00:39:10 +05:00
admin = Administrator.objects.get(pk=userID)
2017-12-09 22:30:10 +05:00
2018-04-18 15:57:49 +05:00
if Websites.objects.filter(domain=domain).exists():
2018-08-18 00:39:10 +05:00
currentACL = ACLManager.loadedACL(userID)
if currentACL['admin'] == 1:
2018-04-18 15:57:49 +05:00
viewStatus = 1
if admin.type == 3:
viewStatus = 0
return render(request, 'filemanager/index.html', {"viewStatus": viewStatus})
else:
website = Websites.objects.get(domain=domain)
if website.admin == admin:
viewStatus = 1
if admin.type == 3:
viewStatus = 0
return render(request, 'filemanager/index.html', {"viewStatus": viewStatus})
else:
return HttpResponse("Domain ownership error.")
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:
val = request.session['userID']
try:
data = json.loads(request.body)
domainName = data['domainName']
website = Websites.objects.get(domain=domainName)
externalApp = website.externalApp
command = "sudo chown -R " + externalApp + ":" + externalApp +" /home/"+domainName
subprocess.call(shlex.split(command))
command = "sudo chown -R nobody:nobody /home/" + domainName+"/logs"
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
## Create file manager entry
if Websites.objects.filter(domain=domainName).exists():
2018-08-18 00:39:10 +05:00
currentACL = ACLManager.loadedACL(userID)
if currentACL['admin'] == 1:
2018-04-18 15:57:49 +05:00
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/filemanager.py"
execPath = execPath + " createTemporaryFile --domainName " + domainName
output = subprocess.check_output(shlex.split(execPath))
if output.find("0,") > -1:
data_ret = {'createTemporaryFile': 0, 'error_message': "None"}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
else:
domainRandomSeed = output.rstrip('\n')
data_ret = {'createTemporaryFile': 1, 'error_message': "None", 'domainRandomSeed':domainRandomSeed}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
else:
website = Websites.objects.get(domain=domainName)
if website.admin == admin:
execPath = "sudo python " + virtualHostUtilities.cyberPanel + "/plogical/filemanager.py"
execPath = execPath + " createTemporaryFile --domainName " + domainName
output = subprocess.check_output(shlex.split(execPath))
if output.find("0,") > -1:
data_ret = {'createTemporaryFile': 0, 'error_message': "None"}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
else:
domainRandomSeed = output.rstrip('\n')
data_ret = {'createTemporaryFile': 1, 'error_message': "None", 'domainRandomSeed': domainRandomSeed}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
else:
data_ret = {'createTemporaryFile': 0, 'error_message': "Domain ownership error."}
json_data = json.dumps(data_ret)
return HttpResponse(json_data)
except KeyError:
return redirect(loadLoginPage)