Skip to content

Commit

Permalink
json_data in fetchPHPExtensions is now a list. as well as
Browse files Browse the repository at this point in the history
with open() is now used to open files and properly close them.
proper indentation is certain places.
Some repeat code is commented as being as such.
  • Loading branch information
Lvl4Sword authored Dec 2, 2023
1 parent bf841e1 commit 0246d2a
Showing 1 changed file with 32 additions and 62 deletions.
94 changes: 32 additions & 62 deletions managePHP/phpManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@


class PHPManager:

@staticmethod
def findPHPVersions():
import re
Expand All @@ -28,20 +27,16 @@ def findPHPVersions():
for item in sorted(php_versions):
# Use regular expression to find numbers in the string
numbers = re.findall(r'\d+', item)

# Join the numbers with dots and add 'PHP' back to the string
result = 'PHP ' + '.'.join(numbers)

result_list.append(result)

return sorted(result_list)

@staticmethod
def getPHPString(phpVersion):
# Ex: "PHP 5.3" type string, return Ex: "53" type string
phpVersion = phpVersion.split()
php = phpVersion[1].replace(".", "")

return php

@staticmethod
Expand Down Expand Up @@ -77,6 +72,7 @@ def getCurrentPHPConfig(phpVersion):

command = "cat " + PHPManager.FindPHPFPMPath(phpVersion)

# this is repeated code from views.py
data = ProcessUtilities.outputExecutioner(command).split('\n')

for items in data:
Expand Down Expand Up @@ -158,36 +154,28 @@ def savePHPConfigBasic(data):

tempStatusPath = "/home/cyberpanel/" + str(randint(1000, 9999))

writeToFile = open(tempStatusPath, 'w')

for items in data:
if items.find("allow_url_fopen") > -1 and items.find("=") > -1:
writeToFile.writelines(allow_url_fopen + "\n")
elif items.find("display_errors") > -1 and items.find("=") > -1:
writeToFile.writelines(display_errors + "\n")
elif items.find("file_uploads") > -1 and items.find("=") > -1 and not items.find(
"max_file_uploads") > -1:
writeToFile.writelines(file_uploads + "\n")
elif items.find("allow_url_include") > -1 and items.find("=") > -1:
writeToFile.writelines(allow_url_include + "\n")

elif items.find("memory_limit") > -1 and items.find("=") > -1:
writeToFile.writelines("memory_limit = " + memory_limit + "\n")

elif items.find("max_execution_time") > -1 and items.find("=") > -1:
writeToFile.writelines("max_execution_time = " + max_execution_time + "\n")

elif items.find("upload_max_filesize") > -1 and items.find("=") > -1:
writeToFile.writelines("upload_max_filesize = " + upload_max_filesize + "\n")

elif items.find("max_input_time") > -1 and items.find("=") > -1:
writeToFile.writelines("max_input_time = " + max_input_time + "\n")
elif items.find("post_max_size") > -1 and items.find("=") > -1:
writeToFile.writelines("post_max_size = " + post_max_size + "\n")
else:
writeToFile.writelines(items + '\n')

writeToFile.close()
with open(tempStatusPath, 'w') as writeToFile:
for items in data:
if items.find("allow_url_fopen") > -1 and items.find("=") > -1:
writeToFile.writelines(allow_url_fopen + "\n")
elif items.find("display_errors") > -1 and items.find("=") > -1:
writeToFile.writelines(display_errors + "\n")
elif items.find("file_uploads") > -1 and items.find("=") > -1 and not items.find("max_file_uploads") > -1:
writeToFile.writelines(file_uploads + "\n")
elif items.find("allow_url_include") > -1 and items.find("=") > -1:
writeToFile.writelines(allow_url_include + "\n")
elif items.find("memory_limit") > -1 and items.find("=") > -1:
writeToFile.writelines("memory_limit = " + memory_limit + "\n")
elif items.find("max_execution_time") > -1 and items.find("=") > -1:
writeToFile.writelines("max_execution_time = " + max_execution_time + "\n")
elif items.find("upload_max_filesize") > -1 and items.find("=") > -1:
writeToFile.writelines("upload_max_filesize = " + upload_max_filesize + "\n")
elif items.find("max_input_time") > -1 and items.find("=") > -1:
writeToFile.writelines("max_input_time = " + max_input_time + "\n")
elif items.find("post_max_size") > -1 and items.find("=") > -1:
writeToFile.writelines("post_max_size = " + post_max_size + "\n")
else:
writeToFile.writelines(items + '\n')

command = "mv %s %s" % (tempStatusPath, path)
ProcessUtilities.executioner(command)
Expand All @@ -211,9 +199,7 @@ def fetchPHPSettingsAdvance(phpVersion):
final_dic = {'fetchStatus': 1,
'configData': data,
'status': 1}

final_json = json.dumps(final_dic)

return HttpResponse(final_json)

@staticmethod
Expand All @@ -225,10 +211,10 @@ def savePHPConfigAdvance(data):

tempStatusPath = "/home/cyberpanel/" + str(randint(1000, 9999))

writeToFile = open(tempStatusPath, 'w')
writeToFile.write(configData)
writeToFile.close()
with open(tempStatusPath, 'w') as writeToFile:
writeToFile.write(configData)

# this is repeat code from ln 179-192
command = "mv %s %s" % (tempStatusPath, path)
ProcessUtilities.executioner(command)

Expand All @@ -246,58 +232,42 @@ def savePHPConfigAdvance(data):

@staticmethod
def fetchPHPExtensions(data):

if ApachePHP.objects.all().count() == 0:
phpfilePath = '/usr/local/CyberCP/ApachController/phpApache.xml'

# php versions
for items in ['54', '55', '56', '70', '71', '72', '73']:
phpvers = ApachePHP(phpVers='php' + items)
phpvers.save()

php = ElementTree.parse(phpfilePath)
phpExtensions = php.findall('extension')

for extension in phpExtensions:
extensionName = extension.find('extensionName').text % (items)
extensionDescription = extension.find('extensionDescription').text
status = int(extension.find('status').text)

phpExtension = installedPackagesApache(phpVers=phpvers,
extensionName=extensionName,
description=extensionDescription,
status=status)

extensionName=extensionName,
description=extensionDescription,
status=status)
phpExtension.save()

phpVers = "php" + PHPManager.getPHPString(data['phpVersion'])

phpVersion = ApachePHP.objects.get(phpVers=phpVers)

records = phpVersion.installedpackagesapache_set.all()

json_data = "["
checker = 0
json_data = []

for items in records:

if items.status == 0:
status = "Not-Installed"
else:
status = "Installed"

dic = {'id': items.id,
'phpVers': items.phpVers.phpVers,
'extensionName': items.extensionName,
'description': items.description,
'status': status
}
json_data.append(dic)

if checker == 0:
json_data = json_data + json.dumps(dic)
checker = 1
else:
json_data = json_data + ',' + json.dumps(dic)

json_data = json_data + ']'
final_json = json.dumps({'status': 1, 'error_message': "None", "data": json_data})
return HttpResponse(final_json)

0 comments on commit 0246d2a

Please sign in to comment.