Skip to content
Browse files

tweaks to how db connections / errors are handled. fixes #46

  • Loading branch information...
1 parent 96dfe22 commit 8eedffa23df01c393c8b76c319450cad8c1e0ad1 Zach Hoeken committed May 25, 2013
Showing with 101 additions and 91 deletions.
  1. +0 −4 extensions/global.php
  2. +8 −1 framework/db.php
  3. +2 −2 web/apiv1.php
  4. +91 −84 web/dispatcher.php
View
4 extensions/global.php
@@ -39,8 +39,4 @@
// to), swap the two elements around the $path_delimiter variable. If you don't have
// the PEAR packages installed, you can leave this like it is and move on.
ini_set('include_path', ini_get('include_path') . $path_delimiter . CLASSES_DIR . 'PEAR');
-
- //load our db if we need it.
- if (defined('RR_DB_NAME'))
- db()->selectDb(RR_DB_NAME);
?>
View
9 framework/db.php
@@ -75,10 +75,17 @@ public function __construct($user, $pass = null, $host = 'localhost', $port = 33
public function reconnect()
{
+ ob_start();
$this->link = new mysqli($this->host, $this->user, $this->pass, RR_DB_NAME, $this->port);
+ ob_end_clean();
+
if ($this->link->connect_errno) {
- die("Failed to connect: " . $this->link->connect_error);
+ throw new Exception("Failed to connect to database!");
}
+
+ //select the right db.
+ if (defined('RR_DB_NAME'))
+ $this->selectDb(RR_DB_NAME);
}
public function error()
View
4 web/apiv1.php
@@ -19,7 +19,7 @@
define("START_TIME", microtime(true));
include("../extensions/global.php");
- include(EXTENSIONS_DIR . "session.php");
+ //include(EXTENSIONS_DIR . "session.php");
//are we in the right place?
if ($_SERVER['HTTP_HOST'] != SITE_HOSTNAME)
@@ -33,5 +33,5 @@
exit();
}
- Controller::byName('apiv1')->renderView('endpoint');
+ Controller::byName('apiv1')->renderView('endpoint');
?>
View
175 web/dispatcher.php
@@ -18,93 +18,100 @@
define("START_TIME", microtime(true));
- include("../extensions/global.php");
- include(EXTENSIONS_DIR . "session.php");
+ try
+ {
+ include("../extensions/global.php");
+ include(EXTENSIONS_DIR . "session.php");
- //are we in the right place?
- if ($_SERVER['HTTP_HOST'] != SITE_HOSTNAME)
- header("Location: http://" . SITE_HOSTNAME . $_SERVER['REQUEST_URI']);
-
- // If page requires SSL, and we're not in SSL mode,
- // redirect to the SSL version of the page
- if(FORCE_SSL && $_SERVER['SERVER_PORT'] != 443) {
- header("HTTP/1.1 301 Moved Permanently");
- header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
- exit();
- }
+ //are we in the right place?
+ if ($_SERVER['HTTP_HOST'] != SITE_HOSTNAME)
+ header("Location: http://" . SITE_HOSTNAME . $_SERVER['REQUEST_URI']);
- //get our stuff.
- $mode = $_GET['mode'];
- $controller = $_GET['controller'];
- $view = $_GET['view'];
+ // If page requires SSL, and we're not in SSL mode,
+ // redirect to the SSL version of the page
+ if(FORCE_SSL && $_SERVER['SERVER_PORT'] != 443) {
+ header("HTTP/1.1 301 Moved Permanently");
+ header("Location: https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
+ exit();
+ }
+
+ //get our stuff.
+ $mode = $_GET['mode'];
+ $controller = $_GET['controller'];
+ $view = $_GET['view'];
+
+ //figure out what mode we're looking at.
+ if (!$mode)
+ {
+ // //do we have a cookie based mode?
+ // if ($_COOKIE['viewmode'])
+ // {
+ // switch ($_COOKIE['viewmode'])
+ // {
+ // case 'iphone':
+ // $mode = 'iphone';
+ // break;
+ //
+ // case 'html':
+ // default:
+ // $mode = 'html';
+ // break;
+ // }
+ // }
+ // else
+ // {
+ // //figure out if we're iphone or not.
+ // if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') !== false)
+ // $mode = 'iphone';
+ // else
+ // $mode = 'html';
+ //
+ // //save it for later!
+ // setcookie('viewmode', $mode, time()+60*60*24*30, '/');
+ // }
+ $mode = 'html';
+ }
- //figure out what mode we're looking at.
- if (!$mode)
- {
- // //do we have a cookie based mode?
- // if ($_COOKIE['viewmode'])
- // {
- // switch ($_COOKIE['viewmode'])
- // {
- // case 'iphone':
- // $mode = 'iphone';
- // break;
- //
- // case 'html':
- // default:
- // $mode = 'html';
- // break;
- // }
- // }
- // else
- // {
- // //figure out if we're iphone or not.
- // if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'iPod') !== false)
- // $mode = 'iphone';
- // else
- // $mode = 'html';
- //
- // //save it for later!
- // setcookie('viewmode', $mode, time()+60*60*24*30, '/');
- // }
- $mode = 'html';
- }
-
- //what controller to show?
- //If the user hasn't supplied a controller (e.g. item), then use either the iphone or main controllers (which display the home page)
- if (!$controller)
- {
- // if ($mode == 'iphone')
- // $controller = 'iphone';
- // else
- $controller = 'main';
- }
- if (!$view)
- $view = 'home';
+ //what controller to show?
+ //If the user hasn't supplied a controller (e.g. item), then use either the iphone or main controllers (which display the home page)
+ if (!$controller)
+ {
+ // if ($mode == 'iphone')
+ // $controller = 'iphone';
+ // else
+ $controller = 'main';
+ }
+ if (!$view)
+ $view = 'home';
- //load the content.
- //Create a new object of a subclass of the controller class
- //$main is an object derived from the Controller class, e.g. iphoneController, mainController, itemController, etc.
- $main = Controller::byName($controller);
-
- //call the renderView function (within the Controller class), passing it only the name of the view (e.g. newest)
- //The renderView function does the following:
- // - Sets the the property, $main->data['items'], equal to an array containing one page worth of object (e.g item) data read in from MySQL
- // - Includes the appropriate view file {controller}.{view}.php file, e.g. item.newest.php
- // - Returns the output of the Controller->renderview (note: this is not displayed to the screen, rather it's stored in the $content variable)
- $content = $main->renderView($view);
-
- //now draw it in our template.
- //Does the following:
- // - Create a new instance of a controller, e.g. htmltemplatecontroller.php (see controllers/htmltemplate.php)
- // - Call the renderView function on that new controller object, telling it to render the 'main' view with the arguments: content, title and sidebar
- // - Echo all that shizzle to the screen
- //Note that $mode often is not defined
- echo Controller::byName("{$mode}Template")->renderView('main', array(
- 'content' => $content,
- 'title' => $main->get('title'),
- 'area' => $main->get('area'),
- 'sidebar' => $main->get('sidebar')
- ));
+ //load the content.
+ //Create a new object of a subclass of the controller class
+ //$main is an object derived from the Controller class, e.g. iphoneController, mainController, itemController, etc.
+ $main = Controller::byName($controller);
+
+ //call the renderView function (within the Controller class), passing it only the name of the view (e.g. newest)
+ //The renderView function does the following:
+ // - Sets the the property, $main->data['items'], equal to an array containing one page worth of object (e.g item) data read in from MySQL
+ // - Includes the appropriate view file {controller}.{view}.php file, e.g. item.newest.php
+ // - Returns the output of the Controller->renderview (note: this is not displayed to the screen, rather it's stored in the $content variable)
+ $content = $main->renderView($view);
+
+ //now draw it in our template.
+ //Does the following:
+ // - Create a new instance of a controller, e.g. htmltemplatecontroller.php (see controllers/htmltemplate.php)
+ // - Call the renderView function on that new controller object, telling it to render the 'main' view with the arguments: content, title and sidebar
+ // - Echo all that shizzle to the screen
+ //Note that $mode often is not defined
+ echo Controller::byName("{$mode}Template")->renderView('main', array(
+ 'content' => $content,
+ 'title' => $main->get('title'),
+ 'area' => $main->get('area'),
+ 'sidebar' => $main->get('sidebar')
+ ));
+ }
+ catch (Exception $ex)
+ {
+ echo "Something bad happened: " . $ex->getMessage();
+ }
?>

0 comments on commit 8eedffa

Please sign in to comment.
Something went wrong with that request. Please try again.