-
Notifications
You must be signed in to change notification settings - Fork 1
/
update_peers_urls.php
68 lines (55 loc) · 2.24 KB
/
update_peers_urls.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
header('Content-Type: application/json');
// Enable error reporting for debugging
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Include database configuration
include 'config_1.php';
// Create a new database connection
$conn = getDbConnection();
if ($conn->connect_error) {
echo json_encode(['success' => false, 'message' => 'Database connection failed: ' . $conn->connect_error]);
exit; // Exit if the connection fails
}
// Get the updated URLs from the request
$updatedUrls = json_decode(file_get_contents('php://input'), true);
// Check if the updated URLs are provided
if (!is_array($updatedUrls)) {
echo json_encode(['success' => false, 'message' => 'Invalid input format.']);
exit;
}
// Prepare a statement for inserting/updating URLs
$stmt = $conn->prepare("INSERT INTO urls (url, timestamp, valid) VALUES (?, ?, ?) ON DUPLICATE KEY UPDATE timestamp = VALUES(timestamp), valid = VALUES(valid)");
if (!$stmt) {
echo json_encode(['success' => false, 'message' => 'Statement preparation failed: ' . $conn->error]);
exit;
}
// Loop through each URL and save it to the database
foreach ($updatedUrls as $urlData) {
if (isset($urlData['url'])) {
// Get the URL and ensure it uses HTTPS
$url = $urlData['url'];
// Force HTTPS protocol
if (stripos($url, 'http://') === 0) {
$url = 'https://' . substr($url, 7); // Replace http:// with https://
} elseif (stripos($url, 'https://') !== 0) {
$url = 'https://' . $url; // Add https:// if no protocol is present
}
$timestamp = date('Y-m-d H:i:s'); // Current timestamp
$valid = isset($urlData['valid']) ? (bool)$urlData['valid'] : true; // Use provided validity or default to true
// Bind parameters and execute the statement
$stmt->bind_param("ssi", $url, $timestamp, $valid);
// Check for execution errors
if (!$stmt->execute()) {
echo json_encode(['success' => false, 'message' => 'Database query failed: ' . $stmt->error]);
exit;
}
}
}
// Close the statement and database connection
$stmt->close();
$conn->close();
// Output success message
echo json_encode(['success' => true]);
?>