FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
SearchController.php
Go to the documentation of this file.
1 <?php
2 /***************************************************************
3  Copyright (C) 2018 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***************************************************************/
25 
31 
32 require_once dirname(dirname(__DIR__)) . "/search-helper.php";
33 
39 {
48  public function performSearch($request, $response, $args)
49  {
50  $searchType = $request->getHeaderLine("searchType");
51  $filename = $request->getHeaderLine("filename");
52  $tag = $request->getHeaderLine("tag");
53  $filesizeMin = $request->getHeaderLine("filesizemin");
54  $filesizeMax = $request->getHeaderLine("filesizemax");
55  $license = $request->getHeaderLine("license");
56  $copyright = $request->getHeaderLine("copyright");
57 
58  // set searchtype to search allfiles by default
59  if (empty($searchType)) {
60  $searchType = "allfiles";
61  }
62 
63  /*
64  * check if at least one parameter was given
65  */
66  if (empty($filename) && empty($tag) && empty($filesizeMin) &&
67  empty($filesizeMax) && empty($license) && empty($copyright)) {
68  $returnVal = new Info(400,
69  "Bad Request. At least one parameter, containing a value is required",
70  InfoType::ERROR);
71  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
72  }
73 
74  /*
75  * check if filesizeMin && filesizeMax are numeric, if existing
76  */
77  if ((! empty($filesizeMin) && (! is_numeric($filesizeMin) || $filesizeMin < 0)) ||
78  (! empty($filesizeMax) && (! is_numeric($filesizeMax) || $filesizeMax < 0))) {
79  $returnVal = new Info(400,
80  "Bad Request. filesizemin and filesizemax need to be positive integers!",
81  InfoType::ERROR);
82  return $response->withJson($returnVal->getArray(), $returnVal->getCode());
83  }
84 
85  $item = GetParm("item", PARM_INTEGER);
86  $results = GetResults($item, $filename, $tag, 0,
87  $filesizeMin, $filesizeMax, $searchType, $license, $copyright,
88  $this->restHelper->getUploadDao(), $this->restHelper->getGroupId(),
89  $GLOBALS['PG_CONN'])[0];
90 
91  $searchResults = [];
92  // rewrite it and add additional information about it's parent upload
93  for ($i = 0; $i < sizeof($results); $i ++) {
94  $currentUpload = $this->dbHelper->getUploads(
95  $this->restHelper->getUserId(), $results[$i]["upload_fk"])[0];
96  $uploadTreePk = $results[$i]["uploadtree_pk"];
97  $filename = $this->dbHelper->getFilenameFromUploadTree($uploadTreePk);
98  $currentResult = new SearchResult($currentUpload, $uploadTreePk, $filename);
99  $searchResults[] = $currentResult->getArray();
100  }
101  return $response->withJson($searchResults, 200);
102  }
103 }
Base controller for REST calls.
Model to hold search results.
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_INTEGER
Definition: common-parm.php:25
Info model to contain general error and return values.
Definition: Info.php:29