Advertisement
Guest User

Untitled

a guest
Apr 13th, 2013
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.84 KB | None | 0 0
  1. <?php
  2.  
  3.     /***********************************************************************
  4.      * functions.php
  5.      *
  6.      * Computer Science 50
  7.      * Problem Set 7
  8.      *
  9.      * Helper functions.
  10.      **********************************************************************/
  11.  
  12.     require_once("constants.php");
  13.  
  14.     /**
  15.      * Apologizes to user with message.
  16.      */
  17.     function apologize($message)
  18.     {
  19.         render("apology.php", ["message" => $message]);
  20.         exit;
  21.     }
  22.  
  23.     /**
  24.      * Facilitates debugging by dumping contents of variable
  25.      * to browser.
  26.      */
  27.     function dump($variable)
  28.     {
  29.         require("../templates/dump.php");
  30.         exit;
  31.     }
  32.  
  33.     /**
  34.      * Logs out current user, if any.  Based on Example #1 at
  35.      * http://us.php.net/manual/en/function.session-destroy.php.
  36.      */
  37.     function logout()
  38.     {
  39.         // unset any session variables
  40.         $_SESSION = array();
  41.  
  42.         // expire cookie
  43.         if (!empty($_COOKIE[session_name()]))
  44.         {
  45.             setcookie(session_name(), "", time() - 42000);
  46.         }
  47.  
  48.         // destroy session
  49.         session_destroy();
  50.     }
  51.  
  52.     /**
  53.      * Returns a stock by symbol (case-insensitively) else false if not found.
  54.      */
  55.     function lookup($symbol)
  56.     {
  57.         // reject symbols that start with ^
  58.         if (preg_match("/^\^/", $symbol))
  59.         {
  60.             return false;
  61.         }
  62.  
  63.         // reject symbols that contain commas
  64.         if (preg_match("/,/", $symbol))
  65.         {
  66.             return false;
  67.         }
  68.  
  69.         // open connection to Yahoo
  70.         $handle = @fopen("http://download.finance.yahoo.com/d/quotes.csv?f=snl1&s=$symbol", "r");
  71.         if ($handle === false)
  72.         {
  73.             // trigger (big, orange) error
  74.             trigger_error("Could not connect to Yahoo!", E_USER_ERROR);
  75.             exit;
  76.         }
  77.  
  78.         // download first line of CSV file
  79.         $data = fgetcsv($handle);
  80.         if ($data === false || count($data) == 1)
  81.         {
  82.             return false;
  83.         }
  84.  
  85.         // close connection to Yahoo
  86.         fclose($handle);
  87.  
  88.         // ensure symbol was found
  89.         if ($data[2] === "0.00")
  90.         {
  91.             return false;
  92.         }
  93.  
  94.         // return stock as an associative array
  95.         return [
  96.             "symbol" => $data[0],
  97.             "name" => $data[1],
  98.             "price" => $data[2],
  99.         ];
  100.     }
  101.  
  102.     /**
  103.      * Executes SQL statement, possibly with parameters, returning
  104.      * an array of all rows in result set or false on (non-fatal) error.
  105.      */
  106.     function query(/* $sql [, ... ] */)
  107.     {
  108.         // SQL statement
  109.         $sql = func_get_arg(0);
  110.  
  111.         // parameters, if any
  112.         $parameters = array_slice(func_get_args(), 1);
  113.  
  114.         // try to connect to database
  115.         static $handle;
  116.         if (!isset($handle))
  117.         {
  118.             try
  119.             {
  120.                 // connect to database
  121.                 $handle = new PDO("mysql:dbname=" . DATABASE . ";host=" . SERVER, USERNAME, PASSWORD);
  122.  
  123.                 // ensure that PDO::prepare returns false when passed invalid SQL
  124.                 $handle->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
  125.             }
  126.             catch (Exception $e)
  127.             {
  128.                 // trigger (big, orange) error
  129.                 trigger_error($e->getMessage(), E_USER_ERROR);
  130.                 exit;
  131.             }
  132.         }
  133.  
  134.         // prepare SQL statement
  135.         $statement = $handle->prepare($sql);
  136.         if ($statement === false)
  137.         {
  138.             // trigger (big, orange) error
  139.             trigger_error($handle->errorInfo()[2], E_USER_ERROR);
  140.             exit;
  141.         }
  142.  
  143.         // execute SQL statement
  144.         $results = $statement->execute($parameters);
  145.  
  146.         // return result set's rows, if any
  147.         if ($results !== false)
  148.         {
  149.             return $statement->fetchAll(PDO::FETCH_ASSOC);
  150.         }
  151.         else
  152.         {
  153.             return false;
  154.         }
  155.     }
  156.  
  157.     /**
  158.      * Redirects user to destination, which can be
  159.      * a URL or a relative path on the local host.
  160.      *
  161.      * Because this function outputs an HTTP header, it
  162.      * must be called before caller outputs any HTML.
  163.      */
  164.     function redirect($destination)
  165.     {
  166.         // handle URL
  167.         if (preg_match("/^https?:\/\//", $destination))
  168.         {
  169.             header("Location: " . $destination);
  170.         }
  171.  
  172.         // handle absolute path
  173.         else if (preg_match("/^\//", $destination))
  174.         {
  175.             $protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
  176.             $host = $_SERVER["HTTP_HOST"];
  177.             header("Location: $protocol://$host$destination");
  178.         }
  179.  
  180.         // handle relative path
  181.         else
  182.         {
  183.             // adapted from http://www.php.net/header
  184.             $protocol = (isset($_SERVER["HTTPS"])) ? "https" : "http";
  185.             $host = $_SERVER["HTTP_HOST"];
  186.             $path = rtrim(dirname($_SERVER["PHP_SELF"]), "/\\");
  187.             header("Location: $protocol://$host$path/$destination");
  188.         }
  189.  
  190.         // exit immediately since we're redirecting anyway
  191.         exit;
  192.     }
  193.  
  194.     /**
  195.      * Renders template, passing in values.
  196.      */
  197.     function render($template, $values = [])
  198.     {
  199.         // if template exists, render it
  200.         if (file_exists("../templates/$template"))
  201.         {
  202.             // extract variables into local scope
  203.             extract($values);
  204.  
  205.             // render header
  206.             require("../templates/header.php");
  207.  
  208.             // render template
  209.             require("../templates/$template");
  210.  
  211.             // render footer
  212.             require("../templates/footer.php");
  213.         }
  214.  
  215.         // else err
  216.         else
  217.         {
  218.             trigger_error("Invalid template: $template", E_USER_ERROR);
  219.         }
  220.     }
  221.  
  222. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement