Files
CyberPanel/filemanager/views.py

171 lines
5.5 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
2019-01-28 15:19:59 +05:00
from .filemanager import FileManager as FM
2019-06-08 21:41:43 +00:00
from plogical.processUtilities import ProcessUtilities
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
2019-06-08 21:41:43 +00:00
ProcessUtilities.popenExecutioner(command)
2018-02-16 00:57:46 +05:00
2018-10-12 18:18:10 +05:00
command = "sudo chown -R lscpd:lscpd /home/" + domainName+"/logs"
2019-06-08 21:41:43 +00:00
ProcessUtilities.popenExecutioner(command)
command = "sudo find %s -type d -exec chmod 0755 {} \;" % ("/home/" + domainName + "/public_html")
ProcessUtilities.popenExecutioner(command)
command = "sudo find %s -type f -exec chmod 0644 {} \;" % ("/home/" + domainName + "/public_html")
ProcessUtilities.popenExecutioner(command)
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)
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)
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)
domainName = data['domainName']
2019-01-28 15:19:59 +05:00
method = data['method']
2019-01-28 15:19:59 +05:00
userID = request.session['userID']
2018-08-18 00:39:10 +05:00
admin = Administrator.objects.get(pk=userID)
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:
2019-01-28 15:19:59 +05:00
return ACLManager.loadErrorJson()
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 == '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
except BaseException, msg:
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
2019-01-28 15:19:59 +05:00
userID = request.session['userID']
admin = Administrator.objects.get(pk=userID)
currentACL = ACLManager.loadedACL(userID)
2018-04-18 15:57:49 +05:00
2019-01-28 15:19:59 +05:00
if ACLManager.checkOwnership(data['domainName'], admin, currentACL) == 1:
pass
else:
return ACLManager.loadErrorJson()
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)