Skip to content

Instantly share code, notes, and snippets.

@Cameron-D
Last active June 16, 2017 00:02
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cameron-D/a685a83196650991875c to your computer and use it in GitHub Desktop.
Save Cameron-D/a685a83196650991875c to your computer and use it in GitHub Desktop.
<?php
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.");
}
$plugins->add_hook('pre_output_page', 'https_output_page');
function https_info() {
return array(
'name' => 'HTTPS Helper',
'description' => 'Provides support for migrating to a HTTPS-enabled domain',
'author' => 'Cameron:D',
'version' => '0.1',
/* Should still work with 1.8, but I haven't tested, I'm still on 1.6 */
'compatibility' => '16*',
);
}
function https_output_page($page) {
// Some image hosts support HTTPS but users still post HTTP URLs, this will change those hosts
// to use HTTPS, removing some (most, in my case) load from the image proxy
$find = '~\<img src="http:\/\/(i\.imgur\.com|[a-z0-9]+\.deviantart\.net|\d+\.media\.tumblr\.com|[a-z0-9\.]+\.edgecastcdn\.net|derpicdn\.net|YOURBOARDURL\.com)~si';
// Simply replaces the HTTP://... with //..... (Protocol-relative URL)
// This could also be $replace = 'src="https://\\1'; to force HTTPS (When this code was writted I supported both HTTP and HTTPS)
$replace = 'src="//\\1';
$page = preg_replace($find, $replace, $page);
//If the users is accessing over HTTPS...
if(isset($_SERVER['HTTPS'])) {
// Find every HTTP image
$find = '~\<img src="(http:\/\/(.+?))"~si';
$page = preg_replace_callback($find, function ($matches) {
global $mybb;
// Encode the image URL
$encoded = bin2hex($matches[1]);
// Sign the URL with the camo secret key (stops people abusing the proxy)
// This requires you to add a new setting through your ACP named 'hmac_key'
// And the key in your camo config needs to be the same
$key = hash_hmac("sha1", $matches[1], $mybb->settings["hmac_key"]);
// Get the original image filename, makes the URLs a bit more user-friendly
// (i.e. if people save the image it has a sane name)
$filename = substr(strrchr($matches[1], "/"), 1);
// Build the new image URL
$newurl = "https://YOURCAMOPROXYADDRESS/{$key}/{$encoded}/{$filename}";
// And output it
return "<img src=\"{$newurl}\"";
}, $page);
}
// This section replace all links to your forum with the protocol-relative link
$find = '~href\="https?:\/\/YOURBOARDURL\.com\~si';
// Get rid of the 's?' above and make it https:// below to just force all old board links to be HTTPS
$replace = 'href="//YOURBOARDURL.com';
$page = preg_replace($find, $replace, $page);
return $page;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment