From c2fffdab88d92b90dcdf37a89bcad2061d242110 Mon Sep 17 00:00:00 2001
From: Ibnu Maksum <ibnumaksum@gmail.com>
Date: Fri, 2 Aug 2024 10:24:11 +0700
Subject: [PATCH] plugin manager detect old installation file

---
 system/controllers/pluginmanager.php | 92 +++++++++++++++++++++++++---
 1 file changed, 83 insertions(+), 9 deletions(-)

diff --git a/system/controllers/pluginmanager.php b/system/controllers/pluginmanager.php
index 3b55ab11..011356b3 100644
--- a/system/controllers/pluginmanager.php
+++ b/system/controllers/pluginmanager.php
@@ -38,7 +38,7 @@ switch ($action) {
         r2(U . "pluginmanager", 's', 'Refresh success');
         break;
     case 'dlinstall':
-        if($_app_stage == 'demo'){
+        if ($_app_stage == 'demo') {
             r2(U . "pluginmanager", 'e', 'Demo Mode cannot install as it Security risk');
         }
         if (!is_writeable($CACHE_PATH)) {
@@ -62,27 +62,46 @@ switch ($action) {
             $zip->open($_FILES['zip_plugin']['tmp_name']);
             $zip->extractTo($cache);
             $zip->close();
+            $plugin = basename($_FILES['zip_plugin']['name']);
             unlink($_FILES['zip_plugin']['tmp_name']);
+            $success = 0;
             //moving
             if (file_exists($cache . 'plugin')) {
                 File::copyFolder($cache . 'plugin' . DIRECTORY_SEPARATOR, $PLUGIN_PATH . DIRECTORY_SEPARATOR);
+                $success++;
             }
             if (file_exists($cache . 'paymentgateway')) {
                 File::copyFolder($cache . 'paymentgateway' . DIRECTORY_SEPARATOR, $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR);
+                $success++;
             }
             if (file_exists($cache . 'theme')) {
                 File::copyFolder($cache . 'theme' . DIRECTORY_SEPARATOR, $UI_PATH . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR);
+                $success++;
             }
             if (file_exists($cache . 'device')) {
                 File::copyFolder($cache . 'device' . DIRECTORY_SEPARATOR, $DEVICE_PATH . DIRECTORY_SEPARATOR);
+                $success++;
+            }
+            if ($success == 0) {
+                // old plugin and theme using this
+                $check = strtolower($ghUrl);
+                if (strpos($check, 'plugin') !== false) {
+                    File::copyFolder($folder, $PLUGIN_PATH . DIRECTORY_SEPARATOR);
+                } else if (strpos($check, 'payment') !== false) {
+                    File::copyFolder($folder, $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR);
+                } else if (strpos($check, 'theme') !== false) {
+                    rename($folder, $UI_PATH . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $plugin);
+                } else if (strpos($check, 'device') !== false) {
+                    File::copyFolder($folder, $DEVICE_PATH . DIRECTORY_SEPARATOR);
+                }
             }
             //Cleaning
             File::deleteFolder($cache);
             r2(U . "pluginmanager", 's', 'Installation success');
         } else if (_post('gh_url', '') != '') {
             $ghUrl = _post('gh_url', '');
-            if(!empty($config['github_token']) && !empty($config['github_username'])) {
-                $ghUrl = str_replace('https://github.com', 'https://'.$config['github_username'].':'.$config['github_token'].'@github.com', $ghUrl);
+            if (!empty($config['github_token']) && !empty($config['github_username'])) {
+                $ghUrl = str_replace('https://github.com', 'https://' . $config['github_username'] . ':' . $config['github_token'] . '@github.com', $ghUrl);
             }
             $plugin = basename($ghUrl);
             $file = $cache . $plugin . '.zip';
@@ -102,17 +121,35 @@ switch ($action) {
             $zip->extractTo($cache);
             $zip->close();
             $folder = $cache . DIRECTORY_SEPARATOR . $plugin . '-main' . DIRECTORY_SEPARATOR;
+            $success = 0;
             if (file_exists($folder . 'plugin')) {
                 File::copyFolder($folder . 'plugin' . DIRECTORY_SEPARATOR, $PLUGIN_PATH . DIRECTORY_SEPARATOR);
+                $success++;
             }
             if (file_exists($folder . 'paymentgateway')) {
                 File::copyFolder($folder . 'paymentgateway' . DIRECTORY_SEPARATOR, $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR);
+                $success++;
             }
             if (file_exists($folder . 'theme')) {
                 File::copyFolder($folder . 'theme' . DIRECTORY_SEPARATOR, $UI_PATH . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR);
+                $success++;
             }
             if (file_exists($folder . 'device')) {
                 File::copyFolder($folder . 'device' . DIRECTORY_SEPARATOR, $DEVICE_PATH . DIRECTORY_SEPARATOR);
+                $success++;
+            }
+            if ($success == 0) {
+                // old plugin and theme using this
+                $check = strtolower($ghUrl);
+                if (strpos($check, 'plugin') !== false) {
+                    File::copyFolder($folder, $PLUGIN_PATH . DIRECTORY_SEPARATOR);
+                } else if (strpos($check, 'payment') !== false) {
+                    File::copyFolder($folder, $PAYMENTGATEWAY_PATH . DIRECTORY_SEPARATOR);
+                } else if (strpos($check, 'theme') !== false) {
+                    rename($folder, $UI_PATH . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $plugin);
+                } else if (strpos($check, 'device') !== false) {
+                    File::copyFolder($folder, $DEVICE_PATH . DIRECTORY_SEPARATOR);
+                }
             }
             File::deleteFolder($cache);
             r2(U . "pluginmanager", 's', 'Installation success');
@@ -135,8 +172,8 @@ switch ($action) {
         if ($tipe == 'plugin') {
             foreach ($json['plugins'] as $plg) {
                 if ($plg['id'] == $plugin) {
-                    if(!empty($config['github_token']) && !empty($config['github_username'])) {
-                        $plg['github'] = str_replace('https://github.com', 'https://'.$config['github_username'].':'.$config['github_token'].'@github.com', $plg['github']);
+                    if (!empty($config['github_token']) && !empty($config['github_username'])) {
+                        $plg['github'] = str_replace('https://github.com', 'https://' . $config['github_username'] . ':' . $config['github_token'] . '@github.com', $plg['github']);
                     }
                     $fp = fopen($file, 'w+');
                     $ch = curl_init($plg['github'] . '/archive/refs/heads/master.zip');
@@ -186,8 +223,8 @@ switch ($action) {
         if ($tipe == 'plugin') {
             foreach ($json['plugins'] as $plg) {
                 if ($plg['id'] == $plugin) {
-                    if(!empty($config['github_token']) && !empty($config['github_username'])) {
-                        $plg['github'] = str_replace('https://github.com', 'https://'.$config['github_username'].':'.$config['github_token'].'@github.com', $plg['github']);
+                    if (!empty($config['github_token']) && !empty($config['github_username'])) {
+                        $plg['github'] = str_replace('https://github.com', 'https://' . $config['github_username'] . ':' . $config['github_token'] . '@github.com', $plg['github']);
                     }
                     $fp = fopen($file, 'w+');
                     $ch = curl_init($plg['github'] . '/archive/refs/heads/master.zip');
@@ -223,8 +260,8 @@ switch ($action) {
         } else if ($tipe == 'payment') {
             foreach ($json['payment_gateway'] as $plg) {
                 if ($plg['id'] == $plugin) {
-                    if(!empty($config['github_token']) && !empty($config['github_username'])) {
-                        $plg['github'] = str_replace('https://github.com', 'https://'.$config['github_username'].':'.$config['github_token'].'@github.com', $plg['github']);
+                    if (!empty($config['github_token']) && !empty($config['github_username'])) {
+                        $plg['github'] = str_replace('https://github.com', 'https://' . $config['github_username'] . ':' . $config['github_token'] . '@github.com', $plg['github']);
                     }
                     $fp = fopen($file, 'w+');
                     $ch = curl_init($plg['github'] . '/archive/refs/heads/master.zip');
@@ -257,6 +294,43 @@ switch ($action) {
                 }
             }
             break;
+        } else if ($tipe == 'device') {
+            foreach ($json['devices'] as $d) {
+                if ($d['id'] == $plugin) {
+                    if (!empty($config['github_token']) && !empty($config['github_username'])) {
+                        $d['github'] = str_replace('https://github.com', 'https://' . $config['github_username'] . ':' . $config['github_token'] . '@github.com', $d['github']);
+                    }
+                    $fp = fopen($file, 'w+');
+                    $ch = curl_init($d['github'] . '/archive/refs/heads/master.zip');
+                    curl_setopt($ch, CURLOPT_POST, 0);
+                    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15);
+                    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
+                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+                    curl_setopt($ch, CURLOPT_FILE, $fp);
+                    curl_exec($ch);
+                    curl_close($ch);
+                    fclose($fp);
+
+                    $zip = new ZipArchive();
+                    $zip->open($file);
+                    $zip->extractTo($CACHE_PATH);
+                    $zip->close();
+                    $folder = $CACHE_PATH . File::pathFixer('/' . $plugin . '-main/');
+                    if (!file_exists($folder)) {
+                        $folder = $CACHE_PATH . File::pathFixer('/' . $plugin . '-master/');
+                    }
+                    if (!file_exists($folder)) {
+                        r2(U . "pluginmanager", 'e', 'Extracted Folder is unknown');
+                    }
+                    File::copyFolder($folder, $DEVICE_PATH . DIRECTORY_SEPARATOR, ['README.md', 'LICENSE']);
+                    File::deleteFolder($folder);
+                    unlink($file);
+                    r2(U . "paymentgateway", 's', 'Payment Gateway ' . $plugin . ' has been installed');
+                    break;
+                }
+            }
+            break;
         }
     default:
         if (class_exists('ZipArchive')) {