from bs4 import BeautifulSoup
import json


class EmtpyValidator:
    __validator_id__ = 1

    @staticmethod
    def validate(response):
        validated = True
        if response is None:
            validated = False
        elif 'error' in response or response == '0' or response == '':
            validated = False

        return validated


class WordpressMD5HrefValidator:
    __validator_id__ = 2

    @staticmethod
    def validate(response):
        validated = False
        if response is not None:
            soup = BeautifulSoup(response)
            links = soup.findAll('link')
            if links:
                for link in links:
                    result = link.get('href')
                    if len(result) == 34 and '$' in result:
                        validated = True

        return validated


class HTMLDivIdValidator:
    __validator_id__ = 3

    # Temporary constants for div ID validators
    validators = [
        'match-qxqvqIEIozpbgQXqzpzq',
    ]

    def validate(self, response):
        validated = False
        if response is not None:
            soup = BeautifulSoup(response)
            links = soup.findAll('div')
            if links:
                for link in links:
                    if link.get('id') in self.validators:
                        validated = True

        return validated


class DatabaseErrorValidator:
    __validator_id__ = 4

    # Temporary constants for wordpress DB validators
    validators = [
        "You have an error in your SQL syntax",
        "WordPress database error"
    ]

    def validate(self, response):
        validated = False
        if response is not None:
            for validator in self.validators:
                if validator in response:
                    validated = True

        return validated


class FileNotFoundValidator:
    __validator_id__ = 5

    @staticmethod
    def validate(response):
        validated = True
        if response is None:
            validated = False
        elif 'File not found' in response or response == '0' or response == '':
            print(response)
            validated = False

        return validated


class JSONValidator:
    __validator_id__ = 6

    @staticmethod
    def validate(response):
        validated = False
        if response is not None:
            try:
                json.loads(response)
                validated = True
            except ValueError:
                pass
            except TypeError:
                pass

        return validated


class VulnpressStringValidator:
    __validator_id__ = 7

    vulnpress_string = '--VULNPRESS--'

    def validate(self, response):
        validated = False
        if response is not None:
            if self.vulnpress_string in response:
                validated = True

        return validated


class WordpressMD5Validator:
    __validator_id__ = 8

    @staticmethod
    def validate(response):
        validated = False
        if response is not None and len(response) == 34 and '$' in response:
            validated = True

        return validated
