FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
search.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2010-2014 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015-2017 Siemens AG
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  ***********************************************************/
19 
20 include_once "search-helper.php";
21 
24 
25 class search extends FO_Plugin
26 {
27  protected $MaxPerPage = 100; /* maximum number of result items per page */
29  private $uploadDao;
30 
31  function __construct()
32  {
33  $this->Name = "search";
34  $this->Title = _("Search");
35  $this->MenuList = "Search";
36  $this->MenuOrder = 90;
37  $this->Dependency = array("browse");
38  $this->DBaccess = PLUGIN_DB_READ;
39  $this->LoginFlag = 0;
40  parent::__construct();
41 
42  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
43  }
44 
45  function PostInitialize()
46  {
47  $this->State = PLUGIN_STATE_READY;
48  return $this->State;
49  }
50 
54  function RegisterMenus()
55  {
56  menu_insert("Main::" . $this->MenuList,$this->MenuOrder,$this->Name,$this->MenuTarget);
57 
58  // For all other menus, permit coming back here.
59  $URI = $this->Name . Traceback_parm_keep(array( "page", "item",));
60  $Item = GetParm("item", PARM_INTEGER);
61  if (! empty($Item)) {
62  if (GetParm("mod", PARM_STRING) == $this->Name) {
63  menu_insert("Browse::Search", 1);
64  } else {
65  menu_insert("Browse::Search", 1, $URI, $this->MenuList);
66  }
67  }
68  } // RegisterMenus()
69 
77  function HTMLResults($UploadtreeRecs, $Page, $GETvars)
78  {
79  $Outbuf = "";
80  $Count = count($UploadtreeRecs);
81  if ($Count == 0) {
82  $Outbuf .= _("No matching files.\n");
83  return $Outbuf;
84  }
85  if (($Page > 0) || ($Count >= $this->MaxPerPage)) {
86  $Uri = Traceback_uri() . "?mod=" . $this->Name . $GETvars;
87  $PageChoices = MenuEndlessPage($Page, ($Count >= $this->MaxPerPage), $Uri) .
88  "<P />\n";
89  $Outbuf .= $PageChoices;
90  } else {
91  $PageChoices = "";
92  }
93  $Outbuf .= UploadtreeFileList($UploadtreeRecs, "browse","view",$Page*$this->MaxPerPage + 1);
94 
95  /* put page menu at the bottom, too */
96  $Outbuf .= $PageChoices;
97 
98  return $Outbuf;
99  }
100 
101 
105  function Output()
106  {
107  global $PG_CONN;
108  if ($this->State != PLUGIN_STATE_READY) {
109  return;
110  }
111  if ($this->OutputType != 'HTML') {
112  return;
113  }
114 
115  $this->vars['baseuri'] = Traceback_uri();
116  $CriteriaCount = 0;
117  $GETvars="";
118 
119  $Item = GetParm("item",PARM_INTEGER);
120  /* Show path if searching an item tree (don't show on global searches) */
121  if ($Item) {
122  $this->vars['pathOfItem'] = Dir2Browse($this->Name, $Item, NULL, 1, NULL) .
123  "<P />\n";
124  $GETvars .= "&item=$Item";
125  }
126 
127  $searchtype = GetParm("searchtype",PARM_STRING);
128  $GETvars .= "&searchtype=" . urlencode($searchtype);
129  if ($searchtype == 'containers') {
130  $this->vars["ContainersChecked"] = "checked=\"checked\"";
131  } else if ($searchtype == 'directory') {
132  $this->vars["DirectoryChecked"] = "checked=\"checked\"";
133  } else {
134  $this->vars["AllFilesChecked"] = "checked=\"checked\"";
135  }
136 
137  $Filename = GetParm("filename", PARM_RAW);
138  if (! empty($Filename)) {
139  $CriteriaCount ++;
140  $GETvars .= "&filename=" . urlencode($Filename);
141  $this->vars["Filename"] = $Filename;
142  }
143 
144  $tag = GetParm("tag", PARM_RAW);
145  if (! empty($tag)) {
146  $CriteriaCount ++;
147  $GETvars .= "&tag=" . urlencode($tag);
148  $this->vars["tag"] = $tag;
149  }
150 
151  $SizeMin = GetParm("sizemin", PARM_TEXT);
152  if (! empty($SizeMin) && is_numeric($SizeMin) && ($SizeMin >= 0)) {
153  $SizeMin = intval($SizeMin);
154  $CriteriaCount ++;
155  $GETvars .= "&sizemin=$SizeMin";
156  $this->vars["SizeMin"] = $SizeMin;
157  }
158 
159  $SizeMax = GetParm("sizemax", PARM_TEXT);
160  if (! empty($SizeMax) && is_numeric($SizeMax) && ($SizeMax >= 0)) {
161  $SizeMax = intval($SizeMax);
162  $CriteriaCount ++;
163  $GETvars .= "&sizemax=$SizeMax";
164  $this->vars["SizeMax"] = $SizeMax;
165  }
166 
167  $License = GetParm("license", PARM_RAW);
168  if (! empty($License)) {
169  $CriteriaCount ++;
170  $GETvars .= "&license=" . urlencode($License);
171  $this->vars["License"] = $License;
172  }
173 
174  $Copyright = GetParm("copyright", PARM_RAW);
175  if (! empty($Copyright)) {
176  $CriteriaCount ++;
177  $GETvars .= "&copyright=" . urlencode($Copyright);
178  $this->vars["Copyright"] = $Copyright;
179  }
180 
181  $Page = GetParm("page",PARM_INTEGER);
182 
183  $this->vars["postUrl"] = Traceback_uri() . "?mod=" . self::getName();
184 
185  if ($CriteriaCount) {
186  if (empty($Page)) {
187  $Page = 0;
188  }
189  $UploadtreeRecsResult = GetResults($Item, $Filename, $tag, $Page, $SizeMin,
190  $SizeMax, $searchtype, $License, $Copyright, $this->uploadDao,
191  Auth::getGroupId(), $PG_CONN);
192  $html = "<hr>\n";
193  $message = _(
194  "The indented search results are same files in different folders");
195  $html .= "<H4>$message</H4>\n";
196  $text = $UploadtreeRecsResult[1] . " " . _("Files matching");
197  $html .= "<H2>$text " . htmlentities($Filename) . "</H2>\n";
198  $html .= $this->HTMLResults($UploadtreeRecsResult[0], $Page, $GETvars,
199  $License, $Copyright);
200  $this->vars["result"] = $html;
201  }
202  }
203 
204  public function getTemplateName()
205  {
206  return "ui-search.html.twig";
207  }
208 }
209 
210 $NewPlugin = new search;
211 $NewPlugin->Initialize();
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:274
const PARM_RAW
Definition: common-parm.php:33
Traceback_uri()
Get the URI without query to this location.
const PARM_TEXT
Definition: common-parm.php:31
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:468
#define PLUGIN_DB_READ
Plugin requires read permission on DB.
Definition: libfossology.h:49
HTMLResults($UploadtreeRecs, $Page, $GETvars)
print search results to stdout
Definition: search.php:77
MenuEndlessPage($Page, $Next=1, $Uri= '')
Create a "First Prev 1 2 ... Next" page links for paged output.
Definition: state.hpp:26
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
Output()
Display the loaded menu and plugins.
Definition: search.php:105
const PARM_INTEGER
Definition: common-parm.php:25
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:67
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...
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
Traceback_parm_keep($List)
Create a new URI, keeping only these items.
RegisterMenus()
Customize submenus.
Definition: search.php:54