Compare commits

...

3 Commits

Author SHA1 Message Date
Maingron
7ea141cc23 Update Readme 2025-09-13 02:24:06 +02:00
Maingron
fa220e0470 Add regex docs to plugin.php page 2025-09-13 02:22:31 +02:00
Maingron
bd050d27c4 Add possible regex handling 2025-09-13 02:11:39 +02:00
2 changed files with 142 additions and 112 deletions

View File

@@ -8,7 +8,7 @@ This plugin is intended to be used with YOURLS (cf. <https://yourls.org>)
It has been tested on YOURLS v1.9.2 and YourlsBlacklistIPs v1.3
Current version is 0.04
Current version is 0.07
**INSTALL :**
@@ -41,6 +41,7 @@ Also thanks to [LudoBoggio](https://github.com/LudoBoggio) for the [YourlsBlackl
---------
v0.07 Maingron added regex support
v0.06 Alphabetize the blacklist
v0.05 Fix all links being blocked
v0.04 Fix https and not both https and http blocking

View File

@@ -45,13 +45,20 @@ function better_blacklist_domain_check( $shunt, $url ) {
$blacklisted_domains = unserialize( $blacklisted_domains );
foreach ( $blacklisted_domains as $blacklisted_domain ) {
// Use a regex to match the domain or subdomain
// Check if the entry starts with '/', then consider regex handling
if (strpos($blacklisted_domain, '/') === 0) {
if (@preg_match($blacklisted_domain, $domain)) {
return blacklist_fail_response();
}
} else {
// Otherwise treat as plain domain (old behavior)
$pattern = '/(?:^|\.)' . preg_quote( $blacklisted_domain, '/' ) . '$/i';
if ( preg_match( $pattern, $domain ) ) {
return blacklist_fail_response();
}
}
}
}
// No match, allow the URL
return $shunt;
@@ -96,7 +103,29 @@ function better_blacklist_display_form() {
<input type="hidden" name="action" value="blacklist_domain" />
<input type="hidden" name="nonce" value="$nonce" />
<p>Enter domains to blacklist (one per line):</p>
<textarea class="blacklist-domains" cols="60" rows="15" name="blacklist_form">$blacklist_domains</textarea>
<details>
<summary>Advanced Usage (Regex)</summary>
<div>
<p>
You can use regular expressions (Regex) to define more complex patterns for blacklisting domains.<br>
To use Regex, enter your pattern between slashes (/).<br>
For example, to block all subdomains of example.com, you could enter <code>/\.example\.com$/i</code>.<br>
The <kbd>i</kbd> flag at the end makes the match case-insensitive. Usually you want to use this flag.
</p>
<p>
<b>Further examples:</b><br>
<code>/.*\.xxx$/i</code> - Blocks all domains ending with .xxx<br>
<br>
Be cautious when using Regex, as incorrect patterns can lead to unintended blocking of domains.<br>
Always test your Regex patterns to ensure they work as expected.
</p>
</div>
</details><br>
<textarea class="blacklist-domains" cols="60" rows="15" name="blacklist_form" placeholder="Example: block.example.com">$blacklist_domains</textarea>
<p><input type="submit" value="Save" /></p>
</form>
HTML;