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/config/post-configure.php
<?php

class PostConfigure
{
    public function __construct($argv)
    {
        $uid = $argv[1];
        $gid = $argv[2];
        $ftpusers = $argv[2];
        $mode = $argv[3];

        $webspace = $this->get('WEB___DIR');

        try {
            if ($mode === 'upgrade') {
                if (file_exists("$webspace/.htaccess.ionos")) {
                    unlink("$webspace/.htaccess");
                    copy("$webspace/.htaccess.ionos", "$webspace/.htaccess");
                    unlink("$webspace/.htaccess.ionos");
                }

                try {
                    $this->wpCli('wp rewrite flush --hard', $webspace);
                } catch (Exception $e) {}
            }

            $this->setPermissions($webspace, $ftpusers, $uid, $gid);
            $this->phpIni($webspace);
            $this->ensureOpcacheFolder($webspace, $uid, $gid);

            exit(0);
        } catch (Exception $e) {
            echo $e;
            exit(1);
        }
    }

    /**
     * @param string $varname
     *
     * @return array | false | string
     * @throws Exception
     */
    protected function get($varname)
    {
        $value = getenv($varname);

        if ($value === false) {
            throw new Exception("Undefined variable: $varname");
        }

        return $value;
    }

    /**
     * @return boolean
     * @throws Exception
     */
    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
     * @throws Exception
     */
    protected function isManaged()
    {
        return filter_var($this->get('managedMode'), FILTER_VALIDATE_BOOLEAN);
    }

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

        return false;
    }

    protected function wpCli($command, $webspace, $print = true, $skip = true)
    {
        $command .=
            " --allow-root" .
            " --path='$webspace'";

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

        $this->execShell($command, $print);
    }

    /**
     * @param string $command
     * @param boolean $print
     *
     * @throws Exception
     */
    protected function execShell($command, &$print = true)
    {
        exec($command . ' 2>&1', $output, $return_var);

        if ($return_var !== 0) {
            print_r($output);
            throw new Exception("Failed command: $command");
        } else {
            if (true === $print) {
                for ($i = 0; $i < count($output); $i++) {
                    print("$output[$i]\n");
                }
            }
        }
    }

    /**
     * @param array $commands
     *
     * @throws Exception
     */
    protected function execShells(array $commands)
    {
        foreach ($commands as $command) {
            $this->execShell($command);
        }
    }

    /**
     * @param string $dir
     * @param string $target
     *
     * @return array
     */
    protected function searchForFile($dir, $target)
    {
        $files = scandir($dir);
        $list = array();

        foreach ($files as $file) {
            if ($file != '.' && $file != '..') {
                if ($file === $target) {
                    $list[] = $dir . '/' . $file;
                }

                if (is_dir($dir . '/' . $file)) {
                    $subList = $this->searchForFile($dir . '/' . $file, $target);
                    $list = array_merge($list, $subList);
                }
            }
        }

        return $list;
    }

    /**
     * @param string $ftpusers
     * @param string $uid
     * @param string $gid
     *
     * @throws Exception
     */
    protected function setPermissions($webspace, $ftpusers, $uid, $gid)
    {
        if ($this->managedMode()) {
            $this->execShells(array(
                "chown -R $uid:$gid $webspace",
                "find $webspace -type d -exec chmod 755 {} \\;",
                "find $webspace -type f -exec chmod 644 {} \\;"
            ));
        }
    }

    /**
     * Configure php.ini
     */
    protected function phpIni($webspace)
    {
        $domainID = explode('htdocs', $webspace)[0];
        $iniList = $this->searchForFile($webspace, 'php.ini');

        $phpIni = file_get_contents(dirname(dirname(dirname(__FILE__))) . '/custom/wordpress/php.ini');
        $phpIni = $this->replace('#OPFOLDER#', $domainID, $phpIni);

        foreach ($iniList as $ini) {
            file_put_contents($ini, $phpIni);
        }
    }

    /**
     * @param string $webspace
     * @param string $uid
     * @param string $gid
     *
     * @throws Exception
     */
    protected function ensureOpcacheFolder($webspace, $uid, $gid)
    {
        $domainID = explode('htdocs', $webspace)[0];
        $opcache = $domainID . 'htdocs/.opcache';

        if (!is_dir($opcache)) {
            mkdir($opcache, 0775, true);
            $this->execShells(array(
                "chown -R $uid:$gid $opcache",
                "chmod -R 775 $opcache"
            ));
        }
    }

    /**
     * @param string $search
     * @param string $replace
     * @param string $subject
     *
     * @return array | string
     */
    public static function replace($search, $replace, $subject)
    {
        if (!is_array($subject)) {
            $searches = is_array($search) ? array_values($search) : array($search);
            $replacements = is_array($replace) ? array_values($replace) : array($replace);
            $replacements = array_pad($replacements, count($searches), '');

            foreach ($searches as $key => $search) {
                $parts = mb_split(preg_quote($search), $subject);
                $subject = implode($replacements[$key], $parts);
            }
        } else {
            foreach ($subject as $key => $value) {
                $subject[$key] = self::replace($search, $replace, $value);
            }
        }

        return $subject;
    }
}