diff --git a/camo-image-proxy.php b/camo-image-proxy.php index f431994b30eb847c9e06ae4a0002a376ff7dc44d..5bfce911edf4230042ec967543caf8b4995d9386 100755 --- a/camo-image-proxy.php +++ b/camo-image-proxy.php @@ -31,6 +31,21 @@ require_once PLUGIN_PATH . '/inc/class-options.php'; */ require_once PLUGIN_PATH . '/inc/class-options-page.php'; +/** + * URL Building + */ +require_once PLUGIN_PATH . '/inc/class-urls.php'; + +/** + * Rewrite WordPress-generated URLs + */ +require_once PLUGIN_PATH . '/inc/class-rewrite-urls.php'; + +/** + * Rewrite URLs in post content + */ +require_once PLUGIN_PATH . '/inc/class-rewrite-content.php'; + /** * Assorted functions */ diff --git a/inc/class-rewrite-content.php b/inc/class-rewrite-content.php new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/inc/class-rewrite-urls.php b/inc/class-rewrite-urls.php new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git a/inc/class-urls.php b/inc/class-urls.php new file mode 100644 index 0000000000000000000000000000000000000000..5c787daff8ce1fbf7040b798ae2f7b36affa0c63 --- /dev/null +++ b/inc/class-urls.php @@ -0,0 +1,72 @@ +<?php +/** + * URL Building + * + * @package Camo_Image_Proxy + */ + +namespace Camo_Image_Proxy; + +/** + * Class URL + */ +class URL { + use Singleton; + + /** + * Can URLs be rewritten to use Camo? + * + * @return bool + */ + public function can_rewrite() : bool { + $host = Options::instance()->get( 'host' ); + $key = Options::instance()->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 ); + } + + /** + * Encode image URL + * + * @param string $url Image URL to encode. + * @return string|bool + */ + public function encode( string $url ) : string { + if ( ! $this->can_rewrite() ) { + return false; + } + + // TODO: validate $url. + + $key = hash_hmac( 'sha1', $url, Options::instance()->get( 'key' ) ); + $url_encoded = bin2hex( $url ); + + $url_encoded = sprintf( '%1$s/%2$s/%3$s', Options::instance()->get( 'host' ), $key, $url_encoded ); + $url_encoded = set_url_scheme( $url_encoded, 'https' ); + + return $url_encoded; + } + + /** + * Decode encoded URL + * + * @param string $url Camo URL to decode. + * @return string|bool + */ + public function decode( string $url ) : string { + return false; + } +} diff --git a/inc/functions.php b/inc/functions.php index a344b5e4b40950433229fc2644e9349e55cbd125..9fce3b24cf1505bbec4cf71f004f19bcb5f9fbc2 100644 --- a/inc/functions.php +++ b/inc/functions.php @@ -6,37 +6,3 @@ */ namespace Camo_Image_Proxy; - -/** - * Access plugin options - * - * @return object - */ -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 ); -}