2016-04-13 23:40:37 +00:00
|
|
|
class NotificationFactory::Template
|
|
|
|
|
2016-11-11 10:17:53 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
examples how to use
|
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
cleaned_template = NotificationFactory::Template.new(
|
2016-11-13 18:33:12 +00:00
|
|
|
'some template <b>#{ticket.title}</b> #{config.fqdn}',
|
|
|
|
true,
|
2016-11-11 15:57:25 +00:00
|
|
|
).to_s
|
2016-11-11 10:17:53 +00:00
|
|
|
|
|
|
|
=end
|
|
|
|
|
2016-11-13 18:33:12 +00:00
|
|
|
def initialize(template, escape)
|
2016-04-13 23:40:37 +00:00
|
|
|
@template = template
|
2016-11-13 18:33:12 +00:00
|
|
|
@escape = escape
|
2016-04-13 23:40:37 +00:00
|
|
|
end
|
|
|
|
|
2016-11-11 15:57:25 +00:00
|
|
|
def to_s
|
2019-02-12 06:46:04 +00:00
|
|
|
@template.gsub(/\#{\s*(.*?)\s*}/m) do
|
|
|
|
# some browsers start adding HTML tags
|
|
|
|
# fixes https://github.com/zammad/zammad/issues/385
|
|
|
|
input_template = $1.gsub(/\A<.+?>\s*|\s*<.+?>\z/, '')
|
|
|
|
|
|
|
|
case input_template
|
|
|
|
when /\At\('(.+?)'\)\z/m
|
|
|
|
%(<%= t "#{sanitize_text($1)}", #{@escape} %>)
|
|
|
|
when /\At\((.+?)\)\z/m
|
|
|
|
%(<%= t d"#{sanitize_object_name($1)}", #{@escape} %>)
|
|
|
|
when /\Aconfig\.(.+?)\z/m
|
|
|
|
%(<%= c "#{sanitize_object_name($1)}", #{@escape} %>)
|
2016-11-13 18:33:12 +00:00
|
|
|
else
|
2019-02-12 06:46:04 +00:00
|
|
|
%(<%= d "#{sanitize_object_name(input_template)}", #{@escape} %>)
|
2016-11-13 18:33:12 +00:00
|
|
|
end
|
|
|
|
end
|
2016-04-13 23:40:37 +00:00
|
|
|
end
|
2016-11-13 18:33:12 +00:00
|
|
|
|
2019-02-12 06:46:04 +00:00
|
|
|
def sanitize_text(string)
|
|
|
|
string&.tr("\t\r\n", '')
|
|
|
|
&.gsub(/(?<!\\)(?=")/, '\\')
|
2016-11-13 18:33:12 +00:00
|
|
|
end
|
|
|
|
|
2019-02-12 06:46:04 +00:00
|
|
|
def sanitize_object_name(string)
|
|
|
|
string&.tr("\t\r\n\f \"'§;", '')
|
2016-11-13 18:33:12 +00:00
|
|
|
end
|
|
|
|
|
2016-04-13 23:40:37 +00:00
|
|
|
end
|