/*******************************************************************************
* ZIP Code and Distance Claculation Class
*******************************************************************************
* Author: Micah Carrick
* Email: email@micahcarrick.com
* Website: http://www.micahcarrick.com
*
* File: zipcode.class.php
* Version: 1.2.0
* Copyright: (c) 2005 - Micah Carrick
* You are free to use, distribute, and modify this software
* under the terms of the GNU General Public License. See the
* included license.txt file.
*
*******************************************************************************
* VERION HISTORY:
* v1.2.0 [Oct 22, 2006] - Using a completely new database based on user
contributions which resolves many data bugs.
- Added sorting to get_zips_in_range()
- Added ability to include/exclude the base zip
from get_zips_in_range()
* v1.1.0 [Apr 30, 2005] - Added Jeff Bearer's code to make it MUCH faster!
* v1.0.1 [Apr 22, 2005] - Fixed a typo :)
* v1.0.0 [Apr 12, 2005] - Initial Version
*
*******************************************************************************
* DESCRIPTION:
* A PHP Class and MySQL table to find the distance between zip codes and
* find all zip codes within a given mileage or kilometer range.
*
*******************************************************************************
*/
// constants for setting the $units data member
define('_UNIT_MILES', 'm');
define('_UNIT_KILOMETERS', 'k');
// constants for passing $sort to get_zips_in_range()
define('_ZIPS_SORT_BY_DISTANCE_ASC', 1);
define('_ZIPS_SORT_BY_DISTANCE_DESC', 2);
define('_ZIPS_SORT_BY_ZIP_ASC', 3);
define('_ZIPS_SORT_BY_ZIP_DESC', 4);
// constant for miles to kilometers conversion
define('_M2KM_FACTOR', 1.609344);
class zipcode_class {
var $last_error = ""; // last error message set by this class
var $last_time = 0; // last function execution time (debug info)
var $units = _UNIT_MILES; // miles or kilometers
var $decimals = 2; // decimal places for returned distance
function get_distance($zip1, $zip2) {
// returns the distance between to zip codes. If there is an error, the
// function will return false and set the $last_error variable.
$this->chronometer(); // start the clock
if ($zip1 == $zip2) return 0; // same zip code means 0 miles between. :)
// get details from database about each zip and exit if there is an error
$details1 = $this->get_zip_point($zip1);
$details2 = $this->get_zip_point($zip2);
if ($details1 == false) {
$this->last_error = "No details found for zip code: $zip1";
return false;
}
if ($details2 == false) {
$this->last_error = "No details found for zip code: $zip2";
return false;
}
// calculate the distance between the two points based on the lattitude
// and longitude pulled out of the database.
$miles = $this->calculate_mileage($details1[0], $details2[0], $details1[1], $details2[1]);
$this->last_time = $this->chronometer();
if ($this->units == _UNIT_KILOMETERS) return round($miles * _M2KM_FACTOR, $this->decimals);
else return round($miles, $this->decimals); // must be miles
}
function get_zip_details($zip) {
global $connection;
// This function pulls the details from the database for a
// given zip code.
$sql = "SELECT lat AS lattitude, lon AS longitude, city, county, state_prefix,
state_name, area_code, time_zone
FROM zip_code
WHERE zip_code='$zip'";
//echo $sql;
$r = mysqli_query($connection, $sql) or die("dead:".mysqli_connect_error($connection));
if (!$r) {
$this->last_error = '';
return false;
} else {
$row = mysqli_fetch_array($r, MYSQLI_ASSOC);
return $row;
}
}
function get_zip_point($zip) {
global $connection;
// This function pulls just the lattitude and longitude from the
// database for a given zip code.
$sql = "SELECT lat, lon from zip_code WHERE zip_code='$zip'";
$r = mysqli_query($connection, $sql);
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();}
if (!$r) {
$this->last_error = 'errorthis';
return false;
} else {
$row = mysqli_fetch_array($r);
return $row;
}
}
function calculate_mileage($lat1, $lat2, $lon1, $lon2) {
// used internally, this function actually performs that calculation to
// determine the mileage between 2 points defined by lattitude and
// longitude coordinates. This calculation is based on the code found
// at http://www.cryptnet.net/fsp/zipdy/
// Convert lattitude/longitude (degrees) to radians for calculations
$lat1 = deg2rad($lat1);
$lon1 = deg2rad($lon1);
$lat2 = deg2rad($lat2);
$lon2 = deg2rad($lon2);
// Find the deltas
$delta_lat = $lat2 - $lat1;
$delta_lon = $lon2 - $lon1;
// Find the Great Circle distance
$temp = pow(sin($delta_lat/2.0),2) + cos($lat1) * cos($lat2) * pow(sin($delta_lon/2.0),2);
$distance = 3956 * 2 * atan2(sqrt($temp),sqrt(1-$temp));
return $distance;
}
function get_zips_in_range($zip, $range, $sort=_ZIPS_SORT_BY_DISTANCE_ASC, $include_base,$commonname) {
global $connection;
// returns an array of the zip codes within $range of $zip. Returns
// an array with keys as zip codes and values as the distance from
// the zipcode defined in $zip.
$this->chronometer(); // start the clock
$details = $this->get_zip_point($zip); // base zip details
if ($details == false) return false;
// This portion of the routine calculates the minimum and maximum lat and
// long within a given range. This portion of the code was written
// by Jeff Bearer (http://www.jeffbearer.com). This significanly decreases
// the time it takes to execute a query. My demo took 3.2 seconds in
// v1.0.0 and now executes in 0.4 seconds! Greate job Jeff!
// Find Max - Min Lat / Long for Radius and zero point and query
// only zips in that range.
$lat_range = $range/69.172;
$lon_range = abs($range/(cos($details[0]) * 69.172));
$min_lat = number_format($details[0] - $lat_range, "4", ".", "");
$max_lat = number_format($details[0] + $lat_range, "4", ".", "");
$min_lon = number_format($details[1] - $lon_range, "4", ".", "");
$max_lon = number_format($details[1] + $lon_range, "4", ".", "");
$return = array(); // declared here for scope
$sql = "SELECT zip_code, lat, lon,state_prefix,city FROM zip_code ";
if (!$include_base) $sql .= "WHERE zip_code <> '$zip' AND ";
else $sql .= "WHERE ";
$sql .= "lat BETWEEN '$min_lat' AND '$max_lat'
AND lon BETWEEN '$min_lon' AND '$max_lon'";
$r = mysqli_query($connection, $sql);
if (!$r) { // sql error
$this->last_error = 'thiserror';
return false;
} else {
while ($row = mysqli_fetch_row($r)) {
// loop through all 40 some thousand zip codes and determine whether
// or not it's within the specified range.
$dist = $this->calculate_mileage($details[0],$row[1],$details[1],$row[2]);
if ($this->units == _UNIT_KILOMETERS) $dist = $dist * _M2KM_FACTOR;
if ($dist <= $range) {
$return[str_pad($row[0], 5, "0", STR_PAD_LEFT)] = round($dist, $this->decimals);
$returnreplace[str_pad($row[0], 5, "0", STR_PAD_LEFT)] = $row[4].','.$row[3];
$returnreplacelatlong[str_pad($row[0], 5, "0", STR_PAD_LEFT)] = $row[4].','.$row[3].','.$row[1].','.$row[2];
$returnreplacearray[str_pad($row[0], 5, "0", STR_PAD_LEFT)] = array('City'=>$row[4],'State'=>$row[3],'lat'=>$row[1],'Long'=>$row[2]);
}
}
}
// sort array
switch($sort)
{
case _ZIPS_SORT_BY_DISTANCE_ASC:
asort($return);
break;
case _ZIPS_SORT_BY_DISTANCE_DESC:
arsort($return);
break;
case _ZIPS_SORT_BY_ZIP_ASC:
ksort($return);
break;
case _ZIPS_SORT_BY_ZIP_DESC:
krsort($return);
break;
}
if($commonname==1){
foreach($return as $z => $a){
$return[$z]=$returnreplace[$z];
}
}
if($commonname==2){
foreach($return as $z => $a){
$return[$z]=$returnreplacearray[$z];
}
}
$this->last_time = $this->chronometer();
if (empty($return)) return false;
return $return;
}
function chronometer() {
// chronometer function taken from the php manual. This is used primarily
// for debugging and anlyzing the functions while developing this class.
$now = microtime(TRUE); // float, in _seconds_
$now = $now + time();
$malt = 1;
$round = 7;
if ($this->last_time > 0) {
/* Stop the chronometer : return the amount of time since it was started,
in ms with a precision of 3 decimal places, and reset the start time.
We could factor the multiplication by 1000 (which converts seconds
into milliseconds) to save memory, but considering that floats can
reach e+308 but only carry 14 decimals, this is certainly more precise */
$retElapsed = round($now * $malt - $this->last_time * $malt, $round);
$this->last_time = $now;
return $retElapsed;
} else {
// Start the chronometer : save the starting time
$this->last_time = $now;
return 0;
}
}
}
?>
class Calendar{
var $isemail;
function __construct(){
global $whichstr;
$dayofweek = date('w',time());
if($dayofweek!=1){
$whichstr = ' today ';
}
}
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
function caldraw($cat,$days,$start,$zip,$shift,$todisplay){
# caldraw($getcats[news_cat_id],'7',$_GET[m_time],$usezip,'','HTML');
global $connection;
global $whichstr;
$nowstart = time();
if($start!=''){ $whichstr = ' next monday '; $nowstart = $start; }
$today=date('m d y',strtotime('today'));
#echo $whichstr.$today;
#echo date('m d y',strtotime("+0 day $whichstr"));
$querycats = mysqli_query($connection,"SELECT * from news_categories where news_cat_id='$cat'");
$catname = mysqli_fetch_array($querycats);
$catname = $catname[news_cat_name];
####START HTML DISPLAY####
if($todisplay=='HTML'){
$caldata .= '
'.$catname.'
'."\n";
}
if($todisplay=='TXT'){
$caldata .=$catname."\n=========================\n\n";
}
for($i=0;$i<$days;$i++){
if($today==date('m d y',strtotime("+$i day ",$nowstart))){
#$color = '#FFCCCC'; $fcolor = '#000000';
}
else
{
$color = ''; $fcolor = '';
}
}
if($this->getevents(strtotime("today",$nowstart),$cat,'1',$zip,$days,$todisplay)!=''){
if($todisplay=='HTML'){
$caldata .=
'
This Weeks Events
'.$this->getevents(strtotime("today",$nowstart),$cat,'1',$zip,$days,$todisplay).'
';
}
if($todisplay=='TXT'){
$caldata .= "++THIS WEEKS EVENTS\n";
$caldata .= $this->getevents(strtotime("today",$nowstart),$cat,'1',$zip,$days,$todisplay);
}
}
/*if($this->getevents(strtotime("today",$nowstart),$cat,'2',$zip,$days,$todisplay)!=''){
if($todisplay=='HTML'){
$caldata .= '
Ongoing Events / Registrations
'.$this->getevents(strtotime("today",$nowstart ),$cat,'2',$zip,$days,$todisplay).'
';
}
if($todisplay=='TXT'){
$caldata .= "\n++ONGOING EVENTS/REGISTRATIONS\n";
$caldata .= $this->getevents(strtotime("today",$nowstart ),$cat,'2',$zip,$days,$todisplay);
}
}
*/
if($this->getevents(strtotime("today",$nowstart ),$cat,'3',$zip,$days,$todisplay)!=''){
if($todisplay=='HTML'){
$caldata .= '
Next Few Weeks
'.$this->getevents(strtotime("today",$nowstart ),$cat,'3',$zip,$days,$todisplay).'
';
}
if($todisplay=='TXT'){
$caldata .="\n++NEXT FEW WEEKS\n";
$caldata .= $this->getevents(strtotime("today",$nowstart),$cat,'3',$zip,$days,$todisplay);
}
}
## | | ADS |
if($todisplay=='HTML'){
}
return $caldata;
}
##########FET EVENTS FUNCTION############
function getevents($whichtime,$whichcat,$whichmode,$basezip,$length,$howdisplay){
file_put_contents('/tmp/variable.txt','getevents '.$whichtime.$whichcat.$whichmode.$basezip.$length.$howdisplay."\n\n",FILE_APPEND);
#getevents(strtotime("today",$nowstart ),$cat,'3',$zip,$days,$todisplay)
#$tocal .= 'whichmode'.$whichmode.' '.date("m d y G:i a",$whichtime).' '.$whichtime;
global $sysvar;
global $connection;
$qispaid = mysqli_query($connection,"SELECT paid_cat from news_categories where news_cat_id='$whichcat'");
$rispaid = mysqli_fetch_array($qispaid);
if($rispaid[paid_cat]=='Y'){
$forpaid = "and news_paid='P' ";
}
else
{
}
if($whichmode==1){
##$tocal .= 'amode1';
if($rispaid[paid_cat]=='N'){
$query = "SELECT * from news_items LEFT JOIN organizations on news_items.news_organization=organizations.org_id where news_story_released='Y' and news_category='$whichcat'
and
(
(news_expiration>=$whichtime and news_expiration<($whichtime+(86399*$length))) or
(news_start_date>=$whichtime and news_start_date<($whichtime+(86399*$length))) or
(news_start_date<=$whichtime and news_expiration>=($whichtime+(86399*$length)))
)
and news_basezip='$basezip' order by news_expiration";
}
if($rispaid[paid_cat]=='Y'){
$query = "SELECT * from news_items LEFT JOIN organizations on news_items.news_organization=organizations.org_id where news_story_released='Y' and news_category='$whichcat'
and
(
(news_expiration>=$whichtime and news_start_listing<=$whichtime )
)
and news_basezip='$basezip' and news_paid='P' order by news_expiration";
}
file_put_contents('/tmp/variable.txt','PAIDCAT:'.$query."\n\n",FILE_APPEND);
}
if($whichmode==2){
##$tocal .= 'mode2';
$endtime = $whichtime+(($length*86400)-1);
$query = "SELECT * from news_items LEFT JOIN organizations on news_items.news_organization=organizations.org_id where news_story_released='Y' and news_category='$whichcat' and ((news_start_date>'$whichtime' and news_start_date<'$endtime') or (news_expiration>'$whichtime' and news_expiration<'$endtime')) and news_basezip='$basezip' and news_style='Multi-Day' $forpaid order by news_expiration";
}
if($whichmode==3){
##$tocal .= 'mode3';
$starttime = $whichtime+(($length*86400));
$endtime = $starttime+((($length+14)*86400));
if($rispaid[paid_cat]=='N'){
$query = "(SELECT * from news_items LEFT JOIN organizations on news_items.news_organization=organizations.org_id where news_story_released='Y' and news_category='$whichcat' and ((news_start_date>'$starttime' and news_start_date<'$endtime')) and news_basezip='$basezip' $forpaid ) order by news_expiration";
}
#$query = $query." order by news_expiration";
}
##begin whichmode 1
if($getlisting = mysqli_query($connection,$query)){
#$tocal .= $query;
if(mysqli_num_rows($getlisting)==0){return '';}
else
{
##begin while loop
while($listingret = mysqli_fetch_array($getlisting)){
if($howdisplay=='HTML'){
$tocal .= '';
###BLUE BOX
$tocal .= '
';
if($listingret[news_style]=='Multi-Day'){
$tocal .= strtoupper(date("D<\b\\r>M j<\b\\r>g:i a",$listingret[news_start_date]));
}
if($listingret[news_style]=='Single-Day' || $listingret[news_style]==''){
$tocal .= strtoupper(date("D<\b\\r>M j<\b\\r>g:i a",$listingret[news_start_date]));
}
$tocal .= '
';
##BLUEBOX CLOSE
#Whichtime='.date("m d y",$whichtime).'Starttime='.date("m d y",$starttime).' Endtime'.date("m d y",$endtime).'
'.$query;
$tocal .= '
'.$listingret[news_ids].$listingret[news_title].'
'.$listingret[org_name].' ▼
isemail!='true'){
$tocal .= ' class="biginfo" >';
error_log("is set to true",0);
}else{
error_log("is set to false",0);
$tocal .=' >';
}
$getseries = mysqli_query($connection,"SELECT * From news_items where series_id='$listingret[series_id]'");
if(mysqli_num_rows($getseries)>1){ $tocal .= '
# EVENTS:
'.mysqli_num_rows($getseries).' in this Series';
$tocal .= "
";
}
##open Dates
$tocal .='
TIME:
';
if($listingret[news_style]=='Multi-Day'){
$tocal .= date("D M j,y g:i a",$listingret[news_start_date]).'
'.date("D M j, y g:i a",$listingret[news_expiration]);
if(stripslashes($listingret[news_time])!=""){$tocal .=' | '. stripslashes($listingret[news_time]).' ';}
}
if($listingret[news_style]=='Single-Day' || $listingret[news_style]==''){
$tocal .= date("D M j,y g:i a",$listingret[news_start_date]).'
'.date("D M j, y g:i a",$listingret[news_expiration]);
if(stripslashes($listingret[news_time])!=""){$tocal .=' | '. stripslashes($listingret[news_time]).' ';}
}
$tocal .= '
';
##close Dates
##begin Address
if($listingret[news_location]!=''){
$tocal .= '
ADDRESS:
'.stripslashes($listingret[news_location]).', '.stripslashes($listingret[news_location_zip]).'
'.'MAP';
$tocal .= "
";
}
#if($listingret[news_admission]!=''){ $tocal .= '$'.stripslashes($listingret[news_admission]);}
##En ADdress
##begin Ticketing
if($listingret[news_needticketing]=='Y'){
$tocal .= "
#TICK/#LEFT:
";
#$bigtotal = mysqli_query($connection,"SELECT IFNULL(SUM(num_avail),0) as bigtotal FROM `news_ticket_breakdown` WHERE is_active='1' and news_series_id='".$listingret[series_id]."'");
$bigtotal = mysqli_query($connection,"SELECT IFNULL((news_tickets),0) as bigtotal FROM `news_items` WHERE series_id='".$listingret[series_id]."'");
$bigtotal2 = mysqli_fetch_assoc($bigtotal);
$tocal .= '#'.$bigtotal2[bigtotal] .' / ';
if(!$totals = mysqli_query($connection,"SELECT ((SELECT IFNULL((news_tickets),0) FROM `news_items` WHERE series_id='".$listingret[series_id]."')-(SELECT IFNULL(SUM(quantity),0) from ticket_ordering where (event_id='".$listingret[series_id]."' and refunded='0'))
) as Test")){
$tocal .= mysqli_error($totals);
}else{
#$tocal .= "success";
#$tocal .= mysqli_num_rows($totals);
$gettotal = mysqli_fetch_assoc($totals);
#$tocal .= print_r($getttoal);
$tocal .= $gettotal[Test];
}
$tocal .= '
';
}
##END TICKETING
##BEGIN PRICING
$getpricing = mysqli_query($connection,"SELECT * from news_ticket_breakdown where news_series_id='$listingret[series_id]' and is_active=1");
if($num = mysqli_num_rows($getpricing)>0){
$tocal .= '
PRICING:';
if($listingret[news_needticketing]=='Y'){
$tocal .= '
';
}
##close pricing block
$tocal .= '
';
if($num = mysqli_num_rows($getpricing)>0){
while($row=mysqli_fetch_assoc($getpricing)){
$tocal .= ''.$row[age_group_desc].': $'.$row[amount].'
';
}
}
$tocal .= '
';
$tocal .= '
';
}
##END PRICING
##BEGIN CONTACT
if($listingret[news_contact]!='' || $listingret[news_email]!='' || $listingret[news_phone]!='' || $listingret[news_web]!=''){
$tocal .= '
CONTACT:
';
if($listingret[news_contact]!=''){
$tocal .= stripslashes($listingret[news_contact]).'
';
}
if($listingret[news_email]!=''){ $tocal .= '
Email';}
if($listingret[news_web]!=''){ $tocal .='
WebSite';}
if($listingret[news_phone]!=''){$tocal .=''.$listingret[news_phone];}
$tocal .= '
';
}
##END CONTACT
##START PHOTO/TEXT
if($whichmode!=3){
$tocal .= '
';
if($listingret[news_image1]!='' && $whichmode!=3){
$tocal .= '
';
}
$tocal .= nl2br($listingret[news_story]);
$tocal .= '
';
}
$tocal .= '
';
if(1==2 && ($whichmode!='2' || $listingret[news_style]!='Multi-Day')){$tocal .='
ADD TO GOOGLE CALENDAR
| ';
}
$tocal .='
';
if($_SESSION[usr_id]!=''){
$tocal .= 'REMIND ME OF EVENT';
$tocal .='
';
}
else
{
$tocal .= '
REMIND ME OF EVENT';
$tocal .='
';
}
##added DIV
##close actionbox
$tocal .='
';
##close
$tocal .='
';
$tocal .='';
#$tocal .= '';
}
if($howdisplay=='TXT'){
$tocal .="------------------------------\n";
$tocal .="EVENT TITLE: ".$listingret[news_title]." -- ".$listingret[org_name]."\n";
$tocal .="EVENT TIME: ";
if($listingret[news_style]=='Multi-Day'){
$tocal .= date("D M j,y g:i a",$listingret[news_start_date]).' - '.date("D M j, y g:i a",$listingret[news_expiration]).' | '. stripslashes($listingret[news_time])."\n";
}
if($listingret[news_style]=='Single-Day' || $listingret[news_style]==''){
$tocal .= date("D M j,y g:i a",$listingret[news_start_date]).' - '.date("D M j, y g:i a",$listingret[news_expiration]).' | '. stripslashes($listingret[news_time])."\n";
}
if($listingret[news_location]!=''){
$tocal .= "NEWS LOCATION: ".stripslashes($listingret[news_location]).','.stripslashes($listingret[news_location_zip])."\n";
}
if($listingret[news_admission]!=''){ $tocal .= "ADMISSION: ".'$'.stripslashes($listingret[news_admission])."\n";}
if($listingret[news_contact]!=''){
$tocal .= "CONTACT:".stripslashes($listingret[news_contact])."\n";
}
if($listingret[news_email]!=''){ $tocal .= "EMAIL: ".$listingret[news_email]."\n";}
if($listingret[news_web]!=''){ $tocal .="WEBSITE: ".$this->addhttp(stripslashes($listingret[news_web]))."\n";}
if($listingret[news_phone]!=''){$tocal .="PHONE: ".$listingret[news_phone]."\n\n";}
$tocal .= nl2br($listingret[news_story])."\n";
$tocal .="------------------------------\n\n";
}
}
##END WHILE LOOP
return $tocal;
}
## END ELSE FOR QUERY
}
## END IN on INITAL QUERY
else
{
return mysqli_error($connection);
}
}
}
class ZipFunctions{
function ziptoName($wzip){
global $connection;
$query = mysqli_query($connection,"SELECT city,state_prefix from zip_code where zip_code='$wzip'");
$return = mysqli_fetch_array($query);
return $return[city].', '.$return[state_prefix];
}
}
function generatePassword($length) {
$salt = "abcdefghijklmnoprstuvwxy1234567890ABCDEFGHIJKLMNOPRSTUVWXY#!";
$i = 0;
while ($i < $length) {
$num = rand(0,63);
$tmp = substr($salt, $num, 1);
$pass = $pass . $tmp;
$i++;
}
return 'q'.$pass.'z';
}
?>