From d2b8a71c573dea6f9c6c18a537d4b8a3e389d148 Mon Sep 17 00:00:00 2001 From: usmannasir <01-134132-158@student.bahria.edu.pk> Date: Fri, 5 Oct 2018 00:00:06 +0500 Subject: [PATCH] Example Plugin --- .../templates/baseTemplate/index.html | 13 + examplePlugin/__init__.py | 1 + examplePlugin/admin.py | 6 + examplePlugin/apps.py | 11 + examplePlugin/migrations/__init__.py | 0 examplePlugin/models.py | 6 + examplePlugin/signals.py | 16 ++ examplePlugin/tests.py | 6 + examplePlugin/urls.py | 7 + examplePlugin/views.py | 9 + pluginInstaller/__init__.py | 0 pluginInstaller/examplePlugin.zip | Bin 0 -> 2336 bytes pluginInstaller/pluginInstaller.py | 253 ++++++++++++++++++ userManagment/__init__.py | 1 - userManagment/apps.py | 3 - websiteFunctions/urls.py | 5 - 16 files changed, 328 insertions(+), 9 deletions(-) create mode 100644 examplePlugin/__init__.py create mode 100644 examplePlugin/admin.py create mode 100644 examplePlugin/apps.py create mode 100644 examplePlugin/migrations/__init__.py create mode 100644 examplePlugin/models.py create mode 100644 examplePlugin/signals.py create mode 100644 examplePlugin/tests.py create mode 100644 examplePlugin/urls.py create mode 100644 examplePlugin/views.py create mode 100644 pluginInstaller/__init__.py create mode 100644 pluginInstaller/examplePlugin.zip create mode 100644 pluginInstaller/pluginInstaller.py diff --git a/baseTemplate/templates/baseTemplate/index.html b/baseTemplate/templates/baseTemplate/index.html index eb135bb4f..b298a07f1 100755 --- a/baseTemplate/templates/baseTemplate/index.html +++ b/baseTemplate/templates/baseTemplate/index.html @@ -575,6 +575,19 @@ +
  • + + + {% trans "Plugins" %} + + +
  • + diff --git a/examplePlugin/__init__.py b/examplePlugin/__init__.py new file mode 100644 index 000000000..93ca77eeb --- /dev/null +++ b/examplePlugin/__init__.py @@ -0,0 +1 @@ +default_app_config = 'examplePlugin.apps.ExamplepluginConfig' \ No newline at end of file diff --git a/examplePlugin/admin.py b/examplePlugin/admin.py new file mode 100644 index 000000000..13be29d96 --- /dev/null +++ b/examplePlugin/admin.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.contrib import admin + +# Register your models here. diff --git a/examplePlugin/apps.py b/examplePlugin/apps.py new file mode 100644 index 000000000..daac439e9 --- /dev/null +++ b/examplePlugin/apps.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.apps import AppConfig + + +class ExamplepluginConfig(AppConfig): + name = 'examplePlugin' + + def ready(self): + import signals diff --git a/examplePlugin/migrations/__init__.py b/examplePlugin/migrations/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/examplePlugin/models.py b/examplePlugin/models.py new file mode 100644 index 000000000..1dfab7604 --- /dev/null +++ b/examplePlugin/models.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import models + +# Create your models here. diff --git a/examplePlugin/signals.py b/examplePlugin/signals.py new file mode 100644 index 000000000..37168d64d --- /dev/null +++ b/examplePlugin/signals.py @@ -0,0 +1,16 @@ +from django.dispatch import receiver +from django.http import HttpResponse +from websiteFunctions.signals import postWebsiteDeletion +from plogical.CyberCPLogFileWriter import CyberCPLogFileWriter as logging + +# This plugin respond to an event after CyberPanel core finished deleting a website. +# Original request object is passed, body can be accessed with request.body. + +# If any Event handler returns a response object, CyberPanel will stop further processing and returns your response to browser. +# To continue processing just return 200 from your events handlers. + +@receiver(postWebsiteDeletion) +def rcvr(sender, **kwargs): + request = kwargs['request'] + logging.writeToFile('Hello World from Example Plugin.') + return HttpResponse('Hello World from Example Plugin.') diff --git a/examplePlugin/tests.py b/examplePlugin/tests.py new file mode 100644 index 000000000..5982e6bcd --- /dev/null +++ b/examplePlugin/tests.py @@ -0,0 +1,6 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.test import TestCase + +# Create your tests here. diff --git a/examplePlugin/urls.py b/examplePlugin/urls.py new file mode 100644 index 000000000..5c68f92d1 --- /dev/null +++ b/examplePlugin/urls.py @@ -0,0 +1,7 @@ +from django.conf.urls import url +import views + +urlpatterns = [ + + url(r'^$', views.examplePlugin, name='examplePlugin'), +] \ No newline at end of file diff --git a/examplePlugin/views.py b/examplePlugin/views.py new file mode 100644 index 000000000..c8c4e619e --- /dev/null +++ b/examplePlugin/views.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.shortcuts import render, HttpResponse + +# Create your views here. + +def examplePlugin(request): + return HttpResponse('This is homepage of an example plugin.') diff --git a/pluginInstaller/__init__.py b/pluginInstaller/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pluginInstaller/examplePlugin.zip b/pluginInstaller/examplePlugin.zip new file mode 100644 index 0000000000000000000000000000000000000000..a44c789b9c2e376debb82ad0f7dc56946e548ae6 GIT binary patch literal 2336 zcmWIWW@Zs#W&ncLB`&@o8V2})jMR$6+=86cfSl6w%sl-7s9Fw&m5W_`=Qp}b8UxkX z0_ocO3I3ur?F5Q|~hl$es6nFqAS^o%1PlOj*+ zL)+yUM%Kb|qD z%mjvmEz@6xNkCU!24Yc6*A)~L10833?mX8a10J>y)$^4e9Mn9sna8H1;ybTb8;1vj?Xzo$v1y3UK;-WUutUS0cAwJuHYB7(hV`bT=GeMrLkidQoCYW`15V*za86^n$KU z1!w?jdO^~LlpJ6dbAT=OsLuSJ1k_mt#NwDHzhK@EejLTPVMV*J8TsaYTru>L^h=}aHhs0=TnBCzHdyf z=03i=e2+E9d_|eqz8vPv*IobFt+wm1ve;e{J!-Q+PI_t6ZoS`5bFOL~eKSd~9pm%%J16j47<)+%J_g{>!%CR^5|G{F>EY8~nJFb47e#x`# z`q^W57!e)^#pv;!mP^us9f~y2|SH*Fnj`Kw3chnQ-IE@0b^vnv3 z+t{?$$wYiQlK=y&Y1fsVzAr^XeHy^M70G3YviJgtz8R0n?`Xjsyy1Ef*)qLdIYCqT*w^zt8U3fP!w7{}4dx2vA7;#4rM>=z|!8yUawm#DEFiB}m5L2_N(_6k%c! zriri;72!J$aMh1q;=v5r(#Q!c?ok5>QT`#Dg01*~nX;sji3QyhSYd)}2DW?-Gh<8R pTnsayxgOmJ^gIbOVo9S3E4njaIW)kVl?|kh8wh^_!%Yy(0|3us8yNrq literal 0 HcmV?d00001 diff --git a/pluginInstaller/pluginInstaller.py b/pluginInstaller/pluginInstaller.py new file mode 100644 index 000000000..a015696a6 --- /dev/null +++ b/pluginInstaller/pluginInstaller.py @@ -0,0 +1,253 @@ +import subprocess +import shlex +import argparse +import os +import tarfile +import shutil +import time + +class pluginInstaller: + installLogPath = "/home/cyberpanel/modSecInstallLog" + tempRulesFile = "/home/cyberpanel/tempModSecRules" + mirrorPath = "cyberpanel.net" + + @staticmethod + def stdOut(message): + print("\n\n") + print ("[" + time.strftime( + "%I-%M-%S-%a-%b-%Y") + "] #########################################################################\n") + print("[" + time.strftime("%I-%M-%S-%a-%b-%Y") + "] " + message + "\n") + print ("[" + time.strftime( + "%I-%M-%S-%a-%b-%Y") + "] #########################################################################\n") + + ### Functions Related to plugin installation. + + @staticmethod + def extractPlugin(pluginName): + pathToPlugin = pluginName + '.zip' + command = 'unzip ' + pathToPlugin + ' -d /usr/local/CyberCP' + subprocess.call(shlex.split(command)) + + @staticmethod + def upgradingSettingsFile(pluginName): + data = open("/usr/local/CyberCP/CyberCP/settings.py", 'r').readlines() + writeToFile = open("/usr/local/CyberCP/CyberCP/settings.py", 'w') + + for items in data: + if items.find("'emailPremium',") > -1: + writeToFile.writelines(items) + writeToFile.writelines(" '" + pluginName + "',\n") + else: + writeToFile.writelines(items) + + writeToFile.close() + + @staticmethod + def upgradingURLs(pluginName): + data = open("/usr/local/CyberCP/CyberCP/urls.py", 'r').readlines() + writeToFile = open("/usr/local/CyberCP/CyberCP/urls.py", 'w') + print('hello world') + + for items in data: + if items.find("manageservices") > -1: + writeToFile.writelines(items) + writeToFile.writelines(" url(r'^" + pluginName + "/',include('" + pluginName + ".urls')),\n") + print('hello world') + else: + writeToFile.writelines(items) + + writeToFile.close() + + @staticmethod + def informCyberPanel(pluginName): + pluginPath = '/home/cyberpanel/plugins' + + if not os.path.exists(pluginPath): + os.mkdir(pluginPath) + + pluginFile = pluginPath + '/' + pluginName + command = 'touch ' + pluginFile + subprocess.call(shlex.split(command)) + + @staticmethod + def addInterfaceLink(pluginName): + data = open("/usr/local/CyberCP/baseTemplate/templates/baseTemplate/index.html", 'r').readlines() + writeToFile = open("/usr/local/CyberCP/baseTemplate/templates/baseTemplate/index.html", 'w') + + pluginCheck = 0 + + for items in data: + if items.find('{% trans "Plugins" %}') > -1: + pluginCheck = 1 + elif pluginCheck == 1 and items.find('
      '): + writeToFile.writelines(items) + writeToFile.writelines('
    • {% trans "' + pluginName + '" %}
    • ') + pluginCheck = 0 + else: + writeToFile.writelines(items) + + writeToFile.close() + + @staticmethod + def installPlugin(pluginName): + try: + ## + + pluginInstaller.stdOut('Extracting plugin.') + pluginInstaller.extractPlugin(pluginName) + pluginInstaller.stdOut('Plugin extracted.') + + ## + + pluginInstaller.stdOut('Restoring settings file.') + pluginInstaller.upgradingSettingsFile(pluginName) + pluginInstaller.stdOut('Settings file restored.') + + ### + + pluginInstaller.stdOut('Upgrading URLs') + pluginInstaller.upgradingURLs(pluginName) + pluginInstaller.stdOut('URLs upgraded.') + + ## + + pluginInstaller.stdOut('Informing CyberPanel about plugin.') + pluginInstaller.informCyberPanel(pluginName) + pluginInstaller.stdOut('CyberPanel core informed about the plugin.') + + ## + + ## + + pluginInstaller.stdOut('Adding interface link..') + pluginInstaller.addInterfaceLink(pluginName) + pluginInstaller.stdOut('Interface link added.') + + ## + + pluginInstaller.restartGunicorn() + + pluginInstaller.stdOut('Plugin successfully installed.') + + except BaseException, msg: + pluginInstaller.stdOut(str(msg)) + + ### Functions Related to plugin installation. + + @staticmethod + def removeFiles(pluginName): + pluginPath = '/usr/local/CyberCP/' + pluginName + if os.path.exists(pluginPath): + shutil.rmtree(pluginPath) + + @staticmethod + def removeFromSettings(pluginName): + data = open("/usr/local/CyberCP/CyberCP/settings.py", 'r').readlines() + writeToFile = open("/usr/local/CyberCP/CyberCP/settings.py", 'w') + + for items in data: + if items.find(pluginName) > -1: + continue + else: + writeToFile.writelines(items) + writeToFile.close() + + @staticmethod + def removeFromURLs(pluginName): + data = open("/usr/local/CyberCP/CyberCP/urls.py", 'r').readlines() + writeToFile = open("/usr/local/CyberCP/CyberCP/urls.py", 'w') + + for items in data: + if items.find(pluginName) > -1: + continue + else: + writeToFile.writelines(items) + + writeToFile.close() + + @staticmethod + def informCyberPanelRemoval(pluginName): + pluginPath = '/home/cyberpanel/plugins' + pluginFile = pluginPath + '/' + pluginName + if os.path.exists(pluginFile): + os.remove(pluginFile) + + @staticmethod + def removeInterfaceLink(pluginName): + data = open("/usr/local/CyberCP/baseTemplate/templates/baseTemplate/index.html", 'r').readlines() + writeToFile = open("/usr/local/CyberCP/baseTemplate/templates/baseTemplate/index.html", 'w') + + for items in data: + if items.find(pluginName) > -1 and items.find('
    • ') > -1: + continue + else: + writeToFile.writelines(items) + writeToFile.close() + + @staticmethod + def removePlugin(pluginName): + try: + ## + + pluginInstaller.stdOut('Removing files..') + pluginInstaller.removeFiles(pluginName) + pluginInstaller.stdOut('Files removed..') + + ## + + pluginInstaller.stdOut('Restoring settings file.') + pluginInstaller.removeFromSettings(pluginName) + pluginInstaller.stdOut('Settings file restored.') + + ### + + pluginInstaller.stdOut('Upgrading URLs') + pluginInstaller.removeFromURLs(pluginName) + pluginInstaller.stdOut('URLs upgraded.') + + ## + + pluginInstaller.stdOut('Informing CyberPanel about plugin removal.') + pluginInstaller.informCyberPanelRemoval(pluginName) + pluginInstaller.stdOut('CyberPanel core informed about the plugin removal.') + + ## + + pluginInstaller.stdOut('Remove interface link..') + pluginInstaller.removeInterfaceLink(pluginName) + pluginInstaller.stdOut('Interface link removed.') + + ## + + pluginInstaller.restartGunicorn() + + pluginInstaller.stdOut('Plugin successfully removed.') + + except BaseException, msg: + pluginInstaller.stdOut(str(msg)) + + #### + + @staticmethod + def restartGunicorn(): + command = 'systemctl restart gunicorn.socket' + subprocess.call(shlex.split(command)) + + + +def main(): + + parser = argparse.ArgumentParser(description='CyberPanel Installer') + parser.add_argument('function', help='Specify a function to call!') + + parser.add_argument('--pluginName', help='Temporary path to configurations data!') + + + args = parser.parse_args() + if args.function == 'install': + pluginInstaller.installPlugin(args.pluginName) + else: + pluginInstaller.removePlugin(args.pluginName) + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/userManagment/__init__.py b/userManagment/__init__.py index ea1f3affe..e69de29bb 100644 --- a/userManagment/__init__.py +++ b/userManagment/__init__.py @@ -1 +0,0 @@ -default_app_config = 'userManagment.apps.UsermanagmentConfig' \ No newline at end of file diff --git a/userManagment/apps.py b/userManagment/apps.py index cb94ead28..a4cc35103 100644 --- a/userManagment/apps.py +++ b/userManagment/apps.py @@ -6,6 +6,3 @@ from django.apps import AppConfig class UsermanagmentConfig(AppConfig): name = 'userManagment' - - def ready(self): - import signals diff --git a/websiteFunctions/urls.py b/websiteFunctions/urls.py index da55fb78c..31d78080f 100644 --- a/websiteFunctions/urls.py +++ b/websiteFunctions/urls.py @@ -71,11 +71,6 @@ urlpatterns = [ ## Openbasedir url(r'^changeOpenBasedir$',views.changeOpenBasedir,name="changeOpenBasedir"), - - ## Application Installer - - url(r'^applicationInstaller$',views.applicationInstaller,name="applicationInstaller"), - ## WP Install url(r'^(?P([\da-z\.-]+\.[a-z\.]{2,12}|[\d\.]+)([\/:?=&#]{1}[\da-z\.-]+)*[\/\?]?)/wordpressInstall$', views.wordpressInstall, name='wordpressInstall'),