FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_tagfoss.php
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2012 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 
17 // $DATAROOTDIR and $PROJECT come from Makefile
18 //require_once "$DATAROOTDIR/$PROJECT/lib/php/bootstrap.php";
19 require_once "/usr/local/share/fossology/lib/php/bootstrap.php";
20 
21 $SysConf = array(); // fo system configuration variables
22 $PG_CONN = 0; // Database connection
23 
24 /* Set SYSCONFDIR and set global (for backward compatibility) */
25 $SysConf = bootstrap();
26 
27 /* Initialize global system configuration variables $SysConfig[] */
28 ConfigInit($SYSCONFDIR, $SysConf);
29 
30 // Maximum number of sha1's to send to antelink in a single batch
31 $MaxSend = 500;
32 
33 /* -p -u {upload_pk} -t {tag_pk}
34  * -u and -t are manditory
35  */
36 $Options = getopt("pt:u:");
37 if ( array_key_exists('t', $Options)
38  && array_key_exists('u', $Options)
39  ) {
40  $Missing = array_key_exists('m', $Options) ? true : false;
41  $tag_pk = $Options['t'];
42  $upload_pk = $Options['u'];
43 } else {
44  echo "Fatal: Missing parameter\n";
45  Usage($argc, $argv);
46  exit -1;
47 }
48 
49 if ( array_key_exists('p', $Options)) {
50  $PrintOnly = true;
51 } else {
52  $PrintOnly = false;
53 }
54 
55 $sql = "select distinct(pfile_fk), pfile_sha1, ufile_name from uploadtree,pfile where upload_fk='$upload_pk' and pfile_pk=pfile_fk";
56 $result = pg_query($PG_CONN, $sql);
57 DBCheckResult($result, $sql, __FILE__, __LINE__);
58 if (pg_num_rows($result) == 0) {
59  echo "Empty upload_pk $upload_pk\n";
60  exit;
61 }
62 
63 /* loop through each row accumulating groups of $MaxSend files (sha1's) to send to antelink */
64 $ToAntelink = array();
65 $TaggedFileCount = 0;
66 $TotalFileCount = 0;
67 while ($row = pg_fetch_assoc($result)) {
68  $TotalFileCount ++;
69  $ToAntelink[] = $row;
70  if (count($ToAntelink) >= $MaxSend) {
71  $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly);
72  $ToAntelink = array();
73  }
74 }
75 exit;
76 if (count($ToAntelink)) {
77  $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly);
78 }
79 
80 echo "$TaggedFileCount files tagged out of $TotalFileCount files.\n";
81 
82 return (0);
83 
84 /**************************** functions ************************************/
85 function Usage($argc, $argv)
86 {
87  echo "$argv[0] -p -u {upload_pk} -t {tag_pk}\n";
88  echo " -p means to only print out filenames to be tagged, but do not update the db.\n";
89 }
90 
91 
99 function QueryTag($ToAntelink, $tag_pk, $PrintOnly)
100 {
101  global $PG_CONN;
102  global $SysConf;
103  $AntepediaServer = "https://api.antepedia.com/acme/v3/bquery/";
104 
105  /* parse http_proxy server and port */
106  $http_proxy = $SysConf['FOSSOLOGY']['http_proxy'];
107  $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy, ":"));
108  $ProxyPort = substr(strrchr($http_proxy, ":"), 1);
109 
110  /* construct array of just sha1's */
111  $sha1array = array();
112  foreach ($ToAntelink as $row) {
113  $sha1array[] = $row['pfile_sha1'];
114  }
115  $PostData = json_encode($sha1array);
116 
117  $curlch = curl_init($AntepediaServer);
118  //turning off the server and peer verification(TrustManager Concept).
119  curl_setopt($curlch, CURLOPT_SSL_VERIFYPEER, FALSE);
120  curl_setopt($curlch, CURLOPT_SSL_VERIFYHOST, 2);
121 
122  curl_setopt($curlch, CURLOPT_POST, TRUE);
123  curl_setopt($curlch,CURLOPT_POSTFIELDS, $PostData);
124  curl_setopt($curlch, CURLOPT_RETURNTRANSFER, TRUE);
125  curl_setopt($curlch,CURLOPT_USERAGENT,'Curl-php');
126 
127  if (! empty($ProxyServer)) {
128  curl_setopt($curlch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
129  curl_setopt($curlch, CURLOPT_PROXY, $ProxyServer);
130  if (! empty($ProxyPort)) {
131  curl_setopt($curlch, CURLOPT_PROXYPORT, $ProxyPort);
132  }
133  curl_setopt($curlch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
134  }
135 
136  curl_setopt($curlch, CURLOPT_HTTPHEADER, array(
137  'Content-Type: application/json',
138  'charset=utf-8',
139  'Accept:application/json, text/javascript, */*; q=0.01'
140  ));
141 
142  //getting response from server
143  $response = curl_exec($curlch);
144 
145  if (curl_errno($curlch)) {
146  // Fatal: display curl errors
147  echo "Error " . curl_errno($curlch) . ": " . curl_error($curlch);
148  exit;
149  }
150 
151  //closing the curl
152  curl_close($curlch);
153 
154  $response = json_decode($response);
155 
156  // print any errors
157  if ($response->error) {
158  echo $response->error . "\n";
159  }
160 
161  //echo "response\n";
162  //print_r($response);
163  /* Add tag or print */
164  foreach ($response->results as $result) {
165  $row = GetRawRow($result->sha1, $ToAntelink);
166 
167  if ($PrintOnly) {
168  echo $row['ufile_name'] . "\n";
169  continue;
170  }
171 
172  /* Tag the pfile (update tag_file table) */
173  /* There is no constraint preventing duplicate tags so do a precheck */
174  $sql = "SELECT * from tag_file where pfile_fk='$row[pfile_fk]' and tag_fk='$tag_pk'";
175  $sqlresult = pg_query($PG_CONN, $sql);
176  DBCheckResult($sqlresult, $sql, __FILE__, __LINE__);
177  if (pg_num_rows($sqlresult) == 0) {
178  $sql = "insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ($tag_pk, '$row[pfile_fk]', now(), NULL)";
179  $InsResult = pg_query($PG_CONN, $sql);
180  DBCheckResult($InsResult, $sql, __FILE__, __LINE__);
181  }
182  pg_free_result($sqlresult);
183  }
184 
185  return;
186 }
187 
193 function GetRawRow($sha1, $ToAntelink)
194 {
195  /* find the sha1 in $ToAntelink and print the ufile_name */
196  foreach ($ToAntelink as $row) {
197  if (strcasecmp($row['pfile_sha1'], $sha1) == 0) {
198  return $row;
199  }
200  }
201 }
202 
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
ConfigInit($sysconfdir, &$SysConf, $exitOnDbFail=true)
Initialize the fossology system after bootstrap().
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:63
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: migratetest.php:82