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