FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_bucket_list.php
Go to the documentation of this file.
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 $Usage = "Usage: " . basename($argv[0]) . "
17  -u upload id :: upload id
18  -t uploadtree id :: uploadtree id
19  -c sysconfdir :: optional - Specify the directory for the system configuration
20  --user username :: user name
21  --password password :: password
22  -b bucket id :: bucket id
23  -a bucket agent id :: bucket agent id
24  -n nomos agent id :: nomos agent id
25  -X excluding :: Exclude files containing [free text] in the path.
26  'mac/' should exclude all files in the mac directory.
27  'mac' and it should exclude all files in any directory containing the substring 'mac'
28  '/mac' and it should exclude all files in any directory that starts with 'mac'
29  -h help, this message
30 ";
31 
32 $upload = $item = $bucket = $bucket_agent = $nomos_agent = "";
33 
34 $excluding = '';
35 $longopts = array("user:", "password:");
36 $options = getopt("c:u:t:b:a:n:hX:", $longopts);
37 if (empty($options) || ! is_array($options)) {
38  print $Usage;
39  return 1;
40 }
41 
42 $user = $passwd = "";
43 
44 foreach ($options as $option => $value) {
45  switch ($option) {
46  case 'c': // handled in fo_wrapper
47  break;
48  case 'u':
49  $upload = $value;
50  break;
51  case 't':
52  $item = $value;
53  break;
54  case 'a':
55  $bucket_agent = $value;
56  break;
57  case 'n':
58  $nomos_agent = $value;
59  break;
60  case 'b':
61  $bucket = $value;
62  break;
63  case 'h':
64  print $Usage;
65  return 1;
66  case 'user':
67  $user = $value;
68  break;
69  case 'password':
70  $passwd = $value;
71  break;
72  case 'X':
73  $excluding = $value;
74  break;
75  default:
76  print "unknown option $option\n";
77  print $Usage;
78  }
79 }
80 
81 if (!(is_numeric($item)) && !(is_numeric($upload))) {
82  print "At least provide uploadtree_id or upload_id.\n";
83  print $Usage;
84  return 1;
85 }
86 
88 $upload_from_item = $uploadtree1stid = "";
89 if (is_numeric($item)) {
91 } else if (empty($item) && is_numeric($upload)) {
92  $uploadtree1stid = Get1stUploadtreeID($upload);
93  if (empty($uploadtree1stid)) {
94  print "Upload $upload does not exist.\n";
95  print $Usage;
96  return 1;
97  } else {
98  $item = $uploadtree1stid;
99  $upload_from_item = $upload;
100  }
101 }
102 
103 // print "\$upload_from_item, \$item, \$upload, \$uploadtree1stid are: $upload_from_item, $item, $upload, $uploadtree1stid \n";
104 
105 if (empty($upload_from_item)) {
106  print "Uploadtree ID $item does not exist.\n";
107  print $Usage;
108  return 1;
109 } else if (empty($upload)) {
110  $upload = $upload_from_item;
111 } else if ($upload_from_item != $upload) {
112  print "Uploadtree ID $item does not under Upload $upload.\n";
113  print $Usage;
114  return 1;
115 }
116 
118 if (!is_numeric($bucket) || !is_numeric($bucket_agent) || !is_numeric($nomos_agent)) {
119  print "please enter the correct bucket agent ID and nomos agent ID and bucket ID.\n";
120  // print "\$upload, \$item are $upload, $item \n";
121  Usage4Options($upload, $item);
122  print $Usage;
123  return 1;
124 }
125 
126 account_check($user, $passwd); // check username/password
127 
128 $return_value = read_permission($upload, $user); // check if the user has the permission to read this upload
129 if (empty($return_value)) {
130  $text = _("The user '$user' has no permission to read the information of upload $upload\n");
131  echo $text;
132  return 1;
133 }
134 
135 require_once("$MODDIR/lib/php/common.php");
136 
138 GetBucketList($bucket, $bucket_agent, $nomos_agent, $item, $upload);
139 return 0;
140 
150 function GetBucketList($bucket_pk, $bucket_agent, $nomos_agent, $uploadtree_pk, $upload_pk = 0)
151 {
152  global $PG_CONN;
153  global $excluding;
154 
156  $sql = "SELECT bucket_name from bucket_def where bucket_pk = $bucket_pk;";
157  $result = pg_query($PG_CONN, $sql);
158  DBCheckResult($result, $sql, __FILE__, __LINE__);
159  $row = pg_fetch_assoc($result);
160  pg_free_result($result);
161 
162  $uploadtree_tablename = GetUploadtreeTableName($upload_pk);
163 
164  /* get the top of tree */
165  $sql = "SELECT upload_fk, lft, rgt, uploadtree_pk from $uploadtree_tablename";
166 
167  if ($uploadtree_pk) { // if uploadtree_pk is null, that means get all data on an upload
168  $sql .= " where uploadtree_pk='$uploadtree_pk';";
169  } else {
170  $sql .= " where upload_fk='$upload_pk' and parent is null;";
171  }
172  $result = pg_query($PG_CONN, $sql);
173  DBCheckResult($result, $sql, __FILE__, __LINE__);
174  $toprow = pg_fetch_assoc($result);
175  $uploadtree_pk = $toprow['uploadtree_pk'];
176  pg_free_result($result);
177 
178  if (empty($toprow)) {
179  print "Sorry, Can not find upload $upload_pk.\n";
180  return 1;
181  }
182 
183  print "For uploadtree $uploadtree_pk under upload $upload_pk has bucket $row[bucket_name]:\n";
184  /* loop through all the records in this tree */
185  $sql = "select uploadtree_pk, ufile_name, lft, rgt from $uploadtree_tablename, bucket_file
186  where upload_fk=$upload_pk
187  and lft>'$toprow[lft]' and rgt<'$toprow[rgt]'
188  and ((ufile_mode & (1<<28)) = 0) and ((ufile_mode & (1<<29)) = 0) and bucket_file.pfile_fk = $uploadtree_tablename.pfile_fk
189  and bucket_fk = '$bucket_pk' and agent_fk = '$bucket_agent' and nomosagent_fk = '$nomos_agent'
190  order by uploadtree_pk";
191  $outerresult = pg_query($PG_CONN, $sql);
192 
193  /* Select each uploadtree row in this tree, write out text */
194  $excluding_flag = 0; // 1: exclude 0: not exclude
195  while ($row = pg_fetch_assoc($outerresult)) {
196  $filepatharray = Dir2Path($row['uploadtree_pk'], $uploadtree_tablename);
197  $filepath = "";
198  foreach ($filepatharray as $uploadtreeRow) {
199  if (! empty($filepath)) {
200  $filepath .= "/";
201  /* filepath contains 'xxxx/', '/xxxx/', 'xxxx', '/xxxx' */
202  $excluding_flag = ContainExcludeString($filepath, $excluding);
203  if (1 == $excluding_flag) {
204  break;
205  }
206  }
207  $filepath .= $uploadtreeRow['ufile_name'];
208  }
209  if (1 == $excluding_flag) {
210  continue; // excluding files whose path contains excluding text
211  }
212  $V = $filepath;
213  print "$V";
214  print "\n";
215  }
216  pg_free_result($outerresult);
217 }
218 
219 function Usage4Options($UploadID, $item)
220 {
221  global $PG_CONN;
222  $sql = "SELECT agent_fk as bucket_agent_id, nomosagent_fk as nomos_agent_id, bucket_pk as bucket_id, bucket_ars.bucketpool_fk as bucketpoo_id, bucket_name from bucket_ars right join bucket_def on bucket_def.bucketpool_fk = bucket_ars.bucketpool_fk where upload_fk = '$UploadID';";
223  $result = pg_query($PG_CONN, $sql);
224  DBCheckResult($result, $sql, __FILE__, __LINE__);
225  $bucket_arr = pg_fetch_all($result);
226  pg_free_result($result);
227  $clause4uploadtree = "";
228  if ($item) {
229  $clause4uploadtree = " uploadtree $item";
230  }
231  if ($bucket_arr) {
232  print "For"."$clause4uploadtree under upload $UploadID, you can specify options below: \n
233  bucket_agent_id : -a
234  nomos_agent_id : -n
235  bucket_id : -b
236  \n";
237  print_r($bucket_arr);
238  } else {
239  print "Please confirm uploadtree $item under upload $UploadID has done one bucket scanning.\n";
240  }
241 }
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
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Dir2Path($uploadtree_pk, $uploadtree_tablename='uploadtree')
Return the path (without artifacts) of an uploadtree_pk.
Definition: common-dir.php:222
ContainExcludeString($FilePath, $ExcludingText)
Check if one file path contains an excluding text.
GetUploadID($uploadtreeid)
Get upload id through uploadtreeid.
Definition: common-ui.php:272
Get1stUploadtreeID($upload)
Get 1st uploadtree id through upload id.
Definition: common-ui.php:293
GetBucketList($bucket_pk, $bucket_agent, $nomos_agent, $uploadtree_pk, $upload_pk=0)
get bucket list of one specified upload or all uploads
foreach($options as $option=> $value) if(!(is_numeric($item)) &&!(is_numeric($upload))) $upload_from_item
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
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN