From 1b4a8ea24336deabfed673a75450f94be5b1038c Mon Sep 17 00:00:00 2001
From: Erick Hitter <git-contrib@ethitter.com>
Date: Sun, 18 Feb 2018 15:45:02 -0800
Subject: [PATCH] Basic URL encoding

---
 camo-image-proxy.php          | 15 ++++++++
 inc/class-rewrite-content.php |  0
 inc/class-rewrite-urls.php    |  0
 inc/class-urls.php            | 72 +++++++++++++++++++++++++++++++++++
 inc/functions.php             | 34 -----------------
 5 files changed, 87 insertions(+), 34 deletions(-)
 create mode 100644 inc/class-rewrite-content.php
 create mode 100644 inc/class-rewrite-urls.php
 create mode 100644 inc/class-urls.php

diff --git a/camo-image-proxy.php b/camo-image-proxy.php
index f431994..5bfce91 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 0000000..e69de29
diff --git a/inc/class-rewrite-urls.php b/inc/class-rewrite-urls.php
new file mode 100644
index 0000000..e69de29
diff --git a/inc/class-urls.php b/inc/class-urls.php
new file mode 100644
index 0000000..5c787da
--- /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 a344b5e..9fce3b2 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 );
-}
-- 
GitLab