| Server IP : 52.202.253.110 / Your IP : 216.73.217.139 Web Server : Apache/2.4.56 (Amazon Linux) OpenSSL/3.0.8 System : Linux ip-172-32-10-67.ec2.internal 6.1.49-70.116.amzn2023.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Sep 6 22:13:07 UTC 2023 x86_64 User : ec2-user ( 1000) PHP Version : 8.2.9 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : OFF Directory : /var/www/vhosts/cirrata_html/wp-content/plugins/widget-context/src/ |
Upload File : |
<?php
namespace Preseto\WidgetContext;
/**
* Match URI path regex-like rules.
*/
class UriRuleMatcher {
/**
* Delimiter used in the regex expressions.
*/
const DELIMITER = '/';
/**
* Map quoted regex patterns to actual regex patterns.
*
* @var array
*/
const QUOTED_PATTERN_TO_REGEX = array(
'\*' => '.*', // Enable the wildcard selectors.
);
/**
* Helper to sanitize and format rules for regex.
*
* @param array $rules List of regex-like rules.
*
* @return array
*/
protected function quote_rules( $rules ) {
return array_map(
function ( $rule ) {
// Escape regex chars before we enable back the wildcards.
$rule = preg_quote( $rule, self::DELIMITER ); // Note that '/' is the delimiter we're using for the final expression below.
// Enable the wildcard checks.
return str_replace(
array_keys( self::QUOTED_PATTERN_TO_REGEX ),
self::QUOTED_PATTERN_TO_REGEX,
$rule
);
},
$rules
);
}
/**
* Build a regex pattern for any set of rules.
*
* @param array $rules List of regular expression rules to match.
*
* @return string
*/
protected function rules_to_expression( $rules ) {
$rules = array_map(
function ( $rule ) {
return sprintf( '(%s$)', $rule );
},
$this->quote_rules( $rules )
);
return sprintf(
'%s^(%s)%si',
self::DELIMITER,
implode( '|', $rules ),
self::DELIMITER
);
}
/**
* Check if a path matches the regex rules.
*
* @param string $uri Path to check.
* @param array $rules List of URIs to check against.
*
* @return boolean
*/
public function uri_matches_rules( $uri, $rules ) {
if ( ! empty( $rules ) ) {
return (bool) preg_match( $this->rules_to_expression( $rules ), $uri );
}
return false;
}
}