Skip to content

modTemplateVar::parseBinding - third parameter is invalid JSON #16200

@sdrenth

Description

@sdrenth

Bug report

Summary

I have a template variable which contains the input option values as:

@SELECT CONCAT(`pagetitle`, ' (', `id`, ')') AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0 AND `context_key` = '[[+context_key]]'

When I open a resource that contains this template variable, the following error is logged in the MODX error log:

[2022-06-02 16:15:22] (ERROR @/core/src/Revolution/modTemplateVar.php : 1016) modTemplateVar::parseBinding - third parameter is invalid JSON :@SELECT CONCAT(`pagetitle`, ' (', `id`, ')') AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0 AND `context_key` = '[[+context_key]]'

Looking at the code it expects to contain a JSON string but it's not. I think the parseBinding method requires some additional logic maybe?

public function parseBinding($binding_string)
    {
        $match = [];
        $match2 = [];
        $binding_string = trim($binding_string);
        $regexp = '/@(' . implode('|', $this->bindings) . ')\s*(.*)/is'; /* Split binding on whitespace */

        if (preg_match($regexp, $binding_string, $match)) {
            /* We can't return the match array directly because the first element is the whole string */

            $regexp2 = '/(\S+)\s+(.+)/is'; /* Split binding on second whitespace to get properties */

            $properties = [];
            if (preg_match($regexp2, $match[2] , $match2)) {
                if (isset($match2[2])) {
                    $props = json_decode($match2[2],true);
                    $valid = json_last_error() === JSON_ERROR_NONE;
                    if ($valid && is_array($props)){
                        $properties = $props;
                        $match[2] = $match2[1];
                    } else {
                        $this->xpdo->log(modX::LOG_LEVEL_ERROR, 'modTemplateVar::parseBinding - third parameter is invalid JSON :' . $binding_string);
                    }
                }
            }

            $binding_array = [
                strtoupper($match[1]),
                trim($match[2]),
                $properties
            ]; /* Make command uppercase */

            return $binding_array;
        }
    }

Step to reproduce

  1. Create a TV with input option values:
@SELECT CONCAT(`pagetitle`, ' (', `id`, ')') AS `name`,`id` FROM `[[+PREFIX]]site_content` WHERE `published` = 1 AND `deleted` = 0 AND `context_key` = '[[+context_key]]'
  1. Open resource
  2. Open MODX error log

Observed behavior

An error is written in the error log which I don't think is okay.

Expected behavior

I'd expect this to not trigger any errors, because it is a valid/working value.

Environment

MODX 3.0.1-pl
PHP 8.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThe issue in the code or project, which should be addressed.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions