2017-05-22 15:42:19 -05:00
|
|
|
require_dependency "onebox/discourse_onebox_sanitize_config"
|
|
|
|
require_dependency 'final_destination'
|
|
|
|
|
2016-10-24 18:25:44 -05:00
|
|
|
Dir["#{Rails.root}/lib/onebox/engine/*_onebox.rb"].sort.each { |f| require f }
|
2014-02-25 12:35:08 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
module Oneboxer
|
2013-04-29 21:43:21 -05:00
|
|
|
# keep reloaders happy
|
|
|
|
unless defined? Oneboxer::Result
|
|
|
|
Result = Struct.new(:doc, :changed) do
|
|
|
|
def to_html
|
|
|
|
doc.to_html
|
|
|
|
end
|
2013-04-10 02:52:38 -05:00
|
|
|
|
2013-04-29 21:43:21 -05:00
|
|
|
def changed?
|
|
|
|
changed
|
|
|
|
end
|
2013-04-10 02:52:38 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-26 14:38:23 -05:00
|
|
|
def self.ignore_redirects
|
2017-07-27 20:20:09 -05:00
|
|
|
@ignore_redirects ||= ['http://www.dropbox.com', 'http://store.steampowered.com', Discourse.base_url]
|
2017-06-26 14:38:23 -05:00
|
|
|
end
|
|
|
|
|
2017-08-08 04:44:27 -05:00
|
|
|
def self.force_get_hosts
|
|
|
|
@force_get_hosts ||= ['http://us.battle.net']
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.preview(url, options = nil)
|
2014-01-27 14:09:09 -06:00
|
|
|
options ||= {}
|
2016-12-19 17:31:10 -06:00
|
|
|
invalidate(url) if options[:invalidate_oneboxes]
|
2018-02-13 17:39:44 -06:00
|
|
|
onebox_raw(url, options)[:preview]
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.onebox(url, options = nil)
|
2014-01-27 14:09:09 -06:00
|
|
|
options ||= {}
|
2016-12-19 17:31:10 -06:00
|
|
|
invalidate(url) if options[:invalidate_oneboxes]
|
2018-02-13 17:39:44 -06:00
|
|
|
onebox_raw(url, options)[:onebox]
|
2014-03-17 21:12:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.cached_onebox(url)
|
2014-03-31 23:29:14 -05:00
|
|
|
if c = Rails.cache.read(onebox_cache_key(url))
|
|
|
|
c[:onebox]
|
|
|
|
end
|
2014-05-28 02:15:10 -05:00
|
|
|
rescue => e
|
|
|
|
invalidate(url)
|
|
|
|
Rails.logger.warn("invalid cached onebox for #{url} #{e}")
|
|
|
|
""
|
2014-03-17 21:12:58 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.cached_preview(url)
|
2014-03-31 23:29:14 -05:00
|
|
|
if c = Rails.cache.read(onebox_cache_key(url))
|
|
|
|
c[:preview]
|
|
|
|
end
|
2014-05-28 02:15:10 -05:00
|
|
|
rescue => e
|
|
|
|
invalidate(url)
|
|
|
|
Rails.logger.warn("invalid cached preview for #{url} #{e}")
|
|
|
|
""
|
2013-08-14 10:05:53 -05:00
|
|
|
end
|
|
|
|
|
2014-01-28 12:18:19 -06:00
|
|
|
def self.invalidate(url)
|
2014-03-17 21:12:58 -05:00
|
|
|
Rails.cache.delete(onebox_cache_key(url))
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
2014-01-28 12:18:19 -06:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
# Parse URLs out of HTML, returning the document when finished.
|
|
|
|
def self.each_onebox_link(string_or_doc)
|
|
|
|
doc = string_or_doc
|
2013-04-10 02:52:38 -05:00
|
|
|
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
onebox_links = doc.search("a.onebox")
|
|
|
|
if onebox_links.present?
|
|
|
|
onebox_links.each do |link|
|
2016-11-03 16:48:32 -05:00
|
|
|
yield(link['href'], link) if link['href'].present?
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doc
|
|
|
|
end
|
|
|
|
|
2017-07-27 20:20:09 -05:00
|
|
|
def self.apply(string_or_doc, args = nil)
|
2013-04-10 02:52:38 -05:00
|
|
|
doc = string_or_doc
|
|
|
|
doc = Nokogiri::HTML::fragment(doc) if doc.is_a?(String)
|
|
|
|
changed = false
|
|
|
|
|
2016-12-19 17:31:10 -06:00
|
|
|
each_onebox_link(doc) do |url, element|
|
2018-02-13 17:39:44 -06:00
|
|
|
onebox, _ = yield(url, element)
|
2013-04-10 02:52:38 -05:00
|
|
|
if onebox
|
|
|
|
parsed_onebox = Nokogiri::HTML::fragment(onebox)
|
2013-05-01 01:37:27 -05:00
|
|
|
next unless parsed_onebox.children.count > 0
|
2013-04-10 02:52:38 -05:00
|
|
|
|
|
|
|
# special logic to strip empty p elements
|
2018-02-13 17:39:44 -06:00
|
|
|
if element&.parent&.node_name&.downcase == "p" && element&.parent&.children&.count == 1
|
2013-05-01 01:37:27 -05:00
|
|
|
element = element.parent
|
2013-04-10 02:52:38 -05:00
|
|
|
end
|
2018-02-13 17:39:44 -06:00
|
|
|
|
2013-04-10 02:52:38 -05:00
|
|
|
changed = true
|
|
|
|
element.swap parsed_onebox.to_html
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
Result.new(doc, changed)
|
|
|
|
end
|
|
|
|
|
2016-12-19 17:31:10 -06:00
|
|
|
def self.is_previewing?(user_id)
|
|
|
|
$redis.get(preview_key(user_id)) == "1"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.preview_onebox!(user_id)
|
|
|
|
$redis.setex(preview_key(user_id), 1.minute, "1")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.onebox_previewed!(user_id)
|
|
|
|
$redis.del(preview_key(user_id))
|
|
|
|
end
|
|
|
|
|
2017-01-05 20:01:14 -06:00
|
|
|
def self.engine(url)
|
|
|
|
Onebox::Matcher.new(url).oneboxed
|
|
|
|
end
|
|
|
|
|
2014-03-17 21:12:58 -05:00
|
|
|
private
|
|
|
|
|
2016-12-19 17:31:10 -06:00
|
|
|
def self.preview_key(user_id)
|
2016-12-20 04:18:47 -06:00
|
|
|
"onebox:preview:#{user_id}"
|
2016-12-19 17:31:10 -06:00
|
|
|
end
|
|
|
|
|
2016-10-24 05:46:22 -05:00
|
|
|
def self.blank_onebox
|
|
|
|
{ preview: "", onebox: "" }
|
2014-04-09 15:57:45 -05:00
|
|
|
end
|
|
|
|
|
2016-10-24 05:46:22 -05:00
|
|
|
def self.onebox_cache_key(url)
|
|
|
|
"onebox__#{url}"
|
|
|
|
end
|
2015-08-23 19:43:07 -05:00
|
|
|
|
2018-02-13 17:39:44 -06:00
|
|
|
def self.onebox_raw(url, opts = {})
|
|
|
|
local_onebox(url, opts) || external_onebox(url)
|
|
|
|
rescue => e
|
|
|
|
# no point warning here, just cause we have an issue oneboxing a url
|
|
|
|
# we can later hunt for failed oneboxes by searching logs if needed
|
|
|
|
Rails.logger.info("Failed to onebox #{url} #{e} #{e.backtrace}")
|
|
|
|
# return a blank hash, so rest of the code works
|
|
|
|
blank_onebox
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_onebox(url, opts = {})
|
|
|
|
return unless route = Discourse.route_for(url)
|
|
|
|
|
|
|
|
html =
|
|
|
|
case route[:controller]
|
|
|
|
when "uploads" then local_upload_html(url)
|
|
|
|
when "topics" then local_topic_html(url, route, opts)
|
|
|
|
when "users" then local_user_html(url, route)
|
|
|
|
end
|
|
|
|
|
|
|
|
html = html.presence || "<a href='#{url}'>#{url}</a>"
|
|
|
|
{ onebox: html, preview: html }
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_upload_html(url)
|
|
|
|
case File.extname(URI(url).path || "")
|
|
|
|
when /^\.(mov|mp4|webm|ogv)$/i
|
|
|
|
"<video width='100%' height='100%' controls><source src='#{url}'><a href='#{url}'>#{url}</a></video>"
|
|
|
|
when /^\.(mp3|ogg|wav|m4a)$/i
|
|
|
|
"<audio controls><source src='#{url}'><a href='#{url}'>#{url}</a></audio>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_topic_html(url, route, opts)
|
|
|
|
return unless current_user = User.find_by(id: opts[:user_id])
|
|
|
|
return unless current_category = Category.find_by(id: opts[:category_id])
|
|
|
|
return unless Guardian.new(current_user).can_see_category?(current_category)
|
|
|
|
|
|
|
|
if route[:post_number].to_i > 1
|
|
|
|
post = Post.find_by(topic_id: route[:topic_id], post_number: route[:post_number])
|
|
|
|
|
|
|
|
return unless post.present? && !post.hidden
|
|
|
|
return unless current_category.id == post.topic.category_id || Guardian.new.can_see_post?(post)
|
|
|
|
|
|
|
|
topic = post.topic
|
|
|
|
excerpt = post.excerpt(SiteSetting.post_onebox_maxlength)
|
|
|
|
excerpt.gsub!(/[\r\n]+/, " ")
|
|
|
|
excerpt.gsub!("[/quote]", "[quote]") # don't break my quote
|
|
|
|
|
|
|
|
quote = "[quote=\"#{post.user.username}, topic:#{topic.id}, post:#{post.post_number}\"]\n#{excerpt}\n[/quote]"
|
|
|
|
|
|
|
|
PrettyText.cook(quote)
|
|
|
|
else
|
|
|
|
return unless topic = Topic.find_by(id: route[:topic_id])
|
|
|
|
return unless current_category.id == topic.category_id || Guardian.new.can_see_topic?(topic)
|
|
|
|
|
|
|
|
first_post = topic.ordered_posts.first
|
|
|
|
|
|
|
|
args = {
|
|
|
|
topic_id: topic.id,
|
|
|
|
avatar: PrettyText.avatar_img(topic.user.avatar_template, "tiny"),
|
|
|
|
original_url: url,
|
|
|
|
title: PrettyText.unescape_emoji(CGI::escapeHTML(topic.title)),
|
|
|
|
category_html: CategoryBadge.html_for(topic.category),
|
|
|
|
quote: first_post.excerpt(SiteSetting.post_onebox_maxlength),
|
|
|
|
}
|
|
|
|
|
|
|
|
template = File.read("#{Rails.root}/lib/onebox/templates/discourse_topic_onebox.hbs")
|
|
|
|
Mustache.render(template, args)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.local_user_html(url, route)
|
|
|
|
username = route[:username] || ""
|
|
|
|
|
|
|
|
if user = User.find_by(username_lower: username.downcase)
|
|
|
|
args = {
|
|
|
|
user_id: user.id,
|
|
|
|
username: user.username,
|
|
|
|
avatar: PrettyText.avatar_img(user.avatar_template, "extra_large"),
|
|
|
|
name: user.name,
|
|
|
|
bio: user.user_profile.bio_excerpt(230),
|
|
|
|
location: user.user_profile.location,
|
|
|
|
joined: I18n.t('joined'),
|
|
|
|
created_at: user.created_at.strftime(I18n.t('datetime_formats.formats.date_only')),
|
|
|
|
website: user.user_profile.website,
|
|
|
|
website_name: UserSerializer.new(user).website_name,
|
|
|
|
original_url: url
|
|
|
|
}
|
|
|
|
|
|
|
|
template = File.read("#{Rails.root}/lib/onebox/templates/discourse_user_onebox.hbs")
|
|
|
|
Mustache.render(template, args)
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.external_onebox(url)
|
2016-10-24 05:46:22 -05:00
|
|
|
Rails.cache.fetch(onebox_cache_key(url), expires_in: 1.day) do
|
2017-08-08 04:44:27 -05:00
|
|
|
fd = FinalDestination.new(url, ignore_redirects: ignore_redirects, force_get_hosts: force_get_hosts)
|
2017-06-06 14:02:11 -05:00
|
|
|
uri = fd.resolve
|
2016-10-24 05:46:22 -05:00
|
|
|
return blank_onebox if uri.blank? || SiteSetting.onebox_domains_blacklist.include?(uri.hostname)
|
2017-12-18 11:31:41 -06:00
|
|
|
|
2017-06-06 14:02:11 -05:00
|
|
|
options = {
|
|
|
|
cache: {},
|
|
|
|
max_width: 695,
|
|
|
|
sanitize_config: Sanitize::Config::DISCOURSE_ONEBOX
|
|
|
|
}
|
|
|
|
|
|
|
|
options[:cookie] = fd.cookie if fd.cookie
|
|
|
|
|
2017-12-18 11:31:41 -06:00
|
|
|
if Rails.env.development? && SiteSetting.port.to_i > 0
|
|
|
|
Onebox.options = { allowed_ports: [80, 443, SiteSetting.port.to_i] }
|
|
|
|
end
|
|
|
|
|
2017-06-06 15:39:15 -05:00
|
|
|
r = Onebox.preview(uri.to_s, options)
|
2017-12-18 11:31:41 -06:00
|
|
|
|
2018-02-13 17:39:44 -06:00
|
|
|
{ onebox: r.to_s, preview: r&.placeholder_html.to_s }
|
2016-10-24 05:46:22 -05:00
|
|
|
end
|
|
|
|
end
|
2014-03-17 21:12:58 -05:00
|
|
|
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|