<?php
abstract class PotPourri
{
// verify that a value lies within a range
public function is_between($TheVal,$TheBot,$TheTop)
{
return ($TheVal <= $TheTop && $TheVal >= $TheBot);
}
// verify that a value is a time stamp
// ASSUMPTION - a time stamp is assumed to be in the format YYYY-MM-DD HH:MM:SS
public function is_date($This1)
{
$TheMonthEnd=array(31,28,31,30,31,30,31,31,30,31,30,31);
$TheParts=explode(" ",$This1);
if(count($TheParts) >= 1)
{
$TheDate=explode("-",$TheParts[0]);
if(count($TheDate) == 3)
{
if(is_numeric($TheDate[0]) && is_numeric($TheDate[1]) && is_numeric($TheDate[2]))
{
if($this->is_between($TheDate[1],1,12))
{
if(!$this->is_between($TheDate[2],1,$TheMonthEnd[$TheDate[1]-1]))
{
if(!$TheDate[1] == 2 || !$TheDate[0] % 4 == 0 || $TheDate[2] != 29) return FALSE;
}
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
$TheTime=explode(":",$TheParts[1]);
if(count($TheDate) == 3)
{
if(is_numeric($TheTime[0]) && is_numeric($TheTime[1]) && is_numeric($TheTime[2]))
{
return ($this->is_between($TheTime[0],1,24) && $this->is_between($TheTime[1],0,59) && $this->is_between($TheTime[2],0,59));
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
else
{
return FALSE;
}
}
// verify that two time stamps (first and last) are in chronological sequence
// ASSUMPTION - all time stamps are in the format YYYY-MM-DD HH:MM:SS
public function is_before($TheFirst,$TheLast)
{
if(!$this->is_date($TheFirst) || !$this->is_date($TheLast)) return FALSE;
$TheParts1=explode(" ",$TheFirst);
$TheDate1=explode("-",$TheParts1[0]);
$TheTime1=explode(":",$TheParts1[1]);
$TheVal1=10000000000*$TheDate1[0]+ 100000000*$TheDate1[1] + 1000000*$TheDate1[2] + 10000*$TheTime1[0] + 100*$TheTime1[1] + $TheTime1[2];
$TheParts2=explode(" ",$TheLast);
$TheDate2=explode("-",$TheParts2[0]);
$TheTime2=explode(":",$TheParts2[1]);
$TheVal2=10000000000*$TheDate2[0]+ 100000000*$TheDate2[1] + 1000000*$TheDate2[2] + 10000*$TheTime2[0] + 100*$TheTime2[1] + $TheTime2[2];
return ($TheVal1 <= $TheVal2);
}
// convert an IP address in text format to Inet format
public function to_Inet($This1)
{
$This12=explode(".",$This1); $Next1=""; $Incr=1;
for($i=count($This12)-1;$i>=0;$i--)
{
$Next1+=$Incr*$This12[$i];
$Incr*=256;
}
return $Next1;
}
// convert an IP address in Inet format, represent as a hex string,
// into dot text format
public function to_IP_disp($This1)
{
$TheIP="";
for($i=0;$i<strlen($This1);$i=$i+2)
{
$TheSep="";
if($i < strlen($This1)-2) $TheSep=".";
$Next1=hexdec(substr($This1,$i,2));
$TheIP.=sprintf("%d",$Next1).$TheSep;
}
return $TheIP;
}
// determine the day of the week from any valid date format
public function DayOfWeek($TheDate)
{
if(is_array(date_parse($TheDate)))
{
$TheDetails=getdate(strtotime($TheDate));
return $TheDetails["weekday"];
}
else
{
return FALSE;
}
}
// determine the last date of a month for a given year
public function LastDay($TheMonth, $TheYear)
{
for($i=1;$i<=31 && checkdate($TheMonth, $i, $TheYear);$i++);
return $i-1;
}
public function TimeStampNow()
{
return date("Y-m-d H:i:s",time());
}
// retrieve a part of a time stamp using the name of the part
// (e.g. minutes, seconds, weekday, year, mday)
public function TimeNow($ThePart)
{
$TheDetails=getdate(time());
return $TheDetails[$ThePart];
}
// determine if a value is an IP address in text format
// NOTE - a second argument (true or false) determines whether
// to look for an IP address or a netmask.
public function is_IP($IPAddr,$NM)
{
$TheSegs=explode(".",$IPAddr);
if(count($TheSegs) == 4)
{
for($i=0;$i<4;$i++)
{
if(is_numeric($TheSegs[$i]))
{
$TheNum=$TheSegs[$i];
if(!$NM && ($TheNum < 0 || $TheNum > 254))
{
return FALSE;
}
else
{
if($NM && ($TheNum < 0 || $TheNum > 255)) return FALSE;
}
}
else
{
return FALSE;
}
}
return TRUE;
}
else
{
return FALSE;
}
}
// extract the 4-byte IEEE 754 format of a floating point value
// NOTE - if the value entered is not a floating point number, the function returns false
public function IEEE754($fIn)
{
$exp = 0;
if(!is_float($fIn)) return FALSE;
if($fIn == 1)
{
$val = 0x3f800000;
return $val;
}
// IEEE 754 floating point format:
// bit 31 - sign
// bits 30-23 - exponent (E section)
// bits 22-0 - fraction (F section)
// .... determine sign (S) section
$list = "0"; if($fIn < 0) $list = "1";
if($fIn != 0)
{
// .... get the value without the sign
$aValIn = abs($fIn);
// .... compute the exponent (E) section
$d1 = $aValIn;
if($d1 >= 2)
{
while($d1 >= 2)
{
$d1 = $d1 / 2;
$exp++;
}
}
else
{
if($d1 < 1)
{
while($d1<1)
{
$d1 *= 2;
$exp--;
}
}
}
$exp += 127; $list .= sprintf("%08b",$exp);
// .... compute the exponent (F) section
$dPart = $d1 -1; $indx = 0;
while($dPart > 0 && $indx < 23)
{
$dPart *= 2; $indx++;
if($dPart >=1)
{
$list .= "1"; $dPart--;
}
else
{
$list .= "0";
}
}
}
else
{
for($i=0;$i<32;$i++) $list .= "0";
}
$val = ""; $hlist = array("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F");
for($i=strlen($list)-1;$i >= 0;$i=$i-4)
{
$k1 = -1; $x=0;
for($j=0;$j<4 && $i >= $j;$j++)
{
$k1++; $k2 = pow(2,$k1);
$x += $list[$i - $j] * $k2;
}
$val = $hlist[$x].$val;
}
$val = "0x".$val + 0x00;
return $val;
}
// extract all keys associated with a value in an array
public function KeyOf($val, $list)
{
$res = "";
if(is_array($list))
{
$k=-1;
for($i=0;$i<count($list);$i++)
{
$This1 = current($list);
if ($This1 == $val)
{
$k++; $res[$k] = key($list);
}
next($list);
}
}
return $res;
}
}
?>