2019-05-02 17:17:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
class PostAnalyzer
|
|
|
|
def initialize(raw, topic_id)
|
2017-07-27 20:20:09 -05:00
|
|
|
@raw = raw
|
2013-05-30 13:34:44 -05:00
|
|
|
@topic_id = topic_id
|
2018-06-13 13:57:32 -05:00
|
|
|
@onebox_urls = []
|
2019-09-10 05:59:48 -05:00
|
|
|
@found_oneboxes = false
|
2016-04-12 13:09:59 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def found_oneboxes?
|
2019-09-10 05:59:48 -05:00
|
|
|
@found_oneboxes
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|
|
|
|
|
2018-02-08 17:26:56 -06:00
|
|
|
def has_oneboxes?
|
|
|
|
return false unless @raw.present?
|
|
|
|
|
|
|
|
cooked_stripped
|
|
|
|
found_oneboxes?
|
|
|
|
end
|
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
# What we use to cook posts
|
2017-10-17 13:37:51 -05:00
|
|
|
def cook(raw, opts = {})
|
|
|
|
cook_method = opts[:cook_method]
|
|
|
|
return raw if cook_method == Post.cook_methods[:raw_html]
|
|
|
|
|
|
|
|
if cook_method == Post.cook_methods[:email]
|
2017-11-15 09:39:29 -06:00
|
|
|
cooked = EmailCook.new(raw).cook(opts)
|
2017-10-17 13:37:51 -05:00
|
|
|
else
|
|
|
|
cooked = PrettyText.cook(raw, opts)
|
|
|
|
end
|
2013-05-30 13:34:44 -05:00
|
|
|
|
2022-03-23 10:36:08 -05:00
|
|
|
limit = SiteSetting.max_oneboxes_per_post
|
2023-01-09 06:20:10 -06:00
|
|
|
result =
|
|
|
|
Oneboxer.apply(cooked, extra_paths: ".inline-onebox-loading") do |url, element|
|
|
|
|
if opts[:invalidate_oneboxes]
|
|
|
|
Oneboxer.invalidate(url)
|
|
|
|
InlineOneboxer.invalidate(url)
|
|
|
|
end
|
|
|
|
next if element["class"] != Oneboxer::ONEBOX_CSS_CLASS
|
|
|
|
next if limit <= 0
|
|
|
|
limit -= 1
|
|
|
|
@onebox_urls << url
|
|
|
|
onebox = Oneboxer.cached_onebox(url)
|
|
|
|
@found_oneboxes = true if onebox.present?
|
|
|
|
onebox
|
2020-06-24 04:54:54 -05:00
|
|
|
end
|
2013-05-30 13:34:44 -05:00
|
|
|
|
FEATURE: Allow hotlinked media to be blocked (#16940)
This commit introduces a new site setting: `block_hotlinked_media`. When enabled, all attempts to hotlink media (images, videos, and audio) will fail, and be replaced with a linked placeholder. Exceptions to the rule can be added via `block_hotlinked_media_exceptions`.
`download_remote_image_to_local` can be used alongside this feature. In that case, hotlinked images will be blocked immediately when the post is created, but will then be replaced with the downloaded version a few seconds later.
This implementation is purely server-side, and does not impact the composer preview.
Technically, there are two stages to this feature:
1. `PrettyText.sanitize_hotlinked_media` is called during `PrettyText.cook`, and whenever new images are introduced by Onebox. It will iterate over all src/srcset attributes in the post HTML and check if they're allowed. If not, the attributes will be removed and replaced with a `data-blocked-hotlinked-src(set)` attribute
2. In the `CookedPostProcessor`, we iterate over all `data-blocked-hotlinked-src(set)` attributes and check whether we have a downloaded version of the media. If yes, we update the src to use the downloaded version. If not, the entire media element is replaced with a placeholder. The placeholder is labelled 'external media', and is a link to the offsite media.
2022-06-07 09:23:04 -05:00
|
|
|
if result.changed?
|
|
|
|
PrettyText.sanitize_hotlinked_media(result.doc)
|
|
|
|
cooked = result.to_html
|
|
|
|
end
|
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
cooked
|
|
|
|
end
|
|
|
|
|
|
|
|
# How many images are present in the post
|
2020-08-07 11:08:59 -05:00
|
|
|
def embedded_media_count
|
2013-05-30 13:34:44 -05:00
|
|
|
return 0 unless @raw.present?
|
|
|
|
|
2020-08-07 11:08:59 -05:00
|
|
|
# TODO - do we need to look for tags other than img, video and audio?
|
2023-01-09 06:20:10 -06:00
|
|
|
cooked_stripped
|
|
|
|
.css("img", "video", "audio")
|
|
|
|
.reject do |t|
|
|
|
|
if dom_class = t["class"]
|
|
|
|
(Post.allowed_image_classes & dom_class.split).count > 0
|
|
|
|
end
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|
2023-01-09 06:20:10 -06:00
|
|
|
.count
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|
|
|
|
|
2013-07-21 19:39:17 -05:00
|
|
|
# How many attachments are present in the post
|
|
|
|
def attachment_count
|
|
|
|
return 0 unless @raw.present?
|
2013-10-17 11:44:09 -05:00
|
|
|
|
2023-01-09 06:20:10 -06:00
|
|
|
attachments =
|
|
|
|
cooked_stripped.css("a.attachment[href^=\"#{Discourse.store.absolute_base_url}\"]")
|
|
|
|
attachments +=
|
|
|
|
cooked_stripped.css(
|
|
|
|
"a.attachment[href^=\"#{Discourse.store.relative_base_url}\"]",
|
|
|
|
) if Discourse.store.internal?
|
2013-07-31 16:26:34 -05:00
|
|
|
attachments.count
|
2013-07-21 19:39:17 -05:00
|
|
|
end
|
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
def raw_mentions
|
|
|
|
return [] if @raw.blank?
|
|
|
|
return @raw_mentions if @raw_mentions.present?
|
2022-12-06 09:10:36 -06:00
|
|
|
@raw_mentions = PrettyText.extract_mentions(cooked_stripped)
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|
|
|
|
|
2015-03-18 00:25:24 -05:00
|
|
|
# from rack ... compat with ruby 2.2
|
|
|
|
def self.parse_uri_rfc2396(uri)
|
|
|
|
@parser ||= defined?(URI::RFC2396_Parser) ? URI::RFC2396_Parser.new : URI
|
|
|
|
@parser.parse(uri)
|
|
|
|
end
|
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
# Count how many hosts are linked in the post
|
|
|
|
def linked_hosts
|
2018-06-13 13:57:32 -05:00
|
|
|
all_links = raw_links + @onebox_urls
|
|
|
|
|
|
|
|
return {} if all_links.blank?
|
2013-05-30 13:34:44 -05:00
|
|
|
return @linked_hosts if @linked_hosts.present?
|
|
|
|
|
|
|
|
@linked_hosts = {}
|
2013-10-17 11:44:09 -05:00
|
|
|
|
2018-06-13 13:57:32 -05:00
|
|
|
all_links.each do |u|
|
2013-07-15 05:11:23 -05:00
|
|
|
begin
|
2015-03-18 00:25:24 -05:00
|
|
|
uri = self.class.parse_uri_rfc2396(u)
|
2013-07-15 05:11:23 -05:00
|
|
|
host = uri.host
|
2014-03-07 03:44:04 -06:00
|
|
|
@linked_hosts[host] ||= 1 unless host.nil?
|
2018-08-14 05:23:32 -05:00
|
|
|
rescue URI::Error
|
2018-06-13 13:57:32 -05:00
|
|
|
# An invalid URI does not count as a host
|
2013-07-15 05:11:23 -05:00
|
|
|
next
|
|
|
|
end
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|
2013-10-17 11:44:09 -05:00
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
@linked_hosts
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns an array of all links in a post excluding mentions
|
|
|
|
def raw_links
|
|
|
|
return [] unless @raw.present?
|
|
|
|
return @raw_links if @raw_links.present?
|
|
|
|
|
|
|
|
@raw_links = []
|
2023-01-09 06:20:10 -06:00
|
|
|
cooked_stripped
|
|
|
|
.css("a")
|
|
|
|
.each do |l|
|
|
|
|
# Don't include @mentions in the link count
|
|
|
|
next if link_is_a_mention?(l)
|
|
|
|
# Don't include heading anchor in the link count
|
|
|
|
next if link_is_an_anchor?(l)
|
|
|
|
# Don't include hashtags in the link count
|
|
|
|
next if link_is_a_hashtag?(l)
|
|
|
|
@raw_links << l["href"].to_s
|
|
|
|
end
|
2013-10-17 11:44:09 -05:00
|
|
|
|
2013-05-30 13:34:44 -05:00
|
|
|
@raw_links
|
|
|
|
end
|
|
|
|
|
|
|
|
# How many links are present in the post
|
|
|
|
def link_count
|
2019-09-10 05:59:48 -05:00
|
|
|
raw_links.size + @onebox_urls.size
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def cooked_stripped
|
2023-01-09 06:20:10 -06:00
|
|
|
@cooked_stripped ||=
|
|
|
|
begin
|
2023-11-08 13:13:25 -06:00
|
|
|
cooked = cook(@raw, topic_id: @topic_id)
|
|
|
|
fragment = Nokogiri::HTML5.fragment(cooked)
|
|
|
|
PostStripper.strip(fragment)
|
2023-01-09 06:20:10 -06:00
|
|
|
end
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2016-12-05 08:19:15 -06:00
|
|
|
|
2018-09-13 03:34:32 -05:00
|
|
|
private
|
|
|
|
|
2018-06-07 00:28:18 -05:00
|
|
|
def link_is_a_mention?(l)
|
2023-01-09 06:20:10 -06:00
|
|
|
href = l["href"].to_s
|
|
|
|
l["class"].to_s["mention"] &&
|
|
|
|
(
|
|
|
|
href.start_with?("#{Discourse.base_path}/u/") ||
|
|
|
|
href.start_with?("#{Discourse.base_path}/users/")
|
|
|
|
)
|
2021-04-14 02:27:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_is_an_anchor?(l)
|
2023-01-09 06:20:10 -06:00
|
|
|
l["class"].to_s["anchor"] && l["href"].to_s.start_with?("#")
|
2021-04-14 02:27:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def link_is_a_hashtag?(l)
|
2023-01-09 06:20:10 -06:00
|
|
|
href = l["href"].to_s
|
|
|
|
l["class"].to_s["hashtag"] &&
|
|
|
|
(
|
|
|
|
href.start_with?("#{Discourse.base_path}/c/") ||
|
|
|
|
href.start_with?("#{Discourse.base_path}/tag/")
|
|
|
|
)
|
2018-06-07 00:28:18 -05:00
|
|
|
end
|
2013-05-30 13:34:44 -05:00
|
|
|
end
|