FOSSology  4.4.0
Open Source License Compliance by Open Source Software
common-license-file.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2009-2014 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014 Siemens AG
5 
6  SPDX-License-Identifier: LGPL-2.1-only
7 */
8 
30 function GetFileLicenses($agent, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree', $duplicate="")
31 {
32  global $PG_CONN;
33 
34  if (empty($agent)) {
35  Fatal("Missing parameter: agent", __FILE__, __LINE__);
36  }
37 
38  if ($uploadtree_pk) {
39  /* Find lft and rgt bounds for this $uploadtree_pk */
40  $sql = "SELECT lft, rgt, upload_fk FROM $uploadtree_tablename
41  WHERE uploadtree_pk = $uploadtree_pk";
42  $result = pg_query($PG_CONN, $sql);
43  DBCheckResult($result, $sql, __FILE__, __LINE__);
44  $row = pg_fetch_assoc($result);
45  $lft = $row["lft"];
46  $rgt = $row["rgt"];
47  $upload_pk = $row["upload_fk"];
48  pg_free_result($result);
49 
50  $agentIdCondition = $agent != "any" ? "and agent_fk=$agent" : "";
51 
52  /* Strip out added upload_pk condition if it isn't needed */
53  if (($uploadtree_tablename == "uploadtree_a") ||
54  ($uploadtree_tablename == "uploadtree")) {
55  $UploadClause = "upload_fk=$upload_pk and ";
56  } else {
57  $UploadClause = "";
58  }
59 
60  /* Get the licenses under this $uploadtree_pk */
61  $sql = "SELECT distinct(rf_shortname) as rf_shortname, rf_pk as rf_fk, fl_pk
62  from license_file_ref,
63  (SELECT distinct(pfile_fk) as PF from $uploadtree_tablename
64  where $UploadClause lft BETWEEN $lft and $rgt) as SS
65  where PF=pfile_fk $agentIdCondition
66  order by rf_shortname asc";
67  $result = pg_query($PG_CONN, $sql);
68  DBCheckResult($result, $sql, __FILE__, __LINE__);
69  } else {
70  Fatal("Missing function inputs", __FILE__, __LINE__);
71  }
72 
73  $LicArray = array();
74  if ($duplicate) {
75  // get duplicated licenses
76  while ($row = pg_fetch_assoc($result)) {
77  $LicArray[$row['fl_pk']] = $row['rf_shortname'];
78  }
79  } else { // do not return duplicated licenses
80  while ($row = pg_fetch_assoc($result)) {
81  $LicArray[$row['rf_fk']] = $row['rf_shortname'];
82  }
83  }
84  pg_free_result($result);
85  return $LicArray;
86 }
87 
98 function GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
99 {
100  $LicStr = "";
101  $LicArray = GetFileLicenses($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename);
102  return implode(', ', $LicArray);
103 }
104 
124 function GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk,
125  $PkgsOnly=false, $offset=0, $limit="ALL",
126  $order="", $tag_pk=null, $uploadtree_tablename="uploadtree")
127 {
128  global $PG_CONN;
129 
130  /* Find lft and rgt bounds for this $uploadtree_pk */
131  $sql = "SELECT lft, rgt, upload_fk FROM $uploadtree_tablename WHERE uploadtree_pk = $uploadtree_pk";
132  $result = pg_query($PG_CONN, $sql);
133  DBCheckResult($result, $sql, __FILE__, __LINE__);
134  $row = pg_fetch_assoc($result);
135  $lft = $row["lft"];
136  $rgt = $row["rgt"];
137  $upload_pk = $row["upload_fk"];
138  pg_free_result($result);
139 
140  /* Find rf_pk for rf_shortname. This will speed up the main query tremendously */
141  $pg_shortname = pg_escape_string($rf_shortname);
142  $sql = "SELECT rf_pk FROM license_ref WHERE rf_shortname='$pg_shortname'";
143  $result = pg_query($PG_CONN, $sql);
144  DBCheckResult($result, $sql, __FILE__, __LINE__);
145  $row = pg_fetch_assoc($result);
146  $rf_pk = $row["rf_pk"];
147  while ($row = pg_fetch_assoc($result)) {
148  $rf_pk .= "," . $row["rf_pk"];
149  }
150  pg_free_result($result);
151 
152  if (empty($rf_pk)) {
153  return array(); // if when the rf_shortname does not exist
154  }
155 
156  /* Optional tag restriction */
157  if (empty($tag_pk)) {
158  $TagTable = "";
159  $TagClause = "";
160  } else {
161  $TagTable = "tag_file,";
162  $TagClause = "and PF=tag_file.pfile_fk and tag_fk=$tag_pk";
163  }
164 
165  $agentCondition = '';
166  if (is_array($agent_pk)) {
167  $agentCondition = ' AND agent_fk IN (' . implode(',', $agent_pk) . ')';
168  } else if ($agent_pk != "any") {
169  $agentCondition = "and agent_fk=$agent_pk";
170  }
171 
172  /* Strip out added upload_pk condition if it isn't needed */
173  if (($uploadtree_tablename == "uploadtree_a") || ($uploadtree_tablename == "uploadtree")) {
174  $UploadClause = "upload_fk=$upload_pk and ";
175  } else {
176  $UploadClause = "";
177  }
178  $theLimit = ($limit=='ALL') ? '' : "LIMIT $limit";
179  $sql = "select uploadtree_pk, license_file.pfile_fk, ufile_name, agent_name, max(agent_pk) agent_pk
180  from license_file, agent, $TagTable
181  (SELECT pfile_fk as PF, uploadtree_pk, ufile_name from $uploadtree_tablename
182  where $UploadClause
183  lft BETWEEN $lft and $rgt
184  and ufile_mode & (3<<28) = 0 ) as SS
185  where PF=license_file.pfile_fk $agentCondition and rf_fk in ($rf_pk)
186  AND agent_pk=agent_fk
187  $TagClause
188  GROUP BY uploadtree_pk, license_file.pfile_fk, ufile_name, agent_name
189  $order $theLimit offset $offset";
190  $result = pg_query($PG_CONN, $sql); // Top uploadtree_pk's
191  DBCheckResult($result, $sql, __FILE__, __LINE__);
192  return $result;
193 }
194 
215 function Level1WithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $uploadtree_tablename="uploadtree")
216 {
217  $pkarray = array();
218 
219  $Children = GetNonArtifactChildren($uploadtree_pk, $uploadtree_tablename);
220 
221  /* Loop throught each top level uploadtree_pk */
222  $offset = 0;
223  $limit = 1;
224  $order = "";
225  $tag_pk = null;
226  $result = NULL;
227  foreach ($Children as $row) {
228  //$uTime2 = microtime(true);
229  $result = GetFilesWithLicense($agent_pk, $rf_shortname, $row['uploadtree_pk'],
230  $PkgsOnly, $offset, $limit, $order, $tag_pk, $uploadtree_tablename);
231  //$Time = microtime(true) - $uTime2;
232  //printf( "GetFilesWithLicense($row[ufile_name]) time: %.2f seconds<br>", $Time);
233 
234  if (pg_num_rows($result) > 0) {
235  $pkarray[$row['uploadtree_pk']] = $row['ufile_name'];
236  }
237  }
238  if ($result) {
239  pg_free_result($result);
240  }
241  return $pkarray;
242 }
243 
244 
273 function FileListLinks($upload_fk, $uploadtree_pk, $napk, $pfile_pk, $Recurse=True, &$UniqueTagArray = array(), $uploadtree_tablename = "uploadtree", $wantTags=true)
274 {
275  $LinkStr = "";
276  global $SysConf;
277 
278  if ($pfile_pk) {
279  $text = _("View");
280  $text1 = _("Info");
281  $text2 = _("Download");
282 
283  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=view-license&upload=$upload_fk&item=$uploadtree_pk&napk=$napk' >$text</a>]";
284  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=view_info&upload=$upload_fk&item=$uploadtree_pk&show=detail' >$text1</a>]";
285  if ($_SESSION['UserLevel'] >= $SysConf['SYSCONFIG']['SourceCodeDownloadRights']) {
286  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=download&upload=$upload_fk&item=$uploadtree_pk' >$text2</a>]";
287  }
288 
289  }
290 
291  /******** Tag ********/
292  if ($wantTags && TagStatus($upload_fk)) { // check if tagging on one upload is disabled or not. 1: enable, 0: disable
293  $TagArray = GetAllTags($uploadtree_pk, $Recurse, $uploadtree_tablename);
294  $TagStr = "";
295  foreach ($TagArray as $TagPair) {
296  /* Build string of tags for this item */
297  if (! empty($TagStr)) {
298  $TagStr .= ",";
299  }
300  $TagStr .= " " . $TagPair['tag_name'];
301 
302  /* Update $UniqueTagArray */
303  $found = false;
304  foreach ($UniqueTagArray as $UTA_key => $UTA_row) {
305  if ($TagPair['tag_pk'] == $UTA_row['tag_pk']) {
306  $found = true;
307  break;
308  }
309  }
310  if (! $found) {
311  $UniqueTagArray[] = $TagPair;
312  }
313  }
314 
315  $text3 = _("Tag");
316  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=tag&upload=$upload_fk&item=$uploadtree_pk' >$text3</a>";
317 
318  $LinkStr .= "<span style='color:#2897B7'>";
319  $LinkStr .= $TagStr;
320  $LinkStr .= "</span>";
321  $LinkStr .= "]";
322  }
323  return $LinkStr;
324 }
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $offset=0, $limit="ALL", $order="", $tag_pk=null, $uploadtree_tablename="uploadtree")
Get files with a given license (shortname).
FileListLinks($upload_fk, $uploadtree_pk, $napk, $pfile_pk, $Recurse=True, &$UniqueTagArray=array(), $uploadtree_tablename="uploadtree", $wantTags=true)
Get list of links: [View][Info][Download]
GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
Same as GetFileLicenses() but returns license list as a single string.
Level1WithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $uploadtree_tablename="uploadtree")
Given an uploadtree_pk, find all the non-artifact, immediate children (uploadtree_pk's) that have lic...
GetFileLicenses($agent, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree', $duplicate="")
get all the licenses for a single file or uploadtree
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
GetAllTags($Item, $Recurse=true, $uploadtree_tablename="uploadtree")
Get all Tags of this uploadtree_pk.
Definition: common-tags.php:26
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:66
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN