diff --git a/index.php b/index.php index fbd1ab4..13b9b9d 100644 --- a/index.php +++ b/index.php @@ -20,3 +20,10 @@ require(APP_PATH.'config/config.php'); // Require the most important files require(SYS_PATH . "common.php"); +//Set error handlers +register_shutdown_function('shutdown'); +set_error_handler('on_error'); +set_exception_handler('on_exception'); + +// And away we go! +route(); \ No newline at end of file diff --git a/sys/common.php b/sys/common.php index 556e496..cf8d99d 100644 --- a/sys/common.php +++ b/sys/common.php @@ -32,6 +32,8 @@ TXT; } } +// -------------------------------------------------------------------------- + /** * Function to search through the tree to find the necessary file * @@ -124,6 +126,24 @@ function on_exception($exception) // -------------------------------------------------------------------------- +/** + * Utility function to check if a variable is set, and is an array or object + * + * @param mixed $var + * @return bool + */ +function is_like_array(&$var) +{ + if( ! isset($var)) + { + return FALSE; + } + + return (is_array($var) OR is_object($var)) && ( ! empty($var)); +} + +// -------------------------------------------------------------------------- + /** * General 404 function */ @@ -315,7 +335,7 @@ function route() */ function site_url($segment) { - return $url = BASE_URL . URL_INDEX_FILE . $segment; + return $url = BASEURL . URL_INDEX_FILE . $segment; } // -------------------------------------------------------------------------- @@ -358,18 +378,10 @@ function do_curl($url, $options=array()) // -------------------------------------------------------------------------- -//Set error handlers -register_shutdown_function('shutdown'); -set_error_handler('on_error'); -set_exception_handler('on_exception'); - // Load Most Common libraries require_once('miniMVC.php'); require_once('output.php'); require_once('page.php'); require_once('db.php'); -//Route to the appropriate function -route(); - -// End of common.php +// End of common.php \ No newline at end of file diff --git a/sys/db.php b/sys/db.php index 3e1fce8..ab849bb 100644 --- a/sys/db.php +++ b/sys/db.php @@ -12,7 +12,7 @@ class db extends PDO { if ( ! isset(self::$instance[$dbname])) { //echo 'Creating new instance of db class.'; - self::$instance[$dbname] = new db($dbname); + self::$instance[$dbname] = new db($dbname, $options); } return self::$instance[$dbname]; @@ -20,11 +20,22 @@ class db extends PDO { function __construct($dbname="default", $options=array()) { - //Include the database config file - require(APP_PATH . 'config/db.php'); + // Include the database config file + require(APP_PATH.'config/db.php'); - // Array manipulation is too verbose - extract($db_conf[$dbname]); + // Get the correct database in the config file + if(is_like_array($db_conf[$dbname])) + { + // Array manipulation is too verbose + extract($db_conf[$dbname]); + } + else + { + // Apparently the database doesn't exist + $this->get_last_error(); + trigger_error("Database does not exist", E_USER_ERROR); + die(); + } // Sqlite doesn't use dbname param $dsn = (stripos($type, "sqlite") === FALSE) ? "{$type}:dbname={$db}" : "{$type}:{$db}"; @@ -48,8 +59,7 @@ class db extends PDO { } $opts = array( - //PDO::ATTR_PERSISTENT => (isset($persist)) ? $persist : FALSE, - //PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING, + PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ); if( ! isset($persist)) @@ -57,11 +67,20 @@ class db extends PDO { unset($opts[PDO::ATTR_PERSISTENT]); } - $options = array_merge($opts, $options); + $options = $opts + $options; - parent::__construct($dsn, $user, $pass, $options); + try + { + parent::__construct($dsn, $user, $pass, $options); + } + catch(Exception $e) + { + if($e->getMessage() !== "The auto-commit mode cannot be changed for this driver") + { + get_last_error(); + } + } - self::$instance[$dbname] =& $this; } // -------------------------------------------------------------------------- @@ -139,49 +158,52 @@ class db extends PDO { // -------------------------------------------------------------------------- /** - * Constructor without the "new" + * Simplifies prepared statements for database queries * - * @param array $members + * @param string $sql + * @param array $data + * @return mixed PDOStatement / FALSE */ - /*function __invoke($db="default") + function prepare_query($sql, $data) { - return self::get_instance($db); - } - - function start_transaction() - { - if( ! $this->inTransaction()) - { - return $this->beginTransaction(); - } - } - - function commit() - { - if($this->inTransaction()) - { - return parent::commit(); - } - } - - function rollback() - { - if($this->inTransaction()) + // Prepare the sql + $query = $this->prepare($sql); + + if( ! is_like_array($query)) { - return parent::rollBack(); + $this->get_last_error(); + return FALSE; } - } - - function prepare($sql, $driver_options=array()) - { - $this->statement = parent::prepare($sql, $driver_options); - return $this->statement; - } - - function set() - { - - }*/ + + // Set the statement in the class variable for easy later access + $this->statement =& $query; + + + if( ! is_like_array($data)) + { + trigger_error("Invalid data argument"); + return FALSE; + } + + // Bind the parameters + foreach($data as $placeholder => $value) + { + $res = $query->bindParam($placeholder, $value); + + if( ! $res) + { + trigger_error("Parameter not successfully bound"); + return FALSE; + } + } + + + + return $query; + + } + + // -------------------------------------------------------------------------- /** * Returns the last error from the database @@ -216,4 +238,4 @@ class db extends PDO { } } -// End of db.php +// End of db.php \ No newline at end of file diff --git a/sys/miniMVC.php b/sys/miniMVC.php index 35a3d85..e63edc3 100644 --- a/sys/miniMVC.php +++ b/sys/miniMVC.php @@ -57,35 +57,38 @@ class JSObject { */ function __toString() { + $args = func_get_args(); + $method = ( ! empty($args)) ? $args[0] : "print_r"; + $data = (isset($args[1])) ? $args[1] : array(); + // 32762 == (E_ALL|E_NOTICE|E_STRICT) & ~(E_ERROR | E_PARSE) - if(ini_get('error_reporting') == 32762) + if(ini_get('error_reporting') == 32762 && empty($data)) { - $args = func_get_args(); - $method = ( ! empty($args)) ? $args[0] : "print_r"; - - $output = '
';
-		
-			if($method == "var_dump")
-			{
-				ob_start();
-				var_dump($this);
-				$output .= ob_get_contents();
-				ob_end_clean();
-			}
-			else if($method == "var_export")
-			{
-				ob_start();
-				var_export($this);
-				$output .= ob_get_contents();
-				ob_end_clean();
-			}	
-			else
-			{
-				$output .= print_r($this, TRUE);
-			}
-		
-			return $output . '
'; + $data =& $this; } + + $output = '
';
+		
+		if($method == "var_dump")
+		{
+			ob_start();
+			var_dump($data);
+			$output .= ob_get_contents();
+			ob_end_clean();
+		}
+		else if($method == "var_export")
+		{
+			ob_start();
+			var_export($data);
+			$output .= ob_get_contents();
+			ob_end_clean();
+		}	
+		else
+		{
+			$output .= print_r($data, TRUE);
+		}
+	
+		return $output . '
'; } /** @@ -353,12 +356,15 @@ class MM_Controller extends miniMVC { $path = MOD_PATH . "{$module}/models/{$file}.php"; } - require_once($path); + if(is_file($path)) + { + require_once($path); + } if( ! empty($args)) { - $this->$file = new $file(extract($args)); + $this->$file = new $file($args); } else { @@ -379,9 +385,9 @@ class MM_Model extends miniMVC { /** * Adds the database class to the current model class */ - function load_db($name) + function load_db($name="default") { - $this->db = new db($name); + $this->db = db::get_instance($name); } } diff --git a/sys/output.php b/sys/output.php index 7ccb57b..df35690 100644 --- a/sys/output.php +++ b/sys/output.php @@ -1,5 +1,8 @@ headers = array(); } + // -------------------------------------------------------------------------- + /** * PHP magic method called when ending the script * Used for outputing HTML @@ -39,12 +44,10 @@ class Output extends miniMVC { echo $this->buffer; ob_end_flush(); } - else - { - echo "No page content"; - } } + // -------------------------------------------------------------------------- + /** * Sets a header for later output * @param string $key @@ -55,6 +58,8 @@ class Output extends miniMVC { $this->headers[$key] = $val; } + // -------------------------------------------------------------------------- + /** * Adds text to the output buffer * @@ -65,6 +70,8 @@ class Output extends miniMVC { $this->buffer .= $string; } + // -------------------------------------------------------------------------- + /** * Sets the output buffer *