$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 $jsonFile = '/opt/adxapp2/datasources/system.json'; $jsonData = file_get_contents($jsonFile); // Decode the JSON data $data = json_decode($jsonData, true); // Extract the necessary fields $host = $data['host']; $dbname = $data['database']; $user = $data['username']; $password = $data['password']; $port = $data['port'] ?? '5432'; // 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(); } ?>