t1mwillis / GWCode-EE3-EE4-EE5

A fork of the GWCode module to support EE3+ as well as PHP7
14 stars 6 forks source link

Error caused with single quotes in cat_name #5

Closed ignetic closed 4 months ago

ignetic commented 3 years ago

I found an issue where a if a single quote is entered in the Category Name field within the categories, example [Mum's Stuff]. This causes a an error which appears to do with the template parsing.

EE: v5.4.0 GWCode Categories: 3.0.0

The error is: Fatal error: Maximum execution time of 90 seconds exceeded in .../system/ee/EllisLab/ExpressionEngine/Library/Parser/AbstractLexer.php on line 105

ignetic commented 3 years ago

Looking into this further, I traced it to this line which sends the variable to the template parser:

return ($this->style != 'linear') ? $gw_output : $this->EE->TMPL->parse_variables(rtrim($this->gw_tagdata), $linear_parse_vars_arr); 

Look at at how EE formats the cat_name field it uses ee()->typography->format_characters when outputting this field. Adding this to pi.gwcode_categories.php within the _generate_output method appears to fix this problem, replacing the line with:

$this->var_prefix.'cat_name' => ee()->typography->format_characters($this->categories[$gw_i]['cat_name']),
ignetic commented 3 years ago

Update: I think it would be good to have similar formatting to ee native categories with Variable Modifiers attached to the variables: https://docs.expressionengine.com/latest/templates/variable-modifiers.html

This could be something like the following by replacing this line:

$this->var_prefix.'cat_name' => $this->categories[$gw_i]['cat_name'],

with this:

$this->var_prefix.'cat_name:raw_content' => $cat_name,
$this->var_prefix.'cat_name:json' => trim(json_encode($cat_name), '"'),
$this->var_prefix.'cat_name:url_encode' => urlencode($cat_name),
$this->var_prefix.'cat_name:url_decode' => urldecode($cat_name),
$this->var_prefix.'cat_name' => ee()->typography->format_characters($cat_name),

This would also allow for a more flexible output.