in this codes (<?php
// index.php
// Error reporting for debugging (remove in production)
error_reporting(E_ALL);
ini_set('display_errors', 1);
// Function to log messages
function logMessage(message) {
logFile = 'change_detector.log';
if (!file_exists(logFile)) {
file_put_contents(logFile, "");
}
timestamp=date(′Y−m−dH:i:s′);logEntry = "[timestamp]message\n";
file_put_contents(logFile,logEntry, FILE_APPEND | LOCK_EX);
}
// Function to get the last check time from the log
function getLastCheckTime() {
logFile=′changelogFile)) {
logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!empty(lastLine = end(lastLine, matches[1];
}
}
}
return 'Never';
}
// Function to check for changes on the page
function checkForChanges(url) {
ch = curl_init();
curl_setopt(ch,CURLOPTURL,url);
curl_setopt(ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0');
$response = curl_exec($ch);
if (curl_errno($ch)) {
logMessage("cURL Error: " . curl_error($ch));
return ['error' => 'Error: ' . curl_error($ch)];
}
curl_close($ch);
$currentHash = md5($response);
$previousHash = file_exists('previous_hash.txt') ? trim(file_get_contents('previous_hash.txt')) : '';
if ($currentHash !== $previousHash) {
file_put_contents('previous_hash.txt', $currentHash);
logMessage("Changes detected. New hash: " . $currentHash);
return [
'changed' => true,
'oldHash' => $previousHash,
'newHash' => $currentHash
];
}
logMessage("No changes detected");
return ['changed' => false];
}
url=′https://lolastCheckTime = getLastCheckTime();
hashInfo = '';
$changesDetected = false;
if (_SERVER['REQUEST_METHOD'] === 'POST' && isset(_POST['check_status'])) {
result=checkForChanges(url);
$lastCheckTime = date('Y-m-d H:i:s');
if (isset($result['error'])) {
$changeStatus = $result['error'];
logMessage("Error occurred: " . $result['error']);
} elseif ($result['changed']) {
$changeStatus = 'Changes detected!';
$hashInfo = "Old Hash: " . substr($result['oldHash'], 0, 8) . "...<br>New Hash: " . substr($result['newHash'], 0, 8) . "...";
logMessage("Changes detected on the page");
$changesDetected = true;
} else {
$changeStatus = 'No changes detected';
$changesDetected = false;
}
}
?>
<div id="hashInfoContainer" class="hash-info" style="display: <?php echo !empty($hashInfo) ? 'block' : 'none'; ?>">
<h2>Hash Information:</h2>
<div id="hashInfoContent"><?php echo $hashInfo; ?></div>
</div>
</div>
<audio id="alarmAudio" loop>
<source src="alarm_1.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
<script>
document.addEventListener('DOMContentLoaded', function() {
var audio = document.getElementById('alarmAudio');
var checkForm = document.getElementById('checkForm');
var statusContainer = document.getElementById('statusContainer');
var lastCheckContainer = document.getElementById('lastCheckContainer');
var hashInfoContainer = document.getElementById('hashInfoContainer');
var hashInfoContent = document.getElementById('hashInfoContent');
var changesDetected = <?php echo json_encode($changesDetected); ?>;
var alarmTimeout;
var checkInterval;
function initiateRefresh() {
if (!changesDetected) {
setTimeout(function() {
location.reload();
}, 10000); // Reload after 10 seconds if no change
}
}
function handleChanges() {
if (changesDetected) {
audio.play();
clearTimeout(alarmTimeout);
clearInterval(checkInterval);
// Play alarm for 10 minutes
alarmTimeout = setTimeout(function() {
audio.pause();
audio.currentTime = 0;
changesDetected = false;
checkInterval = setInterval(checkForChanges, 10000);
}, 600000); // 10 minutes
} else {
audio.pause();
audio.currentTime = 0;
initiateRefresh();
}
}
function checkForChanges() {
var formData = new FormData(checkForm);
fetch('index.php', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(html => {
var parser = new DOMParser();
var doc = parser.parseFromString(html, 'text/html');
var newStatus = doc.querySelector('.status').textContent;
var newLastCheck = doc.querySelector('.last-check').textContent;
var newHashInfo = doc.querySelector('.hash-info');
statusContainer.textContent = newStatus;
statusContainer.className = doc.querySelector('.status').className;
lastCheckContainer.textContent = newLastCheck;
if (newHashInfo) {
hashInfoContainer.style.display = 'block';
hashInfoContent.innerHTML = newHashInfo.innerHTML;
} else {
hashInfoContainer.style.display = 'none';
}
changesDetected = newStatus.includes('Changes detected');
handleChanges();
})
.catch(error => {
console.error('Error:', error);
alert('An error occurred while checking for changes.');
});
}
handleChanges(); // Initial check
// Start periodic checks
checkInterval = setInterval(checkForChanges, 10000); // Check every 10 seconds
// Form submission handling
checkForm.addEventListener('submit', function(event) {
event.preventDefault();
checkForChanges();
});
});
</script>