"While updating on some delightfully unorganized PHP code (no indentation at all, split over hundreds of randomly named files, many included dozens of times), I kept running across comparisons such as:
if (MatchField($login->Value("admin"), "==", "1")) {
"and
if (IsTrue($admin)) {
"I assumed these must have some complicated logic behind them that could be the source of the bug I was hunting down," Ben writes, "so I popped open the include to try to debug them, only to find this:
function MatchField($field, $cond, $value)
{
if ($cond == '<')
return $field < $value;
if ($cond == '<=')
return $field <= $value;
if ($cond == '==')
return $field == $value;
if ($cond == '!=')
return $field != $value;
if ($cond == '>=')
return $field >= $value;
if ($cond == '>')
return $field > $value;
if ($cond == 'like')
return indexof("$field", "$value") >= 0;
if ($cond == 'in')
return indexof("$value", "$field") >= 0;
return false;
}
"(and yes, this developer's indexof() method is a poorly written duplicate of strpos())
function IsTrue($expression)
{
return $expression;
}
"And for good measure, a few more gems:
function ReturnTrue()
{
return true;
}
function truth($fieldValue)
{
if ($fieldValue == "no" || $fieldValue == "false" || $fieldValue == "off") {
return false;
} else {
return $fieldValue;
}
}
// -----------------------------------------------------------------------------
// These functions allow the passing of sensitive information over a URL. They
// store the infomation in the session, and pass a randomly-generated key to it.
function obscure($plaintext)
{
global $obscureData;
reset($obscureData);
while (list($key, $value) = each($obscureData)) {
if ($value == $plaintext) {
return $key;
}
}
//
// Not found; need to add...
//
$key = rand();
$obscureData[$key] = urlencode($plaintext);
return $key;
}
function unobscure($key)
{
global $obscureData;
return urldecode($obscureData[$key]);
}
function GetFormValue($name)
{
global $HTTP_GET_VARS;
global $HTTP_POST_VARS;
if (isset($HTTP_GET_VARS[$name])) {
return stripslashes($HTTP_GET_VARS[$name]);
} else if (isset($HTTP_POST_VARS[$name])) {
return stripslashes($HTTP_POST_VARS[$name]);
} else if (isset($HTTP_GET_VARS["~$name"])) {
return stripslashes($HTTP_GET_VARS["~$name"]);
} else if (isset($HTTP_POST_VARS["~$name"])) {
return stripslashes($HTTP_POST_VARS["~$name"]);
} else if (isset($HTTP_GET_VARS[$name . "_x"])) {
return "image button hit";
} else if (isset($HTTP_POST_VARS[$name . "_y"])) {
return "image button hit";
} else {
return "_no_form_value";
}
}
"...And my personal favorite:"
// Split up a string of the form "arg1=val1&...&argN=valN" into an array
// of argument value pairs.
function queryStringToArray($queryString)
{
global $GL_strlen;
global $GL_strpos;
if ($GL_strlen($queryString) == 0) {
return array();
}
$queryArray = array();
$args = explode("&", $queryString);
for ($i = 0; $i < sizeof($args); $i++) {
if ($GL_strpos($args[$i], "=")) {
list($param, $value) = explode("=", $args[$i]);
$queryArray[$param] = $value;
} else {
$queryArray[$args[$i]] = "";
}
}
return $queryArray;
}
"You know, because why use $_GET, $_POST, or $_REQUEST? ...This is going to be a long project."
Don't lose too much hope, Ben! Maybe you'll find some of those functions to be useful in general.