Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding iframe web/facebook request fix

  • Loading branch information...
commit f3fb9eb72b33b26c8211b650621fc8cefc1866a4 1 parent 015fef6
Russell authored
18 app/controllers/application_controller.rb
View
@@ -27,6 +27,7 @@ def facebook_session_expired
include AuthenticatedSystem
helper :all # include all helpers, all the time
+ before_filter :set_iframe_status
#protect_from_forgery # See ActionController::RequestForgeryProtection for details
before_filter :set_p3p_header
before_filter :set_facebook_session_wrapper
@@ -49,6 +50,7 @@ def facebook_session_expired
helper_method :current_facebook_user
helper_method :get_setting
helper_method :get_ad_layout
+ helper_method :iframe_facebook_request?
def logged_in_to_facebook_and_app_authorized
if ensure_application_is_installed_by_facebook_user
@@ -68,7 +70,7 @@ def set_p3p_header
def set_facebook_session_wrapper
begin
set_facebook_session
- session[:facebook_request] = true if request_comes_from_facebook? or params[:iframe_req].present?
+ #session[:facebook_request] = true if request_comes_from_facebook? or params[:iframe_req].present?
rescue
return facebook_session_expired
end
@@ -251,6 +253,11 @@ def set_locale
cookies[:locale] = locale unless (cookies[:locale] && cookies[:locale] == locale)
end
+ def set_iframe_status
+ @iframe_status = params[:iframe] || false
+ headers["Newscloud-Origin"] = @iframe_status ? 'iframe' : 'web'
+ end
+
def default_url_options(options={})
format = options[:format] || request.format.to_sym
unless ['html', 'fbml', 'json', 'js', 'fbjs', 'xml', 'atom', 'rss'].include? format.to_s
@@ -271,10 +278,13 @@ def default_url_options(options={})
end
end
end
- {
+ opts = {
:locale => I18n.locale,
:format => format
}
+ # Disabled this and moved to IframeRewriter middleware due to fragment caching
+ #opts[:iframe] = @iframe_status if @iframe_status and not options[:canvas] == true
+ opts
end
def tag_cloud(item)
@@ -313,11 +323,11 @@ def expire_cache item
end
def canvas?
- iframe_facebook_request? ? true : false
+ iframe_facebook_request?
end
def iframe_facebook_request?
- (session and session[:facebook_request]) or request_comes_from_facebook?
+ !! @iframe_status
end
def after_facebook_login_url
4 app/helpers/facebook_helper.rb
View
@@ -39,10 +39,6 @@ def fb_meta_share_button item
text
end
- def iframe_facebook_request?
- (session and session[:facebook_request]) or request_comes_from_facebook?
- end
-
private
def build_stream_post item
2  app/workers/comment_messenger.rb
View
@@ -23,7 +23,7 @@ def self.facebook_messenger comment, item_url, app_caption, image_url
fb_client.post("#{comment.user.fb_user_id}/feed", "Post", :message => "commented on", :link => item_url, :name => comment.commentable.item_title, :caption => app_caption, :description => caption, :picture => image_url)
rescue Exception => exception
type, error = exception.to_s.split(':').map {|e| e.strip}
- if type == "OAuthException" and error =~ /^Error processing access token/
+ if type == "OAuthException" and error =~ /^Error (processing|validating) access token/
Rails.logger.error "***FB OAUTH ERROR*** #{exception.inspect}"
vote.voter.update_attribute(:fb_oauth_key, nil)
else
2  app/workers/vote_messenger.rb
View
@@ -23,7 +23,7 @@ def self.facebook_messenger vote, item_url, app_caption, image_url
fb_client.post("#{vote.voter.fb_user_id}/feed", "Post", :message => "likes", :link => item_url, :name => vote.voteable.item_title, :caption => app_caption, :description => caption, :picture => image_url)
rescue Exception => exception
type, error = exception.to_s.split(':').map {|e| e.strip}
- if type == "OAuthException" and error =~ /^Error processing access token/
+ if type == "OAuthException" and error =~ /^Error (processing|validating) access token/
Rails.logger.error "***FB OAUTH ERROR*** #{exception.inspect}"
vote.voter.update_attribute(:fb_oauth_key, nil)
else
5 config/environment.rb
View
@@ -21,7 +21,12 @@
#require "#{RAILS_ROOT}/lib/acts_as_moderatable.rb"
#ActiveRecord::Base.send :include, Newscloud::Acts::Moderatable
+# Load Iframe Rewriter Middleware
+require "#{RAILS_ROOT}/lib/iframe_rewriter.rb"
+
Rails::Initializer.run do |config|
+ config.middleware.use Newscloud::IframeRewriter
+
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
2  config/routes.rb
View
@@ -3,6 +3,8 @@
# Set locale and make pretty urls
map.filter 'locale'
+ # Determine iframe or web origin of request
+ map.filter :iframe
# Mogli
map.resource :oauth, :controller=>"oauth"
49 lib/iframe_rewriter.rb
View
@@ -0,0 +1,49 @@
+# Inspired by [Karma Chameleon](http://gist.github.com/208616)
+
+require "nokogiri"
+require "uri"
+
+module Newscloud
+ class IframeRewriter
+
+ def initialize(app, flag = "iframe")
+ @app = app
+ @flag = flag
+ @base_url = FACEBOOKER["callback_url"].downcase
+ end
+
+ def call(env)
+ status, headers, response = @app.call(env)
+
+ if headers["Newscloud-Origin"] and headers["Newscloud-Origin"] == 'iframe'
+ response = add_iframes(response)
+ headers["Content-Length"] = response[0].size.to_s
+ end
+
+ [status, headers, response]
+ end
+
+ private
+
+ def add_iframes response
+ body = ""
+ response.each {|chunk| body += chunk}
+ doc = Nokogiri::XML(body)
+
+ doc.css("a").each { |link| link["href"] = add_iframe(link["href"]) if link["href"] }
+ doc.css("form").each { |form| form["action"] = add_iframe(form["action"]) if form["action"] }
+
+ response = [doc.to_s]
+ end
+
+ def add_iframe path
+ uri = URI.parse(path)
+ if uri.relative?
+ uri.path = "/#{@flag}#{uri.path}"
+ elsif uri.host.downcase.include? @base_url
+ uri.path = uri.path.sub(%r((^#{@base_url}/?)), '\1iframe/')
+ end
+ uri.to_s
+ end
+ end
+end
42 vendor/plugins/routing-filter/lib/routing_filter/iframe.rb
View
@@ -0,0 +1,42 @@
+require 'routing_filter/base'
+
+module RoutingFilter
+ class Iframe < Base
+ class << self
+ def iframe_pattern
+ @@iframe_pattern ||= %r(^/(iframe)(?=/|$))
+ end
+ end
+
+ def around_recognize(path, env, &block)
+ iframe = extract_iframe!(path)
+ returning yield do |params|
+ params[:iframe] = iframe if iframe
+ end
+ end
+
+ def around_generate(*args, &block)
+ iframe = args.extract_options!.delete(:iframe)
+
+ returning yield do |result|
+ # Disabled due to fragment caching
+ if false and iframe
+ url = result.is_a?(Array) ? result.first : result
+ prepend_iframe!(url, iframe)
+ end
+ end
+ end
+
+ protected
+
+ def extract_iframe!(path)
+ path.sub! self.class.iframe_pattern, ''
+ $1
+ end
+
+ def prepend_iframe!(url, iframe)
+ # Moved this to a rack middleware due to fragment caching issues
+ #url.sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{iframe}#{$2}" }
+ end
+ end
+end
6 vendor/plugins/routing-filter/lib/routing_filter/locale.rb
View
@@ -20,7 +20,7 @@ def locales=(locales)
end
def locales_pattern
- @@locales_pattern ||= %r(^/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
+ @@locales_pattern ||= %r(^(/iframe)?/(#{self.locales.map { |l| Regexp.escape(l.to_s) }.join('|')})(?=/|$))
end
end
@@ -48,8 +48,8 @@ def around_generate(*args, &block)
protected
def extract_locale!(path)
- path.sub! self.class.locales_pattern, ''
- $1
+ path.sub! self.class.locales_pattern, '\1'
+ $2
end
def prepend_locale?(locale)
Please sign in to comment.
Something went wrong with that request. Please try again.