PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / known-bots / bots / class-bing-bot.php

class-bing-bot.php in Defender Security – Malware Scanner, Login Security & Firewall 6.3.0, at src/component/known-bots/bots/class-bing-bot.php

79 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Fetches Bing Bot IPs.
4 *
5 * @package WP_Defender\Component\Known_Bots\Bots
6 */
7
8 namespace WP_Defender\Component\Known_Bots\Bots;
9
10 /**
11 * This class is responsible for fetching and managing Bing Bot IPs.
12 */
13 class Bing_Bot extends Abstract_Bot {
14 /**
15 * Returns the name of the bot.
16 *
17 * @return string The name of the bot.
18 */
19 public function get_name(): string {
20 return 'bingbot';
21 }
22
23 /**
24 * Fetches the IPs for Bing Bot.
25 *
26 * This method retrieves the IP ranges used by Bing Bot from a remote JSON endpoint.
27 * It returns an array of IP addresses in both IPv4 and IPv6 formats.
28 *
29 * @return array An array of IP addresses used by Bing Bot.
30 */
31 public function fetch_ips(): array {
32 $url = 'https://www.bing.com/toolbox/bingbot.json';
33 $ips = array();
34
35 $response = wp_remote_get( $url );
36 if ( is_array( $response ) && ! is_wp_error( $response ) ) {
37 $data = json_decode( wp_remote_retrieve_body( $response ), true );
38 foreach ( $data['prefixes'] ?? array() as $entry ) {
39 if ( isset( $entry['ipv4Prefix'] ) ) {
40 $ips[] = $entry['ipv4Prefix'];
41 }
42
43 if ( isset( $entry['ipv6Prefix'] ) ) {
44 $ips[] = $entry['ipv6Prefix'];
45 }
46 }
47 }
48
49 return $ips;
50 }
51
52 /**
53 * Checks if the user agent belongs to Bing.
54 *
55 * @param string $user_agent Optional user agent string to check.
56 *
57 * @return bool
58 */
59 public function is_ua( string $user_agent = '' ): bool {
60 $ua = $this->normalize_ua( $user_agent );
61 if ( '' === $ua ) {
62 return false;
63 }
64
65 return (bool) preg_match( '/Bingbot|MSNBot|MSNBot-Media|AdIdxBot|BingPreview/i', $ua );
66 }
67
68 /**
69 * Checks if IP is from Bing, based on DNS verification.
70 *
71 * @param string $ip The IP address to check.
72 *
73 * @return bool
74 */
75 public function is_ip( string $ip ): bool {
76 return $this->verify_dns( $ip, '(^|\.)(msn\.com|msnbot\.msn\.com|bing\.com)' );
77 }
78 }
79