FOSSology  4.4.0
Open Source License Compliance by Open Source Software
InfoController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019, 2021 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>, Soham Banerjee <sohambanerjee4abc@hotmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 namespace Fossology\UI\Api\Controllers;
14 
19 use Psr\Http\Message\ServerRequestInterface;
20 use Symfony\Component\Yaml\Exception\ParseException;
21 use Symfony\Component\Yaml\Parser;
22 
28 {
37  public function getInfo($request, $response)
38  {
39  global $SysConf;
40  try {
41  $yaml = new Parser();
42  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
43  $yamlDocArray = $yaml->parse(file_get_contents(
44  dirname(__DIR__) . "/documentation/openapiv2.yaml"));
45  } else {
46  $yamlDocArray = $yaml->parse(file_get_contents(
47  dirname(__DIR__) . "/documentation/openapi.yaml"));
48  }
49  } catch (ParseException $exception) {
50  printf("Unable to parse the YAML string: %s", $exception->getMessage());
51  throw new HttpInternalServerErrorException("Unable to read openapi.yaml",
52  $exception);
53  }
54  $apiTitle = $yamlDocArray["info"]["title"];
55  $apiDescription = $yamlDocArray["info"]["description"];
56  $apiVersion = $yamlDocArray["info"]["version"];
57  $apiContact = $yamlDocArray["info"]["contact"]["email"];
58  $apiLicense = $yamlDocArray["info"]["license"];
59  $security = array();
60  foreach ($yamlDocArray["security"] as $secMethod) {
61  $security[] = key($secMethod);
62  }
63  $fossInfo = [
64  "version" => null,
65  "branchName" => null,
66  "commitHash" => null,
67  "commitDate" => null,
68  "buildDate" => null
69  ];
70  if (array_key_exists('BUILD', $SysConf)) {
71  $fossInfo["version"] = $SysConf['BUILD']['VERSION'];
72  $fossInfo["branchName"] = $SysConf['BUILD']['BRANCH'];
73  $fossInfo["commitHash"] = $SysConf['BUILD']['COMMIT_HASH'];
74  if (strcasecmp($SysConf['BUILD']['COMMIT_DATE'], "unknown") != 0) {
75  $fossInfo["commitDate"] = date(DATE_ATOM,
76  strtotime($SysConf['BUILD']['COMMIT_DATE']));
77  }
78  if (strcasecmp($SysConf['BUILD']['BUILD_DATE'], "unknown") != 0) {
79  $fossInfo["buildDate"] = date(DATE_ATOM,
80  strtotime($SysConf['BUILD']['BUILD_DATE']));
81  }
82  }
83  return $response->withJson(array(
84  "name" => $apiTitle,
85  "description" => $apiDescription,
86  "version" => $apiVersion,
87  "security" => $security,
88  "contact" => $apiContact,
89  "license" => [
90  "name" => $apiLicense["name"],
91  "url" => $apiLicense["url"]
92  ],
93  "fossology" => $fossInfo
94  ), 200);
95  }
96 
105  public function getHealth($request, $response, $args)
106  {
107  $dbStatus = ($args === -1) ? "ERROR" : "OK";
108  $schedStatus = "OK";
109  if (! fo_communicate_with_scheduler("status", $output, $error_msg)
110  && strstr($error_msg, "Connection refused") !== false) {
111  $schedStatus = "ERROR";
112  }
113  $status = "OK";
114  $statusCode = 200;
115  if ($schedStatus !== "OK") {
116  $status = "WARN";
117  }
118  if ($dbStatus !== "OK") {
119  $status = "ERROR";
120  $statusCode = 503;
121  }
122  return $response->withJson(array(
123  "status" => $status,
124  "scheduler" => [
125  "status" => $schedStatus
126  ],
127  "db" => [
128  "status" => $dbStatus
129  ]
130  ), $statusCode);
131  }
132 
141  public function getOpenApi($request, $response)
142  {
143  $isJsonRequest = false;
144  $requestFormat = $request->getHeader("Accept");
145  if (!empty($requestFormat) && !empty($requestFormat[0])) {
146  $requestFormat = $requestFormat[0];
147  } else {
148  $requestFormat = "";
149  }
150  if (strcasecmp($requestFormat, "application/vnd.oai.openapi+json") === 0
151  || strcasecmp($requestFormat, "application/json") === 0) {
152  $isJsonRequest = true;
153  }
154 
155  if (ApiVersion::getVersion($request) == ApiVersion::V2) {
156  $yamlContent = file_get_contents(
157  dirname(__DIR__) . "/documentation/openapiv2.yaml");
158  } else {
159  $yamlContent = file_get_contents(
160  dirname(__DIR__) . "/documentation/openapi.yaml");
161  }
162  if ($isJsonRequest) {
163  try {
164  $yaml = new Parser();
165  $yamlDocArray = $yaml->parse($yamlContent);
166  } catch (ParseException $exception) {
167  printf("Unable to parse the YAML string: %s", $exception->getMessage());
168  throw new HttpInternalServerErrorException("Unable to read openapi.yaml",
169  $exception);
170  }
171  return $response
172  ->withHeader("Content-Disposition", "inline; filename=\"openapi.json\"")
173  ->withJson($yamlDocArray, 200);
174  }
175  if (empty($yamlContent)) {
176  throw new HttpInternalServerErrorException("Unable to read openapi.yaml");
177  }
178  $response->getBody()->write($yamlContent);
179  return $response
180  ->withHeader("Content-Type", "application/vnd.oai.openapi;charset=utf-8")
181  ->withHeader("Content-Disposition", "inline; filename=\"openapi.yaml\"")
182  ->withStatus(200);
183  }
184 }
Controller for REST API version.
Base controller for REST calls.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Definition: ApiVersion.php:29
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.