File: /home/internet/.trash/upload.php.2
<?php
/**
* upload.php
*
* This script:
* - Saves an uploaded text file containing complete API URLs (one per line) to a local folder.
* - Reads each URL from the file.
* - Extracts the domain from the "history" parameter in the URL.
* - Calls the API URL (which returns JSON) and decodes the response.
* - Iterates through all WHOIS records, extracting the registrant_contact's full_name and email_address.
* - Inserts each record into the MySQL database.
* - Waits 1 minute between each API call.
* - Continues running in the background even if the user closes the browser.
* - Sends an email alert when processing is complete.
*/
// Continue execution even if the client disconnects
ignore_user_abort(true);
set_time_limit(0);
// Enable error reporting (for debugging; remove or modify for production)
ini_set('display_errors', 1);
error_reporting(E_ALL);
/*--------------------------- CONFIGURATION ---------------------------*/
// Database credentials
$dbHost = 'localhost';
$dbUser = 'internet_wejai';
$dbPass = 'Remya@158';
$dbName = 'internet_whoishistory';
// Directory where the uploaded file will be saved
$uploadDir = __DIR__ . '/uploads';
// Email alert configuration
$alertEmail = 'psv158@gmail.com'; // Replace with your email address
$emailFrom = 'admin@oyya.net'; // Replace with a valid sender address
// Create uploads directory if it doesn't exist
if (!is_dir($uploadDir)) {
mkdir($uploadDir, 0777, true);
}
// Connect to MySQL database
$mysqli = new mysqli($dbHost, $dbUser, $dbPass, $dbName);
if ($mysqli->connect_error) {
die("Database connection error: " . $mysqli->connect_error);
}
/*--------------------------- PROCESS FILE ---------------------------*/
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['file'])) {
// Save the uploaded file to the uploads folder
$filename = basename($_FILES['file']['name']);
$targetPath = $uploadDir . '/' . $filename;
if (!move_uploaded_file($_FILES['file']['tmp_name'], $targetPath)) {
echo "Failed to move uploaded file.";
exit;
}
echo "File uploaded successfully. Beginning data processing...<br>";
flush();
// Read each line from the file (each line should be a complete API URL)
$apiUrls = file($targetPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!$apiUrls) {
echo "No valid URLs found in file.";
exit;
}
// Loop through each API URL
foreach ($apiUrls as $apiUrl) {
$apiUrl = trim($apiUrl);
if (empty($apiUrl)) {
continue;
}
// Extract domain name from the "history" parameter
$parsedUrl = parse_url($apiUrl);
$domain = '';
if (!empty($parsedUrl['query'])) {
parse_str($parsedUrl['query'], $queryParams);
if (!empty($queryParams['history'])) {
$domain = $queryParams['history'];
}
}
echo "Processing domain: {$domain}<br>";
flush();
// Call the API URL (returns JSON)
$response = @file_get_contents($apiUrl);
if ($response === false) {
echo "Failed to retrieve data for {$domain}<br>";
flush();
sleep(60);
continue;
}
// Decode the JSON response
$data = json_decode($response, true);
if (json_last_error() !== JSON_ERROR_NONE) {
echo "JSON parse error for {$domain}: " . json_last_error_msg() . "<br>";
flush();
sleep(60);
continue;
}
// Check for WHOIS records and process each one
if (!empty($data['whois_records']) && is_array($data['whois_records'])) {
foreach ($data['whois_records'] as $record) {
if (!empty($record['registrant_contact'])) {
$registrant = $record['registrant_contact'];
$fullName = isset($registrant['full_name']) ? $registrant['full_name'] : '';
$emailAddress = isset($registrant['email_address']) ? $registrant['email_address'] : '';
if ($fullName && $emailAddress) {
$stmt = $mysqli->prepare("INSERT INTO domain_contacts (domain_name, full_name, email_address) VALUES (?, ?, ?)");
if ($stmt) {
$stmt->bind_param("sss", $domain, $fullName, $emailAddress);
$stmt->execute();
if ($stmt->error) {
echo "DB insert error for {$domain}: " . $stmt->error . "<br>";
} else {
echo "Inserted data for {$domain}<br>";
}
$stmt->close();
} else {
echo "Failed to prepare statement for {$domain}: " . $mysqli->error . "<br>";
}
}
}
}
} else {
echo "No whois_records found for {$domain}<br>";
}
// Wait 1 minute before processing the next URL
sleep(60);
}
echo "<br>All data processing complete.";
// Send an email alert that processing is complete
$to = $alertEmail;
$subject = "Data Processing Complete Alert";
$message = "The processing of your domain WHOIS data file has completed successfully.";
$headers = "From: " . $emailFrom . "\r\n" .
"Reply-To: " . $emailFrom . "\r\n" .
"X-Mailer: PHP/" . phpversion();
// Check if the mail was sent successfully
if (mail($to, $subject, $message, $headers)) {
echo "<br>Email alert sent to {$to}.";
} else {
echo "<br>Failed to send email alert.";
}
} else {
echo "No file uploaded or wrong request method.";
}
// Close the database connection
$mysqli->close();
?>