<?php
class class_utility {
/*
Utility
Damon Vaughn Caskey
2013-01-09
Miscellaneous utility functions.
*/
public function utl_color_alternation($i, $cColorEven="#DDDDFF", $cColorOdd="#CECEFF")
{
/*
back_alt_0001 - https://www.caskeys.com/dc/?p=4900
Damon Vaughn Caskey
2012-10-18
Output alternating background colors for even/odd rows in tables or other types of layouts.
$i: Current row/location/count.
$cColorEven: Color to output if $i is an even number.
$cColorEven: Color to output if $i is an odd number.
*/
if($i%2) //Even number?
{
return $cColorEven; //Return even color.
}
else
{
return $cColorOdd; //Return odd color.
}
}
public function utl_get_get($cID, $cDefault=NULL)
{
/*
utl_get_get
Damon Vaughn Caskey
2013-01-01
Wrapper to obtain get value.
$cID: Index.
$cDefault: Default on not set.
*/
return $this->utl_validate_isset($_GET[$cID], $cDefault);
}
public function utl_get_post($cID, $cDefault=NULL)
{
/*
utl_get_post
Damon Vaughn Caskey
2013-01-01
Wrapper to obtain post value.
$cID: Index.
$cDefault: Default on not set.
*/
//echo $_POST[$cID];
return $this->utl_validate_isset($_POST[$cID], $cDefault);
}
public function utl_get_server_value($cID, $cDefault=NULL)
{
/*
utl_get_server_value
Damon Vaughn Caskey
2013-01-01
Wrapper to obtain server value.
$cID: Index.
$cDefault: Default on not set.
*/
return $this->utl_validate_isset($_SERVER[$cID], $cDefault);
}
public function utl_str_to_array($cList=NULL, $cDel=",")
{
/*
str_to_array
Damon Vaughn Caskey
2013-01-09
Break string into indexed array with no spaces.
$cList: List array to break up.
$cDel: Delimiter.
*/
/*
If list is populated remove spaces and break into array.
*/
if($cList) //List populated?
{
$cList = str_replace (" ", "", $cList); //Remove spaces.
$cList = explode($cDel, $cList); //Break into array.
}
/*
Return end result.
*/
return $cList;
}
public function utl_redirect($cURL=NULL)
{
/*
utl_redirect
Damon Vaughn Caskey
2013-01-09
Send header that redirects client to new page.
$cURL: Target address.
*/
/*
If headers haven't been sent, redirect user to an error page. Otherwise we'll just have to die and settle for a plain text message.
*/
if(headers_sent())
{
/*
Good coding will always avoid attempting to resend headers, but let's make sure to catch them here before PHP throws a nasty error.
*/
}
else
{
header('Location: '.$cURL);
return TRUE;
}
/*
Return end result.
*/
return FALSE;
}
public function utl_validate_email(&$cValue, $cDefault=NULL)
{
/*
utl_validate_email
Damon Vaughn Caskey
2013_01_01
Validate email variable.
$cValue: Email value.
$cDefault: Default on fail.
*/
if(filter_var($cValue, FILTER_VALIDATE_EMAIL))
{
list($user,$domaine)=explode("@", $cValue, 2);
if(!checkdnsrr($domaine, "MX")&& !checkdnsrr($domaine, "A"))
{
/*
Bad domain.
*/
$cValue = FALSE;
}
else
{
//Domain OK.
}
}
else
{
/*
Bad address.
*/
$cValue = FALSE;
}
if($cValue == FALSE)
{
$cValue = $cDefault;
}
return $cValue;
}
public function utl_validate_ip(&$cValue, $cDefault=NULL)
{
/*
utl_validate_ip
Damon Vaughn Caskey
2013-01-01
Validate ip variable.
$cValue: ip value.
$cDefault: Default on fail.
*/
$cValue = filter_var($cValue, FILTER_VALIDATE_IP);
if($cValue == FALSE)
{
$cValue = $cDefault;
}
return $cValue;
}
public function utl_validate_isset(&$cValue, $cDefault=NULL)
{
/*
utl_validate_isset
Damon Vaughn Caskey
2013-01-01
Return default if variable is not set.
$cValue: Value.
$cDefault: Default on not set.
*/
if(!isset($cValue))
{
$cValue = $cDefault;
}
return $cValue;
}
public function utl_if_exists($cValue, $cValPrefix=NULL, $cValCadence=NULL, $cAlt=NULL)
{
/*
utl_
Damon Vaughn Caskey
2013-01-01
Return self if self has a value, or $cAlt if $cValue is empty or null. Reduces need to retype and
potentiality mix up variable names or array keys twice in common "echo <X> if it has a value" situations.
Preconditions:
$cValue: Value to test and return if it exists.
$cValPrefix: Add to front of $cValue on return.
$cValCadence: Add to end of $cValue on return.
$cAlt: Value to return if $cValue is NULL.
*/
/* Vaiables */
$cReturn = $cAlt; //Final value to return.
/* Value exists? */
if($cValue)
{
/* Tack on additions and return value. */
$cReturn = $cValPrefix.$cValue.$cValCadence;
}
return $cReturn;
}
}
?>
Like this:
Like Loading...