129 lines
4.5 KiB
Ruby
129 lines
4.5 KiB
Ruby
class PageContentsController < OrbitAdminController
|
|
require 'uri'
|
|
|
|
IMG_INFO = {}
|
|
|
|
def index
|
|
params = OrbitHelper.params
|
|
if params["wiki"].present?
|
|
page = WikiPage.where(:uid => params["wiki"].to_s).first
|
|
page.view_count += 1
|
|
page.save
|
|
name = page.name rescue ""
|
|
html = page.content rescue ""
|
|
url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/wiki_pages/#{page.id}/edit?page_id=#{page.page.id.to_s}" : ""
|
|
else
|
|
page = Page.where(:page_id => params[:page_id].to_s).first
|
|
name = page.name rescue ""
|
|
html = page.page_contexts.order(:version=>-1).first.content rescue ""
|
|
url_to_edit = OrbitHelper.user_can_edit?(page) ? "/admin/page_contents/new?page_id=#{page.id.to_s}" : ""
|
|
end
|
|
request = OrbitHelper.request
|
|
meta_desc = html.nil? ? "" : ActionView::Base.full_sanitizer.sanitize(html)[0..200]
|
|
doc = Nokogiri::HTML( html )
|
|
img_srcs = doc.css('img').map{ |i| i['src'] }.compact
|
|
metas = [{"property" => "og:description", "content" => meta_desc},{"property" => "og:type", "content" => "Article"}]
|
|
if img_srcs.count > 0
|
|
img_src = img_srcs[0]
|
|
metas << {"property" => "og:image", "content" => URI.join(request.base_url, URI.encode(img_src)).to_s}
|
|
if img_src.start_with?('/')
|
|
img_src_path = File.join('public', img_src)
|
|
img_src_path = URI.decode(img_src_path)
|
|
if File.exist?(img_src_path)
|
|
image = MiniMagick::Image.open(img_src_path)
|
|
if image[:width]
|
|
metas << {"property" => "og:image:width", "content" => image[:width].to_s}
|
|
end
|
|
if image[:height]
|
|
metas << {"property" => "og:image:height", "content" => image[:height].to_s}
|
|
end
|
|
end
|
|
else
|
|
res = get_response(URI.parse(img_src))
|
|
image = IMG_INFO[img_src]
|
|
if image.nil?
|
|
if res.code == "200"
|
|
image_file = Tempfile.new
|
|
image_file.binmode
|
|
image_file.write(res.body)
|
|
image = MiniMagick::Image.open(image_file.path)
|
|
IMG_INFO[img_src] = {width: image[:width], height: image[:height]}
|
|
if image[:width]
|
|
metas << {"property" => "og:image:width", "content" => image[:width].to_s}
|
|
end
|
|
if image[:height]
|
|
metas << {"property" => "og:image:height", "content" => image[:height].to_s}
|
|
end
|
|
image_file.close
|
|
image_file.unlink
|
|
else
|
|
IMG_INFO[img_src] = {}
|
|
end
|
|
else
|
|
if image[:width]
|
|
metas << {"property" => "og:image:width", "content" => image[:width].to_s}
|
|
end
|
|
if image[:height]
|
|
metas << {"property" => "og:image:height", "content" => image[:height].to_s}
|
|
end
|
|
end
|
|
end
|
|
end
|
|
OrbitHelper.render_meta_tags(metas)
|
|
|
|
{
|
|
"html" => html,
|
|
"title" => name,
|
|
"view_count" => (page.view_count rescue ""),
|
|
"view-count-head" =>t('page_content.view_count'),
|
|
"url_to_edit" => url_to_edit
|
|
}
|
|
end
|
|
|
|
private
|
|
def net_http_get_response(uri,headers={})
|
|
port = uri.port
|
|
host = uri.host
|
|
http = Net::HTTP.new(host, port)
|
|
scheme = uri.scheme
|
|
if scheme == "https" || port.to_i == 443
|
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
|
http.use_ssl = true
|
|
end
|
|
http.open_timeout = 5
|
|
relative_path = (uri.path.blank? ? "/" : uri.path)
|
|
if !relative_path.start_with?("http") && !relative_path.start_with?("/")
|
|
relative_path = "/" + relative_path
|
|
end
|
|
relative_path = relative_path.sub(/^\/\.\.\/(\.\.\/)*/,'/')
|
|
while relative_path.include?("/../") do
|
|
relative_path = relative_path.gsub(/\/[^\/]+\/\.\.\//,'/')
|
|
end
|
|
relative_path = relative_path.gsub('//','/')
|
|
relative_path = URI.decode(relative_path)
|
|
relative_path += (uri.query.blank? ? '' : "?#{URI.decode(uri.query)}")
|
|
res = http.get(relative_path.gsub(" ","%20"))
|
|
if res.code == "400"
|
|
res = http.get(URI.encode(relative_path.gsub(" ","%20")))
|
|
end
|
|
res.uri = uri
|
|
res
|
|
end
|
|
def get_response(uri)
|
|
res = net_http_get_response(uri)
|
|
if res.code == "301" || res.code == "302"
|
|
location = res['Location']
|
|
cookie = res['Set-Cookie']
|
|
headers = {
|
|
'Cookie' => cookie.to_s
|
|
}
|
|
if location[0] == "/"
|
|
uri = URI.parse("#{uri.scheme}://#{uri.host}#{location}")
|
|
else
|
|
uri = URI.parse(location)
|
|
end
|
|
res = net_http_get_response(uri,headers)
|
|
end
|
|
return res
|
|
end
|
|
end |