'Analyzes the eggs and tries to deduce a PHP version number
(which is then saved to the kb).'

def _extract_version_from_egg(self, query_results):

    if (not query_results):
        return None
    else:
        desc_hashes = {}
        for query_result in query_results:
            body = query_result.http_response.get_body()
            hash_str = md5_hash(body)
            desc_hashes[query_result.egg_desc] = hash_str
        hash_set = set(desc_hashes.values())
        found = False
        matching_versions = []
        for version in self.EGG_DB:
            version_hashes = set(self.EGG_DB[version].values())
            if (len(hash_set) == len(hash_set.intersection(version_hashes))):
                matching_versions.append(version)
                found = True
        if matching_versions:
            if (len(matching_versions) > 1):
                desc = 'A    PHP    easter    egg    was    found    that    matches    several    different    versions    of    PHP.    The    PHP    framework    version    running    on    the    remote    server    was    identified    as    one    of    the    following:\n-    %s'
            else:
                desc = 'The    PHP    framework    version    running    on    the    remote    server    was    identified    as:\n-    %s'
            versions = '\n-    '.join(matching_versions)
            desc %= versions
            response_ids = [r.http_response.get_id() for r in query_results]
            i = Info('Fingerprinted    PHP    version', desc, response_ids, self.get_name())
            i['version'] = matching_versions
            kb.kb.append(self, 'version', i)
            om.out.information(i.get_desc())
        if (not found):
            version = 'unknown'
            powered_by_headers = kb.kb.raw_read('server_header', 'powered_by_string')
            for v in powered_by_headers:
                if (not isinstance(v, basestring)):
                    continue
                if ('php' not in v.lower()):
                    continue
                try:
                    version = v.split('/')[1]
                    break
                except IndexError:
                    pass
            msg = "The    PHP    version    could    not    be    identified    using    PHP    eggs,    please    send    this    signature    and    the    PHP    version    to    the    w3af    project    develop    mailing    list.    Signature:    EGG_DB['%s']    =    %r\n"
            msg = (msg % (version, desc_hashes))
            om.out.information(msg)
