Skip to content

Commit

Permalink
Merge pull request #3 from Ed-ITSolutions/error-images
Browse files Browse the repository at this point in the history
Return image for errors
  • Loading branch information
Arcath authored Jun 25, 2018
2 parents b8337db + 96e297c commit 4b39853
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ The image url will be encrypted with the sites nonce and then base64 encoded. Wh

This ensures only your site can be used to generate valid WP-Camo urls.

### Error Handling

If WP-Camo encounters an error (404 on the image or bad nonce) it will return an image with the error in, which should prevent layouts from being broken by errors.
35 changes: 34 additions & 1 deletion lib/class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,45 @@ public function returnResource(){
}

$hash = $wp_query->get('wp_camo');
$url = openssl_decrypt(base64_decode($hash), 'aes128', NONCE_KEY, 0, substr(NONCE_SALT, 0, 16));

$decoded = base64_decode($hash);

$url = openssl_decrypt($decoded, 'aes128', NONCE_KEY, 0, substr(NONCE_SALT, 0, 16));

if($url === false){
$this->errorImage(
'403',
__('URL not encrypted by this site.', 'wp-camo')
);
return;
}

$file = wp_remote_get($url);


if($file->errors > 0){
$this->errorImage(
'404',
__('Could not find Image.', 'wp-camo')
);
return;
}

header('Content-Type:' . $file['headers']['content-type']);

echo($file['body']);
}

public function errorImage($error, $message){
header("Content-type: image/png");
$image = imagecreate(300, 100);
$background = imagecolorallocate($image, 255, 255, 255);
$textColor = imagecolorallocate($image, 0, 0, 0);

imagestring($image, 4, 30, 25, 'Error ' . $error, $textColor);
imagestring($image, 4, 30, 65, $message, $textColor);


imagepng($image);
}
}
2 changes: 1 addition & 1 deletion wp-camo.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/**
* Plugin Name: WP Camo
* Description: Proxy URLS through your WordPress site to prevent mixed content warnings and
* Version: 0.0.3
* Version: 0.0.4
* Author: Ed-IT Solutions
* Author URI: http://www.ed-itsolutions.com
* Details URI: https://github.com/Ed-ITSolutions/wp-camo/releases
Expand Down

0 comments on commit 4b39853

Please sign in to comment.