Skip to content

Commit

Permalink
mod_admin: fix a problem where page_path was multiple time url encoded (
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Mar 31, 2023
1 parent 6b289d6 commit 2e99a70
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/ref/filters/escaping/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Character escaping
../filter_slugify
../filter_unescape
../filter_urlencode
../filter_urldecode
14 changes: 14 additions & 0 deletions doc/ref/filters/filter_urldecode.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.. highlight:: django
.. include:: meta-urldecode.rst
.. seealso:: :ref:`filter-urlencode`

Decode a text where characters are encoded as URL-safe characters.

Translates all percent encoded characters back to their original encoding.

For example::

{{ value|urldecode }}

When value is “msg%3DHello%26World” then the output is “msg=Hello&World”.

3 changes: 2 additions & 1 deletion modules/mod_admin/controllers/controller_admin_edit.erl
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,10 @@ event(#submit{message={rscform, Args}}, Context) ->
case m_rsc:p(Id, category_id, Context) of
CatBefore ->
% Update some automatically generated or adapted fields
PagePath = filter_urldecode:urldecode(m_rsc:p(Id, page_path, Context), Context),
Context1 = z_render:set_value("field-name", m_rsc:p(Id, name, Context), Context),
Context2 = z_render:set_value("field-uri", m_rsc:p(Id, uri, Context), Context1),
Context3 = z_render:set_value("field-page-path", m_rsc:p(Id, page_path, Context), Context2),
Context3 = z_render:set_value("field-page-path", PagePath, Context2),
Context4 = z_render:set_value("website", m_rsc:p(Id, website, Context), Context3),
Context4a = set_value_slug(m_rsc:p(Id, title_slug, Context), Context4),
Context4b= z_render:set_value("visible_for", integer_to_list(m_rsc:p(Id, visible_for, Context)), Context4a),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div class="form-group row">
<label class="control-label col-md-3" for="field-page-path">{_ Page path _}</label>
<div class="col-md-9">
<input class="form-control" type="text" id="field-page-path" name="page_path" value="{{ r.page_path }}" {% if not is_editable %}disabled="disabled"{% endif %} {% include "_language_attrs.tpl" language=`en` %} placeholder="{{ r.default_page_url|escape }}" />
<input class="form-control" type="text" id="field-page-path" name="page_path" value="{{ r.page_path|urldecode|escape }}" {% if not is_editable %}disabled="disabled"{% endif %} {% include "_language_attrs.tpl" language=`en` %} placeholder="{{ r.default_page_url|escape }}" />
</div>
</div>

Expand Down
33 changes: 33 additions & 0 deletions modules/mod_base/filters/filter_urldecode.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2021 Marc Worrell
%% @doc 'urldecode' filter, decode the %-encoded characters in a string.

%% Copyright 2022 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing, software
%% distributed under the License is distributed on an "AS IS" BASIS,
%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License.

-module(filter_urldecode).
-export([urldecode/2]).

-include("zotonic.hrl").

urldecode(undefined, _Context) ->
undefined;
urldecode({trans, _} = Tr, Context) ->
urldecode(z_trans:lookup_fallback(Tr, Context), Context);
urldecode(Input, _Context) when is_binary(Input) ->
list_to_binary(z_url:url_decode(binary_to_list(Input)));
urldecode(Input, _Context) when is_list(Input) ->
z_url:url_decode(iolist_to_binary(Input));
urldecode(_Input, _Context) ->
<<>>.

0 comments on commit 2e99a70

Please sign in to comment.