FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_monk_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 
11 
12 require_once("$MODDIR/lib/php/common-cli.php");
13 cli_Init();
14 
15 $Usage = "Usage: " . basename($argv[0]) . "
16  -u upload id :: upload id
17  -t uploadtree id :: uploadtree id
18  -c sysconfdir :: Specify the directory for the system configuration
19  --username username :: username
20  --password password :: password
21  --container :: include container or not, 1: yes, 0: no (default)
22  -x :: do not show files which have unuseful license 'No_license_found' or no license
23  -X excluding :: Exclude files containing [free text] in the path.
24  'mac/' should exclude all files in the mac directory.
25  'mac' and it should exclude all files in any directory containing the substring 'mac'
26  '/mac' and it should exclude all files in any directory that starts with 'mac'
27  -h help, this message
28 ";
29 $upload = ""; // upload id
30 $item = ""; // uploadtree id
31 $showContainer = 0; // include container or not, 1: yes, 0: no (default)
32 $ignoreFilesWithoutLicense = 0; // do not show files which have no license, 1: yes, 0: no (default)
33 $excluding = '';
34 
35 $longopts = array("username:", "password:", "container:");
36 $options = getopt("c:u:t:hxX:", $longopts);
37 if (empty($options) || !is_array($options)) {
38  print $Usage;
39  return 1;
40 }
41 
42 $user = $passwd = "";
43 foreach ($options as $option => $value) {
44  switch ($option) {
45  case 'c': // handled in fo_wrapper
46  break;
47  case 'u':
48  $upload = $value;
49  break;
50  case 't':
51  $item = $value;
52  break;
53  case 'h':
54  print $Usage;
55  return 1;
56  case 'username':
57  $user = $value;
58  break;
59  case 'password':
60  $passwd = $value;
61  break;
62  case 'container':
63  $showContainer = $value;
64  break;
65  case 'x':
66  $ignoreFilesWithoutLicense = 1;
67  break;
68  case 'X':
69  $excluding = $value;
70  break;
71  default:
72  print "unknown option $option\n";
73  print $Usage;
74  }
75 }
76 
78 if (is_numeric($item) && !is_numeric($upload)) {
79  $upload = GetUploadID($item);
80 }
81 
83 if (!is_numeric($upload) || (!empty($item) && !is_numeric($item))) {
84  print "Upload ID or Uploadtree ID is not digital number\n";
85  print $Usage;
86  return 1;
87 }
88 
89 account_check($user, $passwd); // check username/password
90 
91 $return_value = read_permission($upload, $user); // check if the user has the permission to read this upload
92 if (empty($return_value)) {
93  $text = _("The user '$user' has no permission to read the information of upload $upload\n");
94  echo $text;
95  return 1;
96 }
97 
98 
108 function GetLicenseList($uploadtree_pk, $upload_pk, $showContainer, $excluding, $ignore)
109 {
110  /* @var $dbManager DbManager */
111  $dbManager = $GLOBALS['container']->get('db.manager');
112  /* @var $uploadDao UploadDao */
113  $uploadDao = $GLOBALS['container']->get("dao.upload");
114  /* @var $licenseDao LicenseDao */
115  $licenseDao = $GLOBALS['container']->get("dao.license");
116 
117  if (empty($uploadtree_pk)) {
118  $uploadtreeRec = $dbManager->getSingleRow('SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
119  array($upload_pk),
120  __METHOD__.'.find.uploadtree.to.use.in.browse.link' );
121  $uploadtree_pk = $uploadtreeRec['uploadtree_pk'];
122  }
123 
124  /* get last monk agent_pk that has data for this upload */
125  $AgentRec = AgentARSList("monk_ars", $upload_pk, 1);
126  if ($AgentRec === false) {
127  echo _("No data available \n");
128  return;
129  }
130  $agent_pk = $AgentRec[0]["agent_fk"];
131 
132  $uploadtreeTablename = GetUploadtreeTableName($upload_pk);
134  $itemTreeBounds = $uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
135  $licensesPerFileName = $licenseDao->getLicensesPerFileNameForAgentId(
136  $itemTreeBounds, array($agent_pk), true, $excluding, $ignore);
137 
138  foreach ($licensesPerFileName as $fileName => $licenseData) {
139  if ($licenseData == false) {
140  if ($showContainer) {
141  print($fileName."\n");
142  }
143  continue;
144  }
145 
146  if (! array_key_exists('scanResults', $licenseData) || empty($licenseData['scanResults'])) {
147  continue;
148  }
149 
150  $licenseNames = $licenseData['scanResults'];
151  if ($ignore && $licenseNames !== array()) {
152  continue;
153  }
154 
155  print($fileName .': '.implode($licenseNames,', ')."\n");
156  }
157 }
158 
160 GetLicenseList($item, $upload, $showContainer, $excluding, $ignoreFilesWithoutLicense);
161 return 0;
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
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414