FOSSology  4.4.0
Open Source License Compliance by Open Source Software
search.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2010-2014 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015-2022 Siemens AG
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
13 
14 class search extends FO_Plugin
15 {
16  protected $MaxPerPage = 100; /* maximum number of result items per page */
18  private $uploadDao;
19 
21  private $searchHelperDao;
22 
23  function __construct()
24  {
25  $this->Name = "search";
26  $this->Title = _("Search");
27  $this->MenuList = "Search";
28  $this->MenuOrder = 90;
29  $this->Dependency = array("browse");
30  $this->DBaccess = PLUGIN_DB_READ;
31  $this->LoginFlag = 0;
32  parent::__construct();
33 
34  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
35  $this->searchHelperDao = $GLOBALS['container']->get('dao.searchhelperdao');
36  }
37 
38  function PostInitialize()
39  {
40  $this->State = PLUGIN_STATE_READY;
41  return $this->State;
42  }
43 
44  function loadUploads()
45  {
46  $allUploadsPre = $this->uploadDao->getActiveUploadsArray();
47  $filteredUploadsList = array();
48 
49  return array_filter($allUploadsPre, function($uploadObj){
50  if ($this->uploadDao->isAccessible($uploadObj->getId(), Auth::getGroupId())) {
51  return true;
52  }
53  return false;
54  });
55  }
56 
60  function RegisterMenus()
61  {
62  menu_insert("Main::" . $this->MenuList,$this->MenuOrder,$this->Name,$this->MenuTarget);
63 
64  // For all other menus, permit coming back here.
65  $URI = $this->Name . Traceback_parm_keep(array( "page", "item",));
66  $Item = GetParm("item", PARM_INTEGER);
67  if (! empty($Item)) {
68  if (GetParm("mod", PARM_STRING) == $this->Name) {
69  menu_insert("Browse::Search", 1);
70  } else {
71  menu_insert("Browse::Search", 1, $URI, $this->MenuList);
72  }
73  }
74  } // RegisterMenus()
75 
83  function HTMLResults($UploadtreeRecs, $Page, $GETvars)
84  {
85  $Outbuf = "";
86  $Count = count($UploadtreeRecs);
87  if ($Count == 0) {
88  $Outbuf .= _("No matching files.\n");
89  return $Outbuf;
90  }
91  if (($Page > 0) || ($Count >= $this->MaxPerPage)) {
92  $Uri = Traceback_uri() . "?mod=" . $this->Name . $GETvars;
93  $PageChoices = MenuEndlessPage($Page, ($Count >= $this->MaxPerPage), $Uri) .
94  "<P />\n";
95  $Outbuf .= $PageChoices;
96  } else {
97  $PageChoices = "";
98  }
99  $Outbuf .= UploadtreeFileList($UploadtreeRecs, "browse","view",$Page*$this->MaxPerPage + 1);
100 
101  /* put page menu at the bottom, too */
102  $Outbuf .= $PageChoices;
103 
104  return $Outbuf;
105  }
106 
107 
111  function Output()
112  {
113  global $PG_CONN;
114  if ($this->State != PLUGIN_STATE_READY) {
115  return;
116  }
117  if ($this->OutputType != 'HTML') {
118  return;
119  }
120 
121  $this->vars['baseuri'] = Traceback_uri();
122  $CriteriaCount = 0;
123  $GETvars="";
124 
125  // loads list of all uploads to put in Search filter select field
126  $uploadsArray = $this->loadUploads();
127  $this->vars['uploadsArray'] = $uploadsArray;
128 
129  $Item = GetParm("item",PARM_INTEGER);
130  /* Show path if searching an item tree (don't show on global searches) */
131  if ($Item) {
132  $this->vars['pathOfItem'] = Dir2Browse($this->Name, $Item, NULL, 1, NULL) .
133  "<P />\n";
134  $GETvars .= "&item=$Item";
135  }
136 
137  $searchtype = GetParm("searchtype",PARM_STRING);
138  $GETvars .= "&searchtype=" . urlencode($searchtype);
139  if ($searchtype == 'containers') {
140  $this->vars["ContainersChecked"] = "checked=\"checked\"";
141  } else if ($searchtype == 'directory') {
142  $this->vars["DirectoryChecked"] = "checked=\"checked\"";
143  } else {
144  $this->vars["AllFilesChecked"] = "checked=\"checked\"";
145  }
146 
147  $Filename = GetParm("filename", PARM_RAW);
148  if (! empty($Filename)) {
149  $CriteriaCount ++;
150  $GETvars .= "&filename=" . urlencode($Filename);
151  $this->vars["Filename"] = $Filename;
152  }
153 
154  $Upload = GetParm("upload",PARM_INTEGER);
155  $SelectedUploadName = "All uploads";
156  if ($Upload != 0) {
157  $CriteriaCount++;
158  $GETvars .= "&upload=" . urlencode($Upload);
159  $this->vars["Upload"] = $Upload;
160  foreach ($uploadsArray as $row) {
161  if ($row->getId() == $Upload) {
162  $SelectedUploadName = $row->getFilename() . " from " . Convert2BrowserTime(date("Y-m-d H:i:s",$row->getTimestamp()));
163  }
164  }
165  } else {
166  $this->vars["Upload"] = 0;
167  }
168 
169  $tag = GetParm("tag",PARM_RAW);
170  if (!empty($tag)) {
171  $CriteriaCount++;
172  $GETvars .= "&tag=" . urlencode($tag);
173  $this->vars["tag"] = $tag;
174  }
175 
176  $SizeMin = GetParm("sizemin", PARM_TEXT);
177  if (! empty($SizeMin) && is_numeric($SizeMin) && ($SizeMin >= 0)) {
178  $SizeMin = intval($SizeMin);
179  $CriteriaCount ++;
180  $GETvars .= "&sizemin=$SizeMin";
181  $this->vars["SizeMin"] = $SizeMin;
182  }
183 
184  $SizeMax = GetParm("sizemax", PARM_TEXT);
185  if (! empty($SizeMax) && is_numeric($SizeMax) && ($SizeMax >= 0)) {
186  $SizeMax = intval($SizeMax);
187  $CriteriaCount ++;
188  $GETvars .= "&sizemax=$SizeMax";
189  $this->vars["SizeMax"] = $SizeMax;
190  }
191 
192  $License = GetParm("license", PARM_RAW);
193  if (! empty($License)) {
194  $CriteriaCount ++;
195  $GETvars .= "&license=" . urlencode($License);
196  $this->vars["License"] = $License;
197  }
198 
199  $Copyright = GetParm("copyright", PARM_RAW);
200  if (! empty($Copyright)) {
201  $CriteriaCount ++;
202  $GETvars .= "&copyright=" . urlencode($Copyright);
203  $this->vars["Copyright"] = $Copyright;
204  }
205 
206  $Limit = GetParm("limit",PARM_INTEGER);
207  $Page = GetParm("page",PARM_INTEGER);
208 
209  $this->vars["postUrl"] = Traceback_uri() . "?mod=" . self::getName();
210 
211  if ($CriteriaCount) {
212  if (empty($Page)) {
213  $Page = 0;
214  }
215  $UploadtreeRecsResult = $this->searchHelperDao->GetResults($Item, $Filename,$Upload, $tag, $Page, $Limit, $SizeMin,
216  $SizeMax, $searchtype, $License, $Copyright, $this->uploadDao,
217  Auth::getGroupId());
218  $html = "<hr>\n";
219  $message = _(
220  "The indented search results are same files in different folders");
221  $html .= "<H4>$message</H4>\n";
222  $text = $UploadtreeRecsResult[1] . " " . _("Files matching");
223  $html .= "<H2>$text " . htmlentities($Filename) . " in ". htmlentities($SelectedUploadName) . "</H2>\n";
224  $html .= $this->HTMLResults($UploadtreeRecsResult[0], $Page, $GETvars,
225  $License, $Copyright);
226  $this->vars["result"] = $html;
227  }
228  }
229 
230  public function getTemplateName()
231  {
232  return "ui-search.html.twig";
233  }
234 }
235 
236 $NewPlugin = new search;
237 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
HTMLResults($UploadtreeRecs, $Page, $GETvars)
print search results to stdout
Definition: search.php:83
__construct()
base constructor. Most plugins will just use this
Definition: search.php:23
Output()
Display the loaded menu and plugins.
Definition: search.php:111
PostInitialize()
This function is called before the plugin is used and after all plugins have been initialized....
Definition: search.php:38
RegisterMenus()
Customize submenus.
Definition: search.php:60
UploadtreeFileList($Listing, $IfDirPlugin, $IfFilePlugin, $Count=-1, $ShowPhrase=0)
Given an array of pfiles/uploadtree, sorted by pfile, list all of the breadcrumbs for each file....
Definition: common-dir.php:454
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
MenuEndlessPage($Page, $Next=1, $Uri='')
Create a "First Prev 1 2 ... Next" page links for paged output.
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_TEXT
Definition: common-parm.php:20
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_RAW
Definition: common-parm.php:22
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
Convert2BrowserTime($server_time)
Convert the server time to browser time.
Definition: common-ui.php:312
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:37
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN