From e6149ec3db0c4122b66c0f51ada04c145058773e Mon Sep 17 00:00:00 2001 From: Paul Wilde Date: Tue, 10 Aug 2021 14:21:38 +0100 Subject: [PATCH] Primary build of JSON responses with error messages and test responses --- .gitignore | 2 + config/config.sample.ini | 17 ++++++++ config/services.sample.ini | 19 +++++++++ core/db/db.php | 3 ++ core/init.php | 51 ++++++++++++++++++++++++ core/init/core.php | 57 +++++++++++++++++++++++++++ core/init/errors.php | 32 +++++++++++++++ core/init/loader.php | 44 +++++++++++++++++++++ core/init/responder.php | 79 ++++++++++++++++++++++++++++++++++++++ core/init/user.php | 9 +++++ index.php | 3 ++ public/error.php | 5 +++ public/index.php | 3 ++ public/respond.php | 5 +++ public/unauthorized.php | 5 +++ src/index.php | 0 16 files changed, 334 insertions(+) create mode 100644 .gitignore create mode 100644 config/config.sample.ini create mode 100644 config/services.sample.ini create mode 100644 core/db/db.php create mode 100644 core/init.php create mode 100644 core/init/core.php create mode 100644 core/init/errors.php create mode 100644 core/init/loader.php create mode 100644 core/init/responder.php create mode 100644 core/init/user.php create mode 100644 index.php create mode 100644 public/error.php create mode 100644 public/index.php create mode 100644 public/respond.php create mode 100644 public/unauthorized.php delete mode 100644 src/index.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da59d2d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config/config.ini +config/services.ini diff --git a/config/config.sample.ini b/config/config.sample.ini new file mode 100644 index 0000000..d5ecc51 --- /dev/null +++ b/config/config.sample.ini @@ -0,0 +1,17 @@ +; Sample config.ini file. +; Copy this file to "config/config.ini" and adjust the settings to your requirements + +; Admin User configuration +; not in use yet +;[AdminUser] +;User = "admin" +;Password = "admin" + +; Database configuration +; not in use yet +;[Database] +;Type = "psql" +;Host = "localhost" +;Port = 5432 +;User = "davdiscover" +;Password = "davdiscover" diff --git a/config/services.sample.ini b/config/services.sample.ini new file mode 100644 index 0000000..1464ae0 --- /dev/null +++ b/config/services.sample.ini @@ -0,0 +1,19 @@ +[InMail] +Server = "imap.example.com" +Protocol = "IMAP" +Port = 993 +TLS = true + +[OutMail] +Server = "smtp.example.com" +Protocol = "SMTP" +Port = 465 +TLS = true + +[CalDav] +Server = "https://caldav.example.com/etc/etc/" +Port = 443 + +[CardDav] +Server = "https://carddav.example.com/etc/etc/" +Port = 443 diff --git a/core/db/db.php b/core/db/db.php new file mode 100644 index 0000000..4aaa5b7 --- /dev/null +++ b/core/db/db.php @@ -0,0 +1,3 @@ + diff --git a/core/init.php b/core/init.php new file mode 100644 index 0000000..55124af --- /dev/null +++ b/core/init.php @@ -0,0 +1,51 @@ +request_page(); + } + private function get_config(){ + // define the config file location + $config = Core::root_dir()."/config/config.ini"; + + // if it doesn't exist, use the sample file + // YOU REALLY SHOULD HAVE YOUR OWN CONFIG FILE!!! + + if (!file_exists($config)) { + $config = Core::root_dir()."/config/config.sample.ini"; + } + + // define services file location + $services = Core::root_dir()."/config/services.ini"; + + // if it doesn't exist, use the sample file + // YOU REALLY SHOULD HAVE YOUR OWN SERVICES FILE!!! + + if (!file_exists($services)) { + $services = Core::root_dir()."/config/services.sample.ini"; + } + + // Store the config settings in the Core::Config variable + // the "true" means it's going to parse the headers as well. + Core::$Config = parse_ini_file($config, true); + Core::$Config["Services"] = parse_ini_file($services, true); + Core::$Config["CommitID"] = Core::get_current_git_commit(); + } +} diff --git a/core/init/core.php b/core/init/core.php new file mode 100644 index 0000000..09db228 --- /dev/null +++ b/core/init/core.php @@ -0,0 +1,57 @@ +Name)){ + return "Login"; + } else { + return self::$CurrentPage->Name; + } + } + public static function get_post_data(){ + $data = []; + foreach($_POST as $key=>$value){ + $data[$key] = $value; + } + return $data; + } + public static function get_get_data(){ + $data = []; + foreach($_GET as $key=>$value){ + $data[$key] = $value; + } + return $data; + } + public static function random_string($length = 10) { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; + } + public static function start_session(){ + session_start(); + } + public static function end_session($redirect = true){ + // Unset all of the session variables + $_SESSION = array(); + setcookie("user_session", "", time() - 3600); + // Destroy the session. + session_destroy(); + if($redirect){ + header("location: /?e=LogoutSuccess"); + } + } + public static function get_current_git_commit( $branch='master' ) { + if ( $hash = file_get_contents( sprintf( '.git/refs/heads/%s', $branch ) ) ) { + return trim($hash); + } else { + return false; + } + } +} diff --git a/core/init/errors.php b/core/init/errors.php new file mode 100644 index 0000000..c3ec731 --- /dev/null +++ b/core/init/errors.php @@ -0,0 +1,32 @@ +error = $err; + $this->code = $code; + $this->message = $msg; + $this->url = Core::$CurrentPage; + $this->commit = Core::$Config["CommitID"]; + } +} diff --git a/core/init/loader.php b/core/init/loader.php new file mode 100644 index 0000000..b16e1d3 --- /dev/null +++ b/core/init/loader.php @@ -0,0 +1,44 @@ +go_to_page(true); + } else { + $this->go_to_page(false); + } + } + public function go_to_page($authenticated) { + switch ($authenticated) { + case false: + require(Core::root_dir()."/public/unauthorized.php"); + break; + default: + $p = $this->get_page_name(); + if (substr($p,0,6) != "/admin") { + header('Content-Type: application/json'); // <-- header declaration + } + // if($p != "none") { + Core::$CurrentPage = substr($p,1); + require_once(Core::root_dir()."/public/respond.php"); + // } else { + // Core::$CurrentPage = "Error"; + // require_once(Core::RootDir()."/public/error.php"); + // } + break; + } + } + public function get_page_name(){ + $uri = Core::get_get_data(); + $page = "/none"; + if(isset($uri["page"])){ + $page = parse_url($uri["page"]); + $page = $page["path"]; + } + return $page; + } +} diff --git a/core/init/responder.php b/core/init/responder.php new file mode 100644 index 0000000..8671c08 --- /dev/null +++ b/core/init/responder.php @@ -0,0 +1,79 @@ +get_response(); + if ($response){ + // send response + $this->send_response($response); + } else { + // Send error instead + $e = new Errors(); + $e->throw_error("NotFound"); + } + } + private function send_response($response){ + // Send json encoded response + echo json_encode($response, true); + } + private function get_response(){ + $resp = false; + switch (Core::$CurrentPage){ + case "get/test": + $resp = $this->dummy_response(); + break; + case "get/all": + $resp = $this->all_urls(); + break; + case "none": + case "test": + case "home": + case "root": + $resp = $this->get_test_working(); + break; + default: + break; + } + return $resp; + } + private function all_urls(){ + + // This would be the default request from, say, an app. + $response = new Response(); + + // TODO:: Will work out a better message later + $response->message = "All URLs Requested"; + + // Cycle through each service and add to payload + foreach (Core::$Config["Services"] as $key => $service){ + $response->payload[$key] = $service; + } + + return $response; + } + private function dummy_response(){ + // Generate a dummy response for testing + $response = new Response(); + $response->message = "OK, here's some scrumptious data! Enjoy!"; + $response->payload = array("data" => array("some_data" => "Ohhhhhmmmm nom nom nom nom nom nom", + "extra_data" => array("garnish" => "buuuuuuuuuuurp")), + "more_data" => "yuuuuuum yum yum yum"); + return $response; + } + private function get_test_working(){ + // Generate a dummy response for testing + $response = new Response(); + $response->message = "Success! Things are working! Please request a valid URL i.e. get/all"; + return $response; + } +} +class Response { + public $url; + public $message; + public $payload = array(); + public function __construct(){ + // add requested page to response. I don't know why, but it could helpful for diagnostics at some point + $this->url = Core::$CurrentPage; + $this->commit = Core::$Config["CommitID"]; + } +} diff --git a/core/init/user.php b/core/init/user.php new file mode 100644 index 0000000..5a19619 --- /dev/null +++ b/core/init/user.php @@ -0,0 +1,9 @@ +throw_error("NotFound"); +exit(); diff --git a/public/index.php b/public/index.php new file mode 100644 index 0000000..cea0619 --- /dev/null +++ b/public/index.php @@ -0,0 +1,3 @@ +start(); diff --git a/public/respond.php b/public/respond.php new file mode 100644 index 0000000..265a9e2 --- /dev/null +++ b/public/respond.php @@ -0,0 +1,5 @@ +show_response(); +exit(); diff --git a/public/unauthorized.php b/public/unauthorized.php new file mode 100644 index 0000000..c3ea0ce --- /dev/null +++ b/public/unauthorized.php @@ -0,0 +1,5 @@ +throw_error("Unauthorized"); +exit(); diff --git a/src/index.php b/src/index.php deleted file mode 100644 index e69de29..0000000