Primary build of JSON responses with error messages and test responses
This commit is contained in:
parent
53dbbe4b5b
commit
e6149ec3db
16 changed files with 334 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
config/config.ini
|
||||||
|
config/services.ini
|
17
config/config.sample.ini
Normal file
17
config/config.sample.ini
Normal file
|
@ -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"
|
19
config/services.sample.ini
Normal file
19
config/services.sample.ini
Normal file
|
@ -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
|
3
core/db/db.php
Normal file
3
core/db/db.php
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<?php
|
||||||
|
echo "No Db...yet.";
|
||||||
|
?>
|
51
core/init.php
Normal file
51
core/init.php
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
// this class starts the whole thing going.
|
||||||
|
class StartUp {
|
||||||
|
public function start (){
|
||||||
|
// define a constant for checking later on
|
||||||
|
define("LETSGO",true);
|
||||||
|
|
||||||
|
// require the Load class which also imports the Core class
|
||||||
|
require("init/loader.php");
|
||||||
|
|
||||||
|
// Not sure if we're using a database yet, but leaving this here.
|
||||||
|
//require("db/db.php");
|
||||||
|
|
||||||
|
// Get the config
|
||||||
|
self::get_config();
|
||||||
|
|
||||||
|
// A Session may not be necessary as this is fundamentally an API
|
||||||
|
//Core::StartSession();
|
||||||
|
|
||||||
|
// Continue and load the requested page
|
||||||
|
$loader = new Loader();
|
||||||
|
$loader->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();
|
||||||
|
}
|
||||||
|
}
|
57
core/init/core.php
Normal file
57
core/init/core.php
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
<?php class Core {
|
||||||
|
public static $Config;
|
||||||
|
public static $CurrentPage;
|
||||||
|
public static function root_dir(){
|
||||||
|
return $_SERVER['DOCUMENT_ROOT'];
|
||||||
|
}
|
||||||
|
public static function get_current_page_name(){
|
||||||
|
if (!isset(self::$CurrentPage->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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
32
core/init/errors.php
Normal file
32
core/init/errors.php
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
<?php
|
||||||
|
class Errors {
|
||||||
|
public function throw_error($err){
|
||||||
|
header('Content-Type: application/json'); // <-- header declaration
|
||||||
|
$error = false;
|
||||||
|
switch ($err) {
|
||||||
|
case "NotFound":
|
||||||
|
$error = new ErrorMessage("Error","404","Not Found");
|
||||||
|
break;
|
||||||
|
case "Unauthorized":
|
||||||
|
$error = new ErrorMessage("Error","402","Unauthorized");
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
$error = new ErrorMessage("Error","500","Internal Error");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
echo json_encode($error, true); // <--- encode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
class ErrorMessage {
|
||||||
|
public $error;
|
||||||
|
public $code;
|
||||||
|
public $message;
|
||||||
|
public $commit;
|
||||||
|
public function __construct($err,$code,$msg){
|
||||||
|
$this->error = $err;
|
||||||
|
$this->code = $code;
|
||||||
|
$this->message = $msg;
|
||||||
|
$this->url = Core::$CurrentPage;
|
||||||
|
$this->commit = Core::$Config["CommitID"];
|
||||||
|
}
|
||||||
|
}
|
44
core/init/loader.php
Normal file
44
core/init/loader.php
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php
|
||||||
|
require ("core.php");
|
||||||
|
require ("user.php");
|
||||||
|
require ("responder.php");
|
||||||
|
require ("errors.php");
|
||||||
|
class Loader {
|
||||||
|
public function request_page(){
|
||||||
|
// Check if user is authenticated and go to the relevant section
|
||||||
|
if (User::is_authenticated()){
|
||||||
|
$this->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;
|
||||||
|
}
|
||||||
|
}
|
79
core/init/responder.php
Normal file
79
core/init/responder.php
Normal file
|
@ -0,0 +1,79 @@
|
||||||
|
<?php
|
||||||
|
class Responder {
|
||||||
|
public function show_response(){
|
||||||
|
// get the response detailed by the url requested
|
||||||
|
$response = $this->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"];
|
||||||
|
}
|
||||||
|
}
|
9
core/init/user.php
Normal file
9
core/init/user.php
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
<?php
|
||||||
|
class User {
|
||||||
|
public static function is_authenticated(){
|
||||||
|
// lots of work to do here to handle authentication.
|
||||||
|
// Currently just returns true for testing purposes
|
||||||
|
// I intend to make this authenticate against the primary IMAP server
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
3
index.php
Normal file
3
index.php
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<?php
|
||||||
|
// entry point - inport and run the public index.php file
|
||||||
|
require("public/index.php");
|
5
public/error.php
Normal file
5
public/error.php
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
//return the json response :
|
||||||
|
$e = new Errors();
|
||||||
|
$e->throw_error("NotFound");
|
||||||
|
exit();
|
3
public/index.php
Normal file
3
public/index.php
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
<?php include("core/init.php");
|
||||||
|
$b = new StartUp();
|
||||||
|
$b->start();
|
5
public/respond.php
Normal file
5
public/respond.php
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
//return the json response :
|
||||||
|
$e = new Responder();
|
||||||
|
$e->show_response();
|
||||||
|
exit();
|
5
public/unauthorized.php
Normal file
5
public/unauthorized.php
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?php
|
||||||
|
//return the json response :
|
||||||
|
$e = new Errors();
|
||||||
|
$e->throw_error("Unauthorized");
|
||||||
|
exit();
|
Loading…
Reference in a new issue