Basic PHP loop RBL checker array - php

I am trying to create a basic RBL checker in PHP, I can't seem to get the IP to be added on the end of the url with out error. I am not the best with PHP.
list.inc is just a list of IPs to check each IP is on a new line.
<?
$ips = file("list.inc");
foreach($ips as $ip)
{
$rblurl = 'http://rbl-check.org/rbl_api.php?ipaddress=' . $ip;
//$rblcheckurl = $rblurl . $ip;
$boom = fopen($rblurl, "r");
//print "<pre>";
//print_r($boom);
//print "</pre>";
$rbl = stream_get_contents($boom);
echo "</br>";
$data = explode(";",$rbl);
print_r($data);
echo "</br>";
fclose($boom);
}
?>
Any help would be great!

The values returned by file() include the newline at the end of each line, you need to remove them before appending them to the URL:
foreach ($ips as $ip) {
$rblurl = 'http://rbl-check.org/rbl_api.php?ipaddress=' . trim($ip);
...
}

Related

IP Blacklist lookup with PHP

I am trying to count the number of hits an IP occur in the array function "dnsblookup". If the IP occurs three times, it should echo "3|10", where 10 is the total number of blacklist database in the array. My code returns "1|10" for every IP even if it occurs more than once in the blacklist database in the array. I do not know where I made the mistake. I need help to complete this project. Thank you. I put multiple asterisks (*) before the exact code I am having issues with.
<?php
function dnsbllookup($ip)
{
$dnsbl_lookup = array (
"dnsbl-1.uceprotect.net","all.s5h.net","wormrbl.imp.ch",
"dnsbl-2.uceprotect.net","blacklist.woody.ch",
"dnsbl-3.uceprotect.net","combined.abuse.ch","dnsbl.spfbl.net",
"dnsbl.dronebl.org","http.dnsbl.sorbs.net");
$list = "";
if ($ip) {
$reverse_ip = implode(".", array_reverse(explode(".", $ip)));
foreach ($dnsbl_lookup as $host) {
if (checkdnsrr($reverse_ip . "." . $host . ".", "A")) {
$list .= implode(".", array_reverse(explode(".",$reverse_ip))) . ' <font color="red">
<strong>is Listed in </strong></font>'. $host . '<br />';
}
}
}
if (empty($list)) {
echo 'No record was not found in the IP blacklist database for: '.$ip;
echo $list;
echo "<br>";
echo "0";
echo "|";
echo count($dnsbl_lookup);
********************
} else {
$list1 = explode(" ", $list);
echo ($list);
echo "<br>";
echo "Blacklisted: ".count($list1);
echo "|";
echo count($dnsbl_lookup);
}
}
You should reduce the complexity and use string interpolation for easier reading and debugging.
function dnsbllookup($ip)
{
$dnsbl_lookup =
[
'dnsbl-1.uceprotect.net', 'all.s5h.net', 'wormrbl.imp.ch',
'dnsbl-2.uceprotect.net', 'blacklist.woody.ch',
'dnsbl-3.uceprotect.net', 'combined.abuse.ch', 'dnsbl.spfbl.net',
'dnsbl.dronebl.org', 'http.dnsbl.sorbs.net'
];
$dns_count = count($dnsbl_lookup);
$list = [];
if ($ip)
{
$reverse_ip = implode(".", array_reverse(explode(".", $ip)));
foreach ($dnsbl_lookup as $host)
if (checkdnsrr("$reverse_ip.$host.", "A"))
$list[] = "$ip <font color=\"red\"><strong>is Listed in </strong></font> $host<br />\n";
}
$list_count = count($list);
if (0 === $list_count)
echo "No record was not found in the IP blacklist database for: $ip<br>\n";
else
{
$list_str = implode(" ", $list);
echo "$list_str<br>\nBlacklisted: $list_count<br>\n";
}
echo "$list_count|$dns_count\n";
}
dnsbllookup('54.201.153.20'); // 3 entries
dnsbllookup('185.12.225.17'); // 1 entry

PHP - proxy checker multi-threaded

I already made my personal single thread proxy checker using php,but I couldnt make it multi-thread,some days ago,I found one checker using multi-thread on github,can someone help to change it to save the good proxies into a file (ip:port format)?
https://raw.githubusercontent.com/samuel-allan/FastProxyChecker/master/checker.php
What i have tried:
original line 91:
echo json_encode($arr);
changed to:
$json = json_decode($arr);
$good_proxie = $json['arr']['result']['proxy']['ip'];
echo "$good_proxie";
I did not checked it, but think it'll work ^_^
function CheckMultiProxy($proxies, $timeout, $proxy_type)
{
$data = array();
foreach($proxies as $proxy)
{
$parts = explode(':', trim($proxy));
$url = strtok(curPageURL(),'?');
$data[] = $url . '?ip=' . $parts[0] . "&port=" . $parts[1] . "&timeout=" . $timeout . "&proxy_type=" . $proxy_type;
}
$results = multiRequest($data);
$holder = array();
foreach($results as $result)
{
$holder[] = json_decode($result, true)["result"];
}
$arr = array("results" => $holder);
foreach ($arr['results'] as $proxy) {
if ($proxy['success']) {
file_put_contents('YOUR_FILE_HERE', $proxy['proxy']['ip'].':'.$proxy['proxy']['port'].' '.$proxy['proxy']['type'].PHP_EOL, FILE_APPEND);
}
}
echo json_encode($arr);
}

if $string found break else add new not working

I am doing something wrong?
This doesn't take any effects
$id = $_POST['id'];
$tudof = "\n #QTP ".$qtp." ID: ".$id;
echo "\n";
$fp = fopen('../../../ids.txt', 'a+');
$searchString = "id";
if(exec('grep '.escapeshellarg($searchString).' '.$fp)) {
break;
} else {
// Add the new name
fwrite($fp, $writef);
fclose($fp);
}
How to search a string and if not found add a new name?
I believe this will work.
I use file_get_contents to load the file as one string and use strpos to find "id".
I also include a preg_match version since strpos will match "lid" for "id" which preg_match won't.
$id = $_POST['id'];
$tudof = "\n #QTP ".$qtp." ID: ".$id;
echo "\n";
$str = file_get_contents('../../../ids.txt');
$searchString = "id";
if(strpos($str, $searchString) !==false) {
// Found it! Break won't work.
// If you want to stop all php code use exit;
} else {
// Add the new name
// Not sure what you do here but use file_put_contents
file_put_contents('../../../ids.txt', $str);
}
// Preg_match
if(preg_match("/\b " . $str ."\b/", $searchString)) {

cutting content from returned array with php

I am trying to cut results out of the RBL data it pulls back.
Here is the code.
<?
$ips = file("list.inc");
foreach($ips as $ip)
{
$rblurl = 'http://rbl-check.org/rbl_api.php?ipaddress=' . trim($ip);
$boom = fopen($rblurl, "r");
$rbl = stream_get_contents($boom);
echo "</br>";
$data = explode(";",$rbl);
print "<pre>";
print_r($data[0]);
print "</pre>";
echo "</br>";
//fclose($boom);
}
?>
This is the result.
emailbasura;bl.emailbasura.org;nowebsite;notlisted
Sorbs;zombie.dnsbl.sorbs.net;nowebsite;notlisted
msrbl;combined.rbl.msrbl.net;nowebsite;notlisted
nixspam;ix.dnsbl.manitu.net;nowebsite;notlisted
Spamcop;bl.spamcop.net;nowebsite;notlisted
I am trying to cut the first part and the last part so it only displays this.
emailbasura notlisted
Sorbs notlisted
msrbl notlisted
nixspam notlisted
Spamcop notlisted
Any help would be great!
first you need to explode the data by the line breaks not just the delimeter:
$data = explode("\n",$rbl);
once you've done that you just echo out the data:
foreach($data as $item) {
$item = explode(';',$item);
echo $item[0].' '.$item[3];
}
foreach($data as $d)
{
$arr_data = explode(';',$d);
$first_data = $arr_data[0];
$last_data = $arr_data[3];
}
Change here
print "<pre>";
print_r($data[0]);
print "</pre>"
as
print "<pre>";
$spl = split(';', $data[0]);
echo $spl[0] . $spl[3];
print "<pre>";

PHP foreach loop read files, create array and print file name

Could someone help me with this?
I have a folder with some files (without extention)
/module/mail/templates
With these files:
test
test2
I want to first loop and read the file names (test and test2) and print them to my html form as dropdown items. This works (the rest of the form html tags are above and under the code below, and omitted here).
But I also want to read each files content and assign the content to a var $content and place it in an array I can use later.
This is how I try to achieve this, without luck:
foreach (glob("module/mail/templates/*") as $templateName)
{
$i++;
$content = file_get_contents($templateName, r); // This is not working
echo "<p>" . $content . "</p>"; // this is not working
$tpl = str_replace('module/mail/templates/', '', $templatName);
$tplarray = array($tpl => $content); // not working
echo "<option id=\"".$i."\">". $tpl . "</option>";
print_r($tplarray);//not working
}
This code worked for me:
<?php
$tplarray = array();
$i = 0;
echo '<select>';
foreach(glob('module/mail/templates/*') as $templateName) {
$content = file_get_contents($templateName);
if ($content !== false) {
$tpl = str_replace('module/mail/templates/', '', $templateName);
$tplarray[$tpl] = $content;
echo "<option id=\"$i\">$tpl</option>" . PHP_EOL;
} else {
trigger_error("Cannot read $templateName");
}
$i++;
}
echo '</select>';
print_r($tplarray);
?>
Initialize the array outside of the loop. Then assign it values inside the loop. Don't try to print the array until you are outside of the loop.
The r in the call to file_get_contents is wrong. Take it out. The second argument to file_get_contents is optional and should be a boolean if it is used.
Check that file_get_contents() doesn't return FALSE which is what it returns if there is an error trying to read the file.
You have a typo where you are referring to $templatName rather than $templateName.
$tplarray = array();
foreach (glob("module/mail/templates/*") as $templateName) {
$i++;
$content = file_get_contents($templateName);
if ($content !== FALSE) {
echo "<p>" . $content . "</p>";
} else {
trigger_error("file_get_contents() failed for file $templateName");
}
$tpl = str_replace('module/mail/templates/', '', $templateName);
$tplarray[$tpl] = $content;
echo "<option id=\"".$i."\">". $tpl . "</option>";
}
print_r($tplarray);

Categories