HEX
Server: Apache
System: Linux info 3.0 #1337 SMP Tue Jan 01 00:00:00 CEST 2000 all GNU/Linux
User: u112718762 (3890930)
PHP: 8.1.34
Disabled: NONE
Upload Files
File: //homepages/oneclick/WordPress/6.9/601/scripts/factory/wrapper.php
<?php
namespace Factory;

use \Core\Database;
use \Core\Environment;
use \Core\File;
use \Core\Util;

use \Exception;

abstract class Wrapper implements APS
{
    protected $env;
    protected $db;
    protected $appName;

    public function __construct($appName, Environment $env, Database $db)
    {
        $this->env = $env;
        $this->db = $db;
        $this->appName = $appName;
    }

    protected function managedMode()
    {
        // check if managedMode env var
        if ($this->isManaged() || $this->isSafemode()) {
            return true;
        }

        return false;
    }

    /**
     * Check if installation is a Managed WordPress based on the env vars
     *
     * @return boolean
     */
    protected function isManaged()
    {
        return filter_var($this->env->get('managedMode'), FILTER_VALIDATE_BOOLEAN);
    }

    /**
     * Check if installation is a Safemode WordPress based on the installation folder
     *
     * @return boolean
     */
    protected function isSafemode()
    {
        // check safemode installation
        if ($this->webspace()) {
            return
                preg_match("/^app\d{5,15}$/", basename($this->webspace())) ||
                preg_match("/^trial\d{9,15}$/", basename($this->webspace()));
        }

        return false;
    }

    protected function opcache()
    {
        return filter_var($this->env->get('opcache'), FILTER_VALIDATE_BOOLEAN);
    }

    protected function domain($current = '')
    {
        return $this->env->get("{$current}BASE_URL_HOST");
    }

    protected function baseUrlScheme($current = '')
    {
        return $this->env->get("{$current}BASE_URL_SCHEME");
    }

    protected function baseUrlPath($current = '')
    {
        $path = $this->env->get("{$current}BASE_URL_PATH");

        if (('/' === $path) || ('.' === $path) || ('' === $path) || (' ' === $path)) {
            return '/';
        } else {
            return '/' . trim($path, '/') . '/';
        }
    }

    protected function absoluteUrl($current = '')
    {
        return $this->domain($current) . $this->baseUrlPath($current);
    }

    protected function dbHost($current = '')
    {
        return $this->env->get("{$current}DB_main_HOST");
    }

    protected function dbUsername($current = '')
    {
        return $this->env->get("{$current}DB_main_LOGIN");
    }

    protected function dbName($current = '')
    {
        return $this->env->get("{$current}DB_main_NAME");
    }

    protected function dbPasswd($current = '')
    {
        return $this->env->get("{$current}DB_main_PASSWORD");
    }

    protected function dbPrefix($current = '')
    {
        return $this->env->get("{$current}DB_main_PREFIX");
    }

    protected function dbPrefixFromFallbackParam($current = '')
    {
        return $this->env->get("{$current}dbPrefix");
    }

    protected function dbOldPrefix($current = '')
    {
        return $this->env->get("{$current}OLD_DB_main_PREFIX");
    }

    protected function username($current = '')
    {
        return $this->env->get("{$current}SETTINGS_admin_name");
    }

    protected function firstName($current = '')
    {
        return $this->env->get("{$current}SETTINGS_admin_firstname");
    }

    protected function lastName($current = '')
    {
        return $this->env->get("{$current}SETTINGS_admin_lastname");
    }

    protected function password($current = '')
    {
        return $this->env->get("{$current}SETTINGS_admin_password");
    }

    protected function email($current = '')
    {
        return $this->env->get("{$current}SETTINGS_admin_email");
    }

    protected function websiteName($current = '')
    {
        return $this->env->get("{$current}SETTINGS_title");
    }

    protected function locale($current = '')
    {
        $locale = $this->env->get("{$current}SETTINGS_locale");
        $locale = Util::replace('-', '_', $locale);

        return $locale;
    }
    
    protected function market()
    {
        $market = $this->env->get('market');
        if (! $market) {
            $market = 'US';
        }
        return $market;
    }

    protected function language($current = '')
    {
        return substr($this->locale($current), 0, 2);
    }

    protected function country($current = '')
    {
        return strtolower(substr($this->locale($current), -2));
    }

    protected function webspace($current = '')
    {
        return $this->env->get("{$current}WEB___DIR");
    }

    protected function package()
    {
        return dirname(dirname(__DIR__)) . '/custom/' . $this->appName;
    }

    protected function execShell($command, $print = true, $raw = false)
    {
        if (!$raw) {
            $command = escapeshellcmd($command);
        }

        exec($command . ' 2>&1', $output, $return_var);

        $buffer = null;

        if ($return_var !== 0) {
            print_r($output);
            throw new Exception("Failed command: $command");
        } else {
            foreach ($output as $line) {
                if (true === $print) {
                    print($line . "\n");
                } else {
                    $buffer .= $line . "\n";
                }
            }
        }

        return $buffer;
    }

    protected function escapeShell($input)
    {
        return escapeshellarg($input);
    }

    protected function wpCli($command, $print = true, $skip = true, $raw = false, $haltOnError = false)
    {
        $command .=
            " --allow-root" .
            " --path=" . escapeshellarg($this->webspace());

        if ($skip) {
            $command .= " --skip-plugins --skip-themes";
        }

        try {
            $result = $this->execShell($command, $print, $raw);
        } catch (Exception $e) {
            if ($haltOnError) {
                throw $e;
            }

            echo $e->getMessage();
        }

        return $result;
    }

    protected function htaccess()
    {
        $htaccess = File::readFile($this->package() . '/.htaccess');
        $htaccess = Util::replace('#BASE_URL_PATH#', $this->baseUrlPath(), $htaccess);

        file_put_contents($this->webspace() . '/.htaccess', $htaccess);

        return (string)$htaccess;
    }

    public function patch()
    {
        throw new Exception('Patch mode not implemented.');
    }

    public function remove()
    {
        $this->db->drop($this->dbPrefix());
    }
}