.gitignore000064400000000036144761607060006546 0ustar00/vendor/ /tests/ composer.lockcomposer.json000064400000000701144761607060007277 0ustar00{ "name": "boru/dhapi", "type": "library", "autoload": { "psr-4": { "boru\\dhapi\\": "src/" } }, "authors": [ { "name": "Daniel Hayes", "email": "dhayes@boruapps.com" } ], "require": { "boru/dhutils": "^2." }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] } instructions-composer.txt000064400000000300144761607060011702 0ustar00{ "require": { "boru/dhapi": "*" }, "repositories": [ { "type": "composer", "url": "https://satis.boruapps.com" } ] }src/ApiInterface.php000064400000000205144761607060010406 0ustar00getHeader("x-api-key",false)) !== false) { $api->setUser(true,["all"=>true],["apiKey"=>$authKey]); return true; } return false; } }src/autoloaders/ClassPathLoader.php000064400000001261144761607060013412 0ustar00getBaseDir()); spl_autoload_register( [$this, 'load'] ); } public function load($className) { $parts = explode("_",strtolower($className)); //utilize dhLoader for the including parts.. dhLoader::includeOnce(implode(".",$parts)); if(!class_exists($className)) { echo json_encode(["error"=>true]); exit(); } } }src/dhAPI.php000064400000015370144761607060007012 0ustar00false, "permissions"=>[], "data"=>[] ]; public $validMethods = ["DELETE","POST","GET","PUT"]; public function __construct($options=[]) { if(($router = dhGlobal::getval($options,"router",false)) !== false) { $this->setRouter($router); } if(($autoloader = dhGlobal::getval($options,"autoloader",false)) !== false) { $this->setAutoloader($autoloader); } if(($authenticator = dhGlobal::getval($options,"authenticator",false)) !== false) { $this->setAutoloader($authenticator); } if(($requireAuth = dhGlobal::getval($options,"requireAuth",false)) !== false) { $this->setRequireAuth($requireAuth); } if(($baseDir = dhGlobal::getval($options,"baseDir",false)) !== false) { $this->setBaseDir($baseDir); } } public function init() { dhGlobal::debug("init"); $this->start_time = microtime(true); header("Access-Control-Allow-Orgin: *"); header("Access-Control-Allow-Methods: *"); header("Content-Type: application/json"); $this->request = dhRequest::fromGlobals(true); $this->initAutoloader(); $this->runAuthenticator(); $result = $this->runRouter(); if($result === false) { //we shouldn't get here unless router is set to false. } } public function runRouter() { if(is_callable($this->router)) { //if router is a callback $fn = $this->router; echo $fn($this); return; } elseif($this->router instanceof RouterInterface) { //if router is a defined router return $this->router->route($this); } elseif($this->router !== false) { //default router $this->router = new ClassPathRouter(); return $this->router->route($this); } return false; } public function runAuthenticator() { $authed = null; if(is_callable($this->authenticator)) { //if authenticator is a callback $fn = $this->authenticator; $authed = $fn($this); } elseif($this->authenticator instanceof AuthenticatorInterface) { $authed = $this->authenticator->authenticate($this); } elseif($this->authenticator !== false) { //default authenticator $this->authenticator = new BasicKey(); $authed = $this->authenticator->authenticate($this); } if($this->requireAuth && !$authed) { throw new \Exception("Not authenticated"); } } public function initAutoloader() { if(is_callable($this->autoloader)) { $fn = $this->autoloader; $fn($this); } elseif($this->autolaoder instanceof AutoloaderInterface) { $this->autoloader->init(); } elseif($this->autoloader !== false) { $this->autoloader = new ClassPathLoader($this); $this->autoloader->init($this); } } //request aliases public function getMethod() { return $this->request->getMethod(); } public function getURI($what=null,$default=null) { if(is_null($what)) { return $this->request->get("uri"); } return $this->request->get("uri.".$what,$default); } public function getQuery($what=null,$default=null) { if(is_null($what)) { return $this->request->get("get"); } return $this->request->get("get.".$what,$default); } public function getPost($what=null,$default=null) { if(is_null($what)) { return $this->request->get("post"); } return $this->request->get("post.".$what,$default); } public function getBody($what=null,$default=null) { if(is_null($what)) { return $this->request->get("body"); } return $this->request->get("body.".$what,$default); } public function getHeader($what=null,$default=null) { if(is_null($what)) { return $this->request->get("headers"); } return $this->request->get("headers.".$what,$default); } public function getServer($what=null,$default=null) { if(is_null($what)) { return $this->request->get("server"); } return $this->request->get("server.".$what,$default); } /** * Set the value of autoloader * * @param mixed $autoloader * @return self */ public function setAutoloader($autoloader) { $this->autoloader = $autoloader; return $this; } /** * Set the value of router * * @param mixed $router * @return self */ public function setRouter($router) { $this->router = $router; return $this; } /** * Set the value of authenticator * * @param mixed $authenticator * @return self */ public function setAuthenticator($authenticator) { $this->authenticator = $authenticator; return $this; } /** * Get the value of baseDir * * @return mixed */ public function getBaseDir() { return $this->baseDir; } /** * Set the value of baseDir * * @param mixed $baseDir * @return self */ public function setBaseDir($baseDir) { $this->baseDir = $baseDir; return $this; } public function setRequireAuth($reqAuth) { $this->requireAuth = $reqAuth; return $this; } public function getRequireAuth() { return $this->requireAuth; } public function isAuthed() { return dhGlobal::getVal($this->user,"authed",false); } public function getPerms($permissionKey=null,$default=false) { if(is_null($permissionKey)) { return dhGlobal::getVal($this->user,"permissions",$default); } return dhGlobal::getVal($this->user["permissions"],$permissionKey,$default); } public function setUser($authed=false,$permissions=[],$data=[]) { $this->user["authed"] = $authed; $this->user["permissions"] = $permissions; $this->user["data"] = $data; } }src/routers/ClassPathRouter.php000064400000001210144761607060012637 0ustar00getQuery("PATH",false)) !== false) { $path = dhGlobal::trimString("/", $path,dhGlobal::TRIM_BOTH); $path = dhGlobal::trimString(".php", $path,dhGlobal::TRIM_END); $path = ucwords($path,"/"); $parts = explode("/",$path); $handlerClass = implode("_",$parts); $route = new $handlerClass($api); return $route->process(); } } }src/routers/RegExRouter.php000064400000002641144761607060012000 0ustar00getQuery("PATH",false)) !== false) { $path = dhGlobal::trimString("/", $path,dhGlobal::TRIM_BOTH); $path = dhGlobal::trimString(".php", $path,dhGlobal::TRIM_END); foreach($this->routes as $regex=>$className) { if(preg_match($regex,$path,$matches) !== 0) { array_shift($matches); $route = new $className($api); return $route->process($matches); } } } if(!is_null($this->default)) { $className = $this->default; $route = new $className($api); return $route->process([]); } return false; } public function addRoute($regex,$className) { $this->routes[$regex]=$className; return $this; } /** * Set the value of default * * @param mixed $default * @return self */ public function defaultRoute($default) { $this->default = $default; return $this; } }test/init.php000064400000000073144761607060007212 0ustar00 RewriteEngine On RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule .+ - [L] RewriteRule ^(.+)/?$ index.php?PATH=$1&%{QUERY_STRING} [L] test/web/api/test/load.php000064400000000260144761607060011471 0ustar00"; } public function process() { echo "process!
"; } }test/web/api/whoami.php000064400000000467144761607060011070 0ustar00api = $api; } public function process($opts=[]) { $data = ["class"=>__CLASS__,"opts"=>$opts]; echo json_encode($data); } }test/web/index.php000064400000000455144761607060010137 0ustar00init(); */ $router = new RegExRouter(); $router->addRoute("/^whoami\/(?[0-9]+)$/","Whoami"); $api = new dhAPI(["router"=>$router]); $api->init();