Пример #1
0
    /**
     * Validate the contents of the textarea as either IP addresses, domain name or wildcard domain name (RFC 4592).
     * Used to validate a new line separated list of entries collected from a textarea control.
     *
     * This setting provides support for internationalised domain names (IDNs), however, such UTF-8 names will be converted to
     * their ascii-compatible encoding (punycode) on save, and converted back to their UTF-8 representation when fetched
     * via the get_setting() method, which has been overriden.
     *
     * @param string $data A list of FQDNs, DNS wildcard format domains, and IP addresses, separated by new lines.
     * @return mixed bool true for success or string:error on failure
     */
    public function validate($data) {
        if (empty($data)) {
            return true;
        }
        $entries = explode("\n", $data);
        $badentries = [];

        foreach ($entries as $key => $entry) {
            $entry = trim($entry);
            if (empty($entry)) {
                return get_string('validateemptylineerror', 'admin');
            }

            // Validate each string entry against the supported formats.
            if (\core\ip_utils::is_ip_address($entry) || \core\ip_utils::is_ipv6_range($entry)
                    || \core\ip_utils::is_ipv4_range($entry) || \core\ip_utils::is_domain_name($entry)
                    || \core\ip_utils::is_domain_matching_pattern($entry)) {
                continue;
            }

            // Otherwise, the entry is invalid.
            $badentries[] = $entry;
        }

        if ($badentries) {
            return get_string('validateerrorlist', 'admin', join(', ', $badentries));
        }
        return true;
    }
Пример #2
0
 /**
  * Test for \core\ip_utils::is_ipv6_range().
  *
  * @param string $addressrange the address range to validate.
  * @param bool $expected the expected result.
  * @dataProvider ipv6_range_data_provider
  */
 public function test_is_ipv6_range($addressrange, $expected)
 {
     $this->assertEquals($expected, \core\ip_utils::is_ipv6_range($addressrange));
 }
 /**
  * Helper to get all entries from the admin setting, as an array, sorted by classification.
  * Classifications include 'ipv4', 'ipv6', 'domain', 'domainwildcard'.
  *
  * @return array of host/domain/ip entries from the 'curlsecurityblockedhosts' config.
  */
 protected function get_blacklisted_hosts_by_category()
 {
     // For each of the admin setting entries, check and place in the correct section of the config array.
     $config = ['ipv6' => [], 'ipv4' => [], 'domain' => [], 'domainwildcard' => []];
     $entries = $this->get_blacklisted_hosts();
     foreach ($entries as $entry) {
         if (ip_utils::is_ipv6_address($entry) || ip_utils::is_ipv6_range($entry)) {
             $config['ipv6'][] = $entry;
         } else {
             if (ip_utils::is_ipv4_address($entry) || ip_utils::is_ipv4_range($entry)) {
                 $config['ipv4'][] = $entry;
             } else {
                 if (ip_utils::is_domain_name($entry)) {
                     $config['domain'][] = $entry;
                 } else {
                     if (ip_utils::is_domain_matching_pattern($entry)) {
                         $config['domainwildcard'][] = $entry;
                     }
                 }
             }
         }
     }
     return $config;
 }