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  /* Start the result select stmt */
68  $SQL = "SELECT DISTINCT uploadtree_pk, parent, upload_fk, uploadtree.pfile_fk, ufile_mode, ufile_name FROM uploadtree";
69 
70  if ($searchtype != "directory") {
71  if (!empty($License)) {
72  $SQL .= ", ( SELECT license_ref.rf_shortname, license_file.rf_fk, license_file.pfile_fk
73  FROM license_file JOIN license_ref ON license_file.rf_fk = license_ref.rf_pk) AS pfile_ref";
74  }
75  if (!empty($Copyright)) {
76  $SQL .= ",copyright";
77  }
78  }
79 
80  /* Figure out the tag_pk's of interest */
81  if (!empty($tag)) {
82  $stmt = __METHOD__.$Filename;
83  $sql = "select tag_pk from tag where tag ilike '" . pg_escape_string($tag) . "'";
84  $tag_pk_array = $this->dbManager->getRows($sql, [], $stmt);
85  if (empty($tag_pk_array)) {
86  /* tag doesn't match anything, so no results are possible */
87  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
88  }
89 
90  /* add the tables needed for the tag query */
91  $sql = "select tag_file_pk from tag_file limit 1";
92  $result = $this->dbManager->getRows($sql, [], $stmt);
93  if (empty($result)) {
94  /* tag_file didn't have data, don't add the tag_file table for tag query */
95  $NeedTagfileTable = false;
96  } else {
97  $SQL .= ", tag_file";
98  }
99 
100  /* add the tables needed for the tag query */
101  $sql = "select tag_uploadtree_pk from tag_uploadtree limit 1";
102  $result = $this->dbManager->getRows($sql, [], $stmt);
103  if (empty($result)) {
104  /* tag_uploadtree didn't have data, don't add the tag_uploadtree table for tag query */
105  $NeedTaguploadtreeTable = false;
106  } else {
107  $SQL .= ", tag_uploadtree";
108  }
109 
110  if (!$NeedTagfileTable && !$NeedTaguploadtreeTable) {
111  $SQL .= ", tag_file, tag_uploadtree";
112  }
113  }
114 
115  /* do we need the pfile table? Yes, if any of these are a search critieria. */
116  if (!empty($SizeMin) or !empty($SizeMax)) {
117  $SQL .= ", pfile where pfile_pk=uploadtree.pfile_fk ";
118  $NeedAnd = true;
119  } else {
120  $SQL .= " where ";
121  $NeedAnd = false;
122  }
123 
124  /* add the tag conditions */
125  if (!empty($tag)) {
126  if ($NeedAnd) {
127  $SQL .= " AND";
128  }
129  $SQL .= "(";
130  $NeedOr = false;
131  foreach ($tag_pk_array as $tagRec) {
132  if ($NeedOr) {
133  $SQL .= " OR";
134  }
135  $SQL .= "(";
136  $tag_pk = $tagRec['tag_pk'];
137  if ($NeedTagfileTable && $NeedTaguploadtreeTable) {
138  $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) ";
139  } else if ($NeedTaguploadtreeTable) {
140  $SQL .= "uploadtree_pk=tag_uploadtree.uploadtree_fk and tag_uploadtree.tag_fk=$tag_pk";
141  } else if ($NeedTagfileTable) {
142  $SQL .= "uploadtree.pfile_fk=tag_file.pfile_fk and tag_file.tag_fk=$tag_pk";
143  } else {
144  $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) ";
145  }
146  $SQL .= ")";
147  $NeedOr = 1;
148  }
149  $NeedAnd = 1;
150  $SQL .= ")";
151  }
152 
153  if ($Filename) {
154  if ($NeedAnd) {
155  $SQL .= " AND";
156  }
157  $SQL .= " ufile_name ilike '" . $Filename . "'";
158  $NeedAnd = 1;
159  }
160 
161  if ($Upload != 0) {
162  if ($NeedAnd) {
163  $SQL .= " AND";
164  }
165  $SQL .= " upload_fk = " . $Upload . "";
166  $NeedAnd = 1;
167  }
168 
169  if (!empty($SizeMin) && is_numeric($SizeMin)) {
170  if ($NeedAnd) {
171  $SQL .= " AND";
172  }
173  $SQL .= " pfile.pfile_size >= " . $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 <= " . $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 * $Limit;
231  $stmt = __METHOD__.$Filename;
232  $SQL .= " ORDER BY ufile_name, uploadtree.pfile_fk";
233  $rows = $this->dbManager->getRows($SQL, [], $stmt);
234  if (!empty($rows)) {
235  foreach ($rows as $row) {
236  if (!$uploadDao->isAccessible($row['upload_fk'], $groupID)) {
237  continue;
238  }
239  $totalUploadtreeRecs[] = $row;
240  }
241  }
242  $UploadtreeRecs = array_slice($totalUploadtreeRecs, $Offset, $Limit);
243  $totalUploadtreeRecsCount = sizeof($totalUploadtreeRecs);
244  return array($UploadtreeRecs, $totalUploadtreeRecsCount);
245  }
246 }
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