Skip to content

Instantly share code, notes, and snippets.

@jamesstout
Last active December 19, 2015 02:19
Show Gist options
  • Save jamesstout/5882687 to your computer and use it in GitHub Desktop.
Save jamesstout/5882687 to your computer and use it in GitHub Desktop.
Get historical HKO warning and signals data
<?php
include dirname(__FILE__) . '/KLogger.php';
$logDir = dirname(__FILE__) . '/logs';
$file = $logDir . '/HKOLog.txt';
$log = KLogger::instance($logDir, KLogger::DEBUG, false);
include dirname(__FILE__) . '/tz.php';
// need tz set for date functions
$tz = safeSetTZ();
if($tz != "" and $tz !=null){
$log = KLogger::instance($logDir, KLogger::DEBUG, true);
$log->logDebug("----------------------");
$log->logDebug("Set tzName: $tz");
}
include dirname(__FILE__) . '/utils.php';
include dirname(__FILE__) . '/dbConfig.php';
include dirname(__FILE__) . '/DButils.php';
$curl_error_str = "";
$curl_error_no = 0;
$didTimeOut = false;
$httpCode = 0;
$curl_info = "";
$useHardcodedHTML=false;
$editPage = "";
$page="";
$html="";
$emailAddress = "[email protected]";
$endtime = new DateTime('now', new DateTimeZone('Asia/Hong_Kong'));
$log->logDebug("endtime for query = ". print_r($endtime->format('Ym'), true));
$url = 'http://www.hko.gov.hk/cgi-bin/hko/warndb_e1.pl?opt=3&rcolor=All+colours&start_ym=199801&end_ym=' . $endtime->format('Ym') . '&submit=Submit+Query';
$log->logInfo("url = $url");
if (!$page = CURL($url)){
$str = "\nScript: " . $_SERVER["SCRIPT_FILENAME"] . "\nFile: " . __FILE__ . "\nFunction: "
. __FUNCTION__ . "() - line: " . __LINE__ . "\nERROR($curl_error_no) - $curl_error_str\nHTTP Response Code: $httpCode\nCURL INFO:\n$curl_info";
$log->logError("getting $url: $str");
echo("ERROR getting $url: $str");
$log->logInfo('Use hardcoded HTML');
$useHardcodedHTML=true;
}
else{
// strip out all the header and js rubbish
$start = '<TABLE BORDER=2';
$end = '</TABLE>';
$editPage = GetBetween($page,$start,$end );
//$log->logDebug($editPage);
if($editPage != ''){
// strip off table summary - contains "s
$findme = 'background="">';
$posn = stripos($editPage, $findme);
if ($posn !== false) {
// strip
$editPage = substr($editPage, $posn);
// add table tag back in and my id
$editPage = '<TABLE ID="myid" BORDER=2 ' . $editPage;
//var_dump($editPage);
//$log->logDebug($editPage);
}
else{
$log->logWarn('Cannot find "background" - Use hardcoded HTML');
$useHardcodedHTML=true;
}
}
else{
$log->logWarn('Cannot find "TABLE BORDER" - Use hardcoded HTML');
$useHardcodedHTML=true;
}
}
if($useHardcodedHTML==true){
$html = getHTML();
//$log->logDebug($html);
}
else{
$html = $editPage;
}
// create DOM
$xml = new DOMDocument();
$xml->validateOnParse = true;
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
// get my table
$table =$xpath->query('//*[@id="myid"]')->item(0); // for printing the whole html table just type: print $xml->saveXML($table);
$startDate="";
$startTime="";
$endDate="";
$endTime="";
$signal="";
$duration=array();
$durationStr="";
$rowCount=0;
$history = array();
$startDT = null;
$endDT= null;
$durationDT= null;
// loop through rows
$rows = $table->getElementsByTagName("tr");
foreach ($rows as $row) {
$startDate="";
$startTime="";
$endDate="";
$endTime="";
$signal="";
$duration=array();
$durationStr="";
$startDT = null;
$endDT= null;
$durationDT= null;
$rowCount++;
// get cells for the row
$cells = $row -> getElementsByTagName('td');
$cellCount=0;
//echo "rowCount = $rowCount\n\n";
// loop through cells
foreach ($cells as $cell) {
$cellCount++;
// we want:
// cell 1 = Signal
// cell 2 = start time in 24h format with leading 0s, colon delim. e.g 09:15
// cell 3 = start date in this format 25/May/2013 with leading zeroes
// cell 4 = end time in 24h format with leading 0s, colon delim. e.g 09:15
// cell 5 = end date in this format 25/May/2013 with leading zeroes
// cell 6 = duration in "hh mm" format
if($cellCount == 1){
$signal = trim($cell->nodeValue);
if($signal == "Amber") $signal = 9;
if($signal == "Red") $signal = 10;
if($signal == "Black") $signal = 11;
}
elseif($cellCount == 2){
$startTime = trim($cell->nodeValue);
}
elseif($cellCount == 3){
$startDate = trim($cell->nodeValue);
}
elseif($cellCount == 4){
$endTime = trim($cell->nodeValue);
}
elseif($cellCount == 5){
$endDate = trim($cell->nodeValue);
}
elseif($cellCount == 6){
$durationStr = ltrim($cell->nodeValue, " \n\r");
//echo("durstr = $durationStr\n\n");
$duration = explode(" ", $durationStr);
//print_r($duration);
$durationStr = implode(":", $duration);
}
//print $cell->nodeValue;
$log->logDebug("cell->nodeValue = $cell->nodeValue");
}
// create datetime str from date and time
// use as key in array - should be unique
if($startTime != "" and $startDate != ""){
$log->logDebug("startTime = $startTime");
$log->logDebug("startDate = $startDate");
$log->logDebug("endTime = $endTime");
$log->logDebug("endDate = $endDate");
$log->logDebug("durationStr = $durationStr");
$startDT = getDateTime($startTime, $startDate);
$endDT = getDateTime($endTime, $endDate);
//$durationDT = getTimeFromDuration($durationStr);
$log->logDebug("startDT = ". print_r($startDT->format('Y-m-d H:i'), true));
$log->logDebug("endDT = ". print_r($endDT->format('Y-m-d H:i'), true));
//$log->logDebug("durDT = ". print_r($durationDT->format('Y-m-d H:i'), true));
}
if($signal != "" ){
$history[] = array(
'sig' => $signal,
'startDT' => $startDT,
'endDT' => $endDT,
'duration' => $durationStr,
);
//$history[print_r($startDT->format('Y-m-d H:i'), true)] = $signal;
//$log->logInfo("hist = ", print_r($history, true));
}
}
//exit;
//print_r($rowCount);
//var_dump($history);
if(count($history) != ($rowCount-2)){
echo "WARNING: rowCount != history count\n";
$log->logWarn("rowCount != history count");
echo "rowCount = $rowCount\n";
echo "signal count = " . count($history) . "\n";
//$log->logInfo("history", $history);
$log->logInfo("number of rain signals: " . count($history));
}
else{
//echo "SUCCESS\n";
$log->logInfo("SUCCESS: rowCount == history count");
//echo "rowCount = " . ($rowCount-2) . "\n";
//echo "signal count = " . count($history) . "\n";
$log->logInfo("history", $history);
$log->logInfo("number of rain signals: " . count($history));
}
storetoDB();
function storetoDB(){
global $history;
global $log;
global $dbConfig;
$link = connectToDBMYSQLI($dbConfig['tiberius-hk-test'], __FILE__, true);
foreach ($history as $arr) {
//print_r($arr);
$startDTStr = $arr['startDT']->format('Y-m-d H:i');
$endDTStr = $arr['endDT']->format('Y-m-d H:i');
$durationStr = $arr['duration'];
$sig = $arr['sig'];
$year = $arr['startDT']->format('Y');
$sql="INSERT IGNORE INTO `HKOWarningsHist` (`code`, `startTime`, `endTime`, `duration`, `name`, `intensity`, `year`) VALUES ( $sig, '$startDTStr', '$endDTStr', '$durationStr', '', '', '$year' )";
$log->logDebug("sql = $sql");
if (!$result = mysqli_query($link, $sql)) {
echo "ERROR: store Rain Hist script failed insert row. SQL: " . $sql . "\n";
$log->logError("store Rain Hist script failed insert row. SQL: $sql");
die('Invalid query: ' . mysqli_error($link));
}
}
}
function getDateTime($time, $date) {
global $log;
$newDate = DateTime::createFromFormat('j/M/Y H:i', "$date $time");
if (!is_object($newDate)) {
$log->logError("Invalid date/time string");
throw new Exception('Invalid date/time string');
}
return $newDate; //->format('Y-m-d H:i');
}
// function getTimeFromDuration($duration) {
// $newDate = DateTime::createFromFormat('j/M/Y H i', "01/Jan/1970 $duration");
// if (!is_object($newDate)) {
// $log->logError("Invalid date/time string");
// throw new Exception('Invalid date/time string');
// }
// return $newDate; //->format('Y-m-d H:i');
// }
function getYear($date) {
$newDate = DateTime::createFromFormat('Y-m-d H:i', "$date");
if (!is_object($newDate)) {
$log->logError("Invalid date string");
throw new Exception('Invalid date string');
}
return $newDate->format('Y');
}
function getHTML(){
//skipping this for the gist - just too big
}
?>
<?php
include dirname(__FILE__) . '/KLogger.php';
$logDir = dirname(__FILE__) . '/logs';
$file = $logDir . '/HKOLog.txt';
$log = KLogger::instance($logDir, KLogger::DEBUG, false);
include dirname(__FILE__) . '/tz.php';
// need tz set for date functions
$tz = safeSetTZ();
if($tz != "" and $tz !=null){
$log = KLogger::instance($logDir, KLogger::DEBUG, true);
$log->logDebug("----------------------");
$log->logDebug("Set tzName: $tz");
}
include dirname(__FILE__) . '/utils.php';
include dirname(__FILE__) . '/dbConfig.php';
include dirname(__FILE__) . '/DButils.php';
$curl_error_str = "";
$curl_error_no = 0;
$didTimeOut = false;
$httpCode = 0;
$curl_info = "";
$useHardcodedHTML=false;
$editPage = "";
$page="";
$html="";
$emailAddress = "[email protected]";
$endtime = new DateTime('now', new DateTimeZone('Asia/Hong_Kong'));
$log->logDebug("endtime for query = ". print_r($endtime->format('Ym'), true));
$url = 'http://www.weather.gov.hk/cgi-bin/hko/warndb_e1.pl?opt=1&sgnl=1.or.higher&start_ym=194601&end_ym=' . $endtime->format('Ym') . '&submit=Submit+Query';
$log->logInfo("url = $url");
if (!$page = CURL($url)){
$str = "\nScript: " . $_SERVER["SCRIPT_FILENAME"] . "\nFile: " . __FILE__ . "\nFunction: "
. __FUNCTION__ . "() - line: " . __LINE__ . "\nERROR($curl_error_no) - $curl_error_str\nHTTP Response Code: $httpCode\nCURL INFO:\n$curl_info";
$log->logError("getting $url: $str");
echo("ERROR getting $url: $str");
$log->logInfo('Use hardcoded HTML');
$useHardcodedHTML=true;
}
else{
// strip out all the header and js rubbish
$start = '<TABLE BORDER=2';
$end = '</TABLE>';
$editPage = GetBetween($page,$start,$end );
//$log->logDebug($editPage);
if($editPage != ''){
// strip off table summary - contains "s
$findme = 'background="">';
$posn = stripos($editPage, $findme);
if ($posn !== false) {
// strip
$editPage = substr($editPage, $posn);
// add table tag back in and my id
$editPage = '<TABLE ID="myid" BORDER=2 ' . $editPage;
//var_dump($editPage);
//$log->logDebug($editPage);
}
else{
$log->logWarn('Cannot find "background" - Use hardcoded HTML');
$useHardcodedHTML=true;
}
}
else{
$log->logWarn('Cannot find "TABLE BORDER" - Use hardcoded HTML');
$useHardcodedHTML=true;
}
}
if($useHardcodedHTML==true){
$html = getHTML();
//$log->logDebug($html);
}
else{
$html = $editPage;
}
// create DOM
$xml = new DOMDocument();
$xml->validateOnParse = true;
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
// get my table
$table =$xpath->query('//*[@id="myid"]')->item(0); // for printing the whole html table just type: print $xml->saveXML($table);
$startDate="";
$startTime="";
$endDate="";
$endTime="";
$signal="";
$duration=array();
$durationStr="";
$rowCount=0;
$history = array();
$startDT = null;
$endDT= null;
$intensity="";
$name="";
// loop through rows
$rows = $table->getElementsByTagName("tr");
foreach ($rows as $row) {
$startDate="";
$startTime="";
$endDate="";
$endTime="";
$signal="";
$duration=array();
$durationStr="";
$startDT = null;
$endDT= null;
$intensity="";
$name="";
$rowCount++;
// get cells for the row
$cells = $row -> getElementsByTagName('td');
$cellCount=0;
//echo "rowCount = $rowCount\n\n";
// loop through cells
foreach ($cells as $cell) {
$cellCount++;
// we want:
// cell 1 = Intensity
// cell 2 = name
// cell 3 = signal
// cell 4 = start time in 24h format with leading 0s, colon delim. e.g 09:15
// cell 5 = start date in this format 25/May/2013 with leading zeroes, sometimes with an S at the end
// cell 6 = end time in 24h format with leading 0s, colon delim. e.g 09:15
// cell 7 = end date in this format 25/May/2013 with leading zeroes, sometimes with an S at the end
// cell 8 = duration in "hh mm" format
if($cellCount == 1){
$intensity = trim($cell->nodeValue);
}
elseif($cellCount == 2){
$name = trim($cell->nodeValue);
}
elseif($cellCount == 3){
$signal = trim($cell->nodeValue);
// this needs converting to a warning code
if($signal == "8 NE") $signal = 3;
if($signal == "8 NW ") $signal = 4;
if($signal == "8 SE") $signal = 5;
if($signal == "8 SW") $signal = 6;
if($signal == "9") $signal = 7;
if($signal == "10") $signal = 8;
}
elseif($cellCount == 4){
$startTime = trim($cell->nodeValue);
}
elseif($cellCount == 5){
$startDate = trim($cell->nodeValue);
$startDate = rtrim($startDate, " S");
}
elseif($cellCount == 6){
$endTime = trim($cell->nodeValue);
}
elseif($cellCount == 7){
$endDate = trim($cell->nodeValue);
$endDate = rtrim($endDate, " S");
}
elseif($cellCount == 8){
$durationStr = ltrim($cell->nodeValue, " \n\r");
//echo("durstr = $durationStr\n\n");
$duration = explode(" ", $durationStr);
//print_r($duration);
$durationStr = implode(":", $duration);
}
//print $cell->nodeValue;
//$log->logDebug("cell->nodeValue = $cell->nodeValue");
}
// create datetime str from date and time
// use as key in array - should be unique
if($startTime != "" and $startDate != ""){
$log->logDebug("intensity = $intensity");
$log->logDebug("name = $name");
$log->logDebug("startTime = $startTime");
$log->logDebug("startDate = $startDate");
$log->logDebug("endTime = $endTime");
$log->logDebug("endDate = $endDate");
$log->logDebug("durationStr = $durationStr");
$startDT = getDateTime($startTime, $startDate);
$endDT = getDateTime($endTime, $endDate);
//$durationDT = getTimeFromDuration($durationStr);
$log->logDebug("startDT = ". print_r($startDT->format('Y-m-d H:i'), true));
$log->logDebug("endDT = ". print_r($endDT->format('Y-m-d H:i'), true));
//$log->logDebug("durDT = ". print_r($durationDT->format('Y-m-d H:i'), true));
}
if($signal != "" ){
$history[] = array(
'sig' => intval($signal),
'startDT' => $startDT,
'endDT' => $endDT,
'duration' => $durationStr,
'intensity' => $intensity,
'name' => $name,
);
//$history[print_r($startDT->format('Y-m-d H:i'), true)] = $signal;
//$log->logInfo("hist = ", print_r($history, true));
}
}
//exit;
//print_r($rowCount);
//var_dump($history);
//exit;
if(count($history) != ($rowCount-2)){
echo "WARNING: rowCount != history count\n";
$log->logWarn("rowCount != history count");
echo "rowCount = $rowCount\n";
echo "signal count = " . count($history) . "\n";
//$log->logInfo("history", $history);
$log->logInfo("number of rain signals: " . count($history));
}
else{
echo "SUCCESS\n";
$log->logInfo("SUCCESS: rowCount == history count");
echo "rowCount = " . ($rowCount-2) . "\n";
echo "signal count = " . count($history) . "\n";
$log->logInfo("history", $history);
$log->logInfo("number of rain signals: " . count($history));
}
storetoDB();
function storetoDB(){
global $history;
global $log;
global $dbConfig;
$link = connectToDBMYSQLI($dbConfig['tiberius-hk-test'], __FILE__, true);
foreach ($history as $arr) {
//print_r($arr);
$startDTStr = $arr['startDT']->format('Y-m-d H:i');
$endDTStr = $arr['endDT']->format('Y-m-d H:i');
$durationStr = $arr['duration'];
$sig = $arr['sig'];
$name = $arr['name'];
$intensity = $arr['intensity'];
$year = $arr['startDT']->format('Y');
$sql="INSERT IGNORE INTO `HKOWarningsHist` (`code`, `startTime`, `endTime`, `duration`, `name`, `intensity`, `year`) VALUES ( $sig, '$startDTStr', '$endDTStr', '$durationStr', '$name', '$intensity', '$year' )";
$log->logDebug("sql = $sql");
if (!$result = mysqli_query($link, $sql)) {
echo "ERROR: store TC Hist script failed insert row. SQL: " . $sql . "\n";
$log->logError("store TC Hist script failed insert row. SQL: $sql");
die('Invalid query: ' . mysqli_error($link));
}
}
}
function getDateTime($time, $date) {
global $log;
$newDate = DateTime::createFromFormat('j/M/Y H:i', "$date $time");
if (!is_object($newDate)) {
$log->logError("Invalid date/time string");
throw new Exception('Invalid date/time string');
}
return $newDate; //->format('Y-m-d H:i');
}
// function getTimeFromDuration($duration) {
// $newDate = DateTime::createFromFormat('j/M/Y H i', "01/Jan/1970 $duration");
// if (!is_object($newDate)) {
// $log->logError("Invalid date/time string");
// throw new Exception('Invalid date/time string');
// }
// return $newDate; //->format('Y-m-d H:i');
// }
function getYear($date) {
$newDate = DateTime::createFromFormat('Y-m-d H:i', "$date");
if (!is_object($newDate)) {
$log->logError("Invalid date string");
throw new Exception('Invalid date string');
}
return $newDate->format('Y');
}
function getHTML(){
//skipping this for the gist - just too big
}
?>
<?php
/**
* Finally, a light, permissions-checking logging class.
*
* Originally written for use with wpSearch
*
* Usage:
* $log = new KLogger('/var/log/', KLogger::INFO);
* $log->logInfo('Returned a million search results'); //Prints to the log file
* $log->logFatal('Oh dear.'); //Prints to the log file
* $log->logDebug('x = 5'); //Prints nothing due to current severity threshhold
*
* @author Kenny Katzgrau <[email protected]>
* @since July 26, 2008 — Last update July 1, 2012
* @link http://codefury.net
* @version 0.2.0
*/
/**
* Class documentation
*/
class KLogger
{
/**
* Error severity, from low to high. From BSD syslog RFC, secion 4.1.1
* @link http://www.faqs.org/rfcs/rfc3164.html
*/
const EMERG = 0; // Emergency: system is unusable
const ALERT = 1; // Alert: action must be taken immediately
const CRIT = 2; // Critical: critical conditions
const ERR = 3; // Error: error conditions
const WARN = 4; // Warning: warning conditions
const NOTICE = 5; // Notice: normal but significant condition
const INFO = 6; // Informational: informational messages
const DEBUG = 7; // Debug: debug messages
//custom logging level
/**
* Log nothing at all
*/
const OFF = 8;
/**
* Alias for CRIT
* @deprecated
*/
const FATAL = 2;
/**
* Internal status codes
*/
const STATUS_LOG_OPEN = 1;
const STATUS_OPEN_FAILED = 2;
const STATUS_LOG_CLOSED = 3;
/**
* We need a default argument value in order to add the ability to easily
* print out objects etc. But we can't use NULL, 0, FALSE, etc, because those
* are often the values the developers will test for. So we'll make one up.
*/
const NO_ARGUMENTS = 'KLogger::NO_ARGUMENTS';
/**
* Current status of the log file
* @var integer
*/
private $_logStatus = self::STATUS_LOG_CLOSED;
/**
* Holds messages generated by the class
* @var array
*/
private $_messageQueue = array();
/**
* Path to the log file
* @var string
*/
private $_logFilePath = null;
/**
* Current minimum logging threshold
* @var integer
*/
private $_severityThreshold = self::INFO;
/**
* This holds the file handle for this instance's log file
* @var resource
*/
private $_fileHandle = null;
/**
* use the date() function in filename and logline
* set to false to avoid strict timezone errors
* @var bool
*/
private $_useDate = true;
/**
* Standard messages produced by the class. Can be modified for il8n
* @var array
*/
private $_messages = array(
//'writefail' => 'The file exists, but could not be opened for writing. Check that appropriate permissions have been set.',
'writefail' => 'The file could not be written to. Check that appropriate permissions have been set.',
'opensuccess' => 'The log file was opened successfully.',
'openfail' => 'The file could not be opened. Check permissions.',
);
/**
* Default severity of log messages, if not specified
* @var integer
*/
private static $_defaultSeverity = self::DEBUG;
/**
* Valid PHP date() format string for log timestamps
* @var string
*/
private static $_dateFormat = 'Y-m-d G:i:s';
/**
* Octal notation for default permissions of the log file
* @var integer
*/
private static $_defaultPermissions = 0777;
/**
* Array of KLogger instances, part of Singleton pattern
* @var array
*/
private static $instances = array();
/**
* Partially implements the Singleton pattern. Each $logDirectory gets one
* instance.
*
* @param string $logDirectory File path to the logging directory
* @param integer $severity One of the pre-defined severity constants
* @param bool $useDate use the date() function in filename and logline
* @return KLogger
*/
public static function instance($logDirectory = false, $severity = false, $useDate = true)
{
if ($severity === false) {
$severity = self::$_defaultSeverity;
}
if ($logDirectory === false) {
if (count(self::$instances) > 0) {
return current(self::$instances);
} else {
$logDirectory = dirname(__FILE__);
}
}
if (in_array($logDirectory, self::$instances)) {
return self::$instances[$logDirectory];
}
self::$instances[$logDirectory] = new self($logDirectory, $severity, $useDate);
return self::$instances[$logDirectory];
}
/**
* Class constructor
*
* @param string $logDirectory File path to the logging directory
* @param integer $severity One of the pre-defined severity constants
* @param bool $useDate use the date() function in filename and logline
* @return void
*/
public function __construct($logDirectory, $severity, $useDate )
{
$logDirectory = rtrim($logDirectory, '\\/');
if ($severity === self::OFF) {
return;
}
$this->_useDate = $useDate;
$this->_logFilePath = $logDirectory
. DIRECTORY_SEPARATOR
. 'log_'
. (($useDate===true) ? date('Y-m-d') : 'klogger')
. '.txt';
$this->_severityThreshold = $severity;
if (!file_exists($logDirectory)) {
mkdir($logDirectory, self::$_defaultPermissions, true);
}
if (file_exists($this->_logFilePath) && !is_writable($this->_logFilePath)) {
$this->_logStatus = self::STATUS_OPEN_FAILED;
$this->_messageQueue[] = $this->_messages['writefail'];
return;
}
if (($this->_fileHandle = fopen($this->_logFilePath, 'a'))) {
$this->_logStatus = self::STATUS_LOG_OPEN;
$this->_messageQueue[] = $this->_messages['opensuccess'];
} else {
$this->_logStatus = self::STATUS_OPEN_FAILED;
$this->_messageQueue[] = $this->_messages['openfail'];
}
}
/**
* Class destructor
*/
public function __destruct()
{
if ($this->_fileHandle) {
fclose($this->_fileHandle);
}
}
/**
* Writes a $line to the log with a severity level of DEBUG
*
* @param string $line Information to log
* @return void
*/
public function logDebug($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::DEBUG);
}
/**
* Returns (and removes) the last message from the queue.
* @return string
*/
public function getMessage()
{
return array_pop($this->_messageQueue);
}
/**
* Returns the entire message queue (leaving it intact)
* @return array
*/
public function getMessages()
{
return $this->_messageQueue;
}
/**
* Empties the message queue
* @return void
*/
public function clearMessages()
{
$this->_messageQueue = array();
}
/**
* Sets the date format used by all instances of KLogger
*
* @param string $dateFormat Valid format string for date()
*/
public static function setDateFormat($dateFormat)
{
self::$_dateFormat = $dateFormat;
}
/**
* Writes a $line to the log with a severity level of INFO. Any information
* can be used here, or it could be used with E_STRICT errors
*
* @param string $line Information to log
* @return void
*/
public function logInfo($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::INFO, $args);
}
/**
* Writes a $line to the log with a severity level of NOTICE. Generally
* corresponds to E_STRICT, E_NOTICE, or E_USER_NOTICE errors
*
* @param string $line Information to log
* @return void
*/
public function logNotice($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::NOTICE, $args);
}
/**
* Writes a $line to the log with a severity level of WARN. Generally
* corresponds to E_WARNING, E_USER_WARNING, E_CORE_WARNING, or
* E_COMPILE_WARNING
*
* @param string $line Information to log
* @return void
*/
public function logWarn($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::WARN, $args);
}
/**
* Writes a $line to the log with a severity level of ERR. Most likely used
* with E_RECOVERABLE_ERROR
*
* @param string $line Information to log
* @return void
*/
public function logError($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::ERR, $args);
}
/**
* Writes a $line to the log with a severity level of FATAL. Generally
* corresponds to E_ERROR, E_USER_ERROR, E_CORE_ERROR, or E_COMPILE_ERROR
*
* @param string $line Information to log
* @return void
* @deprecated Use logCrit
*/
public function logFatal($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::FATAL, $args);
}
/**
* Writes a $line to the log with a severity level of ALERT.
*
* @param string $line Information to log
* @return void
*/
public function logAlert($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::ALERT, $args);
}
/**
* Writes a $line to the log with a severity level of CRIT.
*
* @param string $line Information to log
* @return void
*/
public function logCrit($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::CRIT, $args);
}
/**
* Writes a $line to the log with a severity level of EMERG.
*
* @param string $line Information to log
* @return void
*/
public function logEmerg($line, $args = self::NO_ARGUMENTS)
{
$this->log($line, self::EMERG, $args);
}
/**
* Writes a $line to the log with the given severity
*
* @param string $line Text to add to the log
* @param integer $severity Severity level of log message (use constants)
*/
public function log($line, $severity, $args = self::NO_ARGUMENTS)
{
if ($this->_severityThreshold >= $severity) {
$status = $this->_getTimeLine($severity);
$line = "$status $line";
if($args !== self::NO_ARGUMENTS) {
/* Print the passed object value */
$line = $line . '; ' . var_export($args, true);
}
$this->writeFreeFormLine($line . PHP_EOL);
}
}
/**
* Writes a line to the log without prepending a status or timestamp
*
* @param string $line Line to write to the log
* @return void
*/
public function writeFreeFormLine($line)
{
if ($this->_logStatus == self::STATUS_LOG_OPEN
&& $this->_severityThreshold != self::OFF) {
if (fwrite($this->_fileHandle, $line) === false) {
$this->_messageQueue[] = $this->_messages['writefail'];
}
}
}
private function _getTimeLine($level)
{
//$time = date(self::$_dateFormat);
$time = (($this->_useDate===true) ? date(self::$_dateFormat) : 'NODATE');
switch ($level) {
case self::EMERG:
return "$time - EMERG -->";
case self::ALERT:
return "$time - ALERT -->";
case self::CRIT:
return "$time - CRIT -->";
case self::FATAL: # FATAL is an alias of CRIT
return "$time - FATAL -->";
case self::NOTICE:
return "$time - NOTICE -->";
case self::INFO:
return "$time - INFO -->";
case self::WARN:
return "$time - WARN -->";
case self::DEBUG:
return "$time - DEBUG -->";
case self::ERR:
return "$time - ERROR -->";
default:
return "$time - LOG -->";
}
}
}
<?php
function safeSetTZ(){
global $log;
// set up log file
$log->logDebug('Setting error handler');
// to catch all errors, warnings and notices
// we use our own error handler
set_error_handler('handleError');
// then call date_default_timezone_get in try/catch
// date_default_timezone_get raises a strict warning
// if date.timezone is not set
// so if it's not set, we catch the exception here
// and try setting date.timezone another way
try{
$log->logDebug('Trying setTZ()');
setTZ();
}
catch (ErrorException $e) {
$log->logDebug('setTZ() exception, do setTZ2()');
setTZ2();
}
$log->logDebug('Resetting error handler');
// Reset error handler
// we don't know how often we'd need to call restore_error_handler()
// to restore the built-in error handler, so just set anon func
set_error_handler(function() {
return false;
});
// date.timezone should be set now
$tz = date_default_timezone_get();
$log->logInfo("date.timezone = $tz");
return $tz;
}
function handleError($errno, $errstr, $errfile, $errline, array $errcontext)
{
// error was suppressed with the @-operator
if (0 === error_reporting()) {
return false;
}
throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
function setTZ(){
global $log;
if (date_default_timezone_get()) {
$log->logInfo('date.timezone = ' . date_default_timezone_get());
}
else{
$log->logWarn("Shouldn't get here I don't think.");
setTZ2();
}
}
function setTZ2(){
global $log;
$localtime = '/etc/localtime';
$tmpFile="";
if ( file_exists($localtime) ){
$log->logDebug("file_exists: $localtime");
// all these ifs could be put together on one line
// so we wouldn't have so many else{} statements...
if (is_link($localtime)) {
$log->logDebug("is_link: $localtime");
$tmpFile = readlink($localtime);
if($tmpFile != ""){
$log->logDebug("readlink: $tmpFile");
$tmpFile = str_replace('/usr/share/zoneinfo/', '', $tmpFile);
$log->logDebug("after str_replace: $tmpFile");
$timezone_identifiers = DateTimeZone::listIdentifiers();
if (in_array($tmpFile, $timezone_identifiers)) {
$log->logDebug("Found TZ in timezone_identifiers: $tmpFile");
$log->logInfo("Setting date.timezone to $tmpFile");
date_default_timezone_set($tmpFile);
}
else{
tryOffset();
}
}
else{
tryOffset();
}
}
else {
tryOffset();
}
}
else{
tryOffset();
}
}
function tryOffset(){
global $log;
$log->logWarn("/etc/localtime method failed, trying offset");
$offset = exec('date +%z');
//$offset = "";
if($offset != null && $offset != ""){
$log->logDebug("Got offset: $offset");
$sign = substr($offset, -5, 1);
$hh = intval(substr($offset, -4, 2));
$mm = intval(substr($offset, -2, 2));
$seconds = ($hh * 3600) + ($mm * 60);
if($sign == "-")
{
$seconds = -$seconds;
}
$log->logDebug(sprintf("%5s => %d seconds", $offset, $seconds));
$tzName = tz_offset_to_name($seconds);
if($tzName != FALSE){
$log->logDebug("Got tzName from seconds offset: $tzName");
$log->logInfo("Setting date.timezone to $tzName");
date_default_timezone_set($tzName);
}
else{
$log->logWarn("Failed to get tzName from seconds offset: $seconds");
setUTC();
}
}
else{
$log->logWarn("Failed to get offset from date +%z command");
setUTC();
}
}
//timezone_name_from_abbr() sometimes returns FALSE instead of an actual timezone: http://bugs.php.net/44780
/* Takes a GMT offset (in seconds) and returns a timezone name */
function tz_offset_to_name($offset)
{
$abbrarray = timezone_abbreviations_list();
foreach ($abbrarray as $abbr)
{
foreach ($abbr as $city)
{
if ($city['offset'] == $offset)
{
return $city['timezone_id'];
}
}
}
return FALSE;
}
function setUTC(){
global $log;
$log->logWarn("Setting date.timezone to default UTC");
date_default_timezone_set('UTC');
}
?>
<?php
function checkDNSForDomain($domain){
// get the domain
$parse = parse_url($domain);
//echo "domains is ". $parse['host']. "\n";
$result = dns_get_record($parse['host'], DNS_ALL);
$str = var_export($result, true);
return $str;
}
function dump($value,$level=0)
{
if ($level==-1)
{
$trans[' ']='&there4;';
$trans["\t"]='&rArr;';
$trans["\n"]='&para;;';
$trans["\r"]='&lArr;';
$trans["\0"]='&oplus;';
return strtr(htmlspecialchars($value),$trans);
}
if ($level==0) echo '<pre>';
$type= gettype($value);
echo $type;
if ($type=='string')
{
echo '('.strlen($value).')';
$value= dump($value,-1);
}
elseif ($type=='boolean') $value= ($value?'true':'false');
elseif ($type=='object')
{
$props= get_class_vars(get_class($value));
echo '('.count($props).') <u>'.get_class($value).'</u>';
foreach($props as $key=>$val)
{
echo "\n".str_repeat("\t",$level+1).$key.' => ';
dump($value->$key,$level+1);
}
$value= '';
}
elseif ($type=='array')
{
echo '('.count($value).')';
foreach($value as $key=>$val)
{
echo "\n".str_repeat("\t",$level+1).dump($key,-1).' => ';
dump($val,$level+1);
}
$value= '';
}
echo " <b>$value</b>";
if ($level==0) echo '</pre>';
}
function GetBetween($content,$start,$end){
$r = explode($start, $content);
//print_r($r);
if (isset($r[1])){
// echo "\nisset\n";
$r = explode($end, $r[1]);
return $r[0];
}
return '';
}
function GetBetweenCN($content,$start,$end){
mb_internal_encoding("UTF-8");
$startPos = mb_strpos($content, $start, 0, "UTF-8" );
if($startPos == false){
return '';
}
$startLen = mb_strlen($start, "UTF-8");
$endPos = mb_strpos($content, $end, $startPos, "UTF-8" );
if($endPos == false){
return '';
}
$subStr = mb_substr($content, ($startPos+$startLen), ($endPos-$startPos-$startLen),"UTF-8" );
if (isset($subStr)){
return $subStr;
}
else{
return '';
}
}
function array_in_array($needles, $haystack) {
foreach ($needles as $needle) {
if ( in_array($needle, $haystack) ) {
return true;
}
}
return false;
}
function CURL($url, $retries = 3)
{
global $httpCode;
global $curl_error_str;
global $curl_error_no;
global $timeout;
global $curl_info;
global $connectionTimeout;
global $log;
//print_r($timeout);
$curl = curl_init($url);
if (is_resource($curl) === true)
{
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $connectionTimeout);
//curl_setopt($curl, CURLOPT_CONNECTTIMEOUT_MS, 2);
curl_setopt($curl, CURLOPT_NOSIGNAL, 1);
// curl_setopt($curl, CURLOPT_VERBOSE, true);
$result = false;
while (($result === false) && (--$retries > 0))
{
//echo "CURL retries = $retries\n";
$result = curl_exec($curl);
}
$curl_info_temp = curl_getinfo($curl);
$log->logInfo("curl_info", $curl_info_temp);
if(!$result){
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
$curl_info_temp = curl_getinfo($curl);
if(is_array($curl_info_temp) == TRUE){
$curl_info = var_export($curl_info_temp, 1);
}
}
// Check if any error occured
if(curl_errno($curl))
{
$curl_error_str = curl_error($curl);
$curl_error_no = curl_errno($curl);
//echo 'Curl error: ' . curl_error($curl);
//echo 'Curl error no: ' . curl_errno($curl);
}
curl_close($curl);
}
else{
$curl_error_str = "curl_init() failed for $url";
}
return $result;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment