diff --git a/inc/functions.php b/inc/functions.php index 6179babfeff42a399f88114852510367ed34a8ac..a344b5e4b40950433229fc2644e9349e55cbd125 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -12,6 +12,31 @@ namespace Camo_Image_Proxy; * * @return object */ -function Options() { +function Options() { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid, Generic.Functions.OpeningFunctionBraceKernighanRitchie.ContentAfterBrace return Options::instance(); } + +/** + * Can URLs be rewritten to use Camo? + * + * @return bool + */ +function can_rewrite() : bool { + $host = Options()->get( 'host' ); + $key = Options()->get( 'key' ); + + $can_rewrite = true; + + // Validate host. + if ( empty( $host ) || ( ! filter_var( $host, FILTER_VALIDATE_URL ) && ! filter_var( $host, FILTER_VALIDATE_IP ) ) ) { + $can_rewrite = false; + } + + // Validate key. + // TODO: make sure it's an HMAC or something? + if ( empty( $key ) || ! is_string( $key ) ) { + $can_rewrite = false; + } + + return apply_filters( 'camo_image_proxy_can_rewrite', $can_rewrite, $host, $key ); +}