Files
wevads-platform/scripts/ptr-discovery.php
2026-02-26 04:53:11 +01:00

137 lines
4.5 KiB
PHP
Executable File

<?php
// Function to get PTR record of an IP address using GeoJS API
function get_ptr_record($ip) {
$url = "http://46.62.128.22/ptr.php?ip=$ip";
$ptr = file_get_contents($url);
if ($ptr === false || trim($ptr) === "") {
return "Failed to get PTR record";
}
return trim($ptr);
}
// Function to generate a random alphanumeric string followed by .com
function generate_random_domain($length = 12) {
$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString . '.com';
}
// Path to the CSV file
$csvFile = 'Servers.csv';
$ptrFile = 'ptr.txt';
// Open the CSV file
if (($handle = fopen($csvFile, 'r')) !== FALSE) {
// Prepare an array to store the results
$results = [];
// Read the CSV file line by line
while (($data = fgetcsv($handle, 1000, ',')) !== FALSE) {
// Assuming the IP address is in the first column
if (!empty($data[0])) {
$ip = $data[0];
$ptr = get_ptr_record($ip);
if ($ptr === "Failed to get PTR record") {
$ptr = generate_random_domain();
}
$results[] = [
'ip' => $ip,
'ptr' => $ptr
];
}
}
// Close the CSV file
fclose($handle);
// Open the ptr.txt file for writing
if (($ptrHandle = fopen($ptrFile, 'w')) !== FALSE) {
// Write the results to ptr.txt
foreach ($results as $result) {
fwrite($ptrHandle, $result['ip'] . ': ' . $result['ptr'] . "\n");
}
// Close the ptr.txt file
fclose($ptrHandle);
echo "PTR records have been saved to ptr.txt.\n";
} else {
echo "Unable to open ptr.txt for writing.\n";
}
} else {
echo "Unable to open the CSV file.\n";
}
// Path to the JSON file with database credentials
$host = "localhost"; $dbname = "adx_system"; $user = "admin"; $password = "admin123"; $port = "5432";
// Decode the JSON data
// Extract the necessary fields
// Connect to the PostgreSQL database
$dsn = "pgsql:host=$host;dbname=$dbname;port=$port";
try {
$pdo = new PDO($dsn, $user, $password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Read ptr.txt
if (($ptrHandle = fopen($ptrFile, 'r')) !== FALSE) {
$index = 0;
// Get the last domain ID from the database
$stmt = $pdo->query("SELECT MAX(id) AS max_id FROM admin.domains");
$result = $stmt->fetch(PDO::FETCH_ASSOC);
$lastDomainId = $result['max_id'] ?? 0;
while (($line = fgets($ptrHandle)) !== FALSE) {
$line = trim($line);
if ($line === '') {
continue; // Skip empty lines
}
list($ip, $ptr) = explode(': ', $line);
$domainId = $lastDomainId + 1 + $index;
$account_id = 0;
$account_name = 'None';
$account_type = 'none';
$status = 'Activated';
$mta_server_id = NULL;
$ip_id = NULL;
$availability = 'Available';
$has_brand = 'No';
$expiration_date = '2025-07-19';
$created_by = 'yahia.oussama34@gmail.com';
$last_updated_by = 'yahia.oussama34@gmail.com';
$created_date = '2024-07-19';
$last_updated_date = '2024-07-19';
// Insert the PTR record into the database
$stmt = $pdo->prepare("
INSERT INTO admin.domains
(id, account_id, account_name, account_type, status, mta_server_id, ip_id, value, availability, has_brand, expiration_date, created_by, last_updated_by, created_date, last_updated_date)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
");
$stmt->execute([
$domainId, $account_id, $account_name, $account_type, $status, $mta_server_id, $ip_id, $ptr,
$availability, $has_brand, $expiration_date, $created_by, $last_updated_by, $created_date, $last_updated_date
]);
$index++;
}
// Close the ptr.txt file
fclose($ptrHandle);
echo "PTR records have been added to the database.\n";
} else {
echo "Unable to open ptr.txt for reading.\n";
}
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>