Commit 1b4a8ea2 authored by Erick Hitter's avatar Erick Hitter
Browse files

Basic URL encoding

parent 497ef3bf
......@@ -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
*/
......
<?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;
}
}
......@@ -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 );
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment