@@ -6,6 +6,7 @@ class extension_installer {
66 private $ ispconfig_dir = '/usr/local/ispconfig ' ;
77 private $ download_url = 'https://repo.ispconfig.com/packages/ ' ;
88 private $ repo_list_url = 'https://repo.ispconfig.com/api/v1/list/ ' ;
9+ private $ repo_cache = [];
910
1011 public $ errors = [];
1112
@@ -59,8 +60,7 @@ public function checkExtensionName($extension_name, $version = null) {
5960 }
6061
6162 // check if extension exists in repository
62- $ response = file_get_contents ($ this ->getRepoListUrl ());
63- $ repo_extensions = json_decode ($ response , true );
63+ $ repo_extensions = $ this ->getRepoExtensions ();
6464
6565 if (empty ($ repo_extensions )) {
6666 $ this ->addError ('No extensions available in repository. ' );
@@ -357,8 +357,7 @@ public function download_extension($name, $version = null, $force = false) {
357357 }
358358
359359 // check if extension exists in repository
360- $ response = file_get_contents ($ this ->getRepoListUrl ());
361- $ repo_extensions = json_decode ($ response , true );
360+ $ repo_extensions = $ this ->getRepoExtensions ();
362361
363362 if (empty ($ repo_extensions )) {
364363 $ app ->log ('No extensions available in repository ' , LOGLEVEL_WARN );
@@ -669,13 +668,38 @@ public function scan_extensions() {
669668 * @return bool
670669 */
671670
672- public function install_extension ($ name , $ version = null ) {
671+ public function install_extension ($ name , $ version = null , $ install_dependencies = true ) {
673672 global $ app ;
674673
674+ // Check if the extension has dependencies
675+ if ($ install_dependencies ) {
676+ $ repo_extension = $ this ->getRepoExtension ($ name );
677+ if (!empty ($ repo_extension ['dependencies ' ])) {
678+ // split dependencies by ","
679+ $ dependencies = explode (', ' , $ repo_extension ['dependencies ' ]);
680+ foreach ($ dependencies as $ dependency ) {
681+ $ dependency = trim ($ dependency );
682+ if ($ dependency == $ name ) {
683+ continue ;
684+ }
685+ if ($ this ->isExtensionInstalled ($ dependency )) {
686+ $ app ->log ("Dependency {$ dependency } already installed " , LOGLEVEL_DEBUG );
687+ continue ;
688+ }
689+ if (!$ this ->install_extension ($ dependency , null , $ install_dependencies )) {
690+ $ this ->addError ("Failed to install dependency {$ dependency }" );
691+ return false ;
692+ } else {
693+ $ app ->log ("Installed dependency {$ dependency }" , LOGLEVEL_DEBUG );
694+ }
695+ }
696+ }
697+ }
698+
675699 // download extension if not already downloaded
676700 if (!is_dir ($ this ->extension_basedir .'/ ' .$ name )) {
677701 if (!$ this ->download_extension ($ name ,$ version )) {
678- $ this ->addError (' Failed to download extension ' . $ name );
702+ $ this ->addError (" Failed to download extension { $ name}" );
679703 return false ;
680704 }
681705 }
@@ -1015,4 +1039,52 @@ public function deleteLicense($name, $server_id) {
10151039 return true ;
10161040 }
10171041
1042+ /**
1043+ * Get the list of available extensions from the repository
1044+ * @return array
1045+ */
1046+ public function getRepoExtensions () {
1047+ if (empty ($ this ->repo_cache )) {
1048+ $ response = file_get_contents ($ this ->getRepoListUrl ());
1049+ if (empty ($ response )) {
1050+ return [];
1051+ } else {
1052+ $ this ->repo_cache = json_decode ($ response , true );
1053+ }
1054+ }
1055+
1056+ return $ this ->repo_cache ;
1057+ }
1058+
1059+ /**
1060+ * Get the repository extension
1061+ * @param string $name
1062+ * @return array|null
1063+ */
1064+ public function getRepoExtension ($ name ) {
1065+ $ repo_extensions = $ this ->getRepoExtensions ();
1066+
1067+ $ repo_extension = array_filter ($ repo_extensions , function ($ ext ) use ($ name ) {
1068+ return $ ext ['name ' ] === $ name ;
1069+ });
1070+
1071+ if (!empty ($ repo_extension )) {
1072+ return reset ($ repo_extension );
1073+ } else {
1074+ return null ;
1075+ }
1076+ }
1077+
1078+ /**
1079+ * Check if the extension is installed
1080+ * @param string $name
1081+ * @return bool
1082+ */
1083+ public function isExtensionInstalled ($ name ) {
1084+ if (!is_dir ($ this ->extension_basedir .'/ ' .$ name )) {
1085+ return false ;
1086+ }
1087+ return true ;
1088+ }
1089+
10181090}
0 commit comments