From 55e46a4f3595f10ddf96c92113fdcdf12003d34d Mon Sep 17 00:00:00 2001 From: hahn <axel.hahn@iml.unibe.ch> Date: Thu, 11 Jun 2020 22:17:37 +0200 Subject: [PATCH] task#3869 - update sws class; initial stuff for API --- public_html/api/index.php | 138 +++++++++++++++++++ public_html/deployment/classes/sws.class.php | 2 +- 2 files changed, 139 insertions(+), 1 deletion(-) create mode 100644 public_html/api/index.php diff --git a/public_html/api/index.php b/public_html/api/index.php new file mode 100644 index 00000000..aa9d1cc3 --- /dev/null +++ b/public_html/api/index.php @@ -0,0 +1,138 @@ +<?php +/* + * IDEA TO REALIZE: + * + * /api/v1/projects/ + * /api/v1/project/[Name]/[token]/build[/[name-of-branch]] + * /api/v1/project/[Name]/[token]/accept/[phase] + * + */ + + $bDebug=true; + ini_set('display_errors', 1); + ini_set('display_startup_errors', 1); + error_reporting(E_ALL); + + require_once("../../config/inc_projects_config.php"); + require_once(__DIR__.'/../deployment/classes/project.class.php'); + + $aApiItems=array( + 'project', + 'projects', + 'help', + ); + + // ---------------------------------------------------------------------- + // FUNCTIONS + // ---------------------------------------------------------------------- + function _wd($s, $sLevel='info'){ + global $bDebug; + if ($bDebug){ + echo '<div class="debug ">DEBUG: '.$s.'</div>'; + } + return true; + } + + function _quit($s, $iStatus=400){ + $aStatus=array( + 400=>'HTTP/1.0 400 Bad Request', + 404=>'HTTP/1.0 403 Access denied', + ); + header($aStatus[$iStatus]); + _done(array('status'=>$iStatus, 'info'=>$aStatus[$iStatus], 'message'=>$s)); + } + + function _done($Data){ + echo is_array($Data) + ? json_encode($Data, 1, JSON_PRETTY_PRINT) + : $Data + ; + die(); + } + + // ---------------------------------------------------------------------- + // MAIN + // ---------------------------------------------------------------------- + + _wd('Start: '.date('Y-m-d H:i:s').'<style>body{background:#eee; color:#456;} + .debug{background:#ddd; margin-bottom: 2px;} + </style>'); + + _wd('request uri is '.$_SERVER["REQUEST_URI"]); + _wd('<pre>GET: '.print_r($_GET, 1).'</pre>'); + + + // ---------- SPLIT URL + + $aUriSplit= explode('/', preg_replace('/\?.*$/', '', $_SERVER["REQUEST_URI"])); + + array_shift($aUriSplit); + array_shift($aUriSplit); + _wd('<pre>$aUriSplit: '.print_r($aUriSplit, 1).'</pre>'); + /* + + /api/v1/projects/ci/my-token/build?auth=123 + $aUriSplit: Array + ( + [0] => v1 + [1] => projects + [2] => ci + [3] => my-token + [4] => build + ) + */ + $sApiVersion = isset($aUriSplit[0]) ? $aUriSplit[0] : false; + $sApiItem = isset($aUriSplit[1]) ? $aUriSplit[1] : false; + + + if(!$sApiVersion){ + _quit('no paramm for api version was found.'); + } + if(!$sApiItem){ + _quit('ERROR: no paramm for item was found.'); + } + if(!in_array($sApiItem, $aApiItems)){ + _quit('ERROR: item ['.$sApiItem.'] is invalid.'); + } + + + + switch ($sApiVersion){ + case 'v1': + switch($sApiItem){ + case 'projects': + + $oProject=new project(); + $aList=$oProject->getProjects(); + _wd('<pre>'.print_r($aList,1).'</pre>'); + _done($aList); + break;; + case 'project': + + + $sPrjId = isset($aUriSplit[2]) ? $aUriSplit[2] : false; + $sPrjToken = isset($aUriSplit[3]) ? $aUriSplit[3] : false; + $sPrjAction = isset($aUriSplit[4]) ? $aUriSplit[4] : false; + _wd('$sPrjId = '.$sPrjId); + _wd('$sPrjToken = '.$sPrjToken); + _wd('$sPrjAction = '.$sPrjAction); + + $oProject=new project(); + if(!in_array($sPrjId, $oProject->getProjects())){ + _quit('ERROR: project with id ['.$sPrjId.'] does not exist.'); + } + + $oProject->setProjectById($sPrjId); + _wd('TODO: verify given token with that in the config.'); + + $aPrjCfg=$oProject->getConfig(); + _wd('<pre>'.print_r($aPrjCfg, 1).'</pre>'); + + break;; + default: + _quit('ERROR: Wrong item ['.$sApiItem.'].'); + } + break; + default: + _quit('ERROR: Wrong (unsupported) api version.'); + } \ No newline at end of file diff --git a/public_html/deployment/classes/sws.class.php b/public_html/deployment/classes/sws.class.php index 4833108e..433ab7c4 100644 --- a/public_html/deployment/classes/sws.class.php +++ b/public_html/deployment/classes/sws.class.php @@ -344,7 +344,7 @@ class sws { * @param type $sError */ private function _quit($sError, $sMore = "") { - header("Status: 400 Bad Request"); + header("HTTP1.0 400 Bad Request"); echo $this->_wrapAsWebpage( $sMore, '<div class="error">' . $sError . '</div>' ); -- GitLab