Add regex docs to plugin.php page
This commit is contained in:
96
plugin.php
96
plugin.php
@@ -1,26 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
/*
|
/*
|
||||||
Plugin Name: Better Yourls BlackList Domains
|
Plugin Name: Better Yourls BlackList Domains
|
||||||
Plugin URI: https://git.oldgate.org/Sophia/better-yourls-blacklist-domains
|
Plugin URI: https://git.oldgate.org/Sophia/better-yourls-blacklist-domains
|
||||||
Description: Plugin which disallows blacklisted domains and bans the submitter's IP address. GPL v3
|
Description: Plugin which disallows blacklisted domains and bans the submitter's IP address. GPL v3
|
||||||
Version: 0.06
|
Version: 0.06
|
||||||
Author: Sophia Atkinson
|
Author: Sophia Atkinson
|
||||||
Author URI: https://sophia.wtf
|
Author URI: https://sophia.wtf
|
||||||
Original Author: apelly
|
Original Author: apelly
|
||||||
Original Author URI: http://len.io
|
Original Author URI: http://len.io
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// No direct access
|
// No direct access
|
||||||
if( !defined( 'YOURLS_ABSPATH' ) ) die();
|
if( !defined( 'YOURLS_ABSPATH' ) ) die();
|
||||||
|
|
||||||
// Hook the custom function into the 'shunt_add_new_link' event
|
// Hook the custom function into the 'shunt_add_new_link' event
|
||||||
yourls_add_filter( 'shunt_add_new_link', 'better_blacklist_domain_check' );
|
yourls_add_filter( 'shunt_add_new_link', 'better_blacklist_domain_check' );
|
||||||
|
|
||||||
// Hook the admin page into the 'plugins_loaded' event
|
// Hook the admin page into the 'plugins_loaded' event
|
||||||
yourls_add_action( 'plugins_loaded', 'better_blacklist_add_admin_page' );
|
yourls_add_action( 'plugins_loaded', 'better_blacklist_add_admin_page' );
|
||||||
|
|
||||||
// Function to check if a domain is blacklisted
|
// Function to check if a domain is blacklisted
|
||||||
function better_blacklist_domain_check( $shunt, $url ) {
|
function better_blacklist_domain_check( $shunt, $url ) {
|
||||||
// Parse the URL and extract the host
|
// Parse the URL and extract the host
|
||||||
$parsed_url = parse_url( $url );
|
$parsed_url = parse_url( $url );
|
||||||
|
|
||||||
@@ -62,34 +62,34 @@ function better_blacklist_domain_check( $shunt, $url ) {
|
|||||||
|
|
||||||
// No match, allow the URL
|
// No match, allow the URL
|
||||||
return $shunt;
|
return $shunt;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return failure response for blacklisted URLs
|
// Return failure response for blacklisted URLs
|
||||||
function blacklist_fail_response() {
|
function blacklist_fail_response() {
|
||||||
return array(
|
return array(
|
||||||
'status' => 'fail',
|
'status' => 'fail',
|
||||||
'code' => 'error:url',
|
'code' => 'error:url',
|
||||||
'message' => 'This domain is blacklisted',
|
'message' => 'This domain is blacklisted',
|
||||||
'errorCode' => '403',
|
'errorCode' => '403',
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add admin page to handle blacklist management
|
// Add admin page to handle blacklist management
|
||||||
function better_blacklist_add_admin_page() {
|
function better_blacklist_add_admin_page() {
|
||||||
yourls_register_plugin_page( 'better_blacklist_domain', 'Blacklist Domains', 'better_blacklist_admin_page' );
|
yourls_register_plugin_page( 'better_blacklist_domain', 'Blacklist Domains', 'better_blacklist_admin_page' );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display the blacklist admin page
|
// Display the blacklist admin page
|
||||||
function better_blacklist_admin_page() {
|
function better_blacklist_admin_page() {
|
||||||
if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'blacklist_domain' ) {
|
if ( $_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'blacklist_domain' ) {
|
||||||
better_blacklist_process_form();
|
better_blacklist_process_form();
|
||||||
} else {
|
} else {
|
||||||
better_blacklist_display_form();
|
better_blacklist_display_form();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Display the form to update the blacklist
|
// Display the form to update the blacklist
|
||||||
function better_blacklist_display_form() {
|
function better_blacklist_display_form() {
|
||||||
$nonce = yourls_create_nonce( 'blacklist_domain' );
|
$nonce = yourls_create_nonce( 'blacklist_domain' );
|
||||||
$blacklist_domains = yourls_get_option( 'better_blacklist_domain_list', 'Enter domain addresses here, one per line' );
|
$blacklist_domains = yourls_get_option( 'better_blacklist_domain_list', 'Enter domain addresses here, one per line' );
|
||||||
|
|
||||||
@@ -103,14 +103,36 @@ function better_blacklist_display_form() {
|
|||||||
<input type="hidden" name="action" value="blacklist_domain" />
|
<input type="hidden" name="action" value="blacklist_domain" />
|
||||||
<input type="hidden" name="nonce" value="$nonce" />
|
<input type="hidden" name="nonce" value="$nonce" />
|
||||||
<p>Enter domains to blacklist (one per line):</p>
|
<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>
|
<p><input type="submit" value="Save" /></p>
|
||||||
</form>
|
</form>
|
||||||
HTML;
|
HTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the blacklist form submission
|
// Process the blacklist form submission
|
||||||
function better_blacklist_process_form() {
|
function better_blacklist_process_form() {
|
||||||
// Verify nonce for security
|
// Verify nonce for security
|
||||||
yourls_verify_nonce( 'blacklist_domain' );
|
yourls_verify_nonce( 'blacklist_domain' );
|
||||||
|
|
||||||
@@ -133,4 +155,4 @@ function better_blacklist_process_form() {
|
|||||||
}
|
}
|
||||||
echo "</ul>";
|
echo "</ul>";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user