2015-08-18 17:15:46 -04:00
|
|
|
class EmbeddableHost < ActiveRecord::Base
|
2016-01-11 11:06:09 -05:00
|
|
|
validate :host_must_be_valid
|
2015-08-18 17:15:46 -04:00
|
|
|
belongs_to :category
|
|
|
|
|
|
|
|
before_validation do
|
|
|
|
self.host.sub!(/^https?:\/\//, '')
|
|
|
|
self.host.sub!(/\/.*$/, '')
|
|
|
|
end
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
def self.record_for_url(uri)
|
|
|
|
|
|
|
|
if uri.is_a?(String)
|
|
|
|
uri = URI(uri) rescue nil
|
|
|
|
end
|
2015-08-18 17:15:46 -04:00
|
|
|
return false unless uri.present?
|
|
|
|
|
|
|
|
host = uri.host
|
|
|
|
return false unless host.present?
|
|
|
|
|
|
|
|
where("lower(host) = ?", host).first
|
|
|
|
end
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
def self.url_allowed?(url)
|
|
|
|
uri = URI(url) rescue nil
|
|
|
|
return false unless uri.present?
|
|
|
|
|
2016-08-26 12:47:21 -04:00
|
|
|
path = uri.path
|
|
|
|
path << "?" << uri.query if uri.query.present?
|
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
host = record_for_url(uri)
|
2016-08-26 12:47:21 -04:00
|
|
|
|
2016-08-23 14:55:52 -04:00
|
|
|
return host.present? &&
|
2016-08-26 12:47:21 -04:00
|
|
|
(host.path_whitelist.blank? || !Regexp.new(host.path_whitelist).match(path).nil?)
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
|
|
|
|
2016-01-11 11:06:09 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def host_must_be_valid
|
|
|
|
if host !~ /\A[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,7}(:[0-9]{1,5})?(\/.*)?\Z/i &&
|
2016-07-22 15:33:21 -04:00
|
|
|
host !~ /\A(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\Z/ &&
|
|
|
|
host !~ /\A([a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.)?localhost(\:[0-9]{1,5})?(\/.*)?\Z/i
|
2016-01-11 11:06:09 -05:00
|
|
|
errors.add(:host, I18n.t('errors.messages.invalid'))
|
|
|
|
end
|
|
|
|
end
|
2015-08-18 17:15:46 -04:00
|
|
|
end
|
2015-09-17 20:41:10 -04:00
|
|
|
|
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: embeddable_hosts
|
|
|
|
#
|
2016-10-31 05:32:11 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# host :string not null
|
|
|
|
# category_id :integer not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# path_whitelist :string
|
2015-09-17 20:41:10 -04:00
|
|
|
#
|