Skip to content

Instantly share code, notes, and snippets.

@nexocentric
Last active August 29, 2015 13:56
Show Gist options
  • Save nexocentric/9035932 to your computer and use it in GitHub Desktop.
Save nexocentric/9035932 to your computer and use it in GitHub Desktop.
<?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 = "",
$functionName = ""
) {
$superglobalArray = null;
if (stripos($functionName, "get") !== false) {
$superglobalArray = &$_GET;
} else if (stripos($functionName, "files") !== false) {
$superglobalArray = &$_FILES;
} 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 ? $defaultValue : null);
}
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