FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SearchController.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2018 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
13 namespace Fossology\UI\Api\Controllers;
14 
20 use Psr\Http\Message\ServerRequestInterface;
21 
27 {
29  private $searchHelperDao;
30 
40  public function performSearch($request, $response, $args)
41  {
42  $this->searchHelperDao = $this->container->get('dao.searchhelperdao');
43  $apiVersion = ApiVersion::getVersion($request);
44 
45  if ($apiVersion == ApiVersion::V2) {
46  $query = $request->getQueryParams();
47  $searchType = $query["searchType"] ?? "";
48  $filename = $query["filename"] ?? "";
49  $tag = $query["tag"] ?? "";
50  $filesizeMin = $query["filesizemin"] ?? "";
51  $filesizeMax = $query["filesizemax"] ?? "";
52  $license = $query["license"] ?? "";
53  $copyright = $query["copyright"] ?? "";
54  $uploadId = $query["uploadId"] ?? "";
55  $page = $query["page"] ?? "";
56  $limit = $query["limit"] ?? "";
57  } else {
58  $searchType = $request->getHeaderLine("searchType");
59  $filename = $request->getHeaderLine("filename");
60  $tag = $request->getHeaderLine("tag");
61  $filesizeMin = $request->getHeaderLine("filesizemin");
62  $filesizeMax = $request->getHeaderLine("filesizemax");
63  $license = $request->getHeaderLine("license");
64  $copyright = $request->getHeaderLine("copyright");
65  $uploadId = $request->getHeaderLine("uploadId");
66  $page = $request->getHeaderLine("page");
67  $limit = $request->getHeaderLine("limit");
68  }
69 
70  // set searchtype to search allfiles by default
71  if (empty($searchType)) {
72  $searchType = "allfiles";
73  }
74 
75  // set uploadId to 0 - search in all files
76  if (empty($uploadId)) {
77  $uploadId = 0;
78  }
79 
80  /*
81  * check if at least one parameter was given
82  */
83  if (empty($filename) && empty($tag) && empty($filesizeMin) &&
84  empty($filesizeMax) && empty($license) && empty($copyright)) {
85  throw new HttpBadRequestException(
86  "At least one parameter, containing a value is required");
87  }
88 
89  /*
90  * check if filesizeMin && filesizeMax are numeric, if existing
91  */
92  if ((! empty($filesizeMin) && (! is_numeric($filesizeMin) || $filesizeMin < 0)) ||
93  (! empty($filesizeMax) && (! is_numeric($filesizeMax) || $filesizeMax < 0))) {
94  throw new HttpBadRequestException(
95  "filesizemin and filesizemax need to be positive integers!");
96  }
97 
98  /*
99  * check if page && limit are numeric, if existing
100  */
101  if ((! ($page==='') && (! is_numeric($page) || $page < 1)) ||
102  (! ($limit==='') && (! is_numeric($limit) || $limit < 1))) {
103  throw new HttpBadRequestException(
104  "page and limit need to be positive integers!");
105  }
106 
107  // set page to 1 by default
108  if (empty($page)) {
109  $page = 1;
110  }
111 
112  // set limit to 50 by default and max as 100
113  if (empty($limit)) {
114  $limit = 50;
115  } else if ($limit > 100) {
116  $limit = 100;
117  }
118 
119  $item = GetParm("item", PARM_INTEGER);
120  list($results, $count) = $this->searchHelperDao->GetResults($item,
121  $filename, $uploadId, $tag, $page-1, $limit,
122  $filesizeMin, $filesizeMax, $searchType, $license, $copyright,
123  $this->restHelper->getUploadDao(), $this->restHelper->getGroupId());
124  $totalPages = intval(ceil($count / $limit));
125 
126  $searchResults = [];
127  // rewrite it and add additional information about its parent upload
128  foreach ($results as $result) {
129  $currentUpload = $this->dbHelper->getUploads(
130  $this->restHelper->getUserId(), $this->restHelper->getGroupId(), 1, 1,
131  $result["upload_fk"], null, true)[1];
132  if (! empty($currentUpload)) {
133  $currentUpload = $currentUpload[0];
134  } else {
135  continue;
136  }
137  $uploadTreePk = $result["uploadtree_pk"];
138  $filename = $this->dbHelper->getFilenameFromUploadTree($uploadTreePk);
139  $currentResult = new SearchResult($currentUpload, $uploadTreePk, $filename);
140  $searchResults[] = $currentResult->getArray();
141  }
142  return $response->withHeader("X-Total-Pages", $totalPages)->withJson($searchResults, 200);
143  }
144 }
Base controller for REST calls.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Definition: ApiVersion.php:29
Model to hold search results.
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46