#
#
# General functions to be used in various projects including word press custom pages.
#
#
# Formats a GET / POST string for HTML output eg removes <> etc..
function qs4html($m)
{
$m = forHTML($m);
return $m;
}
# for value from db to screen
function forHTML($m)
{
$m = str_replace('&', '&', $m);
$m = str_replace('"', '"', $m);
$m = str_replace('\'', ''', $m);
$m = str_replace('>', '>', $m);
$m = str_replace('<', '<', $m);
$m = str_replace('<', '<', $m);
$m = str_replace("\n", '
', $m);
return trim($m);
}
#
# Form Functions
#
# this function will work out if a form has been reposted.
# if it has been reposted it will return the posted field value
# if it has not been reposted then it will return the $value field
# used for forms with load, or defaults.
function formFieldValue($name, $value)
{
if (Q($name) <> "")
{
return repost($name);
} else
{
return qs4html($value);
}
}
# Returns a post ready for repopulating an input e.g. value=" echo repost("name"); "
function repost($m)
{
if (isSet($_POST[$m]) || isSet($_GET[$m])) return qs4html(Q($m));
return "";
}
# Used in drop down lists
function isSelected($a, $b)
{
if($a == $b) { return " SELECTED ";}
return "";
}
# Returns the input whether from a post or get variable
# Ignores that value if it is the same as a watermark value
function Q($m)
{
$Q = "";
if (isset($_POST[$m])) { $Q = trim($_POST[$m]); }
elseif (isset($_GET[$m])) { $Q = trim($_GET[$m]); }
if (isset($_POST[$m."WaterMark"]) && $_POST[$m."WaterMark"] == $Q) { $Q = ""; }
return $Q;
}
function postOrGet($m)
{
if (isset($_POST[$m])) { return trim($_POST[$m]); }
elseif (isset($_GET[$m])) { return trim($_GET[$m]); }
return $Q;
}
function plural($number, $plural = "s", $singular="")
{
if ($number <> 1) return $plural;
return $singular;
}
#
# Misc formatting functions
#
# Friendly date representation, Today, yesterday etc..
function dbDateInRelativeWords($m, $includeYear = false)
{
$when = "";
$when = date("d/m/Y", strtotime($m));
if ($when == date("d/m/Y")) { $when = "Today"; }
elseif ($when == date('d/m/Y', strtotime("-1 day"))) { $when = "Yesterday"; }
else
{
$when = date("D d M", strtotime($m));
if ($includeYear) { $when .= " " . date("y", strtotime($m)); }
}
return $when;
}
# Friendly date representation, Today, yesterday etc..
function dbTime($m)
{
return date("H:m", strtotime($m));
}
# Returns a number or 0 if not a number
function strToNum($m)
{
if (is_numeric($m)) { return $m; }
return 0;
}
#
# Misc
#
# Default a blank session var to ""
function getSessionVar($str)
{
if(isSet($_SESSION[$str]))
{
return trim($_SESSION[$str]);
}
else return "";
}
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
# Returns the domain name
function getDomain()
{
$pageUR1 = ereg_replace("/(.+)", "", $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]);
$curdomain = str_replace("www.", "", $pageUR1);
return $curdomain;
}
?>
