Moritz Schmidt 9 gadi atpakaļ
vecāks
revīzija
2a89ad2f3e
12 mainītis faili ar 476 papildinājumiem un 1 dzēšanām
  1. 1 1
      README.md
  2. 0 0
      css/style.css
  3. BIN
      favicon.ico
  4. 12 0
      inc/config.inc.php.dist
  5. 71 0
      inc/controller.php
  6. 269 0
      inc/database.php
  7. 23 0
      inc/errorhandler.php
  8. 6 0
      inc/model.php
  9. 64 0
      inc/view.php
  10. 16 0
      index.php
  11. 3 0
      js/scripts.js
  12. 11 0
      templates/default.php

+ 1 - 1
README.md

@@ -1,4 +1,4 @@
 moeCMS
 ======
 
-PHP/MySQL driven CMS with templates, ... 
+PHP/MySQL driven framework for web projects

+ 0 - 0
css/style.css


BIN
favicon.ico


+ 12 - 0
inc/config.inc.php.dist

@@ -0,0 +1,12 @@
+<?php
+
+class Config {
+  public static $dbHost   = "127.0.0.1";
+  public static $dbUser   = "root";
+  public static $dbPass   = "root";
+  public static $dbName   = "moecms";
+  public static $basedir  = ""; // without trailing slash
+  public static $abspath  = "/var/www/cms"; // without trailing slash
+}
+
+?>

+ 71 - 0
inc/controller.php

@@ -0,0 +1,71 @@
+<?php
+class Controller {
+
+	private $request 			= null;
+	private $template 		= '';
+	private $view 				= null;
+
+	/**
+	 *
+	 * @param Array $request $_GET and $_POST
+	 */
+	public function __construct($request) {
+		ErrorHandler::$eh = new ErrorHandler();
+		Database::$db = new Database();
+
+		$this->view = new View();
+		$this->request = $request;
+
+		if(!empty($request['action']) && $request['action']) { // any action needs to be done?
+			$this->actionController();
+		}
+
+		$this->template = !empty(strtok($_SERVER['REQUEST_URI'], '?')) ? strtok($_SERVER['REQUEST_URI'], '?') : 'default';
+	}
+
+	/**
+	 * Return requested page
+	 *
+	 * @return String Content of requested page
+	 */
+	public function display() {
+		$singlePage = false; // set to true if header/footer shouldn't be used
+		switch($this->template) {
+
+			case 'default':
+			default:
+				$singlePage = true;
+				$this->view->setTemplate('default');
+				$this->view->assign("test", "joo");
+				break;
+		}
+
+		if(!$singlePage) {
+			$headerView = new View();
+			$footerView = new View();
+
+			$headerView->setTemplate("header");
+			$footerView->setTemplate("footer");
+
+			$header = $headerView->loadTemplate(); // save them in vars to prevent errors being ignored
+			$footer = $footerView->loadTemplate();
+
+			$this->view->assign("errors", ErrorHandler::$eh->getErrors()); // get errors as last thing
+
+			return $header . $this->view->loadTemplate() . $footer;
+		}
+
+		$this->view->assign("errors", ErrorHandler::$eh->getErrors()); // get errors as last thing
+
+		return $this->view->loadTemplate();
+	}
+
+	private function actionController() {
+		switch($this->request['action']) {
+			default:
+				error_log("Unknown action: " . $this->request['action']);
+				break;
+		}
+	}
+}
+?>

+ 269 - 0
inc/database.php

@@ -0,0 +1,269 @@
+<?php
+
+class Database {
+  private $handle = null;
+
+  public static $db;
+
+  public function __construct() {
+    $this->handle = new mysqli(Config::$dbHost, Config::$dbUser, Config::$dbPass, Config::$dbName);
+    $this->handle->set_charset("utf8");
+
+    if($this->handle->connect_error) {
+      ErrorHandler::$eh->addError("Database Error: Connection failed (" . $this->handle->connect_error . ")");
+    }
+  }
+
+  public function executeQuery($query) {
+    return $this->handle->query($query);
+  }
+
+  public function getString($what, $from, $where, $like) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE " . $whereString . ";";
+    } else {
+      $query = "SELECT `" . $what . "` FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      ErrorHandler::$eh->addError("Database Error: " . $this->handle->error);
+      return false;
+    } else if($res->num_rows != 1) {
+      ErrorHandler::$eh->addError("Database Error: Found more than one or less than one result in getString (" . $what . ")");
+      return false;
+    }
+
+    return $res->fetch_row()[0];
+  }
+
+  public function getAllAssoc($from, $where = null, $like = null) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";";
+    } else if($where && $like) {
+      $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
+    } else {
+      $query = "SELECT * FROM `" . $from . "`;";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      error_log("Failed query: " . $query . PHP_EOL);
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else if($res->num_rows < 1) {
+      ErrorHandler::$eh->addError("Database error: no result (getAllAssoc)");
+      //exit(1);
+    }
+
+    return $res->fetch_all(MYSQLI_ASSOC);
+  }
+
+  public function getAllAssocCustom($from, $custom, $where = null, $like = null) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . " " . $custom . ";";
+    } else if($where && $like) {
+      $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";";
+    } else {
+      $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else if($res->num_rows < 1) {
+      error_log("This shouldn't happen..3");
+      //exit(1);
+    }
+
+    return $res->fetch_all(MYSQLI_ASSOC);
+  }
+
+  public function getAllRow($from, $where = null, $like = null) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";";
+    } else if($where && $like) {
+      $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
+    } else {
+      $query = "SELECT * FROM `" . $from . "`;";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else if($res->num_rows < 1) {
+      error_log("This shouldn't happen..4");
+      exit(1);
+    }
+
+    return $res->fetch_all(MYSQLI_NUM);
+  }
+
+  public function getAllRowCustom($from, $custom) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . " " . $custom . ";";
+    } else if($where && $like) {
+      $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "' " . $custom . ";";
+    } else {
+      $query = "SELECT * FROM `" . $from . "` " . $custom . ";";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else if($res->num_rows < 1) {
+      error_log("This shouldn't happen..5");
+      //exit(1);
+    }
+
+    return $res->fetch_all(MYSQLI_NUM);
+  }
+
+  public function countRows($from, $where = null, $like = null) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "SELECT * FROM `" . $from . "` WHERE " . $whereString . ";";
+    } else if($where && $like) {
+      $query = "SELECT * FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
+    } else {
+      $query = "SELECT * FROM `" . $from . "`;";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else {
+      return $res->num_rows;
+    }
+  }
+
+  public function insertRow($into, $cols, $vals) {
+    foreach($vals as $key => $val) {
+      $vals[$key] = $this->handle->real_escape_string($val);
+    }
+    $colString = "(`" . implode('`, `', $cols) . "`)";
+    $valString = "('" . implode("', '", $vals) . "')";
+
+    $query = "INSERT INTO `" . $into . "` " . $colString . " VALUES " . $valString . ";";
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->error, E_USER_ERROR);
+      exit(1);
+    } else {
+      return $this->handle->insert_id;
+    }
+  }
+
+  public function deleteRows($from, $where, $like) {
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+      $query = "DELETE FROM `" . $from . "` WHERE " . $whereString . ";";
+    } else if($where && $like) {
+      $query = "DELETE FROM `" . $from . "` WHERE `" . $where . "` LIKE '" . $like . "';";
+    }
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else {
+      return true;
+    }
+  }
+
+  public function updateRow($update, $col, $val, $where, $like) {
+    if(gettype($col) == "array" && gettype($val) == "array") {
+      $setString = "SET";
+      foreach($col as $key => $value) {
+        if($key > 0) {
+          $setString .= " ,";
+        }
+        $setString .= " `" . $value . "` = '" . $val[$key] . "'";
+      }
+    } else {
+      $setString = " `" . $col . "` = '" . $val . "'";
+    }
+
+    if(gettype($where) == "array" && gettype($like) == "array") {
+      $whereString = "";
+      foreach($where as $key => $value) {
+        if($key > 0) {
+          $whereString .= " AND";
+        }
+        $whereString .= " `" . $value . "` LIKE '" . $like[$key] . "'";
+      }
+    } else if($where && $like) {
+      $whereString = "`" . $where . "` LIKE '" . $like . "'";
+    }
+
+    $query = "UPDATE `" . $update . "` " . $setString . " WHERE " . $whereString . ";";
+
+    $res = $this->handle->query($query);
+    if($res === false) {
+      echo "DB failed.";
+      trigger_error('Database failed: '  . $this->handle->connect_error, E_USER_ERROR);
+      exit(1);
+    } else {
+      return true;
+    }
+  }
+}

+ 23 - 0
inc/errorhandler.php

@@ -0,0 +1,23 @@
+<?php
+
+class ErrorHandler {
+
+  private $errors = array();
+
+  public static $eh;
+
+  public function __construct() {
+
+  }
+
+  public function addError($errorString) {
+    $this->errors[] = $errorString;
+    //error_log($errorString);
+  }
+
+  public function getErrors() {
+    if(sizeof($this->errors) > 0) {
+      return "<script type=\"text/javascript\">console.log(\"" . implode('\n', $this->errors) . "\");</script>" . PHP_EOL;
+    }
+  }
+}

+ 6 - 0
inc/model.php

@@ -0,0 +1,6 @@
+<?php
+
+class Model {
+
+}
+?>

+ 64 - 0
inc/view.php

@@ -0,0 +1,64 @@
+<?php
+class View {
+
+	// Path to templates dir
+	private $path = 'templates';
+	// Name of template, default: default
+	private $template = 'default';
+
+	// Stores variables for templates
+	private $_ = array();
+
+	/**
+	 * Assign a key/val pair for the template
+	 *
+	 * @param String $key Key
+	 * @param String $value Variable
+	 */
+	public function assign($key, $value) {
+		$this->_[$key] = $value;
+	}
+
+
+	/**
+	 * Sets templates name
+	 *
+	 * @param String $template Name of template
+	 */
+	public function setTemplate($template = 'default') {
+		$this->template = $template;
+	}
+
+
+	/**
+	 * Load template and return result
+	 *
+	 * @return string Output of template
+	 */
+	public function loadTemplate() {
+		$tpl = $this->template;
+
+		$file = $this->path . DIRECTORY_SEPARATOR . $tpl . '.php';
+
+		if(file_exists($file)) {
+			// Write output to buffer
+			ob_start();
+
+			extract($this->_); // make vars available via name not array, forbidden vars: $tpl, $file, $output
+
+			// Include template and save output to $output
+			include($file);
+			$output = ob_get_contents();
+			ob_end_clean();
+
+			return $output . PHP_EOL;
+		}
+		else {
+			// Couldn't find template
+			// TODO: Throw exception
+			// TODO: test if deprecated (see: controller display())
+			ErrorHandler::$eh->addError("Could not load template: " . $tpl);
+		}
+	}
+}
+?>

+ 16 - 0
index.php

@@ -0,0 +1,16 @@
+<?php
+
+require('inc/config.inc.php');
+require('inc/controller.php');
+require('inc/model.php');
+require('inc/view.php');
+require('inc/errorhandler.php');
+require('inc/database.php');
+
+$request = array_merge($_GET, $_POST);
+
+$controller = new Controller($request);
+
+echo $controller->display();
+
+?>

+ 3 - 0
js/scripts.js

@@ -0,0 +1,3 @@
+$(document).ready(function(){
+
+});

+ 11 - 0
templates/default.php

@@ -0,0 +1,11 @@
+<html>
+  <head>
+    <title>test</title>
+
+  </head>
+  <body>
+    <h1><?= $test; ?></h1>
+    <p>Jo</p>
+    <?= $errors; ?>
+  </body>
+</html>