I have a Google Chrome extension for my image host and it send an URL to my website. That URL gets encoded via Javascript's escape
method.
An URL encoded by escape
looks like this:
http%253A//4.bp.blogspot.com/-xa4Krfq2V6g/UF2K5XYv3kI/AAAAAAAAAJg/8wrqZQP9ru8/s1600/LuffyTimeSkip.png
I need to get the URL back to normal via PHP somehow, so I can check it against filter_var($the_url, FILTER_VALIDATE_URL)
and it obviously fails if the URL is like above.
This is how the Javascript looks like:
function upload(img) {
var baseurl="http://imgit.org/remote?sent-urls=1&remote-urls=" + escape(img.srcUrl);
uploadImage(baseurl);
}
function uploadImage(imgurl) {
chrome.tabs.create({
url: imgurl,
selected: true
});
}
var title = "Upload this image to IMGit";
var id = chrome.contextMenus.create({"title": title, "contexts": ['image'], "onclick": upload});
And this is what I do in PHP:
if (!filter_var($file, FILTER_VALIDATE_URL) || !filter_var(urldecode($file), FILTER_VALIDATE_URL))
{
throw_error('The entered URLs do not have a valid URL format.', 'index.php#remote'); break;
}
Frankly, urldecode()
doesn't do the job for me. And as you can notice, I am receiving the URL via $_GET
.
What would be the best way to handle this situation?
Actual question: How do I unescape the escaped URL in PHP? Is there a better way to handle this problem?