FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo_tagfoss.php
1 #!/usr/bin/php
2 <?php
3 /***********************************************************
4  Copyright (C) 2012 Hewlett-Packard Development Company, L.P.
5 
6  This program is free software; you can redistribute it and/or
7  modify it under the terms of the GNU General Public License
8  version 2 as published by the Free Software Foundation.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License along
16  with this program; if not, write to the Free Software Foundation, Inc.,
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
27 // $DATAROOTDIR and $PROJECT come from Makefile
28 //require_once "$DATAROOTDIR/$PROJECT/lib/php/bootstrap.php";
29 require_once "/usr/local/share/fossology/lib/php/bootstrap.php";
30 
31 $SysConf = array(); // fo system configuration variables
32 $PG_CONN = 0; // Database connection
33 
34 /* Set SYSCONFDIR and set global (for backward compatibility) */
35 $SysConf = bootstrap();
36 
37 /* Initialize global system configuration variables $SysConfig[] */
38 ConfigInit($SYSCONFDIR, $SysConf);
39 
40 // Maximum number of sha1's to send to antelink in a single batch
41 $MaxSend = 500;
42 
43 /* -p -u {upload_pk} -t {tag_pk}
44  * -u and -t are manditory
45  */
46 $Options = getopt("pt:u:");
47 if ( array_key_exists('t', $Options)
48  && array_key_exists('u', $Options)
49  ) {
50  $Missing = array_key_exists('m', $Options) ? true : false;
51  $tag_pk = $Options['t'];
52  $upload_pk = $Options['u'];
53 } else {
54  echo "Fatal: Missing parameter\n";
55  Usage($argc, $argv);
56  exit -1;
57 }
58 
59 if ( array_key_exists('p', $Options)) {
60  $PrintOnly = true;
61 } else {
62  $PrintOnly = false;
63 }
64 
65 $sql = "select distinct(pfile_fk), pfile_sha1, ufile_name from uploadtree,pfile where upload_fk='$upload_pk' and pfile_pk=pfile_fk";
66 $result = pg_query($PG_CONN, $sql);
67 DBCheckResult($result, $sql, __FILE__, __LINE__);
68 if (pg_num_rows($result) == 0) {
69  echo "Empty upload_pk $upload_pk\n";
70  exit;
71 }
72 
73 /* loop through each row accumulating groups of $MaxSend files (sha1's) to send to antelink */
74 $ToAntelink = array();
75 $TaggedFileCount = 0;
76 $TotalFileCount = 0;
77 while ($row = pg_fetch_assoc($result)) {
78  $TotalFileCount ++;
79  $ToAntelink[] = $row;
80  if (count($ToAntelink) >= $MaxSend) {
81  $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly);
82  $ToAntelink = array();
83  }
84 }
85 exit;
86 if (count($ToAntelink)) {
87  $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly);
88 }
89 
90 echo "$TaggedFileCount files tagged out of $TotalFileCount files.\n";
91 
92 return (0);
93 
94 /**************************** functions ************************************/
95 function Usage($argc, $argv)
96 {
97  echo "$argv[0] -p -u {upload_pk} -t {tag_pk}\n";
98  echo " -p means to only print out filenames to be tagged, but do not update the db.\n";
99 }
100 
101 
109 function QueryTag($ToAntelink, $tag_pk, $PrintOnly)
110 {
111  global $PG_CONN;
112  global $SysConf;
113  $AntepediaServer = "https://api.antepedia.com/acme/v3/bquery/";
114 
115  /* parse http_proxy server and port */
116  $http_proxy = $SysConf['FOSSOLOGY']['http_proxy'];
117  $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy, ":"));
118  $ProxyPort = substr(strrchr($http_proxy, ":"), 1);
119 
120  /* construct array of just sha1's */
121  $sha1array = array();
122  foreach ($ToAntelink as $row) {
123  $sha1array[] = $row['pfile_sha1'];
124  }
125  $PostData = json_encode($sha1array);
126 
127  $curlch = curl_init($AntepediaServer);
128  //turning off the server and peer verification(TrustManager Concept).
129  curl_setopt($curlch, CURLOPT_SSL_VERIFYPEER, FALSE);
130  curl_setopt($curlch, CURLOPT_SSL_VERIFYHOST, 2);
131 
132  curl_setopt($curlch, CURLOPT_POST, TRUE);
133  curl_setopt($curlch,CURLOPT_POSTFIELDS, $PostData);
134  curl_setopt($curlch, CURLOPT_RETURNTRANSFER, TRUE);
135  curl_setopt($curlch,CURLOPT_USERAGENT,'Curl-php');
136 
137  if (! empty($ProxyServer)) {
138  curl_setopt($curlch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
139  curl_setopt($curlch, CURLOPT_PROXY, $ProxyServer);
140  if (! empty($ProxyPort)) {
141  curl_setopt($curlch, CURLOPT_PROXYPORT, $ProxyPort);
142  }
143  curl_setopt($curlch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
144  }
145 
146  curl_setopt($curlch, CURLOPT_HTTPHEADER, array(
147  'Content-Type: application/json',
148  'charset=utf-8',
149  'Accept:application/json, text/javascript, */*; q=0.01'
150  ));
151 
152  //getting response from server
153  $response = curl_exec($curlch);
154 
155  if (curl_errno($curlch)) {
156  // Fatal: display curl errors
157  echo "Error " . curl_errno($curlch) . ": " . curl_error($curlch);
158  exit;
159  }
160 
161  //closing the curl
162  curl_close($curlch);
163 
164  $response = json_decode($response);
165 
166  // print any errors
167  if ($response->error) {
168  echo $response->error . "\n";
169  }
170 
171  //echo "response\n";
172  //print_r($response);
173  /* Add tag or print */
174  foreach ($response->results as $result) {
175  $row = GetRawRow($result->sha1, $ToAntelink);
176 
177  if ($PrintOnly) {
178  echo $row['ufile_name'] . "\n";
179  continue;
180  }
181 
182  /* Tag the pfile (update tag_file table) */
183  /* There is no constraint preventing duplicate tags so do a precheck */
184  $sql = "SELECT * from tag_file where pfile_fk='$row[pfile_fk]' and tag_fk='$tag_pk'";
185  $sqlresult = pg_query($PG_CONN, $sql);
186  DBCheckResult($sqlresult, $sql, __FILE__, __LINE__);
187  if (pg_num_rows($sqlresult) == 0) {
188  $sql = "insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ($tag_pk, '$row[pfile_fk]', now(), NULL)";
189  $InsResult = pg_query($PG_CONN, $sql);
190  DBCheckResult($InsResult, $sql, __FILE__, __LINE__);
191  }
192  pg_free_result($sqlresult);
193  }
194 
195  return;
196 }
197 
203 function GetRawRow($sha1, $ToAntelink)
204 {
205  /* find the sha1 in $ToAntelink and print the ufile_name */
206  foreach ($ToAntelink as $row) {
207  if (strcasecmp($row['pfile_sha1'], $sha1) == 0) {
208  return $row;
209  }
210  }
211 }
212 
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:75
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
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: migratetest.php:93
ConfigInit($sysconfdir, &$SysConf)
Initialize the fossology system after bootstrap().