FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SearchHelperDao.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Rohit Pandey <rohit.pandey4900@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Dao;
9 
11 
13 {
17  private $dbManager;
18 
19  function __construct(DbManager $dbManager)
20  {
21  $this->dbManager = $dbManager;
22  }
23 
42  public function GetResults($Item, $Filename, $Upload, $tag, $Page, $Limit, $SizeMin, $SizeMax, $searchtype, $License, $Copyright, $uploadDao, $groupID)
43  {
44  $UploadtreeRecs = array(); // uploadtree record array to return
45  $totalUploadtreeRecs = array(); // total uploadtree record array
46  $totalUploadtreeRecsCount = 0; // total uploadtree records count to return
47  $NeedTagfileTable = true;
48  $NeedTaguploadtreeTable = true;
49 
50  if ($Item) {
51  /* Find lft and rgt bounds for this $Uploadtree_pk */
52  $row = $uploadDao->getUploadEntry($Item);
53  if (empty($row)) {
54  $text = _("Invalid URL, nonexistant item");
55  return "<h2>$text $Item</h2>";
56  }
57  $lft = $row["lft"];
58  $rgt = $row["rgt"];
59  $upload_pk = $row["upload_fk"];
60 
61  /* Check upload permission */
62  if (!$uploadDao->isAccessible($upload_pk, $groupID)) {
63  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
64  }
65  }
66 
67  $SQLBase = "SELECT DISTINCT uploadtree_pk, parent, upload_fk, uploadtree.pfile_fk, ufile_mode, ufile_name FROM uploadtree";
68  $SQLWhere = " ";
69  $SQLOrderLimitOffset = "";
70 
71  if ($searchtype != "directory") {
72  if (!empty($License)) {
73  $SQLWhere .= ", ( SELECT license_ref.rf_shortname, license_file.rf_fk, license_file.pfile_fk
74  FROM license_file JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk) AS pfile_ref";
75  }
76  if (!empty($Copyright)) {
77  $SQLWhere .= ",copyright";
78  }
79  }
80 
81  /* Figure out the tag_pk's of interest */
82  if (!empty($tag)) {
83  $stmt = __METHOD__.$Filename;
84  $sql = "select tag_pk from tag where tag ilike '" . pg_escape_string($tag) . "'";
85  $tag_pk_array = $this->dbManager->getRows($sql, [], $stmt);
86  if (empty($tag_pk_array)) {
87  /* tag doesn't match anything, so no results are possible */
88  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
89  }
90 
91  /* add the tables needed for the tag query */
92  $sql = "select tag_file_pk from tag_file limit 1";
93  $result = $this->dbManager->getRows($sql, [], $stmt);
94  if (empty($result)) {
95  /* tag_file didn't have data, don't add the tag_file table for tag query */
96  $NeedTagfileTable = false;
97  } else {
98  $SQLWhere .= ", tag_file";
99  }
100 
101  /* add the tables needed for the tag query */
102  $sql = "select tag_uploadtree_pk from tag_uploadtree limit 1";
103  $result = $this->dbManager->getRows($sql, [], $stmt);
104  if (empty($result)) {
105  /* tag_uploadtree didn't have data, don't add the tag_uploadtree table for tag query */
106  $NeedTaguploadtreeTable = false;
107  } else {
108  $SQLWhere .= ", tag_uploadtree";
109  }
110 
111  if (!$NeedTagfileTable && !$NeedTaguploadtreeTable) {
112  $SQLWhere .= ", tag_file, tag_uploadtree";
113  }
114  }
115 
116  /* do we need the pfile table? Yes, if any of these are a search critieria. */
117  if (!empty($SizeMin) or !empty($SizeMax)) {
118  $SQLWhere .= ", pfile where pfile_pk=uploadtree.pfile_fk ";
119  $NeedAnd = true;
120  } else {
121  $SQLWhere .= " where ";
122  $NeedAnd = false;
123  }
124 
125  /* add the tag conditions */
126  if (!empty($tag)) {
127  if ($NeedAnd) {
128  $SQLWhere .= " AND";
129  }
130  $SQLWhere .= "(";
131  $NeedOr = false;
132  foreach ($tag_pk_array as $tagRec) {
133  if ($NeedOr) {
134  $SQLWhere .= " OR";
135  }
136  $SQLWhere .= "(";
137  $tag_pk = $tagRec['tag_pk'];
138  if ($NeedTagfileTable && $NeedTaguploadtreeTable) {
139  $SQLWhere .= "(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) ";
140  } else if ($NeedTaguploadtreeTable) {
141  $SQLWhere .= "uploadtree_pk=tag_uploadtree.uploadtree_fk and tag_uploadtree.tag_fk=$tag_pk";
142  } else if ($NeedTagfileTable) {
143  $SQLWhere .= "uploadtree.pfile_fk=tag_file.pfile_fk and tag_file.tag_fk=$tag_pk";
144  } else {
145  $SQLWhere .= "(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) ";
146  }
147  $SQLWhere .= ")";
148  $NeedOr = 1;
149  }
150  $NeedAnd = 1;
151  $SQLWhere .= ")";
152  }
153 
154  if ($Filename) {
155  if ($NeedAnd) {
156  $SQLWhere .= " AND";
157  }
158  $SQLWhere .= " ufile_name ilike '" . $Filename . "'";
159  $NeedAnd = 1;
160  }
161 
162  if ($Upload != 0) {
163  if ($NeedAnd) {
164  $SQLWhere .= " AND";
165  }
166  $SQLWhere .= " upload_fk = " . $Upload . "";
167  $NeedAnd = 1;
168  }
169 
170  if (!empty($SizeMin) && is_numeric($SizeMin)) {
171  if ($NeedAnd) {
172  $SQLWhere .= " AND";
173  }
174  $SQLWhere .= " pfile.pfile_size >= " . $SizeMin;
175  $NeedAnd = 1;
176  }
177 
178  if (!empty($SizeMax) && is_numeric($SizeMax)) {
179  if ($NeedAnd) {
180  $SQLWhere .= " AND";
181  }
182  $SQLWhere .= " pfile.pfile_size <= " . $SizeMax;
183  $NeedAnd = 1;
184  }
185 
186  if ($Item) {
187  if ($NeedAnd) {
188  $SQLWhere .= " AND";
189  }
190  $SQLWhere .= " upload_fk = $upload_pk AND lft >= $lft AND rgt <= $rgt";
191  $NeedAnd = 1;
192  }
193 
194  /* search only containers */
195  if ($searchtype == 'containers') {
196  if ($NeedAnd) {
197  $SQLWhere .= " AND";
198  }
199  $SQLWhere .= " ((ufile_mode & (1<<29))!=0) AND ((ufile_mode & (1<<28))=0)";
200  $NeedAnd = 1;
201  }
202  $dir_ufile_mode = 536888320;
203  if ($searchtype == 'directory') {
204  if ($NeedAnd) {
205  $SQLWhere .= " AND";
206  }
207  $SQLWhere .= " ((ufile_mode & (1<<29))!=0) AND ((ufile_mode & (1<<28))=0) AND (ufile_mode != $dir_ufile_mode) and pfile_fk != 0";
208  $NeedAnd = 1;
209  }
210 
212  if ($searchtype != "directory") {
213  if (!empty($License)) {
214  if ($NeedAnd) {
215  $SQLWhere .= " AND";
216  }
217 
218  $SQLWhere .= " uploadtree.pfile_fk=pfile_ref.pfile_fk and pfile_ref.rf_shortname ilike '" .
219  pg_escape_string($License) . "'";
220  $NeedAnd = 1;
221  }
222  if (!empty($Copyright)) {
223  if ($NeedAnd) {
224  $SQLWhere .= " AND";
225  }
226  $SQLWhere .= " uploadtree.pfile_fk=copyright.pfile_fk and copyright.content ilike '%" .
227  pg_escape_string($Copyright) . "%'";
228  }
229  }
230 
231  $Offset = $Page * $Limit;
232 
233  $SQLOrderLimitOffset = " ORDER BY ufile_name, uploadtree.pfile_fk LIMIT $Limit OFFSET $Offset";
234  $PaginatedSQL = $SQLBase . $SQLWhere . $SQLOrderLimitOffset;
235 
236  $CountSQL = "SELECT COUNT(DISTINCT uploadtree_pk) FROM uploadtree" . $SQLWhere;
237 
238  $stmt = __METHOD__ . "_count";
239  $countRow = $this->dbManager->getSingleRow($CountSQL, [], $stmt);
240  $totalUploadtreeRecsCount = $countRow ? reset($countRow) : 0;
241 
242  $stmt = __METHOD__ . "_paginated";
243  $rows = $this->dbManager->getRows($PaginatedSQL, [], $stmt);
244  if (!empty($rows)) {
245  foreach ($rows as $row) {
246  if (!$uploadDao->isAccessible($row['upload_fk'], $groupID)) {
247  continue;
248  }
249  $totalUploadtreeRecs[] = $row;
250  }
251  }
252  return array($totalUploadtreeRecs, $totalUploadtreeRecsCount);
253  }
254 }
GetResults($Item, $Filename, $Upload, $tag, $Page, $Limit, $SizeMin, $SizeMax, $searchtype, $License, $Copyright, $uploadDao, $groupID)
Given a filename, return all uploadtree.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16