$val) { if (strpos($key, "OR") === 0) { checkVars($vars[$key], true); continue; } // Only check type of optional variables if they're set, and don't // mark them as bad if they're not set if (strpos($key, " (optional)") !== false) { $key = str_replace(" (optional)", "", $key); if (empty($VARS[$key])) { continue; } } else { if (empty($VARS[$key])) { $ok[$key] = false; continue; } } // If there's no pattern at all if (empty($val)) { $ok[$key] = true; continue; } if (strpos($val, "/") === 0) { // regex $ok[$key] = preg_match($val, $VARS[$key]) === 1; } else { $checkmethod = "is_$val"; $ok[$key] = !($checkmethod($VARS[$key]) !== true); } } if ($or) { $success = false; $bad = ""; foreach ($ok as $k => $v) { if ($v) { $success = true; break; } else { $bad = $k; } } if (!$success) { http_response_code(400); die("400 Bad request: variable $bad is missing or invalid"); } } else { foreach ($ok as $key => $bool) { if (!$bool) { http_response_code(400); die("400 Bad request: variable $key is missing or invalid"); } } } }