5) { for ($i = 2; $i < strlen($key) - 2; $i++) { $resp[$i] = "*"; } } return $resp; } /** * Check if the request is allowed * @global array $VARS * @return bool true if the request should continue, false if the request is bad */ function authenticate(): bool { global $VARS; // HTTP basic auth if (!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])) { $username = $_SERVER['PHP_AUTH_USER']; $password = $_SERVER['PHP_AUTH_PW']; } else if (!empty($VARS['username']) && !empty($VARS['password'])) { $username = $VARS['username']; $password = $VARS['password']; } else { return false; } $user = User::byUsername($username); if (!$user->exists()) { return false; } if ($user->checkPassword($password, true)) { return true; } return false; } /** * Get the User whose credentials were used to make the request. */ function getRequestUser(): User { global $VARS; if (!empty($_SERVER['PHP_AUTH_USER'])) { return User::byUsername($_SERVER['PHP_AUTH_USER']); } else { return User::byUsername($VARS['username']); } } function checkVars($vars, $or = false) { global $VARS; $ok = []; foreach ($vars as $key => $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 (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"); } } } }