$message]); } /** * Facilitates debugging by dumping contents of argument(s) * to browser. */ function dump() { $arguments = func_get_args(); require("../views/dump.php"); exit; } /** * Logs out current user, if any. Based on Example #1 at * http://us.php.net/manual/en/function.session-destroy.php. */ function logout() { // unset any session variables $_SESSION = []; // expire cookie if (!empty($_COOKIE[session_name()])) { setcookie(session_name(), "", time() - 42000); } // destroy session session_destroy(); } /** * Returns a stock by symbol (case-insensitively) else false if not found. */ function lookup($symbol) { // reject symbols that start with ^ if (preg_match("/^\^/", $symbol)) { return false; } // reject symbols that contain commas if (preg_match("/,/", $symbol)) { return false; } // headers for proxy servers $headers = [ "Accept" => "*/*", "Connection" => "Keep-Alive", "User-Agent" => sprintf("curl/%s", curl_version()["version"]) ]; // open connection to Yahoo $context = stream_context_create([ "http" => [ "header" => implode(array_map(function($value, $key) { return sprintf("%s: %s\r\n", $key, $value); }, $headers, array_keys($headers))), "method" => "GET" ] ]); $handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s={$symbol}", "r", false, $context); if ($handle === false) { // trigger (big, orange) error trigger_error("Could not connect to Yahoo!", E_USER_ERROR); exit; } // download first line of CSV file $data = fgetcsv($handle); if ($data === false || count($data) == 1) { return false; } // close connection to Yahoo fclose($handle); // ensure symbol was found if ($data[2] === "N/A" || $data[2] === "0.00") { return false; } // return stock as an associative array return [ "symbol" => strtoupper($data[0]), "name" => $data[1], "price" => floatval($data[2]) ]; } /** * Redirects user to location, which can be a URL or * a relative path on the local host. * * http://stackoverflow.com/a/25643550/5156190 * * Because this function outputs an HTTP header, it * must be called before caller outputs any HTML. */ function redirect($location) { if (headers_sent($file, $line)) { trigger_error("HTTP headers already sent at {$file}:{$line}", E_USER_ERROR); } header("Location: {$location}"); exit; } /** * Renders view, passing in values. */ function render($view, $values = []) { // if view exists, render it if (file_exists("../views/{$view}")) { // extract variables into local scope extract($values); // render view (between header and footer) require("../views/header.php"); require("../views/{$view}"); require("../views/footer.php"); exit; } // else err else { trigger_error("Invalid view: {$view}", E_USER_ERROR); } } ?>