FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-license-file.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2009-2014 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2014 Siemens AG
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License version 2.1 as published by the Free Software Foundation.
9 
10  This library 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 GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this library; if not, write to the Free Software Foundation, Inc.0
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 ***********************************************************/
19 
41 function GetFileLicenses($agent, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree', $duplicate="")
42 {
43  global $PG_CONN;
44 
45  if (empty($agent)) {
46  Fatal("Missing parameter: agent", __FILE__, __LINE__);
47  }
48 
49  if ($uploadtree_pk) {
50  /* Find lft and rgt bounds for this $uploadtree_pk */
51  $sql = "SELECT lft, rgt, upload_fk FROM $uploadtree_tablename
52  WHERE uploadtree_pk = $uploadtree_pk";
53  $result = pg_query($PG_CONN, $sql);
54  DBCheckResult($result, $sql, __FILE__, __LINE__);
55  $row = pg_fetch_assoc($result);
56  $lft = $row["lft"];
57  $rgt = $row["rgt"];
58  $upload_pk = $row["upload_fk"];
59  pg_free_result($result);
60 
61  $agentIdCondition = $agent != "any" ? "and agent_fk=$agent" : "";
62 
63  /* Strip out added upload_pk condition if it isn't needed */
64  if (($uploadtree_tablename == "uploadtree_a") ||
65  ($uploadtree_tablename == "uploadtree")) {
66  $UploadClause = "upload_fk=$upload_pk and ";
67  } else {
68  $UploadClause = "";
69  }
70 
71  /* Get the licenses under this $uploadtree_pk */
72  $sql = "SELECT distinct(rf_shortname) as rf_shortname, rf_pk as rf_fk, fl_pk
73  from license_file_ref,
74  (SELECT distinct(pfile_fk) as PF from $uploadtree_tablename
75  where $UploadClause lft BETWEEN $lft and $rgt) as SS
76  where PF=pfile_fk $agentIdCondition
77  order by rf_shortname asc";
78  $result = pg_query($PG_CONN, $sql);
79  DBCheckResult($result, $sql, __FILE__, __LINE__);
80  } else {
81  Fatal("Missing function inputs", __FILE__, __LINE__);
82  }
83 
84  $LicArray = array();
85  if ($duplicate) {
86  // get duplicated licenses
87  while ($row = pg_fetch_assoc($result)) {
88  $LicArray[$row['fl_pk']] = $row['rf_shortname'];
89  }
90  } else { // do not return duplicated licenses
91  while ($row = pg_fetch_assoc($result)) {
92  $LicArray[$row['rf_fk']] = $row['rf_shortname'];
93  }
94  }
95  pg_free_result($result);
96  return $LicArray;
97 }
98 
109 function GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
110 {
111  $LicStr = "";
112  $LicArray = GetFileLicenses($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename);
113  return implode($LicArray,', ');
114 }
115 
135 function GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk,
136  $PkgsOnly=false, $offset=0, $limit="ALL",
137  $order="", $tag_pk=null, $uploadtree_tablename)
138 {
139  global $PG_CONN;
140 
141  /* Find lft and rgt bounds for this $uploadtree_pk */
142  $sql = "SELECT lft, rgt, upload_fk FROM $uploadtree_tablename WHERE uploadtree_pk = $uploadtree_pk";
143  $result = pg_query($PG_CONN, $sql);
144  DBCheckResult($result, $sql, __FILE__, __LINE__);
145  $row = pg_fetch_assoc($result);
146  $lft = $row["lft"];
147  $rgt = $row["rgt"];
148  $upload_pk = $row["upload_fk"];
149  pg_free_result($result);
150 
151  /* Find rf_pk for rf_shortname. This will speed up the main query tremendously */
152  $sql = "SELECT rf_pk FROM license_ref WHERE rf_shortname='$rf_shortname'";
153  $result = pg_query($PG_CONN, $sql);
154  DBCheckResult($result, $sql, __FILE__, __LINE__);
155  $row = pg_fetch_assoc($result);
156  $rf_pk = $row["rf_pk"];
157  while ($row = pg_fetch_assoc($result)) {
158  $rf_pk .= "," . $row["rf_pk"];
159  }
160  pg_free_result($result);
161 
162  if (empty($rf_pk)) {
163  return array(); // if when the rf_shortname does not exist
164  }
165 
166  $shortname = pg_escape_string($rf_shortname);
167 
168  /* Optional tag restriction */
169  if (empty($tag_pk)) {
170  $TagTable = "";
171  $TagClause = "";
172  } else {
173  $TagTable = "tag_file,";
174  $TagClause = "and PF=tag_file.pfile_fk and tag_fk=$tag_pk";
175  }
176 
177  $agentCondition = '';
178  if (is_array($agent_pk)) {
179  $agentCondition = ' AND agent_fk IN (' . implode(',', $agent_pk) . ')';
180  } else if ($agent_pk != "any") {
181  $agentCondition = "and agent_fk=$agent_pk";
182  }
183 
184  /* Strip out added upload_pk condition if it isn't needed */
185  if (($uploadtree_tablename == "uploadtree_a") || ($uploadtree_tablename == "uploadtree")) {
186  $UploadClause = "upload_fk=$upload_pk and ";
187  } else {
188  $UploadClause = "";
189  }
190  $theLimit = ($limit=='ALL') ? '' : "LIMIT $limit";
191  $sql = "select uploadtree_pk, license_file.pfile_fk, ufile_name, agent_name, max(agent_pk) agent_pk
192  from license_file, agent, $TagTable
193  (SELECT pfile_fk as PF, uploadtree_pk, ufile_name from $uploadtree_tablename
194  where $UploadClause
195  lft BETWEEN $lft and $rgt
196  and ufile_mode & (3<<28) = 0 ) as SS
197  where PF=license_file.pfile_fk $agentCondition and rf_fk in ($rf_pk)
198  AND agent_pk=agent_fk
199  $TagClause
200  GROUP BY uploadtree_pk, license_file.pfile_fk, ufile_name, agent_name
201  $order $theLimit offset $offset";
202  $result = pg_query($PG_CONN, $sql); // Top uploadtree_pk's
203  DBCheckResult($result, $sql, __FILE__, __LINE__);
204  return $result;
205 }
206 
227 function Level1WithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $uploadtree_tablename)
228 {
229  $pkarray = array();
230 
231  $Children = GetNonArtifactChildren($uploadtree_pk, $uploadtree_tablename);
232 
233  /* Loop throught each top level uploadtree_pk */
234  $offset = 0;
235  $limit = 1;
236  $order = "";
237  $tag_pk = null;
238  $result = NULL;
239  foreach ($Children as $row) {
240  //$uTime2 = microtime(true);
241  $result = GetFilesWithLicense($agent_pk, $rf_shortname, $row['uploadtree_pk'],
242  $PkgsOnly, $offset, $limit, $order, $tag_pk, $uploadtree_tablename);
243  //$Time = microtime(true) - $uTime2;
244  //printf( "GetFilesWithLicense($row[ufile_name]) time: %.2f seconds<br>", $Time);
245 
246  if (pg_num_rows($result) > 0) {
247  $pkarray[$row['uploadtree_pk']] = $row['ufile_name'];
248  }
249  }
250  if ($result) {
251  pg_free_result($result);
252  }
253  return $pkarray;
254 }
255 
256 
285 function FileListLinks($upload_fk, $uploadtree_pk, $napk, $pfile_pk, $Recurse=True, &$UniqueTagArray = array(), $uploadtree_tablename = "uploadtree", $wantTags=true)
286 {
287  $LinkStr = "";
288  global $SysConf;
289 
290  if ($pfile_pk) {
291  $text = _("View");
292  $text1 = _("Info");
293  $text2 = _("Download");
294 
295  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=view-license&upload=$upload_fk&item=$uploadtree_pk&napk=$napk' >$text</a>]";
296  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=view_info&upload=$upload_fk&item=$uploadtree_pk&show=detail' >$text1</a>]";
297  if ($_SESSION['UserLevel'] >= $SysConf['SYSCONFIG']['SourceCodeDownloadRights']) {
298  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=download&upload=$upload_fk&item=$uploadtree_pk' >$text2</a>]";
299  }
300 
301  }
302 
303  /******** Tag ********/
304  if ($wantTags && TagStatus($upload_fk)) { // check if tagging on one upload is disabled or not. 1: enable, 0: disable
305  $TagArray = GetAllTags($uploadtree_pk, $Recurse, $uploadtree_tablename);
306  $TagStr = "";
307  foreach ($TagArray as $TagPair) {
308  /* Build string of tags for this item */
309  if (! empty($TagStr)) {
310  $TagStr .= ",";
311  }
312  $TagStr .= " " . $TagPair['tag_name'];
313 
314  /* Update $UniqueTagArray */
315  $found = false;
316  foreach ($UniqueTagArray as $UTA_key => $UTA_row) {
317  if ($TagPair['tag_pk'] == $UTA_row['tag_pk']) {
318  $found = true;
319  break;
320  }
321  }
322  if (! $found) {
323  $UniqueTagArray[] = $TagPair;
324  }
325  }
326 
327  $text3 = _("Tag");
328  $LinkStr .= "[<a href='" . Traceback_uri() . "?mod=tag&upload=$upload_fk&item=$uploadtree_pk' >$text3</a>";
329 
330  $LinkStr .= "<span style='color:#2897B7'>";
331  $LinkStr .= $TagStr;
332  $LinkStr .= "</span>";
333  $LinkStr .= "]";
334  }
335  return $LinkStr;
336 }
Traceback_uri()
Get the URI without query to this location.
GetAllTags($Item, $Recurse=true, $uploadtree_tablename="uploadtree")
Get all Tags of this uploadtree_pk.
Definition: common-tags.php:37
GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
Same as GetFileLicenses() but returns license list as a single string.
FileListLinks($upload_fk, $uploadtree_pk, $napk, $pfile_pk, $Recurse=True, &$UniqueTagArray=array(), $uploadtree_tablename="uploadtree", $wantTags=true)
Get list of links: [View][Info][Download]
Level1WithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $uploadtree_tablename)
Given an uploadtree_pk, find all the non-artifact, immediate children (uploadtree_pk&#39;s) that have lic...
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
GetFileLicenses($agent, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree', $duplicate="")
get all the licenses for a single file or uploadtree
Fatal($msg, $filenm, $lineno)
Write message to stdout and die.
Definition: common-ui.php:75
GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $offset=0, $limit="ALL", $order="", $tag_pk=null, $uploadtree_tablename)
Get files with a given license (shortname).