blesta / module-pterodactyl

Blesta API integration with Pterodactyl
MIT License
12 stars 7 forks source link

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

Open kakduman opened 1 year ago

kakduman commented 1 year ago

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.

JReissmueller commented 1 year ago

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;
    }