Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Problem in \StringUtil::parseSimpleTokens with hyphens #8436

Closed
fritzmg opened this issue Aug 11, 2016 · 8 comments
Closed

Problem in \StringUtil::parseSimpleTokens with hyphens #8436

fritzmg opened this issue Aug 11, 2016 · 8 comments
Assignees
Labels
Milestone

Comments

@fritzmg
Copy link
Contributor

fritzmg commented Aug 11, 2016

This issue arises in combination with terminal42/contao-leads for example. Suppose you created a form in the form generator with a field named E-mail. Now suppose you also want to create a lead with that and you use ##E-mail## in the Record label setting, so that this field is shown in the backend, when viewing the entries of that lead.

However, if you try to do that, you won't be able to view that lead in the backend, because there will be a script timeout or a memory overflow. The \Contao\StringUtil::parseSimpleTokens function does not work if you have a simple token with a hyphen (-) in it.

So currently you simply have to either avoid using field names with hyphens or avoid using field names with hyphens in the Record label - however this is something that won't be clear to the regular user.

@Toflar suggested that may be \StringUtil::parseSimpleTokens should convert invalid chars first before running

preg_replace('/##([A-Za-z0-9_]+)##/i', '<?php echo $arrData[\'$1\']; ?>', $strReturn);
@Toflar
Copy link
Member

Toflar commented Aug 11, 2016

@leofeyer You can assign this to me, I'll fix it.

@fritzmg
Copy link
Contributor Author

fritzmg commented Aug 11, 2016

Ok, the problem isn't actually in the Contao core. The function simply ignores such simple tokens, however, this causes an endless recursion in \Haste\Util\StringUtil::recursiveReplaceTokensAndTags - and that's the problem.

Sort of related to this problem: #8403 (i.e., if hyphens in field names would not be allowed, this problem would not occurs - however this can't be the solution in this case, I guess).

@Toflar
Copy link
Member

Toflar commented Aug 11, 2016

This needs to be marked up for discussion anyway because I'm not happy with the behaviour of Contao at all.

@aschempp
Copy link
Member

The solution should be fairly simple:

$strReturn = str_replace(
    array_map(
        function ($v) {
            return '##' . $v . '##';
        },
        array_keys($arrData)
    ),
    $arrData,
    $strReturn
);

preg_replace('/##([A-Za-z0-9_]+)##/i', '', $strReturn);

@aschempp
Copy link
Member

aschempp commented Aug 11, 2016

next version:

$stReturn = preg_replace_callback(
    '/##([^#\s]+)##/i',
    function (array $matches) use ($arrData) {
        if (!isset($arrData[$matches[1]]) {
            System::log(sprintf('Unknown simple token "%s"', $matches[1]));
            return '';
        }

        return $arrData[$matches[1]];
    },
    $strReturn
);

@Toflar
Copy link
Member

Toflar commented Aug 12, 2016

Fixed missing support for any character and added log entries: #8438.
See screenshot for error messages :)

@leofeyer
Copy link
Member

See #8438.

@leofeyer
Copy link
Member

Fixed in a8d10c9 thanks to @Toflar and @ausi.

jsonn pushed a commit to jsonn/pkgsrc that referenced this issue Sep 8, 2016
### 4.2.3 (2016-09-06)

 * Do not double URL encode the content syndication links.
 * Use CSS3 transforms instead of transitions to animate the off-canvas navigation.
 * Improve the exception handling when using the resource locator (see #557).
 * Correctly reset the filter menu in parent view.
 * Support all characters but =!<> and whitespace in simple tokens (see contao/core#8436).
 * Check the user's permission when generating links in the picker (see contao/core#8407).
 * Handle forward pages without target in the navigation modules (see contao/core#8377).
 * Provide the same template variables for downloads and enclosures (see contao/core#8392).
 * Handle %n when parsing date formats (see contao/core#8411).
 * Fix the module wizard's accessibility (see contao/core#8391).
 * Correctly initialize TinyMCE in sub-palettes in Firefox (see contao/core#3673).
 * Validate form field names more accurately (see contao/core#8403).
 * Correctly show the ctime, mtime and atime of a folder (see contao/core#8408).
 * Correctly index changed pages (see contao/core#8439).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants