FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo_monk_license_list.php
1 <?php
2 /***********************************************************
3  Copyright (C) 2013-2014 Hewlett-Packard Development Company, L.P.
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU General Public License
7  version 2 as published by the Free Software Foundation.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License along
15  with this program; if not, write to the Free Software Foundation, Inc.,
16  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
22 
23 require_once("$MODDIR/lib/php/common-cli.php");
24 cli_Init();
25 
26 $Usage = "Usage: " . basename($argv[0]) . "
27  -u upload id :: upload id
28  -t uploadtree id :: uploadtree id
29  -c sysconfdir :: Specify the directory for the system configuration
30  --username username :: username
31  --password password :: password
32  --container :: include container or not, 1: yes, 0: no (default)
33  -x :: do not show files which have unuseful license 'No_license_found' or no license
34  -X excluding :: Exclude files containing [free text] in the path.
35  'mac/' should exclude all files in the mac directory.
36  'mac' and it should exclude all files in any directory containing the substring 'mac'
37  '/mac' and it should exclude all files in any directory that starts with 'mac'
38  -h help, this message
39 ";
40 $upload = ""; // upload id
41 $item = ""; // uploadtree id
42 $showContainer = 0; // include container or not, 1: yes, 0: no (default)
43 $ignoreFilesWithoutLicense = 0; // do not show files which have no license, 1: yes, 0: no (default)
44 $excluding = '';
45 
46 $longopts = array("username:", "password:", "container:");
47 $options = getopt("c:u:t:hxX:", $longopts);
48 if (empty($options) || !is_array($options)) {
49  print $Usage;
50  return 1;
51 }
52 
53 $user = $passwd = "";
54 foreach ($options as $option => $value) {
55  switch ($option) {
56  case 'c': // handled in fo_wrapper
57  break;
58  case 'u':
59  $upload = $value;
60  break;
61  case 't':
62  $item = $value;
63  break;
64  case 'h':
65  print $Usage;
66  return 1;
67  case 'username':
68  $user = $value;
69  break;
70  case 'password':
71  $passwd = $value;
72  break;
73  case 'container':
74  $showContainer = $value;
75  break;
76  case 'x':
77  $ignoreFilesWithoutLicense = 1;
78  break;
79  case 'X':
80  $excluding = $value;
81  break;
82  default:
83  print "unknown option $option\n";
84  print $Usage;
85  }
86 }
87 
89 if (is_numeric($item) && !is_numeric($upload)) {
90  $upload = GetUploadID($item);
91 }
92 
94 if (!is_numeric($upload) || (!empty($item) && !is_numeric($item))) {
95  print "Upload ID or Uploadtree ID is not digital number\n";
96  print $Usage;
97  return 1;
98 }
99 
100 account_check($user, $passwd); // check username/password
101 
102 $return_value = read_permission($upload, $user); // check if the user has the permission to read this upload
103 if (empty($return_value)) {
104  $text = _("The user '$user' has no permission to read the information of upload $upload\n");
105  echo $text;
106  return 1;
107 }
108 
109 
119 function GetLicenseList($uploadtree_pk, $upload_pk, $showContainer, $excluding, $ignore)
120 {
121  /* @var $dbManager DbManager */
122  $dbManager = $GLOBALS['container']->get('db.manager');
123  /* @var $uploadDao UploadDao */
124  $uploadDao = $GLOBALS['container']->get("dao.upload");
125  /* @var $licenseDao LicenseDao */
126  $licenseDao = $GLOBALS['container']->get("dao.license");
127 
128  if (empty($uploadtree_pk)) {
129  $uploadtreeRec = $dbManager->getSingleRow('SELECT uploadtree_pk FROM uploadtree WHERE parent IS NULL AND upload_fk=$1',
130  array($upload_pk),
131  __METHOD__.'.find.uploadtree.to.use.in.browse.link' );
132  $uploadtree_pk = $uploadtreeRec['uploadtree_pk'];
133  }
134 
135  /* get last monk agent_pk that has data for this upload */
136  $AgentRec = AgentARSList("monk_ars", $upload_pk, 1);
137  if ($AgentRec === false) {
138  echo _("No data available \n");
139  return;
140  }
141  $agent_pk = $AgentRec[0]["agent_fk"];
142 
143  $uploadtreeTablename = GetUploadtreeTableName($upload_pk);
145  $itemTreeBounds = $uploadDao->getItemTreeBounds($uploadtree_pk, $uploadtreeTablename);
146  $licensesPerFileName = $licenseDao->getLicensesPerFileNameForAgentId(
147  $itemTreeBounds, array($agent_pk), true, $excluding, $ignore);
148 
149  foreach ($licensesPerFileName as $fileName => $licenseData) {
150  if ($licenseData == false) {
151  if ($showContainer) {
152  print($fileName."\n");
153  }
154  continue;
155  }
156 
157  if (! array_key_exists('scanResults', $licenseData) || empty($licenseData['scanResults'])) {
158  continue;
159  }
160 
161  $licenseNames = $licenseData['scanResults'];
162  if ($ignore && $licenseNames !== array()) {
163  continue;
164  }
165 
166  print($fileName .': '.implode($licenseNames,', ')."\n");
167  }
168 }
169 
171 GetLicenseList($item, $upload, $showContainer, $excluding, $ignoreFilesWithoutLicense);
172 return 0;
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:421
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:87
GetUploadID($uploadtreeid)
Get upload id through uploadtreeid.
Definition: common-ui.php:258
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:36
read_permission($upload, $user)
Check if the user has the permission to read the copyright/license/etc information of this upload...