From 506e1656426bb6d2fb3a1ccc028a9e4fb32a718d Mon Sep 17 00:00:00 2001 From: Tahir Almas Date: Sat, 10 Oct 2026 03:25:27 +0500 Subject: [PATCH] Fix fax and voice sending under Twig 3 Since the PHP 8.1+ move (#23) every outbound fax and voice call failed before dialing: POST /transmissions/{id}/send returned 'Unexpected "spaceless" tag' from function.twig. Twig 3 removed both {% spaceless %} and "for ... if", and the set_prefix macro used both. It renders the dial string for fax.json and voice.json, so nothing could be originated. Rewritten with whitespace control and an explicit match flag, same behaviour: the first "remove|add" provider prefix rule that changes the number wins, and a number with no matching rule passes through unchanged. Checked against nine cases under Twig 3.28 (empty and null rules, 00|, 0|92, multiple rules, |9, bare remove), and a send on the ICTFax image now reaches FreeSWITCH originate. --- .../Freeswitch/templates/function.twig | 43 +++++++++++-------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/core/Gateway/Freeswitch/templates/function.twig b/core/Gateway/Freeswitch/templates/function.twig index b2b4f4b..cb60fe3 100755 --- a/core/Gateway/Freeswitch/templates/function.twig +++ b/core/Gateway/Freeswitch/templates/function.twig @@ -1,19 +1,24 @@ -{% macro set_prefix(in_phone, in_prefix) %} -{% spaceless %} - {% set phone = in_phone %} - {% set listPrefix = in_prefix|split(',') %} - {% set prefix_matched = false %} - {% for prefix in listPrefix if not prefix_matched %} - {% set prefix_remove = prefix|split('|', 2)[0] %} - {% set prefix_add = prefix|split('|', 2)[1] %} - {% set phone = '^' ~ in_phone %} - {% set prefix_remove = '^' ~ prefix_remove %} - {% set phone = phone|replace({(prefix_remove): (prefix_add)}) %} - {% set phone = phone|replace({'^': ''}) %} - {% if in_phone is not same as(phone) %} - {% set prefix_matched = true %} - {% endif %} - {% endfor %} - {{ phone }} -{% endspaceless %} -{% endmacro %} +{# + set_prefix(phone, prefix_rules): apply the first matching provider prefix rule to a number. + prefix_rules is a comma separated list of "remove|add" pairs, e.g. "00|,0|92": a leading 00 is + dropped, else a leading 0 becomes 92. The first rule that changes the number wins; with no match + the number is returned unchanged. + Twig 3: no {% spaceless %} and no "for ... if", so whitespace control and an explicit flag instead. +#} +{% macro set_prefix(in_phone, in_prefix) -%} + {%- set phone = in_phone -%} + {%- set prefix_matched = false -%} + {%- for prefix in (in_prefix ?? '')|split(',') -%} + {%- if not prefix_matched -%} + {%- set parts = prefix|split('|', 2) -%} + {%- set prefix_remove = '^' ~ parts[0] -%} + {%- set prefix_add = parts[1] ?? '' -%} + {%- set candidate = ('^' ~ in_phone)|replace({(prefix_remove): (prefix_add)})|replace({'^': ''}) -%} + {%- if candidate is not same as(in_phone ~ '') -%} + {%- set phone = candidate -%} + {%- set prefix_matched = true -%} + {%- endif -%} + {%- endif -%} + {%- endfor -%} + {{- phone -}} +{%- endmacro %}