From 6a4e63bf93d7ed51a1a062f34f4c931808fb13ba Mon Sep 17 00:00:00 2001 From: usmannasir Date: Sun, 26 Nov 2023 14:32:35 +0400 Subject: [PATCH] dynamically detect php version in ref to: https://community.cyberpanel.net/t/php-8-2-in-cyberpanel/49703 --- managePHP/phpManager.py | 32 +++++-- plogical/acl.py | 70 ++++++++++++++- plogical/applicationInstaller.py | 150 +++++++++++++++++++++---------- plogical/phpUtilities.py | 14 ++- plogical/sslUtilities.py | 4 +- plogical/sslv2.py | 3 +- plogical/test.py | 13 +++ 7 files changed, 227 insertions(+), 59 deletions(-) diff --git a/managePHP/phpManager.py b/managePHP/phpManager.py index f21d9674c..9f2a118ad 100755 --- a/managePHP/phpManager.py +++ b/managePHP/phpManager.py @@ -12,14 +12,30 @@ class PHPManager: @staticmethod def findPHPVersions(): - distro = ProcessUtilities.decideDistro() - if distro == ProcessUtilities.centos: - return ['PHP 5.3', 'PHP 5.4', 'PHP 5.5', 'PHP 5.6', 'PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] - elif distro == ProcessUtilities.cent8: - return ['PHP 7.1','PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] - elif distro == ProcessUtilities.ubuntu20: - return ['PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] - else: + # distro = ProcessUtilities.decideDistro() + # if distro == ProcessUtilities.centos: + # return ['PHP 5.3', 'PHP 5.4', 'PHP 5.5', 'PHP 5.6', 'PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] + # elif distro == ProcessUtilities.cent8: + # return ['PHP 7.1','PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] + # elif distro == ProcessUtilities.ubuntu20: + # return ['PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] + # else: + # return ['PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] + + try: + + # Run the shell command and capture the output + result = ProcessUtilities.outputExecutioner('ls -la /usr/local/lsws') + + # Get the lines containing 'lsphp' in the output + lsphp_lines = [line for line in result.split('\n') if 'lsphp' in line] + + # Extract the version from the lines and format it as 'PHP x.y' + php_versions = ['PHP ' + line.split()[8][5] + '.' + line.split()[8][6:] for line in lsphp_lines] + + # Now php_versions contains the formatted PHP versions + return php_versions + except BaseException as msg: return ['PHP 7.0', 'PHP 7.1', 'PHP 7.2', 'PHP 7.3', 'PHP 7.4', 'PHP 8.0', 'PHP 8.1'] @staticmethod diff --git a/plogical/acl.py b/plogical/acl.py index ea9f5d68b..46e2688f3 100644 --- a/plogical/acl.py +++ b/plogical/acl.py @@ -1,6 +1,7 @@ #!/usr/local/CyberCP/bin/python import os,sys +from ApachController.ApacheVhosts import ApacheVhost from manageServices.models import PDNSStatus from .processUtilities import ProcessUtilities @@ -674,8 +675,6 @@ def findAllWebsites(currentACL, userID): @staticmethod def checkOwnership(domain, admin, currentACL): - - try: childDomain = ChildDomains.objects.get(domain=domain) @@ -995,3 +994,70 @@ def FetchCloudFlareAPIKeyFromAcme(): except BaseException as msg: return 0, str(msg), None + + @staticmethod + def FindDocRootOfSite(vhostConf,domainName): + try: + if vhostConf == None: + vhostConf = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf' + + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + command = "awk '/docRoot/ {print $2}' " + vhostConf + docRoot = ProcessUtilities.outputExecutioner(command, 'root', True).rstrip('\n') + #docRoot = docRoot.replace('$VH_ROOT', f'/home/{domainName}') + return docRoot + else: + command = "awk '/DocumentRoot/ {print $2; exit}' " + vhostConf + docRoot = ProcessUtilities.outputExecutioner(command, 'root', True).rstrip('\n') + return docRoot + except: + pass + + @staticmethod + def ReplaceDocRoot(vhostConf, domainName, NewDocRoot): + try: + if vhostConf == None: + vhostConf = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf' + + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + #command = f"sed -i 's/docRoot\s\s*.*/docRoot {NewDocRoot}/g " + vhostConf + command = f"sed -i 's#docRoot\s\s*.*#docRoot {NewDocRoot}#g' " + vhostConf + ProcessUtilities.executioner(command, 'root', True) + else: + command = f"sed -i 's#DocumentRoot\s\s*[^[:space:]]*#DocumentRoot {NewDocRoot}#g' " + vhostConf + ProcessUtilities.executioner(command, 'root', True) + + except: + pass + + @staticmethod + def FindDocRootOfSiteApache(vhostConf, domainName): + try: + finalConfPath = ApacheVhost.configBasePath + domainName + '.conf' + + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + + if os.path.exists(finalConfPath): + command = "awk '/DocumentRoot/ {print $2; exit}' " + finalConfPath + docRoot = ProcessUtilities.outputExecutioner(command, 'root', True).rstrip('\n') + return docRoot + else: + return None + else: + return None + + except: + return None + + @staticmethod + def ReplaceDocRootApache(vhostConf, domainName, NewDocRoot): + try: + finalConfPath = ApacheVhost.configBasePath + domainName + '.conf' + + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + command = f"sed -i 's#DocumentRoot\s\s*[^[:space:]]*#DocumentRoot {NewDocRoot}#g' " + finalConfPath + ProcessUtilities.executioner(command, 'root', True) + except: + pass + + diff --git a/plogical/applicationInstaller.py b/plogical/applicationInstaller.py index 6a2dcb626..862278330 100755 --- a/plogical/applicationInstaller.py +++ b/plogical/applicationInstaller.py @@ -4,6 +4,7 @@ import shutil import time +from ApachController.ApacheVhosts import ApacheVhost from loginSystem.models import Administrator from managePHP.phpManager import PHPManager from plogical.acl import ACLManager @@ -112,7 +113,7 @@ def installMautic(self): vhFile = f'/usr/local/lsws/conf/vhosts/{domainName}/vhost.conf' - phpPath = phpUtilities.GetPHPVersionFromFile(vhFile) + phpPath = phpUtilities.GetPHPVersionFromFile(vhFile, domainName) ### basically for now php 8.0 is being checked @@ -188,6 +189,13 @@ def installMautic(self): command = 'mkdir -p ' + finalPath ProcessUtilities.executioner(command, externalApp) + command = f'rm -rf {finalPath}*' + ProcessUtilities.executioner(command, externalApp) + + command = f'rm -rf {finalPath}.*' + ProcessUtilities.executioner(command, externalApp) + + ## checking for directories/files if self.dataLossCheck(finalPath, tempStatusPath, externalApp) == 0: @@ -199,16 +207,20 @@ def installMautic(self): statusFile.writelines('Downloading Mautic Core,30') statusFile.close() - command = "wget https://github.com/mautic/mautic/releases/download/%s/%s.zip" % ( - ApplicationInstaller.MauticVersion, ApplicationInstaller.MauticVersion) - ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath) + #command = "wget https://github.com/mautic/mautic/releases/download/%s/%s.zip" % ( + #ApplicationInstaller.MauticVersion, ApplicationInstaller.MauticVersion) + + ### replace command with composer install + command = f'{phpPath} /usr/bin/composer create-project mautic/recommended-project:^4 {finalPath}' + ProcessUtilities.outputExecutioner(command, externalApp, None) statusFile = open(tempStatusPath, 'w') statusFile.writelines('Extracting Mautic Core,50') statusFile.close() - command = "unzip %s.zip" % (ApplicationInstaller.MauticVersion) - ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath) + ### replace command with composer install + #command = "unzip %s.zip" % (ApplicationInstaller.MauticVersion) + #ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath) ## @@ -222,53 +234,99 @@ def installMautic(self): else: finalURL = domainName - ACLManager.CreateSecureDir() - localDB = '%s/%s' % ('/usr/local/CyberCP/tmp', str(randint(1000, 9999))) - - localDBContent = """ 'localhost', - 'db_table_prefix' => null, - 'db_port' => 3306, - 'db_name' => '%s', - 'db_user' => '%s', - 'db_password' => '%s', - 'db_backup_tables' => true, - 'db_backup_prefix' => 'bak_', - 'admin_email' => '%s', - 'admin_password' => '%s', - 'mailer_transport' => null, - 'mailer_host' => null, - 'mailer_port' => null, - 'mailer_user' => null, - 'mailer_password' => null, - 'mailer_api_key' => null, - 'mailer_encryption' => null, - 'mailer_auth_mode' => null, -);""" % (dbName, dbUser, dbPassword, email, password) - - writeToFile = open(localDB, 'w') - writeToFile.write(localDBContent) - writeToFile.close() - - command = 'rm -rf %s/app/config/local.php' % (finalPath) - ProcessUtilities.executioner(command, externalApp) - - command = 'chown %s:%s %s' % (externalApp, externalApp, localDB) - ProcessUtilities.executioner(command) - command = 'cp %s %s/app/config/local.php' % (localDB, finalPath) - ProcessUtilities.executioner(command, externalApp) +# ACLManager.CreateSecureDir() +# localDB = '%s/%s' % ('/usr/local/CyberCP/tmp', str(randint(1000, 9999))) +# +# localDBContent = """ 'localhost', +# 'db_table_prefix' => null, +# 'db_port' => 3306, +# 'db_name' => '%s', +# 'db_user' => '%s', +# 'db_password' => '%s', +# 'db_backup_tables' => true, +# 'db_backup_prefix' => 'bak_', +# 'admin_email' => '%s', +# 'admin_password' => '%s', +# 'mailer_transport' => null, +# 'mailer_host' => null, +# 'mailer_port' => null, +# 'mailer_user' => null, +# 'mailer_password' => null, +# 'mailer_api_key' => null, +# 'mailer_encryption' => null, +# 'mailer_auth_mode' => null, +# );""" % (dbName, dbUser, dbPassword, email, password) +# +# writeToFile = open(localDB, 'w') +# writeToFile.write(localDBContent) +# writeToFile.close() + + #command = 'rm -rf %s/app/config/local.php' % (finalPath) + #ProcessUtilities.executioner(command, externalApp) + + #command = 'chown %s:%s %s' % (externalApp, externalApp, localDB) + #ProcessUtilities.executioner(command) + + #command = 'cp %s %s/app/config/local.php' % (localDB, finalPath) + #ProcessUtilities.executioner(command, externalApp) + + ### replace install command with comspoer soo + #command = f"{phpPath} bin/console mautic:install http://%s -f" % (finalURL) + + command = f"{phpPath} bin/console mautic:install --db_host='localhost' --db_name='{dbName}' --db_user='{dbUser}' --db_password='{dbPassword}' --admin_username='{username}' --admin_email='{email}' --admin_password='{password}' --db_port='3306' http://{finalURL} -f" - command = f"{phpPath} bin/console mautic:install http://%s -f" % (finalURL) result = ProcessUtilities.outputExecutioner(command, externalApp, None, finalPath) if result.find('Install complete') == -1: raise BaseException(result) - os.remove(localDB) + + ExistingDocRoot = ACLManager.FindDocRootOfSite(None, domainName) + + if ExistingDocRoot.find('docroot') > -1: + ExistingDocRoot = ExistingDocRoot.replace('docroot', '') + + + NewDocRoot = f'{ExistingDocRoot}/docroot' + ACLManager.ReplaceDocRoot(None, domainName, NewDocRoot) + + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: + + try: + + ExistingDocRootApache = ACLManager.FindDocRootOfSiteApache(None, domainName) + + if ExistingDocRootApache.find('docroot') == -1: + NewDocRootApache = f'{ExistingDocRootApache}docroot' + else: + NewDocRootApache = ExistingDocRootApache + + if ExistingDocRootApache != None: + ACLManager.ReplaceDocRootApache(None, domainName, NewDocRootApache) + except: + pass + + ### fix incorrect rules in .htaccess of mautic + + if ProcessUtilities.decideServer() == ProcessUtilities.ent: + htAccessPath = f'{finalPath}docroot/.htaccess' + + command = f"sed -i '/# Fallback for Apache < 2.4/,/<\/IfModule>/d' {htAccessPath}" + ProcessUtilities.executioner(command, externalApp, True) + + command = f"sed -i '/# Apache 2.4+/,/<\/IfModule>/d' {htAccessPath}" + ProcessUtilities.executioner(command, externalApp, True) + + + #os.remove(localDB) + command = f"systemctl restart {ApacheVhost.serviceName}" + ProcessUtilities.normalExecutioner(command) + installUtilities.reStartLiteSpeedSocket() statusFile = open(tempStatusPath, 'w') diff --git a/plogical/phpUtilities.py b/plogical/phpUtilities.py index 4a0a130d7..c16d09046 100755 --- a/plogical/phpUtilities.py +++ b/plogical/phpUtilities.py @@ -1,4 +1,7 @@ import sys + +from ApachController.ApacheVhosts import ApacheVhost + sys.path.append('/usr/local/CyberCP') from plogical import CyberCPLogFileWriter as logging import subprocess @@ -217,7 +220,16 @@ def GetStagingInJson(stagings): return msg @staticmethod - def GetPHPVersionFromFile(vhFile): + def GetPHPVersionFromFile(vhFile, domainName=None): + finalConfPath = ApacheVhost.configBasePath + domainName + '.conf' + if os.path.exists(finalConfPath): + command = f'grep -Eo -m 1 "php[0-9]+" {finalConfPath} | sed -n "1p"' + result = ProcessUtilities.outputExecutioner(command, None, True).rstrip('\n') + result = f'/usr/local/lsws/ls{result}/bin/lsphp' + result = result.rsplit("lsphp", 1)[0] + "php" + return result + + if ProcessUtilities.decideServer() == ProcessUtilities.OLS: command = f'grep -Eo "/usr/local/lsws/lsphp[0-9]+/bin/lsphp" {vhFile}' result = ProcessUtilities.outputExecutioner(command, None, True).rstrip('\n') diff --git a/plogical/sslUtilities.py b/plogical/sslUtilities.py index 22c9880a5..d81d2ce1b 100755 --- a/plogical/sslUtilities.py +++ b/plogical/sslUtilities.py @@ -10,6 +10,7 @@ from websiteFunctions.models import ChildDomains, Websites except: pass +from plogical.acl import ACLManager class sslUtilities: @@ -320,7 +321,8 @@ def installSSLForDomain(virtualHostName, adminEmail='example@example.org'): except BaseException as msg: website = Websites.objects.get(domain=virtualHostName) externalApp = website.externalApp - DocumentRoot = ' DocumentRoot /home/' + virtualHostName + '/public_html\n' + docRoot = ACLManager.FindDocRootOfSite(None, virtualHostName) + DocumentRoot = f' DocumentRoot {docRoot}\n' data = open(completePathToConfigFile, 'r').readlines() phpHandler = '' diff --git a/plogical/sslv2.py b/plogical/sslv2.py index ee4a4f5e7..97967adb1 100755 --- a/plogical/sslv2.py +++ b/plogical/sslv2.py @@ -270,7 +270,8 @@ def installSSLForDomain(virtualHostName, adminEmail='example@example.org'): except BaseException as msg: website = Websites.objects.get(domain=virtualHostName) externalApp = website.externalApp - DocumentRoot = ' DocumentRoot /home/' + virtualHostName + '/public_html\n' + docRoot = ACLManager.FindDocRootOfSite(None, virtualHostName) + DocumentRoot = f' DocumentRoot {docRoot}\n' data = open(completePathToConfigFile, 'r').readlines() phpHandler = '' diff --git a/plogical/test.py b/plogical/test.py index e69de29bb..e66d26509 100644 --- a/plogical/test.py +++ b/plogical/test.py @@ -0,0 +1,13 @@ +import subprocess + +# Run the shell command and capture the output +result = subprocess.run(['ls', '-la'], capture_output=True, text=True) + +# Get the lines containing 'lsphp' in the output +lsphp_lines = [line for line in result.stdout.split('\n') if 'lsphp' in line] + +# Extract the version from the lines +php_versions = [line.split()[8] for line in lsphp_lines] + +print(php_versions) +