Skip to content
Snippets Groups Projects
Commit eea7a809 authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

update api/index.php

parent 652d9b45
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
......@@ -11,6 +11,7 @@
* ----------------------------------------------------------------------
* 2020-06-16 v0.9 <axel.hahn@iml.unibe.ch>
* 2021-03-29 v1.2 <axel.hahn@iml.unibe.ch> support slashes in branch names
* 2024-09-02 v1.3 <axel.hahn@unibe.ch> php8 only; added variable types; short array syntax
* ======================================================================
*/
......@@ -19,51 +20,69 @@
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/**
* Path to deployment classes
* @var string
*/
$sDirClasses = __DIR__ . '/../deployment/classes/';
/**
* Allowed time delta for client or server
* @var integer
*/
$iMaxAge = 60;
require_once("../../config/inc_projects_config.php");
$sDirClasses=__DIR__.'/../deployment/classes/';
require_once($sDirClasses . '/project.class.php');
require_once($sDirClasses . 'logger.class.php');
$iMaxAge=60;
// ----------------------------------------------------------------------
// FUNCTIONS
// ----------------------------------------------------------------------
/**
* write debug text (if enabled)
* Write debug text (if enabled)
*
* @global boolean $bDebug
*
* @param string $s message
* @param string $sLevel level; one of info|
* @return boolean
*/
function _wd($s, $sLevel='info'){
function _wd(string $s, string $sLevel = 'info'): bool
{
global $bDebug;
if ($bDebug) {
echo '<div class="debug debug-'.$sLevel.'">DEBUG: '.$s.'</div>';
echo "<div class=\"debug debug-$sLevel\">DEBUG: $s</div>";
}
return true;
}
/**
* abort execution with error
* Abort execution of API requestwith error
*
* @param string $s message
* @param integer $iStatus http status code to send
*/
function _quit($s, $iStatus=400){
$aStatus=array(
function _quit(string $s, int $iStatus = 400): void
{
$aStatus = [
400 => 'HTTP/1.0 400 Bad Request',
403 => 'HTTP/1.0 403 Access denied',
404 => 'HTTP/1.0 404 Not found',
);
];
header($aStatus[$iStatus]);
_done(array('status'=>$iStatus, 'info'=>$aStatus[$iStatus], 'message'=>$s));
_done(['status' => $iStatus, 'info' => $aStatus[$iStatus], 'message' => $s]);
}
/**
* end with OK output
* @param type $Data
* End with OK output
*
* @param array $Data array data to show as JSON
* @return void
*/
function _done($Data){
function _done(array $Data): void
{
echo is_array($Data)
? json_encode($Data, JSON_PRETTY_PRINT)
: $Data
......@@ -74,11 +93,14 @@
/**
* Check authorization in the http request header and age of timestamp
* On a failed check the request will be terminated
*
* @global int $iMaxAge max allowed age
*
* @param string $sProjectSecret
* @return boolean
*/
function _checkAuth($sProjectSecret){
function _checkAuth(string $sProjectSecret): bool
{
global $iMaxAge;
$aReqHeaders = apache_request_headers();
_wd('<pre>' . print_r($aReqHeaders, 1) . '</pre>');
......@@ -167,7 +189,8 @@
$aList = $oProject->getProjects();
_wd('<pre>' . print_r($aList, 1) . '</pre>');
_done($aList);
break;;
break;
;
case 'project':
// path /api/v1/project
......@@ -225,26 +248,29 @@
// echo "branch is set to ".$oProject->getBranchname()."\n";
if ($sMethod === 'GET') {
$sNext = $oProject->getNextPhase();
_done(array(
_done([
'branch' => $sBranchname,
'phase' => $sNext,
'repo' => $aRepodata[$sBranchname]
));
]);
}
if ($sMethod === 'POST') {
echo "starting build() ...";
flush();
echo $oProject->build();
}
break;;
break;
;
case "phases":
_done($oProject->getAllPhaseInfos());
break;;
break;
;
default:
_quit('ERROR: Wrong action [' . $sApiItem . '].');
}
break;;
break;
;
default:
_quit('ERROR: item [' . $sApiItem . '] is invalid.');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment