2013-02-06 03:16:51 +08:00
|
|
|
require 'v8'
|
|
|
|
require 'nokogiri'
|
2015-01-30 05:53:48 +08:00
|
|
|
require_dependency 'url_helper'
|
2013-05-28 07:48:47 +08:00
|
|
|
require_dependency 'excerpt_parser'
|
2013-10-16 06:33:06 +08:00
|
|
|
require_dependency 'post'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
module PrettyText
|
|
|
|
|
|
|
|
class Helpers
|
2015-01-30 05:53:48 +08:00
|
|
|
include UrlHelper
|
2013-07-16 15:48:48 +08:00
|
|
|
|
|
|
|
def t(key, opts)
|
2014-10-03 00:19:01 +08:00
|
|
|
key = "js." + key
|
|
|
|
unless opts
|
2015-04-09 23:04:14 +08:00
|
|
|
I18n.t(key)
|
2014-10-03 00:19:01 +08:00
|
|
|
else
|
|
|
|
str = I18n.t(key, Hash[opts.entries].symbolize_keys).dup
|
2015-04-09 23:04:14 +08:00
|
|
|
opts.each { |k,v| str.gsub!("{{#{k.to_s}}}", v.to_s) }
|
|
|
|
str
|
2013-07-16 15:48:48 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-13 03:51:28 +08:00
|
|
|
# functions here are available to v8
|
2013-02-06 03:16:51 +08:00
|
|
|
def avatar_template(username)
|
|
|
|
return "" unless username
|
2014-05-06 21:41:59 +08:00
|
|
|
user = User.find_by(username_lower: username.downcase)
|
2015-01-30 05:53:48 +08:00
|
|
|
return "" unless user.present?
|
2015-03-13 03:51:28 +08:00
|
|
|
|
|
|
|
# TODO: Add support for ES6 and call `avatar-template` directly
|
|
|
|
if !user.uploaded_avatar_id && SiteSetting.default_avatars.present?
|
|
|
|
split_avatars = SiteSetting.default_avatars.split("\n")
|
|
|
|
if split_avatars.present?
|
|
|
|
hash = username.each_char.reduce(0) do |result, char|
|
|
|
|
[((result << 5) - result) + char.ord].pack('L').unpack('l').first
|
|
|
|
end
|
|
|
|
|
|
|
|
avatar_template = split_avatars[hash.abs % split_avatars.size]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
avatar_template = user.avatar_template
|
|
|
|
end
|
|
|
|
|
|
|
|
schemaless absolute avatar_template
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def is_username_valid(username)
|
|
|
|
return false unless username
|
|
|
|
username = username.downcase
|
2015-01-30 05:53:48 +08:00
|
|
|
User.exec_sql('SELECT 1 FROM users WHERE username_lower = ?', username).values.length == 1
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@mutex = Mutex.new
|
2013-08-16 06:12:10 +08:00
|
|
|
@ctx_init = Mutex.new
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def self.mention_matcher
|
2013-03-24 08:57:00 +08:00
|
|
|
Regexp.new("(\@[a-zA-Z0-9_]{#{User.username_length.begin},#{User.username_length.end}})")
|
2013-02-26 00:42:20 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
def self.app_root
|
|
|
|
Rails.root
|
|
|
|
end
|
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
def self.create_new_context
|
2013-12-09 09:43:48 +08:00
|
|
|
# timeout any eval that takes longer that 5 seconds
|
|
|
|
ctx = V8::Context.new(timeout: 5000)
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx["helpers"] = Helpers.new
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx_load(ctx,
|
2014-04-15 04:55:57 +08:00
|
|
|
"vendor/assets/javascripts/md5.js",
|
|
|
|
"vendor/assets/javascripts/lodash.js",
|
|
|
|
"vendor/assets/javascripts/Markdown.Converter.js",
|
|
|
|
"lib/headless-ember.js",
|
|
|
|
"vendor/assets/javascripts/rsvp.js",
|
|
|
|
Rails.configuration.ember.handlebars_location
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-01-06 13:50:04 +08:00
|
|
|
ctx.eval("var Discourse = {}; Discourse.SiteSettings = {};")
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx.eval("var window = {}; window.devicePixelRatio = 2;") # hack to make code think stuff is retina
|
|
|
|
ctx.eval("var I18n = {}; I18n.t = function(a,b){ return helpers.t(a,b); }");
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-03-13 03:51:28 +08:00
|
|
|
ctx.eval("var modules = {};")
|
|
|
|
|
2013-08-30 01:11:12 +08:00
|
|
|
decorate_context(ctx)
|
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx_load(ctx,
|
2014-04-15 04:55:57 +08:00
|
|
|
"vendor/assets/javascripts/better_markdown.js",
|
|
|
|
"app/assets/javascripts/defer/html-sanitizer-bundle.js",
|
|
|
|
"app/assets/javascripts/discourse/dialects/dialect.js",
|
|
|
|
"app/assets/javascripts/discourse/lib/utilities.js",
|
|
|
|
"app/assets/javascripts/discourse/lib/html.js",
|
2015-03-13 03:51:28 +08:00
|
|
|
"app/assets/javascripts/discourse/lib/markdown.js",
|
2014-04-15 04:55:57 +08:00
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2015-01-24 07:07:39 +08:00
|
|
|
Dir["#{app_root}/app/assets/javascripts/discourse/dialects/**.js"].sort.each do |dialect|
|
2015-01-30 00:35:52 +08:00
|
|
|
ctx.load(dialect) unless dialect =~ /\/dialect\.js$/
|
2013-08-09 06:14:12 +08:00
|
|
|
end
|
|
|
|
|
2015-01-30 00:35:52 +08:00
|
|
|
# emojis
|
2015-01-24 07:07:39 +08:00
|
|
|
emoji = ERB.new(File.read("#{app_root}/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb"))
|
2014-12-23 18:06:55 +08:00
|
|
|
ctx.eval(emoji.result)
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# Load server side javascripts
|
|
|
|
if DiscoursePluginRegistry.server_side_javascripts.present?
|
|
|
|
DiscoursePluginRegistry.server_side_javascripts.each do |ssjs|
|
2013-11-20 11:38:21 +08:00
|
|
|
if(ssjs =~ /\.erb/)
|
|
|
|
erb = ERB.new(File.read(ssjs))
|
|
|
|
erb.filename = ssjs
|
|
|
|
ctx.eval(erb.result)
|
|
|
|
else
|
|
|
|
ctx.load(ssjs)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.v8
|
|
|
|
return @ctx if @ctx
|
|
|
|
|
|
|
|
# ensure we only init one of these
|
|
|
|
@ctx_init.synchronize do
|
|
|
|
return @ctx if @ctx
|
|
|
|
@ctx = create_new_context
|
|
|
|
end
|
2014-04-15 04:55:57 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
@ctx
|
|
|
|
end
|
|
|
|
|
2014-11-14 14:51:04 +08:00
|
|
|
def self.reset_context
|
|
|
|
@ctx_init.synchronize do
|
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-08-30 01:11:12 +08:00
|
|
|
def self.decorate_context(context)
|
|
|
|
context.eval("Discourse.SiteSettings = #{SiteSetting.client_settings_json};")
|
|
|
|
context.eval("Discourse.CDN = '#{Rails.configuration.action_controller.asset_host}';")
|
|
|
|
context.eval("Discourse.BaseUrl = 'http://#{RailsMultisite::ConnectionManagement.current_hostname}';")
|
2015-01-30 05:53:48 +08:00
|
|
|
context.eval("Discourse.getURL = function(url) { return '#{Discourse::base_uri}' + url };")
|
|
|
|
context.eval("Discourse.getURLWithCDN = function(url) { url = Discourse.getURL(url); if (Discourse.CDN) { url = Discourse.CDN + url; } return url; };")
|
2013-08-30 01:11:12 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.markdown(text, opts=nil)
|
|
|
|
# we use the exact same markdown converter as the client
|
2013-02-26 00:42:20 +08:00
|
|
|
# TODO: use the same extensions on both client and server (in particular the template for mentions)
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
baked = nil
|
|
|
|
|
2014-02-04 08:12:53 +08:00
|
|
|
protect do
|
2013-08-16 11:03:47 +08:00
|
|
|
context = v8
|
2013-02-06 03:16:51 +08:00
|
|
|
# we need to do this to work in a multi site environment, many sites, many settings
|
2013-08-30 01:11:12 +08:00
|
|
|
decorate_context(context)
|
2013-10-12 04:24:27 +08:00
|
|
|
|
|
|
|
context_opts = opts || {}
|
|
|
|
context_opts[:sanitize] ||= true
|
|
|
|
context['opts'] = context_opts
|
2013-08-16 11:03:47 +08:00
|
|
|
context['raw'] = text
|
2013-10-12 04:24:27 +08:00
|
|
|
|
|
|
|
if Post.white_listed_image_classes.present?
|
|
|
|
Post.white_listed_image_classes.each do |klass|
|
|
|
|
context.eval("Discourse.Markdown.whiteListClass('#{klass}')")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-01-30 00:35:52 +08:00
|
|
|
# custom emojis
|
|
|
|
Emoji.custom.each do |emoji|
|
|
|
|
context.eval("Discourse.Dialect.registerEmoji('#{emoji.name}', '#{emoji.url}');")
|
|
|
|
end
|
|
|
|
|
2013-08-16 11:03:47 +08:00
|
|
|
context.eval('opts["mentionLookup"] = function(u){return helpers.is_username_valid(u);}')
|
|
|
|
context.eval('opts["lookupAvatar"] = function(p){return Discourse.Utilities.avatarImg({size: "tiny", avatarTemplate: helpers.avatar_template(p)});}')
|
|
|
|
baked = context.eval('Discourse.Markdown.markdownConverter(opts).makeHtml(raw)')
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-11-14 14:51:04 +08:00
|
|
|
if baked.blank? && !(opts || {})[:skip_blank_test]
|
|
|
|
# we may have a js engine issue
|
|
|
|
test = markdown("a", skip_blank_test: true)
|
|
|
|
if test.blank?
|
|
|
|
Rails.logger.warn("Markdown engine appears to have crashed, resetting context")
|
|
|
|
reset_context
|
|
|
|
opts ||= {}
|
|
|
|
opts = opts.dup
|
|
|
|
opts[:skip_blank_test] = true
|
|
|
|
baked = markdown(text, opts)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
baked
|
|
|
|
end
|
|
|
|
|
|
|
|
# leaving this here, cause it invokes v8, don't want to implement twice
|
2013-08-14 04:08:29 +08:00
|
|
|
def self.avatar_img(avatar_template, size)
|
2014-02-04 08:12:53 +08:00
|
|
|
protect do
|
2013-08-14 04:08:29 +08:00
|
|
|
v8['avatarTemplate'] = avatar_template
|
2013-02-06 03:16:51 +08:00
|
|
|
v8['size'] = size
|
2013-08-30 01:11:12 +08:00
|
|
|
decorate_context(v8)
|
2014-02-04 08:12:53 +08:00
|
|
|
v8.eval("Discourse.Utilities.avatarImg({ avatarTemplate: avatarTemplate, size: size });")
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.cook(text, opts={})
|
2015-04-24 01:33:29 +08:00
|
|
|
options = opts.dup
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
# we have a minor inconsistency
|
2015-04-24 01:33:29 +08:00
|
|
|
options[:topicId] = opts[:topic_id]
|
|
|
|
|
|
|
|
sanitized = markdown(text.dup, options)
|
2015-05-26 09:13:12 +08:00
|
|
|
|
|
|
|
doc = Nokogiri::HTML.fragment(sanitized)
|
|
|
|
|
|
|
|
if !options[:omit_nofollow] && SiteSetting.add_rel_nofollow_to_user_content
|
|
|
|
add_rel_nofollow_to_user_content(doc)
|
|
|
|
end
|
|
|
|
|
|
|
|
if SiteSetting.s3_cdn_url.present? && SiteSetting.enable_s3_uploads
|
|
|
|
add_s3_cdn(doc)
|
|
|
|
end
|
|
|
|
|
|
|
|
doc.to_html
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.add_s3_cdn(doc)
|
|
|
|
doc.css("img").each do |img|
|
|
|
|
img["src"] = img["src"].sub(Discourse.store.absolute_base_url, SiteSetting.s3_cdn_url)
|
|
|
|
end
|
2013-02-11 08:43:07 +08:00
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2015-05-26 09:13:12 +08:00
|
|
|
def self.add_rel_nofollow_to_user_content(doc)
|
2013-02-11 15:58:19 +08:00
|
|
|
whitelist = []
|
2013-02-11 16:01:33 +08:00
|
|
|
|
2013-11-20 11:38:21 +08:00
|
|
|
domains = SiteSetting.exclude_rel_nofollow_domains
|
2014-03-30 07:50:44 +08:00
|
|
|
whitelist = domains.split('|') if domains.present?
|
2013-02-11 16:01:33 +08:00
|
|
|
|
2013-02-11 08:43:07 +08:00
|
|
|
site_uri = nil
|
|
|
|
doc.css("a").each do |l|
|
|
|
|
href = l["href"].to_s
|
2013-02-26 00:42:20 +08:00
|
|
|
begin
|
2013-02-11 08:43:07 +08:00
|
|
|
uri = URI(href)
|
|
|
|
site_uri ||= URI(Discourse.base_url)
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-11-06 02:04:47 +08:00
|
|
|
if !uri.host.present? ||
|
|
|
|
uri.host.ends_with?(site_uri.host) ||
|
|
|
|
whitelist.any?{|u| uri.host.ends_with?(u)}
|
2013-02-11 08:43:07 +08:00
|
|
|
# we are good no need for nofollow
|
|
|
|
else
|
|
|
|
l["rel"] = "nofollow"
|
|
|
|
end
|
2014-03-07 17:44:04 +08:00
|
|
|
rescue URI::InvalidURIError, URI::InvalidComponentError
|
2013-02-26 00:42:20 +08:00
|
|
|
# add a nofollow anyway
|
2013-02-11 08:43:07 +08:00
|
|
|
l["rel"] = "nofollow"
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
class DetectedLink
|
|
|
|
attr_accessor :is_quote, :url
|
|
|
|
|
|
|
|
def initialize(url, is_quote=false)
|
|
|
|
@url = url
|
|
|
|
@is_quote = is_quote
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
def self.extract_links(html)
|
|
|
|
links = []
|
2013-06-06 02:53:07 +08:00
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
# remove href inside quotes
|
|
|
|
doc.css("aside.quote a").each { |l| l["href"] = "" }
|
2014-07-11 12:17:01 +08:00
|
|
|
|
2013-06-06 02:53:07 +08:00
|
|
|
# extract all links from the post
|
2014-07-11 12:17:01 +08:00
|
|
|
doc.css("a").each { |l|
|
|
|
|
unless l["href"].blank?
|
|
|
|
links << DetectedLink.new(l["href"])
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2013-06-06 02:53:07 +08:00
|
|
|
# extract links to quotes
|
2014-05-21 05:20:52 +08:00
|
|
|
doc.css("aside.quote[data-topic]").each do |a|
|
2013-06-06 02:53:07 +08:00
|
|
|
topic_id = a['data-topic']
|
2013-02-26 00:42:20 +08:00
|
|
|
|
2013-02-14 04:22:04 +08:00
|
|
|
url = "/t/topic/#{topic_id}"
|
2013-06-06 02:53:07 +08:00
|
|
|
if post_number = a['data-post']
|
2013-02-14 04:22:04 +08:00
|
|
|
url << "/#{post_number}"
|
|
|
|
end
|
|
|
|
|
2014-07-11 12:17:01 +08:00
|
|
|
links << DetectedLink.new(url, true)
|
2013-02-14 04:22:04 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
links
|
|
|
|
end
|
|
|
|
|
2013-05-28 07:48:47 +08:00
|
|
|
def self.excerpt(html, max_length, options={})
|
2014-11-06 03:37:00 +08:00
|
|
|
# TODO: properly fix this HACK in ExcerptParser without introducing XSS
|
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
strip_image_wrapping(doc)
|
|
|
|
html = doc.to_html
|
|
|
|
|
2013-05-28 07:48:47 +08:00
|
|
|
ExcerptParser.get_excerpt(html, max_length, options)
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-06 03:28:10 +08:00
|
|
|
def self.strip_links(string)
|
|
|
|
return string if string.blank?
|
|
|
|
|
|
|
|
# If the user is not basic, strip links from their bio
|
|
|
|
fragment = Nokogiri::HTML.fragment(string)
|
2014-09-18 00:08:00 +08:00
|
|
|
fragment.css('a').each {|a| a.replace(a.inner_html) }
|
2013-06-06 03:28:10 +08:00
|
|
|
fragment.to_html
|
|
|
|
end
|
|
|
|
|
2014-04-18 00:32:51 +08:00
|
|
|
# Given a Nokogiri doc, convert all links to absolute
|
|
|
|
def self.make_all_links_absolute(doc)
|
2013-11-29 04:57:21 +08:00
|
|
|
site_uri = nil
|
2014-02-04 09:57:16 +08:00
|
|
|
doc.css("a").each do |link|
|
|
|
|
href = link["href"].to_s
|
2013-11-29 04:57:21 +08:00
|
|
|
begin
|
|
|
|
uri = URI(href)
|
|
|
|
site_uri ||= URI(Discourse.base_url)
|
2014-02-04 09:57:16 +08:00
|
|
|
link["href"] = "#{site_uri}#{link['href']}" unless uri.host.present?
|
2014-07-30 15:09:55 +08:00
|
|
|
rescue URI::InvalidURIError, URI::InvalidComponentError
|
2013-11-29 04:57:21 +08:00
|
|
|
# leave it
|
|
|
|
end
|
|
|
|
end
|
2014-04-18 00:32:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.strip_image_wrapping(doc)
|
|
|
|
doc.css(".lightbox-wrapper .meta").remove
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.format_for_email(html)
|
|
|
|
doc = Nokogiri::HTML.fragment(html)
|
|
|
|
make_all_links_absolute(doc)
|
|
|
|
strip_image_wrapping(doc)
|
2013-11-29 04:57:21 +08:00
|
|
|
doc.to_html
|
|
|
|
end
|
|
|
|
|
2013-05-28 07:48:47 +08:00
|
|
|
protected
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-02-04 08:12:53 +08:00
|
|
|
class JavaScriptError < StandardError
|
|
|
|
attr_accessor :message, :backtrace
|
|
|
|
|
|
|
|
def initialize(message, backtrace)
|
|
|
|
@message = message
|
|
|
|
@backtrace = backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.protect
|
|
|
|
rval = nil
|
|
|
|
@mutex.synchronize do
|
|
|
|
begin
|
|
|
|
rval = yield
|
|
|
|
# This may seem a bit odd, but we don't want to leak out
|
|
|
|
# objects that require locks on the v8 vm, to get a backtrace
|
|
|
|
# you need a lock, if this happens in the wrong spot you can
|
|
|
|
# deadlock a process
|
|
|
|
rescue V8::Error => e
|
|
|
|
raise JavaScriptError.new(e.message, e.backtrace)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rval
|
|
|
|
end
|
|
|
|
|
2013-08-16 06:12:10 +08:00
|
|
|
def self.ctx_load(ctx, *files)
|
2013-05-28 07:48:47 +08:00
|
|
|
files.each do |file|
|
2013-08-16 06:12:10 +08:00
|
|
|
ctx.load(app_root + file)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|