array(), 'rules' => array(), ); $current = count( $groups ) - 1; } $groups[ $current ]['agents'][] = strtolower( $value ); $in_ua = true; } elseif ( ( 'allow' === $field || 'disallow' === $field ) && null !== $current ) { $groups[ $current ]['rules'][] = array( $field, $value ); $in_ua = false; } } return $groups; } /** * Is a crawler disallowed from the whole site (the "/" path)? * * Uses the group(s) naming the bot, falling back to "*" groups. Among the * rules that match "/", the longest path wins and Allow wins ties. * * @param string $robots robots.txt body ('' = no robots.txt = allowed). * @param string $bot User-agent token. * @return bool */ public static function is_blocked( $robots, $bot ) { $groups = self::parse( $robots ); $bot = strtolower( $bot ); $rules = array(); foreach ( $groups as $g ) { if ( in_array( $bot, $g['agents'], true ) ) { $rules = array_merge( $rules, $g['rules'] ); } } if ( empty( $rules ) ) { foreach ( $groups as $g ) { if ( in_array( '*', $g['agents'], true ) ) { $rules = array_merge( $rules, $g['rules'] ); } } } $best_len = -1; $best_allow = true; foreach ( $rules as $rule ) { list( $type, $path ) = $rule; if ( '' === $path ) { continue; // "Disallow:" with an empty value allows everything. } // Only rules covering every path matter for "whole site" ("/$" covers just the home page). if ( ! in_array( $path, array( '/', '/*', '*' ), true ) ) { continue; } $len = strlen( $path ); $allow = ( 'allow' === $type ); if ( $len > $best_len || ( $len === $best_len && $allow ) ) { $best_len = $len; $best_allow = $allow; } } return ! $best_allow; } }