tracks/app/controllers/integrations_controller.rb

61 lines
1.5 KiB
Ruby
Raw Normal View History

class IntegrationsController < ApplicationController
require 'mail'
2014-08-14 21:05:05 -05:00
skip_before_action :login_required, :only => [:cloudmailin, :search_plugin]
skip_before_action :verify_authenticity_token, only: [:cloudmailin]
2014-08-14 21:05:05 -05:00
def index
@page_title = 'TRACKS::Integrations'
end
2014-08-14 21:05:05 -05:00
def rest_api
@page_title = 'TRACKS::REST API Documentation'
end
2014-08-14 21:05:05 -05:00
def search_plugin
@icon_data = [File.open(File.join(Rails.root, 'app', 'assets', 'images', 'done.png')).read].
pack('m').gsub(/\n/, '')
end
def cloudmailin
2013-05-05 20:32:32 +02:00
if !verify_cloudmailin_signature
render :text => "Message signature verification failed.", :status => 403
return false
end
2014-08-14 21:05:05 -05:00
2013-05-05 20:32:32 +02:00
if process_message(params[:message])
2012-07-12 13:14:21 +02:00
render :text => 'success', :status => 200
else
2012-07-12 13:14:21 +02:00
render :text => "No user found or other error", :status => 404
end
end
2014-08-14 21:05:05 -05:00
2012-07-12 13:14:21 +02:00
private
2013-05-05 20:32:32 +02:00
def process_message(message)
MessageGateway::receive(Mail.new(message))
end
def verify_cloudmailin_signature
provided = request.request_parameters.delete(:signature)
signature = Digest::MD5.hexdigest(flatten_params(request.request_parameters).sort.map{|k,v| v}.join + SITE_CONFIG['cloudmailin'])
2013-05-05 20:32:32 +02:00
return provided == signature
end
2014-08-14 21:05:05 -05:00
def flatten_params(params, title = nil, result = {})
params.each do |key, value|
if value.kind_of?(Hash)
key_name = title ? "#{title}[#{key}]" : key
flatten_params(value, key_name, result)
else
key_name = title ? "#{title}[#{key}]" : key
result[key_name] = value
end
end
return result
end
end