I was experimenting with new PHP code when I came across a function that allowed me to neatly check whether the page you are currently viewing contains unparsed antidote code.
For those who are unaware of the whole thing, the Scratch Team has taken down images a while ago, so a user-contributed plugin called Antidote was introduced (developed by rookwood101 with some contribution from my part) to counter the loss of images.
However, only users who have antidote installed are able to see any images posted on the forums. Those without the plugin will see ugly [img] tags in text. Therefore I have come up with code to check the page for antidote code, and appropriately recommend the user to install Antidote to view the images and videos.
I would suggest that anyone who uses the img tags often should put this simple code into your signature, so that people are aware that they need to download antidote to view your posts. The beauty of this is that if the page contains no images, it displays a smaller and more peaceful reminder to download antidote anyway
Use this code in your signatures:
[url=http://scratch.mit.edu/forums/viewtopic.php?id=85229][img]http://modshare.tk/misc/antidote.php?antidoteexists=false[/img][/url]
Furthermore, if you wish to have a bigger image in your signature, please use the parameter "big" in the URL. For example, add &big=both at the end.
For those of you who have antidote you can see what the result is:
Of course, depending on the page, the image will vary. Have fun using it!
Last edited by LS97 (2012-03-27 12:40:38)
Offline
Can you open-source this? It looks interesting.
Offline
bobbybee wrote:
Can you open-source this? It looks interesting.
Sure! I'll do it tomorrow when I'm on my other computer.
Meanwhile, if it helps, I mainly used the REFERER superglobal to check out the page the user was on, and then simply looked for traces of unparsed BBCode
It's relatively short code.
Offline
I see...
Wait, how would it detect anti-dote if they're are no images?
Offline
bobbybee wrote:
I see...
Wait, how would it detect anti-dote if they're are no images?
It doesn't detect antidote, it detects whether the page needs antidote or not. To detect the actual antidote I would have to use JS too (which I cannot, from a PHP image).
Offline
LS97 wrote:
bobbybee wrote:
I see...
Wait, how would it detect anti-dote if they're are no images?It doesn't detect antidote, it detects whether the page needs antidote or not. To detect the actual antidote I would have to use JS too (which I cannot, from a PHP image).
Makes sense, I suppose.
Offline
That is pretty neat! I would love to have a look at the code for it and I think this is a pretty neat usage of dynamic signatures! I might add it to my API thread as a link at some point
How does antidote change page HTML? Perhaps antidote could be detected by checking for [img ] tags? If there are no [img ] tags it would mean either there are no images, or antidote is installed and has replaced all the tags with <img> tags?
Offline
sparks wrote:
That is pretty neat! I would love to have a look at the code for it and I think this is a pretty neat usage of dynamic signatures! I might add it to my API thread as a link at some point
![]()
How does antidote change page HTML? Perhaps antidote could be detected by checking for [img ] tags? If there are no [img ] tags it would mean either there are no images, or antidote is installed and has replaced all the tags with <img> tags?
Maybe it could load the page and detect [img] tags. If there is, check if there is [img] tags in the page. That way it would actually know.
Offline
sparks,
The problem/aspect I came across during the testing is that the image, being a PHP one, cannot check the contents of the page the user is on. That would mean a big security hole for any browser! Therefore I had to get the page server-side only to check if the original contained BBCode -- not whether it had been parsed.
Javascript is essential to check the user's side, and unfortunately JS cannot be injected through images. I had been thinking about linking to a page that contained img tags, so that the user could check whether their browser effectively supported Antidote, but of course antidote only parses the code if it's on the scratch forums.
I hope that makes sense, because it doesn't to me
Offline
As promised yesterday, here's the source code!
<?php
// Check if page contains antidote
$ref = $_SERVER['HTTP_REFERER'];
if(strstr($ref, 'scratch.mit.edu/forums/viewtopic.php')) {
// Check for unconverted [img], youtube, and project tags
$page = file_get_contents($ref);
if(strstr($page, '[img]') || strstr($page, '[youtube]') || strstr($page, '[scratch=flash]')) {
// There is unparsed antidote on the page!
$filename = '2';
} else {
// Either the page has no antidote or the code has been parsed already
$filename = '3';
}
} else {
// View this image on a scratch site!
$filename = '1';
}
// start making image
$img = imagecreatefrompng('anti' . $filename . '.png');
//alert browser about content
header('Content-type: image/png');
//create image
imagepng($img);
?>The $filename bit just determines which image to display. Then the code later displays the appropriate one!
Offline
Haha, antidote has converted most of that code to images
I'd better quote it to read.
How about making an antidote update so that antidote also checks for img tags on a page on your server? That might then be readable.
That is surprisingly simple to code! It takes brains to work out it can be done though, well done!
Last edited by sparks (2012-03-23 07:58:26)
Offline
?
I'm using Antidote but the image still pops up...
Offline
Hardmath123 wrote:
?
I'm using Antidote but the image still pops up...
Yes, as I explained it the post to sparks, the image can only check whether there is BBCode, not whether the code has been parsed or not!
Offline
sparks wrote:
Haha, antidote has converted most of that code to images
I'd better quote it to read.
How about making an antidote update so that antidote also checks for img tags on a page on your server? That might then be readable.
That is surprisingly simple to code! It takes brains to work out it can be done though, well done!
That's weird -- I have antidote installed, but the code looks fine to me. Do you have the latest fixed version? I tried talking to rookwood about this, but his Skype habits aren't exactly top-etiquette...
I tried to avoid antidote updating since so many people will not have the latest version. Of course, that could be seen as a plus, because those with older versions would still be prompted to install! Additionally, the new version could automatically remove my "missing plugin" images. Unfortunately I will have to talk to rookwood about that.
And yes, this is my favourite kind of coding. Not too difficult code-wise, easy to debug, but that requires thinking!
Offline
Ah, you see I didn't know that there was an antidote update! I shall go download it
What you need is a way to detect the version
Does the firefox addon system allow for AJAX? You could use that to send the version number to a php file that checks to see if a newer version is avaliable and display a message with an update link?
By the way, if you're more of a PHPer like me, you might like this little function I built for Javascript to simulate PHP's file_get_contents():
function file_get_contents(url){
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}
else if(window.ActiveXObject){
request = new ActiveXObject("Microsoft.XMLHTTP");
}
request.open("GET", url, false);
request.send(null);
return request.responseText;
}It just lets me use AJAX in a method I'm more familiar with
EDIT: Updated, that's better
Last edited by sparks (2012-03-23 08:38:39)
Offline
sparks wrote:
Ah, you see I didn't know that there was an antidote update! I shall go download it
![]()
What you need is a way to detect the versionDoes the firefox addon system allow for AJAX? You could use that to send the version number to a php file that checks to see if a newer version is avaliable and display a message with an update link?
By the way, if you're more of a PHPer like me, you might like this little function I built for Javascript to simulate PHP's file_get_contents():Code:
function file_get_contents(url){ if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } else if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } request.open("GET", url, false); request.send(null); return request.responseText; }It just lets me use AJAX in a method I'm more familiar with
![]()
EDIT: Updated, that's better![]()
Nice function
I do believe Firefox is just about as permissive as Chrome in its addons. It would probably be simpler, however, to actually inject the version into the JS using PHP beforehand, and then the javascript would simply have to run an equality test!
Offline
cool make it say
img uploading...
anidite finding imgs
Offline
sparks wrote:
Ah, you see I didn't know that there was an antidote update! I shall go download it
![]()
What you need is a way to detect the versionDoes the firefox addon system allow for AJAX? You could use that to send the version number to a php file that checks to see if a newer version is avaliable and display a message with an update link?
By the way, if you're more of a PHPer like me, you might like this little function I built for Javascript to simulate PHP's file_get_contents():Code:
function file_get_contents(url){ if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } else if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } request.open("GET", url, false); request.send(null); return request.responseText; }It just lets me use AJAX in a method I'm more familiar with
![]()
EDIT: Updated, that's better![]()
Wouldn't it be better to do:
function file_get_contents(url){
if(window.XMLHttpRequest){
request = new XMLHttpRequest();
}
else if(window.ActiveXObject){
request = new ActiveXObject("Microsoft.XMLHTTP");
}
else {
return false;
}
request.open("GET", url, false);
request.send(null);
return request.responseText;
}That way if neither method exists (I have no idea when that would be true, but hey
) it still works and doesn't error - or at least it errors nicely.
I suppose that through my use of compiled languages as opposed to interpreted languages, I have learned that error handling is of great importance
- otherwise it just crashes.
Last edited by rookwood101 (2012-03-23 18:04:21)
Offline
rookwood101 wrote:
sparks wrote:
Ah, you see I didn't know that there was an antidote update! I shall go download it
![]()
What you need is a way to detect the versionDoes the firefox addon system allow for AJAX? You could use that to send the version number to a php file that checks to see if a newer version is avaliable and display a message with an update link?
By the way, if you're more of a PHPer like me, you might like this little function I built for Javascript to simulate PHP's file_get_contents():Code:
function file_get_contents(url){ if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } else if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } request.open("GET", url, false); request.send(null); return request.responseText; }It just lets me use AJAX in a method I'm more familiar with
![]()
EDIT: Updated, that's better![]()
Wouldn't it be better to do:
Code:
function file_get_contents(url){ if(window.XMLHttpRequest){ request = new XMLHttpRequest(); } else if(window.ActiveXObject){ request = new ActiveXObject("Microsoft.XMLHTTP"); } else { return false; } request.open("GET", url, false); request.send(null); return request.responseText; }That way if neither method exists (I have no idea when that would be true, but hey
) it still works and doesn't error - or at least it errors nicely.
I suppose that through my use of compiled languages as opposed to interpreted languages, I have learned that error handling is of great importance- otherwise it just crashes.
Wait, hold on: this only works with local files, right? (I mean files on the same server) I thought this was a JS safety feature...
Last edited by Hardmath123 (2012-03-24 03:55:18)
Offline
Hardmath123 wrote:
Wait, hold on: this only works with local files, right? (I mean files on the same server) I thought this was a JS safety feature...
![]()
That's right. Unless the other server has expressedly allowed connection from the local server, in which case a connection can be made.
Offline