diff --git a/lib/common/models/wp_version.rb b/lib/common/models/wp_version.rb index ae7eab4f..eb8f918d 100755 --- a/lib/common/models/wp_version.rb +++ b/lib/common/models/wp_version.rb @@ -23,4 +23,11 @@ class WpVersion < WpItem number == other.number end + # @return [ Array ] All the stable versions from version_file + def self.all(versions_file = WP_VERSIONS_FILE) + Nokogiri.XML(File.open(versions_file)).css('version').reduce([]) do |a, node| + a << node.text.to_s + end + end + end diff --git a/lib/common/models/wp_version/findable.rb b/lib/common/models/wp_version/findable.rb index bdfdf2f5..dbc6ab50 100755 --- a/lib/common/models/wp_version/findable.rb +++ b/lib/common/models/wp_version/findable.rb @@ -114,6 +114,33 @@ class WpVersion < WpItem ) end + def find_from_stylesheets_numbers(target_uri) + wp_versions = WpVersion.all + found = {} + pattern = /ver=([0-9\.]+)/i + + Nokogiri::HTML(Browser.get(target_uri.to_s).body).css('link,script').each do |tag| + %w(herf src).each do |attribute| + attr_value = tag.attribute(attribute).to_s + + next if attr_value.nil? || attr_value.empty? + + uri = Addressable::URI.parse(attr_value) + next unless uri.query && uri.query.match(pattern) + + version = Regexp.last_match[1].to_s + + found[version] ||= 0 + found[version] += 1 + end + end + + found.delete_if { |v, _| !wp_versions.include?(v) } + + best_guess = found.sort_by(&:last).last + best_guess ? best_guess[0] : nil + end + # Uses data/wp_versions.xml to try to identify a # wordpress version. # diff --git a/spec/lib/common/models/wp_version_spec.rb b/spec/lib/common/models/wp_version_spec.rb index 49071cc8..d9ad5795 100644 --- a/spec/lib/common/models/wp_version_spec.rb +++ b/spec/lib/common/models/wp_version_spec.rb @@ -29,4 +29,12 @@ describe WpVersion do end end + describe '#all' do + let(:versions_file) { File.join(MODELS_FIXTURES, 'wp_version', 'findable', 'advanced_fingerprinting', 'wp_versions.xml') } + + it 'returns the array containign the two versions' do + expect(WpVersion.all(versions_file)).to eq ['3.2.1', '3.2'] + end + end + end