$b
HTML; } /* generate an error page with message $m */ function page_err($m, $goto = "/") { /* say "go home" if we go home otherwise "go back" */ $loc = $goto == "/" ? "home" : "back"; return page("error! :: $m", <<error

$m

go $loc

HTML); } /* check if $d has the right keys set & at the right len * * accepts key/val array $a, where $a is an array of * [ "key for $_POST" => [ min_length, max_length ] ] * * accepts dictionary $d */ function dict_has_vals($a, $d) { foreach ($a as $k => $v) { if (isset($d[$k])) { $x = $d[$k]; /* len */ $l = strlen($x); /* min */ $i = $v[0]; /* max */ $a = $v[1]; if (!($l >= $v[0] && $l <= $v[1])) { throw new Exception( "$k must be between $i and $a chars" ); } } else { throw new Exception( "$k not sent in form" ); } } } function form_has_post($a) { return dict_has_vals($a, $_POST); } function form_has_get($a) { return dict_has_vals($a, $_GET); } function session_has($a) { return dict_has_vals($a, $_SESSION); } /* checks the session for a user * * accepts a database $db */ function whoami($db) { try { session_has(["id" => [0, 32]]); } catch (Exception $e) { eputs("invalid session: " . $e->getMessage()); return null; } try { return $db->get_user_by_id((int)($_SESSION["id"])); } catch (Exception $e) { eputs("error getting user: " . $e->getMessage()); return null; } } ?>