Skip to content

Instantly share code, notes, and snippets.

@nexocentric
Last active August 29, 2015 13:56
Show Gist options
  • Save nexocentric/9065488 to your computer and use it in GitHub Desktop.
Save nexocentric/9065488 to your computer and use it in GitHub Desktop.
<!-- start scripts for auto reloading=> -->
<meta name="previousUpdateStatusList" content="">
<meta name="monitorList" content="./htdocs/cmtrend_stop.php,./htdocs/style.css,./template/cmtrend_stop.tmpl,./lib/libcommon,./lib/libcable,./lib/libpage,./lib/libcable,./conf/1217141131.00,./conf/1217141131.01,./conf/1217141131.02,./conf/1217141131.03,./conf/1217141131.04,./conf/1217141131.05,./conf/1217141131.06,./conf/1217141131.07,./conf/1217141131.08,./conf/1217141131.09">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<script type="text/javascript">
var ajaxHandlerPath = 'http://192.168.2.175/~dzakuma/update-monitor.php';
var ajaxRequest = null;
var ajaxTimeout = 300000;
function showUpdated(json)
{
var updateTime = json.lastUpdated;
console.log(json);
var previousUpdateStatusList = json.previousUpdateStatusList;
var updateTimeList = previousUpdateStatusList.split(',');
previousUpdateStatusList = previousUpdateStatusList.replace(':updated', '');
var time = $('meta[name=lastUpdated]').attr('content');
var updateFlag = 0;
var currentList = $('meta[name=previousUpdateStatusList]').attr('content').split(',');
for (var updated = 0; updated < updateTimeList.length; updated++) {
var current = updateTimeList[updated];
if (currentList[updated] === '') {
$('meta[name=previousUpdateStatusList]').attr('content', previousUpdateStatusList);
break;
} else if (current.contains('update')) {
updateFlag = 1;
}
}
if (updateFlag) {
$('meta[name=previousUpdateStatusList]').attr('content', previousUpdateStatusList);
console.log(' PHP: ' + current);
console.log('HTML: ' + currentList[updated]);
location.reload();
}
}
(function pollUpdates(){
var lastUpdated = $('meta[name=lastUpdated]').attr('content');
var monitorList = $('meta[name=monitorList]').attr('content');
var previousUpdateStatusList = $('meta[name=previousUpdateStatusList]').attr('content');
monitorList = monitorList.split(',');
previousUpdateStatusList = previousUpdateStatusList.split(',');
$.ajax({
url: ajaxHandlerPath,
type: 'POST',
data: {
requestType : '../dynamic.php',
'monitorList[]' : monitorList,
'previousUpdateStatusList[]' : previousUpdateStatusList,
timeout : ajaxTimeout,
lastUpdated : lastUpdated
},
success: showUpdated,
dataType: 'json',
complete: pollUpdates,
timeout: ajaxTimeout
});
})();
</script>
<!-- <=end scripts for auto reloading -->
<?php
#===========================================================
# [author]
# Dodzi Y. Dzakuma
# [summary]
# This function checks if a superglobal parameter is set in
# specified superglobal. If no superglobal is specified this
# function will check the $_REQUEST superglobal by default.
# [parameters]
# 1) parameter name to check
# 2) value to return if not set (optional)
# 3) name of superglobal array to search (optional)
# [return]
# 1) the value of spefcified parameter
# 2) the default value if specified parameter doesn't exist
# 3) null if no default value spcified
#===========================================================
function checkRequest(
$variableName,
$defaultValue = null,
$functionName = ""
) {
$superglobalArray = null;
if (stripos($functionName, "get") !== false) {
$superglobalArray = &$_GET;
} else if (stripos($functionName, "files") !== false) {
$superglobalArray = &$_FILES;
if(empty($superglobalArray[$variableName]["tmp_name"])) {
return $defaultValue;
}
} else if (stripos($functionName, "post") !== false) {
$superglobalArray = &$_POST;
} else if (stripos($functionName, "cookie") !== false) {
$superglobalArray = &$_COOKIE;
} else if (stripos($functionName, "session") !== false) {
$superglobalArray = &$_SESSION;
} else{
$superglobalArray = &$_REQUEST;
}
#
if (!isset($superglobalArray[$variableName])) {
return $defaultValue;
}
return $superglobalArray[$variableName];
}
function checkCookie($variableName, $defaultValue = "") {
return checkRequest($variableName, $defaultValue, __FUNCTION__);
}
function checkPost($variableName, $defaultValue = "") {
return checkRequest($variableName, $defaultValue, __FUNCTION__);
}
function checkFiles($variableName, $defaultValue = "") {
return checkRequest($variableName, $defaultValue, __FUNCTION__);
}
function checkGet($variableName, $defaultValue = "") {
return checkRequest($variableName, $defaultValue, __FUNCTION__);
}
$timeout = checkPost("timeout");
$requestType = checkPost("requestType");
$monitorList = checkPost("monitorList");
$previousUpdateStatusList = checkPost("previousUpdateStatusList");
$currentUpdateStatusList = array();
$maximumRetries = checkPost("timeout", 300);
for ($retryCount = 0; $retryCount < $maximumRetries; $retryCount++) {
// #things
$currentUpdateStatusList = array();
$updateFlag = false;
$fileCount = count($monitorList);
for ($file = 0; $file < $fileCount; $file++) {
$filePath = $monitorList[$file];
$fileStatus = stat($filePath);
$currentUpdateStatusList[] = $fileStatus["mtime"];
if (
isset($previousUpdateStatusList[$file]) &&
$currentUpdateStatusList[$file] != $previousUpdateStatusList[$file]
) {
$updateFlag = !$updateFlag;
$currentUpdateStatusList[$file] .= ":updated";
}
}
if (!$updateFlag) {
sleep(1);
} else {
$response["previousUpdateStatusList"] = implode(",", $currentUpdateStatusList);
header("Content-Type: application/json");
header("Expires: Tue, 01 Jan 2000 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
print(json_encode($response));
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment