renderer.rb 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. # Copyright (C) 2012-2021 Zammad Foundation, http://zammad-foundation.org/
  2. class NotificationFactory::Renderer
  3. =begin
  4. examples how to use
  5. message_subject = NotificationFactory::Renderer.new(
  6. objects: {
  7. ticket: Ticket.first,
  8. },
  9. locale: 'de-de',
  10. timezone: 'America/Port-au-Prince',
  11. template: 'some template <b>#{ticket.title}</b> {config.fqdn}',
  12. escape: false
  13. ).render
  14. message_body = NotificationFactory::Renderer.new(
  15. objects: {
  16. ticket: Ticket.first,
  17. },
  18. locale: 'de-de',
  19. timezone: 'America/Port-au-Prince',
  20. template: 'some template <b>#{ticket.title}</b> #{config.fqdn}',
  21. ).render
  22. =end
  23. def initialize(objects:, template:, locale: nil, timezone: nil, escape: true)
  24. @objects = objects
  25. @locale = locale || Locale.default
  26. @timezone = timezone || Setting.get('timezone_default')
  27. @template = NotificationFactory::Template.new(template, escape)
  28. @escape = escape
  29. end
  30. def render
  31. ERB.new(@template.to_s).result(binding)
  32. rescue Exception => e # rubocop:disable Lint/RescueException
  33. raise StandardError, e.message if e.is_a? SyntaxError
  34. raise
  35. end
  36. # d - data of object
  37. # d('user.firstname', htmlEscape)
  38. def d(key, escape = nil)
  39. # do validation, ignore some methods
  40. return "\#{#{key} / not allowed}" if !data_key_valid?(key)
  41. # aliases
  42. map = {
  43. 'article.body' => 'article.body_as_text_with_quote.text2html',
  44. 'ticket.tags' => 'ticket.tag_list',
  45. }
  46. if map[key]
  47. key = map[key]
  48. end
  49. # escape in html mode
  50. if escape
  51. no_escape = {
  52. 'article.body_as_html' => true,
  53. 'article.body_as_text_with_quote.text2html' => true,
  54. }
  55. if no_escape[key]
  56. escape = false
  57. end
  58. end
  59. value = nil
  60. object_methods = key.split('.')
  61. object_name = object_methods.shift
  62. # if no object is given, just return
  63. return '#{no such object}' if object_name.blank? # rubocop:disable Lint/InterpolationCheck
  64. object_refs = @objects[object_name] || @objects[object_name.to_sym]
  65. # if object is not in available objects, just return
  66. return "\#{#{object_name} / no such object}" if !object_refs
  67. # if content of method is a complex datatype, just return
  68. if object_methods.blank? && object_refs.class != String && object_refs.class != Float && object_refs.class != Integer
  69. return "\#{#{key} / no such method}"
  70. end
  71. previous_object_refs = ''
  72. object_methods_s = ''
  73. object_methods.each do |method_raw|
  74. method = method_raw.strip
  75. if method == 'value'
  76. temp = object_refs
  77. object_refs = display_value(previous_object_refs, method, object_methods_s, object_refs)
  78. previous_object_refs = temp
  79. end
  80. if object_methods_s != ''
  81. object_methods_s += '.'
  82. end
  83. object_methods_s += method
  84. next if method == 'value'
  85. if object_methods_s == ''
  86. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  87. break
  88. end
  89. arguments = nil
  90. if %r{\A(?<method_id>[^(]+)\((?<parameter>[^)]+)\)\z} =~ method
  91. if parameter != parameter.to_i.to_s
  92. value = "\#{#{object_name}.#{object_methods_s} / invalid parameter: #{parameter}}"
  93. break
  94. end
  95. begin
  96. arguments = Array(parameter.to_i)
  97. method = method_id
  98. rescue
  99. value = "\#{#{object_name}.#{object_methods_s} / #{e.message}}"
  100. break
  101. end
  102. end
  103. # if method exists
  104. if !object_refs.respond_to?(method.to_sym)
  105. value = "\#{#{object_name}.#{object_methods_s} / no such method}"
  106. break
  107. end
  108. begin
  109. previous_object_refs = object_refs
  110. object_refs = object_refs.send(method.to_sym, *arguments)
  111. # body_as_html should trigger the cloning of all inline attachments from the parent article (issue #2399)
  112. if method.to_sym == :body_as_html && previous_object_refs.respond_to?(:should_clone_inline_attachments)
  113. previous_object_refs.should_clone_inline_attachments = true
  114. end
  115. rescue => e
  116. value = "\#{#{object_name}.#{object_methods_s} / #{e.message}}"
  117. break
  118. end
  119. end
  120. placeholder = value || object_refs
  121. escaping(convert_to_timezone(placeholder), escape)
  122. end
  123. # c - config
  124. # c('fqdn', htmlEscape)
  125. def c(key, escape = nil)
  126. config = Setting.get(key)
  127. escaping(config, escape)
  128. end
  129. # t - translation
  130. # t('yes', htmlEscape)
  131. def t(key, escape = nil)
  132. translation = Translation.translate(@locale, key)
  133. escaping(translation, escape)
  134. end
  135. # h - htmlEscape
  136. # h(htmlEscape)
  137. def h(value)
  138. return value if !value
  139. CGI.escapeHTML(convert_to_timezone(value).to_s)
  140. end
  141. private
  142. def convert_to_timezone(value)
  143. return Translation.timestamp(@locale, @timezone, value) if value.instance_of?(ActiveSupport::TimeWithZone)
  144. return Translation.date(@locale, value) if value.instance_of?(Date)
  145. value
  146. end
  147. def escaping(key, escape)
  148. return escaping(key.join(', '), escape) if key.respond_to?(:join)
  149. return key if escape == false
  150. return key if escape.nil? && !@escape
  151. h key
  152. end
  153. def data_key_valid?(key)
  154. return false if key =~ %r{`|\.(|\s*)(save|destroy|delete|remove|drop|update|create|new|all|where|find|raise|dump|rollback|freeze)}i && key !~ %r{(update|create)d_(at|by)}i
  155. true
  156. end
  157. def display_value(object, method_name, previous_method_names, key)
  158. return key if method_name != 'value' ||
  159. !key.instance_of?(String)
  160. attributes = ObjectManager::Attribute
  161. .where(object_lookup_id: ObjectLookup.by_name(object.class.to_s))
  162. .where(name: previous_method_names.split('.').last)
  163. return key if attributes.count.zero? || attributes.first.data_type != 'select'
  164. attributes.first.data_option['options'][key] || key
  165. end
  166. end