forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_content.rb
197 lines (172 loc) · 6.47 KB
/
user_content.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
module UserContent
def self.escape(str, current_host = nil)
html = Nokogiri::HTML::DocumentFragment.parse(str)
find_user_content(html) do |obj, uc|
uuid = CanvasUUID.generate
child = Nokogiri::XML::Node.new("iframe", html)
child['class'] = 'user_content_iframe'
child['name'] = uuid
child['style'] = "width: #{uc.width}; height: #{uc.height}"
child['frameborder'] = '0'
form = Nokogiri::XML::Node.new("form", html)
form['action'] = "//#{HostUrl.file_host(@domain_root_account || Account.default, current_host)}/object_snippet"
form['method'] = 'post'
form['class'] = 'user_content_post_form'
form['target'] = uuid
form['id'] = "form-#{uuid}"
input = Nokogiri::XML::Node.new("input", html)
input['type'] = 'hidden'
input['name'] = 'object_data'
input['value'] = uc.node_string
form.add_child(input)
s_input = Nokogiri::XML::Node.new("input", html)
s_input['type'] = 'hidden'
s_input['name'] = 's'
s_input['value'] = uc.node_hmac
form.add_child(s_input)
obj.replace(child)
child.add_next_sibling(form)
end
html.css('img.equation_image').each do |node|
mathml = Nokogiri::HTML::DocumentFragment.parse('<span class="hidden-readable">' + Ritex::Parser.new.parse(node.delete('alt').value) + '</span>') rescue next
node.add_next_sibling(mathml)
end
html.to_s.html_safe
end
class Node < Struct.new(:width, :height, :node_string, :node_hmac)
end
# for each user content in the nokogiri document, yields |nokogiri_node, UserContent::Node|
def self.find_user_content(html)
html.css('object,embed').each do |obj|
styles = {}
params = {}
obj.css('param').each do |param|
params[param['key']] = param['value']
end
(obj['style'] || '').split(/\;/).each do |attr|
key, value = attr.split(/\:/).map(&:strip)
styles[key] = value
end
width = css_size(obj['width'])
width ||= css_size(params['width'])
width ||= css_size(styles['width'])
width ||= '400px'
height = css_size(obj['height'])
height ||= css_size(params['height'])
height ||= css_size(styles['height'])
height ||= '300px'
snippet = Base64.encode64(obj.to_s).gsub("\n", '')
hmac = Canvas::Security.hmac_sha1(snippet)
uc = Node.new(width, height, snippet, hmac)
yield obj, uc
end
end
# TODO: try and discover the motivation behind the "huhs"
def self.css_size(val)
if !val || val.to_f == 0
# no value, non-numeric value, or 0 value (whether "0", "0px", "0%",
# etc.); ignore
nil
elsif val == "#{val.to_f.to_s}%" || val == "#{val.to_f.to_s}px"
# numeric percentage or specific px value; use as is
val
elsif val.to_f.to_s == val
# unadorned numeric value; make px (after adding 10... huh?)
(val.to_f + 10).to_s + "px"
else
# numeric value embedded, but has additional text we didn't recognize;
# just extract the numeric part (without a px... huh?)
val.to_f.to_s
end
end
class HtmlRewriter
AssetTypes = {
'assignments' => Assignment,
'announcements' => Announcement,
'calendar_events' => CalendarEvent,
'discussion_topics' => DiscussionTopic,
'collaborations' => Collaboration,
'files' => Attachment,
'conferences' => WebConference,
'quizzes' => Quizzes::Quiz,
'groups' => Group,
'wiki' => WikiPage,
'pages' => WikiPage,
'grades' => nil,
'users' => nil,
'external_tools' => nil,
'file_contents' => nil,
'modules' => ContextModule,
'items' => ContentTag
}
DefaultAllowedTypes = AssetTypes.keys
def initialize(context, user)
raise(ArgumentError, "context required") unless context
@context = context
@user = user
# capture group 1 is the object type, group 2 is the object id, if it's
# there, and group 3 is the rest of the url, including any beginning '/'
@toplevel_regex = %r{/#{context.class.name.tableize}/#{context.id}/(\w+)(?:/([^\s"<'\?\/]*)([^\s"<']*))?}
@handlers = {}
@default_handler = nil
@unknown_handler = nil
@allowed_types = DefaultAllowedTypes
end
attr_reader :user, :context
class UriMatch < Struct.new(:url, :type, :obj_class, :obj_id, :rest)
end
# specify a url type like "assignments" or "file_contents"
def set_handler(type, &handler)
@handlers[type] = handler
end
def set_default_handler(&handler)
@default_handler = handler
end
def set_unknown_handler(&handler)
@unknown_handler = handler
end
def allowed_types=(new_types)
@allowed_types = Array(new_types)
end
def translate_content(html)
return html if html.blank?
asset_types = AssetTypes.reject { |k,v| !@allowed_types.include?(k) }
html.gsub(@toplevel_regex) do |relative_url|
type, obj_id, rest = [$1, $2, $3]
if type != "wiki" && type != "pages"
if obj_id.to_i > 0
obj_id = obj_id.to_i
else
rest = "/#{obj_id}#{rest}" if obj_id.present? || rest.present?
obj_id = nil
end
end
if module_item = rest.try(:match, %r{/items/(\d+)})
type = 'items'
obj_id = module_item[1].to_i
end
if asset_types.key?(type)
match = UriMatch.new(relative_url, type, asset_types[type], obj_id, rest)
handler = @handlers[type] || @default_handler
(handler && handler.call(match)) || relative_url
else
match = UriMatch.new(relative_url, type)
(@unknown_handler && @unknown_handler.call(match)) || relative_url
end
end
end
# if content is nil, it'll query the block for the content if needed (lazy content load)
def user_can_view_content?(content = nil, &get_content)
return false if user.blank? && content.respond_to?(:locked?) && content.locked?
return true unless user
# if user given, check that the user is allowed to manage all
# context content, or read that specific item (and it's not locked)
@manage_content ||= context.grants_right?(user, :manage_content)
return true if @manage_content
content ||= get_content.call
allow = true if content.respond_to?(:grants_right?) && content.grants_right?(user, :read)
allow = false if allow && content.respond_to?(:locked_for?) && content.locked_for?(user)
return allow
end
end
end