template.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. class NotificationFactory::Template
  3. =begin
  4. examples how to use
  5. cleaned_template = NotificationFactory::Template.new(
  6. 'some template <b>#{ticket.title}</b> #{config.fqdn}',
  7. true,
  8. ).to_s
  9. =end
  10. def initialize(template, escape)
  11. @template = template
  12. @escape = escape
  13. end
  14. def to_s
  15. @template.gsub(%r{\#{\s*(.*?)\s*}}m) do
  16. # some browsers start adding HTML tags
  17. # fixes https://github.com/zammad/zammad/issues/385
  18. input_template = $1.gsub(%r{\A<.+?>\s*|\s*<.+?>\z}, '')
  19. case input_template
  20. when %r{\At\('(.+?)'\)\z}m
  21. %(<%= t "#{sanitize_text($1)}", #{@escape} %>)
  22. when %r{\At\((.+?)\)\z}m
  23. %(<%= t d"#{sanitize_object_name($1)}", #{@escape} %>)
  24. when %r{\Aconfig\.(.+?)\z}m
  25. %(<%= c "#{sanitize_object_name($1)}", #{@escape} %>)
  26. else
  27. %(<%= d "#{sanitize_object_name(input_template)}", #{@escape} %>)
  28. end
  29. end
  30. end
  31. def sanitize_text(string)
  32. string&.tr("\t\r\n", '')
  33. &.gsub(%r{(?<!\\)(?=")}, '\\')
  34. end
  35. def sanitize_object_name(string)
  36. string&.tr("\t\r\n\f \"'§;", '')
  37. end
  38. end