FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
search-helper.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 
39 function GetResults($Item, $Filename, $tag, $Page, $SizeMin, $SizeMax, $searchtype,
40  $License, $Copyright, $uploadDao, $groupID, $PG_CONN)
41 {
42  $MaxPerPage = 100; /* maximum number of result items per page */
43  $UploadtreeRecs = array(); // uploadtree record array to return
44  $totalUploadtreeRecs = array(); // total uploadtree record array
45  $totalUploadtreeRecsCount = 0; // total uploadtree records count to return
46  $NeedTagfileTable = true;
47  $NeedTaguploadtreeTable = true;
48 
49  if ($Item) {
50  /* Find lft and rgt bounds for this $Uploadtree_pk */
51  $row = $uploadDao->getUploadEntry($Item);
52  if (empty($row)) {
53  $text = _("Invalid URL, nonexistant item");
54  return "<h2>$text $Item</h2>";
55  }
56  $lft = $row["lft"];
57  $rgt = $row["rgt"];
58  $upload_pk = $row["upload_fk"];
59 
60  /* Check upload permission */
61  if (!$uploadDao->isAccessible($upload_pk, $groupID)) {
62  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
63  }
64  }
65 
66  /* Start the result select stmt */
67  $SQL = "SELECT DISTINCT uploadtree_pk, parent, upload_fk, uploadtree.pfile_fk, ufile_mode, ufile_name FROM uploadtree";
68 
69  if ($searchtype != "directory") {
70  if (! empty($License)) {
71  $SQL .= ", ( SELECT license_ref.rf_shortname, license_file.rf_fk, license_file.pfile_fk
72  FROM license_file JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk) AS pfile_ref";
73  }
74  if (! empty($Copyright)) {
75  $SQL .= ",copyright";
76  }
77  }
78 
79  /* Figure out the tag_pk's of interest */
80  if (! empty($tag)) {
81  $sql = "select tag_pk from tag where tag ilike '" . pg_escape_string($tag) . "'";
82  $result = pg_query($PG_CONN, $sql);
83  DBCheckResult($result, $sql, __FILE__, __LINE__);
84  if (pg_num_rows($result) < 1) {
85  /* tag doesn't match anything, so no results are possible */
86  pg_free_result($result);
87  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
88  }
89 
90  /* Make a list of the tag_pk's that satisfy the criteria */
91  $tag_pk_array = pg_fetch_all($result);
92  pg_free_result($result);
93 
94  /* add the tables needed for the tag query */
95  $sql = "select tag_file_pk from tag_file limit 1";
96  $result = pg_query($PG_CONN, $sql);
97  DBCheckResult($result, $sql, __FILE__, __LINE__);
98  if (pg_num_rows($result) < 1) {
99  /* tag_file didn't have data, don't add the tag_file table for tag query */
100  $NeedTagfileTable = false;
101  } else {
102  $SQL .= ", tag_file";
103  }
104  pg_free_result($result);
105 
106  /* add the tables needed for the tag query */
107  $sql = "select tag_uploadtree_pk from tag_uploadtree limit 1";
108  $result = pg_query($PG_CONN, $sql);
109  DBCheckResult($result, $sql, __FILE__, __LINE__);
110  if (pg_num_rows($result) < 1) {
111  /* tag_uploadtree didn't have data, don't add the tag_uploadtree table for tag query */
112  $NeedTaguploadtreeTable = false;
113  } else {
114  $SQL .= ", tag_uploadtree";
115  }
116  pg_free_result($result);
117 
118  if (!$NeedTagfileTable && !$NeedTaguploadtreeTable) {
119  $SQL .= ", tag_file, tag_uploadtree";
120  }
121  }
122 
123  /* do we need the pfile table? Yes, if any of these are a search critieria. */
124  if (!empty($SizeMin) or !empty($SizeMax)) {
125  $SQL .= ", pfile where pfile_pk=uploadtree.pfile_fk ";
126  $NeedAnd = true;
127  } else {
128  $SQL .= " where ";
129  $NeedAnd = false;
130  }
131 
132  /* add the tag conditions */
133  if (!empty($tag)) {
134  if ($NeedAnd) {
135  $SQL .= " AND";
136  }
137  $SQL .= "(";
138  $NeedOr = false;
139  foreach ($tag_pk_array as $tagRec) {
140  if ($NeedOr) {
141  $SQL .= " OR";
142  }
143  $SQL .= "(";
144  $tag_pk = $tagRec['tag_pk'];
145  if ($NeedTagfileTable && $NeedTaguploadtreeTable) {
146  $SQL .= "(uploadtree.pfile_fk=tag_file.pfile_fk and tag_file.tag_fk=$tag_pk) or (uploadtree_pk=tag_uploadtree.uploadtree_fk and tag_uploadtree.tag_fk=$tag_pk) ";
147  } else if ($NeedTaguploadtreeTable) {
148  $SQL .= "uploadtree_pk=tag_uploadtree.uploadtree_fk and tag_uploadtree.tag_fk=$tag_pk";
149  } else if ($NeedTagfileTable) {
150  $SQL .= "uploadtree.pfile_fk=tag_file.pfile_fk and tag_file.tag_fk=$tag_pk";
151  } else {
152  $SQL .= "(uploadtree.pfile_fk=tag_file.pfile_fk and tag_file.tag_fk=$tag_pk) or (uploadtree_pk=tag_uploadtree.uploadtree_fk and tag_uploadtree.tag_fk=$tag_pk) ";
153  }
154  $SQL .= ")";
155  $NeedOr=1;
156  }
157  $NeedAnd=1;
158  $SQL .= ")";
159  }
160 
161  if ($Filename) {
162  if ($NeedAnd) {
163  $SQL .= " AND";
164  }
165  $SQL .= " ufile_name ilike '". pg_escape_string($Filename) . "'";
166  $NeedAnd=1;
167  }
168 
169  if (!empty($SizeMin) && is_numeric($SizeMin)) {
170  if ($NeedAnd) {
171  $SQL .= " AND";
172  }
173  $SQL .= " pfile.pfile_size >= ".pg_escape_string($SizeMin);
174  $NeedAnd=1;
175  }
176 
177  if (!empty($SizeMax) && is_numeric($SizeMax)) {
178  if ($NeedAnd) {
179  $SQL .= " AND";
180  }
181  $SQL .= " pfile.pfile_size <= ".pg_escape_string($SizeMax);
182  $NeedAnd=1;
183  }
184 
185  if ($Item) {
186  if ($NeedAnd) {
187  $SQL .= " AND";
188  }
189  $SQL .= " upload_fk = $upload_pk AND lft >= $lft AND rgt <= $rgt";
190  $NeedAnd=1;
191  }
192 
193  /* search only containers */
194  if ($searchtype == 'containers') {
195  if ($NeedAnd) {
196  $SQL .= " AND";
197  }
198  $SQL .= " ((ufile_mode & (1<<29))!=0) AND ((ufile_mode & (1<<28))=0)";
199  $NeedAnd=1;
200  }
201  $dir_ufile_mode = 536888320;
202  if ($searchtype == 'directory') {
203  if ($NeedAnd) {
204  $SQL .= " AND";
205  }
206  $SQL .= " ((ufile_mode & (1<<29))!=0) AND ((ufile_mode & (1<<28))=0) AND (ufile_mode != $dir_ufile_mode) and pfile_fk != 0";
207  $NeedAnd = 1;
208  }
209 
211  if ($searchtype != "directory") {
212  if (! empty($License)) {
213  if ($NeedAnd) {
214  $SQL .= " AND";
215  }
216 
217  $SQL .= " uploadtree.pfile_fk=pfile_ref.pfile_fk and pfile_ref.rf_shortname ilike '" .
218  pg_escape_string($License) . "'";
219  $NeedAnd = 1;
220  }
221  if (! empty($Copyright)) {
222  if ($NeedAnd) {
223  $SQL .= " AND";
224  }
225  $SQL .= " uploadtree.pfile_fk=copyright.pfile_fk and copyright.content ilike '%" .
226  pg_escape_string($Copyright) . "%'";
227  }
228  }
229 
230  $Offset = $Page * $MaxPerPage;
231  $SQL .= " ORDER BY ufile_name, uploadtree.pfile_fk";
232  $result = pg_query($PG_CONN, $SQL);
233  DBCheckResult($result, $SQL, __FILE__, __LINE__);
234  if (pg_num_rows($result)) {
235  while ($row = pg_fetch_assoc($result)) {
236  if (! $uploadDao->isAccessible($row['upload_fk'], $groupID)) {
237  continue;
238  }
239  $totalUploadtreeRecs[] = $row;
240  }
241  }
242  pg_free_result($result);
243  $UploadtreeRecs = array_slice($totalUploadtreeRecs, $Offset, $MaxPerPage);
244  $totalUploadtreeRecsCount = sizeof($totalUploadtreeRecs);
245  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
246 } // GetResults()
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198