From 497ef3bfa2cbf9a6df31f68aab7319b7c2df7444 Mon Sep 17 00:00:00 2001 From: Erick Hitter <git-contrib@ethitter.com> Date: Sun, 18 Feb 2018 15:07:41 -0800 Subject: [PATCH] Conditional to confirm we can rewrite --- inc/functions.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/inc/functions.php b/inc/functions.php index 6179bab..a344b5e 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 ); +} -- GitLab