<?
define("SERVER","localhost");
define("USER","middleware_admin");
define("PASSWORD","1L0veraunak");
define("DB","middleware");
define("URL","https://www.reliableid.com/apply/");
define("EMAILFROM","Reliable ID Enrollments<securit@reliableid.com>");
define("EMAILSUBJECT","Enrollment documents");
define("OFFICERSUBJECT","New Enrollee for you");
define("OURLEGALNAME","Reliable ID");
define("GOOGLEKEY","ABQIAAAAlDqe0fX2HhjnltNo_fB04hQRMg6-gZ96b75SQzwcJL73ESLdJhTMAAgxfQZiGNqNpygFm72gq5UuFw");
define("UPLOADDIRECTORY",dirname($_SERVER["SCRIPT_FILENAME"])."/upload");
define("LOGFILE","/var/log/httpd/reliableid.log");

$script=$_SERVER["PHP_SELF"];
if ((isset($_SERVER["HTTPS"])) && ($_SERVER["HTTPS"] == "on"))
{
	header("location:https://".$_SERVER["HTTP_HOST"]."/$script");
	die("You should have come here with HTTPS, not HTTP.");
}
$logfilep=NULL;


function reporterror($where,$query="")
{
	$a="Error in $where";
	if (strlen($query)>0)
	{
		$a.=" with a query of<BR>$query";
		$a.="<BR>The reported error condition is<BR>".mysql_error();
	}
	return $a;
}

function loggywoggy($msg)
{
	return;
}


function show_fields($table)		#Might want to echo <PRE> around it.
{
$result = mysql_query("SHOW COLUMNS FROM $table");
	if (!$result)
	{
	    return 'Could not run query to show columns of the $table table: ' . mysql_error();
	}
	$a="";
	if (mysql_num_rows($result) > 0)
	{
		while ($row = mysql_fetch_assoc($result))
		{
			$a.=print_r($row,TRUE);
		}
	}
	return $a;
}

function spill_query_results($query,$title,$border=1,$limit=60)
{
	$res=mysql_query($query);
	if (!$res)
	{
		return reporterror("showing query results",$query);
	}
	$a="<H4>$title</H4><TABLE border=$border><TR>";
	$nf=mysql_num_fields($res);
	for ($i=0;$i<$nf;$i++) $a.="<TH>".mysql_field_name($res,$i)."</TH>";
	echo "</TR>";
	while ($row=mysql_fetch_array($res))
	{
		$a.="<TR>";
		for ($i=0;$i<$nf;$i++) $a.="<TD>".$row[$i]."</TD>";
		$a.="</TR>";
	}
	$a.="</TABLE>";
	return $a;
}

function spill_table($table,$border=1,$limit=4)
{
	$query="select * from $table limit $limit";
	return spill_query_results($query,"$table table",$border,$limit);
}

function if_selected($test)
{
	if ($test) return " SELECTED >";
	else return ">";
}

function spew_options($link,$query,$value,$display,$default)
{
	$res=mysql_query($query,$link);
	$a="";
	while ($row=mysql_fetch_array($res))
		$a.="<OPTION value=".$row[$value].if_selected($row[$value] == $default).$row[$display]."</OPTION>";
	return $a;
}

function mysql_getRow($query)
{
	$res=mysql_query($query);
	if (!$res) return NULL;
	if (is_null($res)) return NULL;
	if (mysql_num_rows($res) == 0) return NULL;
	$row=mysql_fetch_array($res);
	return $row;
}

function mysql_getOne($query)
{
	$row=mysql_getRow($query);
	if (is_null($row)) return NULL;
	return $row[0];
}

##getStateId will get the id for the state named state_name in country $country_id or insert a new line in the state table.
function getStateId($state_name,$country_id)
{
  if (strlen($state_name) > 0)
  {
    $state_sql="select * from state where (state_name like '%$state_name%' or state_code like '%$state_name%') and country_id=$country_id";
    $state=mysql_getRow($state_sql);
    if (!$state || is_null($state))
    {
      $state_sql="insert into state set state_name='$state_name',country_id=$country_id";
      mysql_query($state_sql);
      $state_id=mysql_insert_id();
      return $state_id;
    }
    else  #is_null($state)
      return $state["state_id"];
  }
  else    #strlen($state_name) > 0
    return 0;
}

##getCityId will get the city named $city_name in the state and country given orcreate a new row in the city table.
function getCityId($city_name,$state_id,$country_id)
{
  if (strlen($city_name) > 0)   #In future, we can try to deal with abbreviations like St. Peters.
  {
    $city_sql="select * from city where city_name like '%$city_name%' and country_code=$country_id";
    if ($state_id > 0) $city_sql.=" and state_id=$state_id";
    $city=mysql_getRow($city_sql);
    if (!$city || is_null($city))
    {
      $city_sql="insert into city set state_id=$state_id,	country_code=$country_id,	city_name='$city_name'";
      mysql_query($city_sql);
      $city_id=mysql_insert_id();
      return $city_id;
    }
    else    #is_null($city)
      return $city["city_id"];
  }
  else    #strlen($city_name) > 0
    return 0;
}

##I assemble a name from the incoming database $row
function makeName($row)
{
	$name=$row["fisrt_name"];
	if (strlen($row["middle_name"])> 0) $name.=" ".$row["middle_name"];
	$name.=" ".$row["last_name"];
	return $name;
}

##I assemble an address from the incoming database $row.  Other parameters tell me whether to include city, state, zip, and country
function makeAddress($row,$therest=0)
{
	$address=$row["street_line_01"];
	if(strlen($row["street_line_02"]) > 0) $address.="<BR>".$row["street_line_02"];
	if(strlen($row["street_line_03"]) > 0) $address.="<BR>".$row["street_line_03"];
	if ($threst > 0)
	{
		if (($therest & 1) == 1) $address.="<BR>".$row["city"];
		if (($therest & 2) == 2) $address.=", ".$row["state"];
		if (($therest & 4) == 4) $address.="  ".$row["zip_code"];
		if (($therest & 8) == 8) $address.="<BR>".$row["country"];
	}
	return $address;
}

##prettyDate will turn $date from 1948-9-1 to something more human looking.
function prettyDate($date)
{
	return $date;
}

#user_agent parses the user agent string to determine what operating system and browser are in use.
#it returns an array so it should be called as list($os,$br,$opsys,$browser_=user_agent();
if (!function_exists('user_agent'))
{
function user_agent()
{
	$agent=" ".$_SERVER["HTTP_USER_AGENT"];
	if (strpos($agent,"Konqueror") > 0) {$browser="Konqueror";$br="ko";}
	else if (strpos($agent,"MSIE 7.0") > 0) {$browser="InternetExplorer 7";$br="e7";}
	else if (strpos($agent,"MSIE") > 0) {$browser="InternetExplorer";$br="ie";}
	else if (strpos($agent,"Firefox") > 0) {$browser="Firefox";$br="ff";}
	else if (strpos($agent,"Mozilla") > 0) {$browser="Mozilla";$br="mo";}
	else if (strpos($agent,"Safari") > 0) {$browser="Safari";$br="sf";}
	else
	{
		PKH_log("Could not figure out browser from $agent.");
		$browser="dn";
	}

	if (strpos($agent,"Linux") > 0) $os="li";
	else if (strpos($agent,"FreeBSD") > 0) {$opsys="Free BSD";$os="fr";}
	else if (strpos($agent,"Windows NT 6.0") > 0) {$opsys="Windows Vista";$os="vi";}
	else if (strpos($agent,"Windows") > 0) {$opsys="Windows";$os="wi";}
	else if (strpos($agent,"Macintosh") > 0) {$opsys="Macintosh";$os="ma";}
	else
	{
		PKH_log("Could not find operating system from $agent.");
		$os="ig";
	}

	return array($os,$br,$opsys,$browser);
}
}
?>