FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_nomos_license_list.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2013-2014 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
16 
17 require_once("$MODDIR/lib/php/common-cli.php");
18 cli_Init();
19 
20 $Usage = "Usage: " . basename($argv[0]) . "
21  -u upload id :: upload id
22  -t uploadtree id :: uploadtree id
23  -c sysconfdir :: Specify the directory for the system configuration
24  --type export type :: For License: license (default), For Copyright: copyright
25  --username username :: username
26  --password password :: password
27  --container :: include container or not, 1: yes, 0: no (default)
28  -x :: License from files which do not have unuseful license 'No_license_found' or no license
29  -y :: Copyrights from files which do not have license
30  Files without license refers to:
31  File had no license finding by agents and no license was added by users.
32  File had license findings by agents but were either removed or file was marked as irrelevant.
33  -X excluding :: Exclude files containing [free text] in the path
34  'mac/' should exclude all files in the mac directory.
35  'mac' and it should exclude all files in any directory containing the substring 'mac'
36  '/mac' and it should exclude all files in any directory that starts with 'mac'
37  -h help, this message
38 ";
39 $upload = ""; // upload id
40 $item = ""; // uploadtree id
41 $showContainer = 0; // include container or not, 1: yes, 0: no (default)
42 $ignoreFilesWithoutLicense = 0; // do not show files which have no license, 1: yes, 0: no (default)
43 $ignoreFilesWithLicense = 0; // Copyrights from files which do not have license, 1: yes, 0: no (default)
44 $excluding = '';
45 
46 $longopts = array("username:", "password:", "container:", "type:");
47 $options = getopt("c:u:t:hxyX:", $longopts);
48 if (empty($options) || !is_array($options)) {
49  print $Usage;
50  return 1;
51 }
52 
53 $user = $passwd = "";
54 $type = "license";
55 foreach ($options as $option => $value) {
56  switch ($option) {
57  case 'c': // handled in fo_wrapper
58  break;
59  case 'u':
60  $upload = $value;
61  break;
62  case 't':
63  $item = $value;
64  break;
65  case 'h':
66  print $Usage;
67  return 1;
68  case 'type':
69  $type = $value;
70  break;
71  case 'username':
72  $user = $value;
73  break;
74  case 'password':
75  $passwd = $value;
76  break;
77  case 'container':
78  $showContainer = $value;
79  break;
80  case 'x':
81  $ignoreFilesWithoutLicense = 1;
82  break;
83  case 'y':
84  $ignoreFilesWithLicense = 1;
85  break;
86  case 'X':
87  $excluding = $value;
88  break;
89  default:
90  print "unknown option $option\n";
91  print $Usage;
92  }
93 }
94 
96 if (is_numeric($item) && !is_numeric($upload)) {
97  $upload = GetUploadID($item);
98 }
99 
101 if (!is_numeric($upload) || (!empty($item) && !is_numeric($item))) {
102  print "Upload ID or Uploadtree ID is not digital number\n";
103  print $Usage;
104  return 1;
105 }
106 
107 account_check($user, $passwd); // check username/password
108 
109 $return_value = read_permission($upload, $user); // check if the user has the permission to read this upload
110 if (empty($return_value)) {
111  $text = _("The user '$user' has no permission to read the information of upload $upload\n");
112  echo $text;
113  return 1;
114 }
115 
116 
126 function GetLicenseList($uploadtree_pk, $upload_pk, $showContainer, $excluding, $ignore)
127 {
128  /* @var $dbManager DbManager */
129  $dbManager = $GLOBALS['container']->get('db.manager');
130  /* @var $uploadDao UploadDao */
131  $uploadDao = $GLOBALS['container']->get("dao.upload");
132  /* @var $licenseDao LicenseDao */
133  $licenseDao = $GLOBALS['container']->get("dao.license");
134 
135  if (empty($uploadtree_pk)) {
136  $uploadtreeRec = $dbManager->getSingleRow('SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
137  array($upload_pk),
138  __METHOD__.'.find.uploadtree.to.use.in.browse.link' );
139  $uploadtree_pk = $uploadtreeRec['uploadtree_pk'];
140  }
141 
142  /* get last nomos agent_pk that has data for this upload */
143  $AgentRec = AgentARSList("nomos_ars", $upload_pk, 1);
144  if ($AgentRec === false) {
145  echo _("No data available\n");
146  return;
147  }
148  $agent_pk = $AgentRec[0]["agent_fk"];
149 
150  $uploadtreeTablename = getUploadtreeTableName($upload_pk);
152  $itemTreeBounds = $uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
153  $licensesPerFileName = $licenseDao->getLicensesPerFileNameForAgentId(
154  $itemTreeBounds, array($agent_pk), true, $excluding, $ignore);
155 
156  foreach ($licensesPerFileName as $fileName => $licenseData) {
157  if ($licenseData == false) {
158  if ($showContainer) {
159  print($fileName."\n");
160  }
161  continue;
162  }
163 
164  if (! array_key_exists('scanResults', $licenseData) || empty($licenseData['scanResults'])) {
165  continue;
166  }
167 
168  $licenseNames = $licenseData['scanResults'];
169  if (($ignore && (empty($licenseNames) || in_array("No_license_found", $licenseNames) || in_array("Void", $licenseNames)))) {
170  continue;
171  }
172 
173  print($fileName .': '.implode($licenseNames,', ')."\n");
174  }
175 }
176 
185 function GetCopyrightList($uploadtree_pk, $upload_pk, $exclude, $ignore)
186 {
187  /* @var $dbManager DbManager */
188  $dbManager = $GLOBALS['container']->get('db.manager');
189  /* @var $uploadDao UploadDao */
190  $uploadDao = $GLOBALS['container']->get("dao.upload");
191  /* @var $copyrightDao CopyrightDao */
192  $copyrightDao = $GLOBALS['container']->get("dao.copyright");
193  /* @var $treeDao TreeDao */
194  $treeDao = $GLOBALS['container']->get("dao.tree");
195 
196  if (empty($uploadtree_pk)) {
197  try {
198  $uploadtree_pk = $uploadDao->getUploadParent($upload_pk);
199  } catch(Exception $e) {
200  print($e);
201  return;
202  }
203  }
204 
205  $agentName = "copyright";
206  $scanJobProxy = new ScanJobProxy($GLOBALS['container']->get('dao.agent'), $upload_pk);
207  $scanJobProxy->createAgentStatus([$agentName]);
208  $selectedScanners = $scanJobProxy->getLatestSuccessfulAgentIds();
209  $latestAgentId = $selectedScanners[$agentName];
210  $agentFilter = ' AND C.agent_fk='.$latestAgentId;
211  $uploadtreeTablename = getUploadtreeTableName($upload_pk);
212  $itemTreeBounds = $uploadDao->getItemTreeBounds($uploadtree_pk,$uploadtreeTablename);
213  $extrawhere = "UT.lft BETWEEN " . $itemTreeBounds->getLeft() . " AND " .
214  $itemTreeBounds->getRight();
215 
216  $lines = [];
217  $copyrights = $copyrightDao->getScannerEntries($agentName, $uploadtreeTablename, $upload_pk, null, $extrawhere . $agentFilter);
218  foreach ($copyrights as $copyright) {
219  $row = [];
220  $row["content"] = $copyright["content"];
221  $row["filePath"] = $treeDao->getFullPath($copyright["uploadtree_pk"], $uploadtreeTablename);
222  $lines[$row["filePath"]][] = $row;
223  }
224  $copyrights = $copyrightDao->getEditedEntries('copyright_decision', $uploadtreeTablename, $upload_pk, [], $extrawhere);
225  foreach ($copyrights as $copyright) {
226  $row = [];
227  $row["content"] = $copyright["textfinding"];
228  $row["filePath"] = $treeDao->getFullPath($copyright["uploadtree_pk"], $uploadtreeTablename);
229  $lines[$row["filePath"]][] = $row;
230  }
231 
232  if ($ignore) {
233  $agentList = [];
234  foreach (AgentRef::AGENT_LIST as $agentname => $value) {
235  $AgentRec = AgentARSList($agentname."_ars", $upload_pk, 1);
236  if (!empty($AgentRec)) {
237  $agentList[] = $AgentRec[0]["agent_fk"];
238  }
239  }
240  removeCopyrightWithLicense($lines, $itemTreeBounds, $agentList, $exclude);
241  }
242 
243  $reducedLines = array();
244  foreach ($lines as $line) {
245  foreach ($line as $copyright) {
246  $reducedLines[] = $copyright;
247  }
248  }
249 
250  foreach ($reducedLines as $row) {
251  if (!empty($exclude) && false!==strpos("$row[filePath]", $exclude)) {
252  continue;
253  } else {
254  print($row['filePath'] . ": " . ($row['content']) . "\n");
255  }
256  }
257 }
258 
267 function removeCopyrightWithLicense(&$lines, $itemTreeBounds, $agentList, $exclude)
268 {
269  /* @var $clearingDao ClearingDao */
270  $clearingDao = $GLOBALS['container']->get("dao.clearing");
271  /* @var $clearingFilter ClearingFilter */
272  $clearingFilter = $GLOBALS['container']->get("businessrules.clearing_decision_filter");
273  /* @var $licenseDao LicenseDao */
274  $licenseDao = $GLOBALS['container']->get("dao.license");
275 
276  $licensesPerFileName = array();
277  $allDecisions = $clearingDao->getFileClearingsFolder($itemTreeBounds, Auth::getGroupId());
278  $editedMappedLicenses = $clearingFilter->filterCurrentClearingDecisionsForCopyrightList($allDecisions);
279  $licensesPerFileName = $licenseDao->getLicensesPerFileNameForAgentId($itemTreeBounds, $agentList, true, $exclude, true, $editedMappedLicenses);
280  foreach ($licensesPerFileName as $fileName => $licenseNames) {
281  if ($licenseNames !== false && count($licenseNames) > 0) {
282  if (array_key_exists('concludedResults', $licenseNames)) {
283  $consolidatedConclusions = array();
284  foreach ($licenseNames['concludedResults'] as $conclusion) {
285  $consolidatedConclusions = array_merge($consolidatedConclusions, $conclusion);
286  }
287  $conclusions = array_unique($consolidatedConclusions);
288  if (in_array("Void", $conclusions)) {
289  // File has all licenses removed or irrelevant decision
290  continue;
291  }
292  // File has license conclusions
293  foreach (array_keys($lines) as $file) {
294  if (strpos($file, $fileName) !== false) {
295  unset($lines[$file]);
296  break;
297  }
298  }
299  }
300  if ((! empty($licenseNames['scanResults'])) && ! (in_array("No_license_found", $licenseNames['scanResults']) || in_array("Void", $licenseNames['scanResults']))) {
301  foreach (array_keys($lines) as $file) {
302  if (strpos($file, $fileName) !== false) {
303  unset($lines[$file]);
304  break;
305  }
306  }
307  }
308  }
309  }
310 }
311 
313 if ($type=="license") {
314  GetLicenseList($item, $upload, $showContainer, $excluding, $ignoreFilesWithoutLicense);
315 } else {
316  GetCopyrightList($item, $upload, $excluding, $ignoreFilesWithLicense);
317 }
318 return 0;
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
read_permission($upload, $user)
Check if the user has the permission to read the copyright/license/etc information of this upload.
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:75
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:25
GetUploadID($uploadtreeid)
Get upload id through uploadtreeid.
Definition: common-ui.php:272