diff --git a/assets/img/favicon.ico b/assets/img/favicon.ico new file mode 100644 index 0000000..7f909cb Binary files /dev/null and b/assets/img/favicon.ico differ diff --git a/assets/img/logo.png b/assets/img/logo.png new file mode 100644 index 0000000..97dd86a Binary files /dev/null and b/assets/img/logo.png differ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a6ad7a5 --- /dev/null +++ b/composer.json @@ -0,0 +1,13 @@ +{ + "name": "nbeij/phpcat", + "description": "A small basic framework", + "type": "project", + "license": "MIT", + "authors": [ + { + "name": "Noah Beij", + "email": "thedutchgunslinger@gmail.com" + } + ], + "require": {} +} diff --git a/core/autoload.php b/core/autoload.php new file mode 100644 index 0000000..e51321d --- /dev/null +++ b/core/autoload.php @@ -0,0 +1,38 @@ +$class = new $class(); + } + } + }); + + +?> diff --git a/core/classes/common.php b/core/classes/common.php new file mode 100644 index 0000000..ed79e04 --- /dev/null +++ b/core/classes/common.php @@ -0,0 +1,10 @@ + diff --git a/core/classes/controller.php b/core/classes/controller.php new file mode 100644 index 0000000..8e33ea2 --- /dev/null +++ b/core/classes/controller.php @@ -0,0 +1,9 @@ + diff --git a/core/classes/database.php b/core/classes/database.php new file mode 100644 index 0000000..29cab7d --- /dev/null +++ b/core/classes/database.php @@ -0,0 +1,146 @@ +obj = new mysqli($host, $username, $password); + }else{ + $this->obj = new mysqli($host, $username, $password, $database); + } + } + + function changeDB($database){ + $this->obj->select_db($database); + } + + function refValues($arr){ + if(strnatcmp(phpversion(), "5.3") >= 0){ + $refs = array(); + foreach($arr as $key => $value){ + $refs[$key] = &$arr[$key]; + } + return $refs; + } + return $arr; + } + + function query($query, $args = null){ + if(is_null($args)){ + $this->result = $this->obj->query($query); + $this->current_field = $this->result->current_field; + $this->lengths = $this->result->lengths; + $this->num_rows = $this->result->num_rows; + return $this->result; + }else{ + if(!is_array($args)){ + $argsBkp = $args; + $args = array($argsBkp); + } + if($stmt = $this->obj->prepare($query)){ + $datatypes = ""; + foreach($args as $value){ + if(is_int($value)){ + $datatypes .= "i"; + }else if(is_double($value)){ + $datatypes .= "d"; + }else if(is_string($value)){ + $datatypes .= "s"; + }else{ + $datatypes .= "b"; + } + } + array_unshift($args, $datatypes); + if(call_user_func_array(array($stmt, "bind_param"), $this->refValues($args))){ + $stmt->execute(); + + $this->result = $stmt->get_result(); + if($this->result){ + $this->current_field = $this->result->current_field; + $this->lengths = $this->result->lengths; + $this->num_rows = $this->result->num_rows; + }else{ + $this->current_field = ""; + $this->lengths = 0; + $this->num_rows = 0; + } + $this->error = $stmt->error; + return $this->result; + }else{ + $this->current_field = ""; + $this->lengths = 0; + $this->num_rows = 0; + return false; + } + }else{ + $this->current_field = ""; + $this->lengths = 0; + $this->num_rows = 0; + return false; + } + } + } + + + function data_seek($offset = 0){ + return $this->result->data_seek($offset); + } + + function fetch_all(){ + return $this->result->fetch_all(); + } + + function fetch_array(){ + return $this->result->fetch_array(); + } + + function fetch_assoc(){ + return $this->result->fetch_assoc(); + } + + function fetch_field_direct($field){ + return $this->result->fetch_field_direct($field); + } + + function fetch_field(){ + return $this->result->fetch_field(); + } + + function fetch_fields(){ + return $this->result->fetch_fields(); + } + + function fetch_object($class_name = "stdClass", $params = null){ + if(is_null($params)){ + return $this->result->fetch_object($class_name); + }else{ + return $this->result->fetch_object($class_name, $params); + } + } + + function fetch_row(){ + return $this->result->fetch_row(); + } + + function field_seek($field){ + return $this->result->field_seek($field); + } + + function insert_id(){ + return $this->result->insert_id; + } + + function fetch_all_kv(){ + $out = array(); + while($row = $this->result->fetch_assoc()){ + $out[] = $row; + } + return $out; + } + } +?> diff --git a/core/classes/error.php b/core/classes/error.php new file mode 100644 index 0000000..e8761f8 --- /dev/null +++ b/core/classes/error.php @@ -0,0 +1,9 @@ + diff --git a/core/classes/load.php b/core/classes/load.php new file mode 100644 index 0000000..ccdc92e --- /dev/null +++ b/core/classes/load.php @@ -0,0 +1,15 @@ + diff --git a/core/classes/model.php b/core/classes/model.php new file mode 100644 index 0000000..f1df74b --- /dev/null +++ b/core/classes/model.php @@ -0,0 +1,11 @@ +model = new database(); + $this->model->connect($GLOBALS["config"]["database"]["host"],$GLOBALS["config"]["database"]["username"], + $GLOBALS["config"]["database"]["password"],$GLOBALS["config"]["database"]["name"]); + } + } + ?> diff --git a/core/classes/router.php b/core/classes/router.php new file mode 100644 index 0000000..f46705a --- /dev/null +++ b/core/classes/router.php @@ -0,0 +1,72 @@ +routes = $GLOBALS["config"]["routes"]; + $route = $this->findRoute(); + if(class_exists($route["controller"])){ + $controller = new $route["controller"](); + if(method_exists($controller, $route["method"])){ + $controller->{$route["method"]}(); + }else{ + // error::show(404); + // echo "Error 404"; + load::view("main::error"); + } + }else{ + // error::show(404); + // echo "Error 404"; + load::view("main::error"); + } + } + + private function routePart($route){ + if(is_array($route)){ + $route = $route["url"]; + } + $parts = explode("/", $route); + return $parts; + } + + static function uri($part){ + $parts = explode("/",$_SERVER["REQUEST_URI"]); + if($parts[1] == $GLOBALS["config"]["path"]["index"]){ + $part++; + } + return (isset($parts[$part])) ? $parts[$part] : ""; + } + + private function findRoute(){ + foreach($this->routes as $route){ + $parts = $this->routePart($route); + $allMatch = true; + foreach($parts as $key => $value){ + if($value != "*"){ + if(router::uri($key) != $value){ + $allMatch = false; + } + } + } + if($allMatch){ + return $route; + } + } + $uri_1 = router::uri(1); + $uri_2 = router::uri(2); + if($uri_1 == ""){ + $uri_1 = $GLOBALS["config"]["defaults"]["controller"]; + } + if($uri_2 == ""){ + $uri_2 = $GLOBALS["config"]["defaults"]["method"]; + } + $route = array( + "controller" => $uri_1, + "method" => $uri_2 + ); + return $route; + } + + } +?> diff --git a/core/classes/session.php b/core/classes/session.php new file mode 100644 index 0000000..c38886f --- /dev/null +++ b/core/classes/session.php @@ -0,0 +1,79 @@ + $value){ + if(!isset($_SESSION[$key])){ + json_decode($value); + if(json_last_error() == JSON_ERROR_NONE){ + $_SESSION[$key] = json_decode($value); + }else{ + $_SESSION[$key] = $value; + } + } + } + } + static function check($key){ + if(is_array($key)){ + $set = true; + foreach($key as $k){ + if(!session::check($k)){ + $set = false; + } + } + return $set; + }else{ + $key = session::generateSessionKey($key); + return isset($_SESSION[$key]); + } + } + + static function get($key){ + if(isset($_SESSION[session::generateSessionKey($key)])){ + return $_SESSION[session::generateSessionKey($key)]; + }else{ + return false; + } + } + + static function set($key, $value, $ttl = 0){ + $_SESSION[session::generateSessionKey($key)] = $value; + if($ttl !== 0){ + if(is_object($value) || is_array($value)){ + $value = json_encode($value); + } + setcookie(session::generateSessionKey($key),$value,(time()+$ttl),"/",$_SERVER["HTTP_HOST"]); + } + } + + static function kill($key){ + if(isset($_SESSION[session::generateSessionKey($key)])){ + unset($_SESSION[session::generateSessionKey($key)]); + } + if(isset($_COOKIE[session::generateSessionKey($key)])){ + setcookie(session::generateSessionKey($key),"",(time()-5000),"/",$_SERVER[HTTP_HOST]); + } + } + + static function endSession(){ + foreach($_SESSION as $key => $value){ + unset($_SESSION[$key]); + } + foreach($_COOKIE as $key => $value){ + setcookie($key, "", (time()-5000),"/",$_SERVER[HTTP_HOST]); + } + session_destroy(); + } + + static function generateSessionKey($key){ + $append = $GLOBALS["config"]["appName"]; + $version = $GLOBALS["config"]["version"]; + return md5($key.$append.$version); + } + } + ?> diff --git a/core/classes/url.php b/core/classes/url.php new file mode 100644 index 0000000..c3846af --- /dev/null +++ b/core/classes/url.php @@ -0,0 +1,62 @@ + $param){ + $append .= ($append == "") ? "?" : "&"; + $append .= urlencode($key)."=".urlencode($param); + } + return $prefix.$append; + } + + static function simple($url){ + if(strpos($url,"//") === false){ + $prefix = "//".$GLOBALS["config"]["domain"]; + }else{ + $prefix = ""; + } + return $prefix; + } + + static function redir($to, $exit = true){ + if(headers_sent()){ + echo ""; + }else{ + header("location: {$to}"); + } + if($exit){ + die(); + } + } + } + ?> diff --git a/core/interfaces/controllerInterface.php b/core/interfaces/controllerInterface.php new file mode 100644 index 0000000..f3cdca2 --- /dev/null +++ b/core/interfaces/controllerInterface.php @@ -0,0 +1,5 @@ + diff --git a/core/process.php b/core/process.php new file mode 100644 index 0000000..405bc7d --- /dev/null +++ b/core/process.php @@ -0,0 +1,8 @@ + diff --git a/index.php b/index.php new file mode 100644 index 0000000..67a681c --- /dev/null +++ b/index.php @@ -0,0 +1,34 @@ + "PHPcat", //your app name + "version" => "0.1.0", //your app version + "domain" => "localhost:6969", //your domain + "timezone" => "europe/amsterdam", //your timezone + "devmode" => true, //error handling on or of + "path" => array( + "app" => "app/", + "core" => "core/", + "session" => "app/sessions/", + "basePath" => "/Users/yourname/Documents/your-app", //your server path this is the example + "index" => "index.php" + ), + "defaults" => array( + "controller" => "main", + "method" => "index" + ), + "routes" => array(), + "database" => array( //your database connection + "host" => "localhost", + "username" => "root", + "password" => "", + "name" => "your-database" + ) + ); + + $GLOBALS["instances"] = array(); + require_once $GLOBALS["config"]["path"]["core"]."autoload.php"; + require_once $GLOBALS["config"]["path"]["core"]."process.php"; + + new router(); + + ?>