Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incompatible with PHP 8.1 / Array to String Conversion Error #81

Open
kakduman opened this issue Feb 16, 2023 · 1 comment
Open

Incompatible with PHP 8.1 / Array to String Conversion Error #81

kakduman opened this issue Feb 16, 2023 · 1 comment

Comments

@kakduman
Copy link

kakduman commented Feb 16, 2023

When creating a new server, proceeding to the "Review Order" step results in an HTTP Error 500. Logs show the following:

[2023-02-16T18:46:54.853257+00:00] general.ERROR: Uncaught Exception TypeError: "str_replace(): Argument #2 ($replace) must be of type string when argument #1 ($search) is a string" at /etc/blesta/blesta/components/modules/pterodactyl/lib/pterodactyl_rule.php line 44 {"exception":"[object] (TypeError(code: 0): str_replace(): Argument #2 ($replace) must be of type string when argument #1 ($search) is a string at /etc/blesta/blesta/components/modules/pterodactyl/lib/pterodactyl_rule.php:44)"}

This makes it impossible to order new services on PHP 8.1. Reverting to PHP 7.4 fixes the issue. However, PHP 7.4 seems unable to provision pending services automatically, instead sending this notice to logs. The cron fails to complete and perpetually has a task lock.

[2023-02-12T19:50:01.570370+00:00] general.NOTICE: E_NOTICE: Array to string conversion {"code":8,"message":"Array to string conversion","file":"/etc/blesta/blesta/components/modules/pterodactyl/lib/pterodactyl_rule.php","line":44}
[2023-02-12T19:50:02.846685+00:00] general.NOTICE: Message Not Sent {"messenger":{"missing":"No messenger is configured for the given type."}}

I am using the latest Blesta (5.6.1) and the latest Pterodactyl module (1.8.1) running on Ubuntu 20.04.

@kakduman kakduman changed the title Incompatible with PHP 8.1 Incompatible with PHP 8.1 / Array to String Conversion Error Feb 16, 2023
@JReissmueller
Copy link
Member

JReissmueller commented Mar 22, 2023

In components/modules/pterodactyl/lib/pterodactyl_rule.php replace parseEggVariable() with the following

    /**
     * Parses an egg variable from Pterodactyl and returns a Blesta input validation rule array
     *
     * @param string $eggVariable The egg variabe from Pteerodactyl to create rules for and from
     * @return array A list of Blesta rules parsed from the Pterodactyl rule string
     */
    public function parseEggVariable($eggVariable)
    {
        // Get the name of the field being validated
        $fieldName = $eggVariable->attributes->name;

        // Parse rule string for regexes and remove them to simplify parsing
        $ruleString = $eggVariable->attributes->rules;

        // Match the regex strings and store them in an array
        $regexRuleStrings = [];
        preg_match_all("/regex\\:(\\/.*?\\/)/i", $ruleString, $regexRuleStrings);

        // Remove the regex stings and replace them with {{{regex}}}
        $regexFilteredRuleString = str_replace($regexRuleStrings[1] ?? [], '{{{regex}}}', $ruleString);

        // Get a the list of OR separated validation rules
        $ruleStrings = explode('|', $regexFilteredRuleString);

        // Parse rules from the string
        $rules = [];
        foreach ($ruleStrings as $ruleString) {
            $ruleParts = explode(':', $ruleString);
            $ruleName = str_replace('_', '', lcfirst(ucwords($ruleParts[0], '_')));

            $ruleParameters = [];
            if (isset($ruleParts[1])) {
                $ruleParameters = explode(',', $ruleParts[1]);
            }

            // Re-add filtered regexes
            if (!empty($regexRuleStrings[1])) {
                foreach ($ruleParameters as &$ruleParameter) {
                    $ruleParameter = str_replace('{{{regex}}}', array_shift($regexRuleStrings[1]), $ruleParameter);
                }
            }

            // Generate validation rule
            if (method_exists($this, $ruleName)) {
                $rules[$ruleName] = call_user_func_array(
                    [$this, $ruleName],
                    [$fieldName, $ruleParameters]
                );
            }
        }

        // Make all rules conditional on field existence
        if (strpos($eggVariable->attributes->rules, 'required') === false) {
            foreach ($rules as &$rule) {
                $rule['if_set'] = true;
            }
        }

        return $rules;
    }

@JReissmueller JReissmueller mentioned this issue Mar 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants