12 namespace Fossology\UI\Api\Controllers;
25 use Psr\Container\ContainerInterface;
26 use Psr\Http\Message\ServerRequestInterface as Request;
27 use Slim\Psr7\Factory\StreamFactory;
64 $this->compatibilityDao = $this->container->get(
'dao.compatibility');
76 public function getRules($request, $response, $args)
80 $query = $request->getQueryParams();
85 $limit = filter_var($limit, FILTER_VALIDATE_INT);
86 if ($limit ===
false || $limit < 1) {
88 "limit should be positive integer > 1");
95 if (! is_string($searchTerm)) {
98 $searchTerm =
trim($searchTerm);
99 if (! empty($searchTerm)) {
100 $searchTerm =
"%" . $searchTerm .
"%";
103 $totalPages = $this->compatibilityDao->getTotalRulesCount($searchTerm);
104 $totalPages = intval(ceil($totalPages / $limit));
108 $page = filter_var($page, FILTER_VALIDATE_INT);
109 if ($page ===
false || $page < 1) {
111 "page should be positive integer > 0");
113 if ($totalPages != 0 && $page > $totalPages) {
115 "Can not exceed total pages: $totalPages"))
116 ->setHeaders([
"X-Total-Pages" => $totalPages]);
123 foreach ($this->compatibilityDao->getAllRules($limit,
124 $limit * ($page - 1), $searchTerm) as $row) {
126 ->getArray($apiVersion);
128 return $response->withHeader(
"X-Total-Pages", $totalPages)
129 ->withJson($rules, 200);
146 $ruleId = $this->compatibilityDao->insertRule($rule[
"firstLic"],
147 $rule[
"secondLic"], $rule[
"firstType"], $rule[
"secondType"],
148 $rule[
"comment"], $rule[
"result"]);
151 "Unable to create the compatibility rule.");
154 $info =
new Info(201,
"Rule $ruleId added successfully.", InfoType::INFO);
155 return $response->withJson($info->getArray(), $info->getCode());
170 $ruleId = intval($args[
'id']);
171 if ($this->compatibilityDao->getRuleById($ruleId) ===
null) {
181 $updated = $this->compatibilityDao->updateRuleFromArray([$ruleId => $rule]);
182 }
catch (\UnexpectedValueException $e) {
187 "Unable to update the compatibility rule.");
190 $info =
new Info(200,
"Rule $ruleId updated successfully.", InfoType::INFO);
191 return $response->withJson($info->getArray(), $info->getCode());
206 $ruleId = intval($args[
'id']);
207 if ($this->compatibilityDao->getRuleById($ruleId) ===
null) {
211 if (! $this->compatibilityDao->deleteRule($ruleId)) {
213 "Unable to delete the compatibility rule.");
216 $info =
new Info(200,
"Rule $ruleId deleted successfully.", InfoType::INFO);
217 return $response->withJson($info->getArray(), $info->getCode());
229 public function importRules($request, $response, $args)
234 $symReq = \Symfony\Component\HttpFoundation\Request::createFromGlobals();
236 $adminLicenseFromYaml = $this->restHelper->getPlugin(
'admin_license_from_yaml');
238 $uploadedFile = $symReq->files->get(
239 $adminLicenseFromYaml->getFileInputName($apiVersion),
null);
241 $res = $adminLicenseFromYaml->handleFileUpload($uploadedFile,
true);
246 $newInfo =
new Info($res[2], $res[1], InfoType::INFO);
247 return $response->withJson($newInfo->getArray(), $newInfo->getCode());
262 $query = $request->getQueryParams();
264 if (array_key_exists(
'id', $query)) {
265 $ruleId = intval($query[
'id']);
268 $this->compatibilityDao->getRuleById($ruleId) ===
null) {
273 $this->dbHelper->getDbManager(), $this->compatibilityDao);
274 $content = $licenseYamlExport->createYaml($ruleId);
275 $fileName =
"fossology-license-comp-rules-export-" . date(
"YMj-Gis");
276 $newResponse = $response
277 ->withHeader(
'Content-type',
'text/x-yaml; charset=UTF-8')
278 ->withHeader(
'Content-Disposition',
279 'attachment; filename=' . $fileName .
'.yaml')
280 ->withHeader(
'Pragma',
'no-cache')
281 ->withHeader(
'Cache-Control',
282 'no-cache, must-revalidate, maxage=1, post-check=0, pre-check=0')
283 ->withHeader(
'Expires',
'Expires: Thu, 19 Nov 1981 08:52:00 GMT');
284 $sf =
new StreamFactory();
285 return $newResponse->withBody(
286 $content ? $sf->createStream($content) : $sf->createStream(
'')
305 if (empty($body) || ! is_array($body)) {
310 list($exists, $value) = $this->
getRuleField($body,
"firstLicenseId",
315 list($exists, $value) = $this->
getRuleField($body,
"secondLicenseId",
316 "second_license_id");
320 list($exists, $value) = $this->
getRuleField($body,
"firstType",
325 list($exists, $value) = $this->
getRuleField($body,
"secondType",
330 if (array_key_exists(
"comment", $body)) {
331 if (! is_string($body[
"comment"]) || empty(
trim($body[
"comment"]))) {
333 "comment should be a non-empty string.");
335 $rule[
"comment"] =
trim($body[
"comment"]);
337 if (array_key_exists(
"compatibility", $body)) {
338 $compatibility = filter_var($body[
"compatibility"],
339 FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
340 if ($compatibility ===
null) {
343 $rule[
"result"] = $compatibility;
349 foreach ([
"comment",
"result"] as $mandatory) {
350 if (! array_key_exists($mandatory, $rule)) {
352 "required to create a rule.");
355 $rule += [
"firstLic" =>
null,
"secondLic" =>
null,
"firstType" =>
null,
356 "secondType" =>
null];
357 if ($rule[
"firstLic"] ===
null && $rule[
"secondLic"] ===
null &&
358 $rule[
"firstType"] ===
null && $rule[
"secondType"] ===
null) {
360 "is required to create a rule.");
377 if (array_key_exists($nameV2, $body)) {
378 return [
true, $body[$nameV2]];
380 if (array_key_exists($nameV1, $body)) {
381 return [
true, $body[$nameV1]];
383 return [
false,
null];
395 if ($licenseId ===
null || $licenseId ===
"") {
398 $licenseId = filter_var($licenseId, FILTER_VALIDATE_INT);
399 if ($licenseId ===
false || $licenseId < 1) {
402 if (! $this->dbHelper->doesIdExist(
"license_ref",
"rf_pk", $licenseId)) {
404 "No license found with id '$licenseId'.");
418 if ($licenseType ===
null || $licenseType ===
"") {
421 if (! is_string($licenseType)) {
424 $licenseType =
trim($licenseType);
426 if (! in_array($licenseType, $licenseTypes)) {
428 "allowed values are: " . implode(
", ", $licenseTypes) .
".");
441 $licenseTypes = $SysConf[
'SYSCONFIG'][
'LicenseTypes'] ??
"";
442 return array_filter(array_map(
'trim', explode(
',', $licenseTypes)));
Helper class to export license list as a YAML from the DB.
Controller for license compatibility rules.
getRules($request, $response, $args)
createRule($request, $response, $args)
getLicenseTypes()
Get the license types configured on the server.
validateLicenseType($licenseType, $field)
Validate a license type sent in the request body.
getRuleField($body, $nameV2, $nameV1)
Read a rule field from the request body.
parseRule($body, $isNewRule)
Validate the rule values sent in the request body.
deleteRule($request, $response, $args)
validateLicenseId($licenseId, $field)
Validate a license ID sent in the request body.
updateRule($request, $response, $args)
exportRules($request, $response, $args)
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Different type of infos provided by REST.
Info model to contain general error and return values.
Model to hold a single license compatibility rule.
char * trim(char *ptext)
Trimming whitespace.