You are not logged in.
- Topics: Active | Unanswered
Pages: 1
#1 2009-04-10 7:22 pm
- MadnessRed
- Member
- Registered: 2009-04-10
- Posts: 6
Basic PHP file to read the API
Hi, I saw this site and the API and though i would write a script to use it.
I have tried to comment is well as I can. Hopefully people can make use of this.
<?php
function checkspammers($input,$inputtype='ip'){
//Load the file, and implode the array.
$xml = implode('',file("http://www.stopforumspam.com/api?".$inputtype."=".$input));
//Start new xml parser.
$p = xml_parser_create();
//Get the xml into an array
xml_parse_into_struct($p, $xml, $vals, $index);
//Free some memory by clearing the xml parser
xml_parser_free($p);
//We don't need $index or the $xml any more
unset($index,$xml);
//Prepare the return array
$return = array();
//Now we are going to make the aray useable
foreach($vals as $array){
//If it's the opening array we can do it slightly differnetly
if($array['type'] == 'open'){
//Just get weather it was sucess or not.
$return[$array['tag']] = $array['attributes']['SUCCESS'];
}elseif($array['type'] == 'complete'){
//Else just get the value
$return[$array['tag']] = $array['value'];
}
}
//Save a bit mroe memory by clearing the vals array
unset($vals);
//Now make time into a unix timestamp
if($return['LASTSEEN']){
//Sepparate the timestamp into the time and the date
$time = explode(' ',$return['LASTSEEN']);
//Sepparate the date
$date = explode("-",$time[0]);
//Sepparate the time
$time = explode("-",$time[0]);
//Now make the time, note we times by 1 to remove leading zeros, if we don't then php cansometimes use teh octal system instead of decimal.
$return['UNIXLASTSEEN'] = gmmktime($time[0]*1,$time[1]*1,$time[2]*1,$date[1]*1,$date[2]*1,$date[0]*1);
}
//RESPONCE would be better as booleen, not a string
if($return['RESPONCE'] == 'true'){ $return['RESPONCE'] = true; }else{ $return['RESPONCE'] = false; }
//Now return our array.
return $return;
}
//Example function to check in ip
function isaspammer($ip){
//Get the xml results as an array from teh function above
$result = checkspammers($ip);
//Is he reported?
if($result['FREQUENCY'] > 0){
//He is a spammer
return true;
}else{
//He is not reported as a spammer
return false;
}
}
//Demo of example function
//Is he a spammer
if(isaspammer($_GET['ip'])){
//Yes
echo $_GET['ip'].' is a spammer.';
}else{
//No
echo $_GET['ip'].' is ok.';
}
?>
Edit: At a quick glance at the rest of the forum some people want to know how to only get recently reported people.
$result = checkspammers($ip);
if($result['FREQUENCY'] > 0 && (time() - $result['UNIXLASTSEEN']) < (3600 * 24 * 365)){
//We have a spammer active in the last 365 days
}
Edit2: I have an smf forum, this is what I did to implement this checker at registration.
./sources/Register.php - Line 142
Add $_SERVER to the global.
Just below that add the following code
// Is he a reported spammer?
if(isaspammer($_SERVER['REMOTE_ADDR'])){
die("You are a reported spammer, please contact an admin regarding registration");
}
And at the top of that file, around line 43, just before the Register() function starts, add the isaspammer() and checkspammers() functions listed above. That should stop anyone listed as a spammer from registering
Last edited by MadnessRed (2009-04-10 7:48 pm)
Offline
#2 2009-04-11 12:34 am
- pedigree
- uıɐbɐ ʎɐqǝ ɯoɹɟ pɹɐoqʎǝʞ ɐ buıʎnq ɹǝʌǝu ɯ,ı
- From: New Zealand
- Registered: 2008-04-16
- Posts: 7,095
Re: Basic PHP file to read the API
Might want to chcek your php version before hand, thats php5
Offline
#3 2009-04-11 12:48 pm
- MadnessRed
- Member
- Registered: 2009-04-10
- Posts: 6
Re: Basic PHP file to read the API
Might want to chcek your php version before hand, thats php5
yh, sorry I didn't check what version of php the functions were for. I'm guessing that most people have version 5 though? The only person I know how doesn't has the beta of version 6.
Offline
#4 2009-04-11 3:02 pm
- MysteryFCM
- Member
- From: Tyneside, UK
- Registered: 2008-01-16
- Posts: 606
- Website
Re: Basic PHP file to read the API
There are actually still thousands of people still using PHP4, either through choice, or because their hosting co doesn't want to upgrade for whatever reason.
Regards
Steven Burn
I.T. Mate / hpHosts
it-mate.co.uk / hosts-file.net
Offline
#5 2009-04-12 11:09 am
- MadnessRed
- Member
- Registered: 2009-04-10
- Posts: 6
Re: Basic PHP file to read the API
There are actually still thousands of people still using PHP4, either through choice, or because their hosting co doesn't want to upgrade for whatever reason.
ok, probably a stupid question but why not upgrade? Are there advantages to php4?
Offline
#6 2009-04-12 3:00 pm
- AsciiD
- Member
- Registered: 2008-12-07
- Posts: 49
Re: Basic PHP file to read the API
Many people regard php5 to be badly broken and a waste of time.
Offline
#7 2009-04-12 3:25 pm
- pedigree
- uıɐbɐ ʎɐqǝ ɯoɹɟ pɹɐoqʎǝʞ ɐ buıʎnq ɹǝʌǝu ɯ,ı
- From: New Zealand
- Registered: 2008-04-16
- Posts: 7,095
Re: Basic PHP file to read the API
There are so many hosting companies out there that would break a serious amount of their customers sites if they upgraded to php5. So many in fact that I doubt any of them would do an in place upgrade
Offline
#8 2009-04-14 2:47 am
- zaphod
- Jägermonster
- From: USA
- Registered: 2008-11-22
- Posts: 2,985
- Website
Re: Basic PHP file to read the API
What they need to do is make PHP5 a concurrent PHP version with 4 at it's core, and simply use <?php5 ?> as a preamble to alert the interpereter that the code is PHP5. This way nothing breaks, new functions are still available.
Zap
Get Protected, Stay Protected...
With ZB Block, GNU/GPL Freeware Anti-Spam/Anti-Hack protection for your php based website.
Little boxes in the server farm, little boxes running php...
Offline
#9 2011-04-01 10:11 am
- Knut
- Member
- Registered: 2009-06-18
- Posts: 22
- Website
Re: Basic PHP file to read the API
I know this is an old thread, but this is a small and useful script and I would like to thank MadnessRed for sharing it. I am planning to use it with lastseen and frequency.
Is there any way to rewrite this script to do ip and email lookups (with only one call to SFS)?
Offline
#10 2011-04-04 7:14 pm
- MadnessRed
- Member
- Registered: 2009-04-10
- Posts: 6
Re: Basic PHP file to read the API
Hi, yes It should be possible, the api does support multiple inputs. I'll edit the code.
Offline
#11 2011-04-04 7:58 pm
- Erik
- Member
- From: Belgium
- Registered: 2008-09-07
- Posts: 187
Re: Basic PHP file to read the API
hi,
Well i think this script needs
$input = htmlspecialchars($input);
$inputtype = htmlspecialchars($inputtype);
to close the door to possible XSS holes
Last edited by Erik (2011-04-04 7:59 pm)
Offline
#12 2011-04-04 8:05 pm
- pedigree
- uıɐbɐ ʎɐqǝ ɯoɹɟ pɹɐoqʎǝʞ ɐ buıʎnq ɹǝʌǝu ɯ,ı
- From: New Zealand
- Registered: 2008-04-16
- Posts: 7,095
Re: Basic PHP file to read the API
doesnt completely close the doors, for that people should really look at decoding utf8 and using htmlpurifier
Offline
#13 2011-04-04 8:24 pm
- MadnessRed
- Member
- Registered: 2009-04-10
- Posts: 6
Re: Basic PHP file to read the API
Hi, this should work:
<?php
header("Content-type: text/plain");
function checkspammers($input){
//Create a get style array to supply data
$get = array();
//Loop through the inputs
foreach($input as $type => $name){
//If we have a list of names...
if(is_array($name)){
//...loop through them...
foreach($name as $n){
//...and supply them as a GET array
$get[] = urlencode($type) . "[]=" . urlencode($n);
}
}
//Otherwise...
else{
//...just add them.
$get[] = urlencode($type) . "=" . urlencode($name);
}
}
//Load the file, and implode the array.
$xml = implode('',file("http://www.stopforumspam.com/api?".implode("&",$get)));
//Start new xml parser.
$p = xml_parser_create();
//Get the xml into an array
xml_parse_into_struct($p, $xml, $vals, $index);
//Free some memory by clearing the xml parser
xml_parser_free($p);
//We don't need $index or the $xml any more
unset($index,$xml);
//Prepare the return array
$return = array('success' => false, 'all' => array("appears" => false, "frequency" => 0, "lastseen" => 0));
//Keep track of the current type
$type = '';
//ok, formate the results
foreach($vals as $array){
//Different actions for different tags
switch(strtolower($array['tag'])){
//Opening tag
case "response":
if($array["type"] == "open"){
$return['success'] = $array['attributes']['SUCCESS'];
}
break;
//Changing type
case "type":
$type = $array['value'];
$return[$type] = array("appears" => false, "frequency" => 0, "lastseen" => 0);
break;
//Appears
case "appears":
$return[$type]["appears"] = (strtolower($array['value']) == "yes" ? true : false);
$return["all"]["appears"] = ($return[$type]["appears"] || $return["all"]["appears"]);
break;
//Frequency
case "frequency":
$return[$type]["frequency"] = $array['value']*1;
$return["all"]["frequency"] += $return[$type]["frequency"];
break;
//Last seen
case "lastseen":
$return[$type]["lastseen"] = tounix($array['value']);
$return["all"]["lastseen"] = max($return["all"]["lastseen"], $return[$type]["lastseen"]);
}
}
//Save a bit more memory by clearing the vals array
unset($vals);
//RESPONCE would be better as booleen, not a string
$return['success'] = ($return['success'] == 'true');
//Now return our array.
return $return;
}
//Function to convert a time to unix
function tounix($time){
//Sepparate the timestamp into the time and the date
$time = explode(' ',$time);
//Sepparate the date
$date = explode("-",$time[0]);
//Sepparate the time
$time = explode(":",$time[1]);
//Now make the time, note we times by 1 to remove leading zeros, if we don't then php cansometimes use teh octal system instead of decimal.
return gmmktime($time[0]*1,$time[1]*1,$time[2]*1,$date[1]*1,$date[2]*1,$date[0]*1);
}
//Example function to check in ip
function isaspammer($ip){
//Get the xml results as an array from teh function above
$result = checkspammers(array('ip' => $ip));
//Is he reported?
if($result['all']['frequency'] > 0){
//He is a spammer
return true;
}else{
//He is not reported as a spammer
return false;
}
}
//Demo of example function
print_r(checkspammers(array('username' => 'Gamserkuserse', 'email' => 'villarrealraymond8@gmail.com', 'ip' => '95.64.11.46')));
?>
Offline
#14 2011-04-04 8:44 pm
- Katana
- Member
- Registered: 2009-08-18
- Posts: 1,886
Re: Basic PHP file to read the API
//Function to convert a time to unix function tounix($time){ //Sepparate the timestamp into the time and the date $time = explode(' ',$time); //Sepparate the date $date = explode("-",$time[0]); //Sepparate the time $time = explode(":",$time[1]); //Now make the time, note we times by 1 to remove leading zeros, if we don't then php cansometimes use teh octal system instead of decimal. return gmmktime($time[0]*1,$time[1]*1,$time[2]*1,$date[1]*1,$date[2]*1,$date[0]*1); }
Check this out, it'll be more efficient: http://us3.php.net/manual/en/book.datetime.php
Requires PHP 5.2.0+
うるさいうるさいうるさい!
Offline
#15 2011-04-04 8:47 pm
- Knut
- Member
- Registered: 2009-06-18
- Posts: 22
- Website
Re: Basic PHP file to read the API
Thank you very much for sharing your knowledge MadnessRed, I really appreciate it
Not everyone is a php experts so this is a really useful script. Maybe one of the admins could put a link to this post from the SFS "mods & plugins" page?
Last edited by Knut (2011-04-04 8:50 pm)
Offline
#16 2011-04-04 11:08 pm
- MadnessRed
- Member
- Registered: 2009-04-10
- Posts: 6
Re: Basic PHP file to read the API
MadnessRed wrote://Function to convert a time to unix function tounix($time){ //Sepparate the timestamp into the time and the date $time = explode(' ',$time); //Sepparate the date $date = explode("-",$time[0]); //Sepparate the time $time = explode(":",$time[1]); //Now make the time, note we times by 1 to remove leading zeros, if we don't then php cansometimes use teh octal system instead of decimal. return gmmktime($time[0]*1,$time[1]*1,$time[2]*1,$date[1]*1,$date[2]*1,$date[0]*1); }
Check this out, it'll be more efficient: http://us3.php.net/manual/en/book.datetime.php
Requires PHP 5.2.0+
ok, many thanks:
function checkspammers($input){
//Create a get style array to supply data
$get = array();
//Loop through the inputs
foreach($input as $type => $name){
//If we have a list of names...
if(is_array($name)){
//...loop through them...
foreach($name as $n){
//...and supply them as a GET array
$get[] = urlencode($type) . "[]=" . urlencode($n);
}
}
//Otherwise...
else{
//...just add them.
$get[] = urlencode($type) . "=" . urlencode($name);
}
}
//Load the file, and implode the array.
$xml = implode('',file("http://www.stopforumspam.com/api?".implode("&",$get)));
//Start new xml parser.
$p = xml_parser_create();
//Get the xml into an array
xml_parse_into_struct($p, $xml, $vals, $index);
//Free some memory by clearing the xml parser
xml_parser_free($p);
//We don't need $index or the $xml any more
unset($index,$xml);
//Prepare the return array
$return = array('success' => false, 'all' => array("appears" => false, "frequency" => 0, "lastseen" => 0));
//Keep track of the current type
$type = '';
//ok, formate the results
foreach($vals as $array){
//Different actions for different tags
switch(strtolower($array['tag'])){
//Opening tag
case "response":
if($array["type"] == "open"){
$return['success'] = $array['attributes']['SUCCESS'];
}
break;
//Changing type
case "type":
$type = $array['value'];
$return[$type] = array("appears" => false, "frequency" => 0, "lastseen" => 0);
break;
//Appears
case "appears":
$return[$type]["appears"] = (strtolower($array['value']) == "yes" ? true : false);
$return["all"]["appears"] = ($return[$type]["appears"] || $return["all"]["appears"]);
break;
//Frequency
case "frequency":
$return[$type]["frequency"] = $array['value']*1;
$return["all"]["frequency"] += $return[$type]["frequency"];
break;
//Last seen
case "lastseen":
$return[$type]["lastseen"] = date_format(date_create_from_format("Y-m-d H:i:s",$array['value']),"U");
$return["all"]["lastseen"] = max($return["all"]["lastseen"], $return[$type]["lastseen"]);
}
}
//Save a bit more memory by clearing the vals array
unset($vals);
//RESPONCE would be better as booleen, not a string
$return['success'] = ($return['success'] == 'true');
//Now return our array.
return $return;
}
Thank you very much for sharing your knowledge MadnessRed, I really appreciate it
Not everyone is a php experts so this is a really useful script. Maybe one of the admins could put a link to this post from the SFS "mods & plugins" page?
No problem, glad my script is being found useful.
Offline
#17 2011-04-06 1:03 am
- gibsonm
- Member
- Registered: 2011-04-06
- Posts: 2
Re: Basic PHP file to read the API
Here's some other PHP code - I wrote this before I saw yours here. But since I finished it and it seems to work I thought I would chuck it up here as well since it doesn't hurt to have more examples...
// Form an url to stopforumspam.com, set the flag for JSON format,
// and add parameters for username= , ip= , and email=
$url = "http://www.stopforumspam.com/api?f=json";
$url .= "&username=" . urlencode($logon);
$ip = $_SERVER['REMOTE_ADDR'];
if ( $ip != "127.0.0.1" )
$url .= "&ip=" . $ip;
if ( isset($email) && $email != "none" )
$url .= "&email=" . urlencode($email);
$data = file_get_contents( $url );
$result = json_decode( $data, true );
// The result will be an array structured like this:
//
//[
// 'success' => 1
// 'username' => [ 'frequency' = x, 'appears' = 0 ]
// 'email' => [ 'frequency' = x, 'appears' = 0 ]
// 'ip' => [ 'frequency' = x, 'appears' = 0 ]
//]
//
// the 'appears' field will be set to 1 or true if it appears in the spam database.
// First check for overall success of the query.
if ( is_array($result) && isset($result['success']) && $result['success'] )
{
// Now see if username, email, or ip appears in the database.
function appears( $val, $result )
{
if ( isset( $result[$val] ) && is_array( $result[$val] ) && isset( $result[$val]['appears'] ) )
return $result[$val]['appears'];
return false;
}
if ( appears( 'username', $result )
|| appears( 'email', $result )
|| appears( 'ip', $result ) )
{
// If we get to here, the username, email, or ip has been recorded as being
// a known spammer in the stopforumspam database.
// So exit or whatever you want - don't allow the registration to proceed.
Fill in stuff here...
}
}
Offline
#18 2011-04-06 2:04 am
- Katana
- Member
- Registered: 2009-08-18
- Posts: 1,886
Re: Basic PHP file to read the API
You might want to add some basic error checking to that, to check if the JSON is malformed (in case of, say, packet loss or connection interruption) or if no data was returned by file_get_contents().
e.g. after file_get_contents and json_decode()
if($data !== NULL && $result === NULL)
{
// error handling goeth here, invalid json detected
}
As an alternative though, there's this:
https://github.com/damianb/OpenFlame-Fr … y/JSON.php
Requires PHP 5.3.0+, but it provides full error handling for JSON strings. Just pass it the string from file_get_contents and wrap in a try/catch block (the json decode uses the php-internal class RuntimeException).
It'll also tell you the reason the JSON decode failed in the exception message.
うるさいうるさいうるさい!
Offline
#19 2011-04-06 7:38 am
- gibsonm
- Member
- Registered: 2011-04-06
- Posts: 2
Re: Basic PHP file to read the API
If json_decode() fails, it won't return an array value, right?
So the test for is_array() shouldn't pass if json_decode() failed... It will only look at the result if json_decode() returned an array value, and that array has a 'success' value set in it.
But certainly more error handling code wouldn't hurt!
Just caught my first spam registration with this, I am thrilled!
Offline
#20 2011-04-06 9:36 am
- pedigree
- uıɐbɐ ʎɐqǝ ɯoɹɟ pɹɐoqʎǝʞ ɐ buıʎnq ɹǝʌǝu ɯ,ı
- From: New Zealand
- Registered: 2008-04-16
- Posts: 7,095
Re: Basic PHP file to read the API
<?php
// Encode the data.
$json = json_encode(
array(
1 => array(
'English' => array(
'One',
'January'
),
'French' => array(
'Une',
'Janvier'
)
)
)
);
// Define the errors.
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
// Show the errors for different depths.
foreach(range(4, 3, -1) as $depth) {
var_dump(json_decode($json, True, $depth));
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
?>
Offline
#21 2011-04-06 10:26 am
- Knut
- Member
- Registered: 2009-06-18
- Posts: 22
- Website
Re: Basic PHP file to read the API
http://github.com/damianb/SFSIntegration
StopForumSpam Integration Library for PHP 5.3.0+
Obsidian, you have made a whole PHP library for SFS.
How can this be used on a generic site to check email and ip against the SFS database?
Maybe you can post a "quick start manual" here (or include it in the library), so that it is more easy for new beginners to PHP to use your library?
Offline
#22 2011-04-06 2:20 pm
- Katana
- Member
- Registered: 2009-08-18
- Posts: 1,886
Re: Basic PHP file to read the API
Obsidian wrote:http://github.com/damianb/SFSIntegration
StopForumSpam Integration Library for PHP 5.3.0+Obsidian, you have made a whole PHP library for SFS.
How can this be used on a generic site to check email and ip against the SFS database?
Maybe you can post a "quick start manual" here (or include it in the library), so that it is more easy for new beginners to PHP to use your library?
I can't, at the moment. I'm writing the library for PHP 5.3+ at the moment, most of the codebase has been shredded to pieces.
The previous version in development, 0.3.0-dev is here though: https://github.com/damianb/SFSIntegration/tree/master
There's an example of it here: https://github.com/damianb/SFSIntegrati … xample.php
But, again, I'm rewriting it to make it more efficient and a bit easier to work with. It'll be a while.
うるさいうるさいうるさい!
Offline
#23 2011-04-07 5:16 pm
- Knut
- Member
- Registered: 2009-06-18
- Posts: 22
- Website
Re: Basic PHP file to read the API
Thanks for the information, links and example, Obsidian.
Hope that you will announce it here in the forum when then new version of your script is ready.
Offline
#24 2011-04-07 5:17 pm
- Katana
- Member
- Registered: 2009-08-18
- Posts: 1,886
Re: Basic PHP file to read the API
Thanks for the information, links and example, Obsidian.
Hope that you will announce it here in the forum when then new version of your script is ready.
うるさいうるさいうるさい!
Offline
Pages: 1