29 require_once
"/usr/local/share/fossology/lib/php/bootstrap.php";
32 $acmekey = file_get_contents(
"acme.key");
34 $acmebaseurl =
'https://api.antepedia.com/acme/v3';
36 $acmeBinaryqueryurl = $acmebaseurl .
"/bquery/$acmekey";
37 $acmequeryurl = $acmebaseurl .
"/squery/$acmekey";
38 $acmekeycheckurl = $acmebaseurl .
"/checkey/$acmekey";
50 $ch = curl_init($acmekeycheckurl);
52 $contents = curl_exec( $ch );
53 $response=json_decode($contents);
55 if (! $response->authorized) {
56 echo
"Invalid antelink acme key.\n";
67 $Options = getopt(
"vpt:u:");
68 if ( array_key_exists(
't', $Options)
69 && array_key_exists(
'u', $Options)
71 $tag_pk = $Options[
't'];
72 $upload_pk = $Options[
'u'];
74 echo
"Fatal: Missing parameter\n";
79 $PrintOnly = ( array_key_exists(
'p', $Options)) ?
true :
false;
80 $Verbose = ( array_key_exists(
'v', $Options)) ?
true :
false;
83 $sql =
"SELECT pfile_pk, pfile_sha1, ufile_name, acme_pfile_pk FROM (SELECT distinct(pfile_fk) AS PF, ufile_name FROM uploadtree 84 WHERE upload_fk='$upload_pk' and (ufile_mode&x'10000000'::int)=0) as SS 85 inner join pfile on (PF=pfile_pk) 86 left join acme_pfile on (PF=acme_pfile.pfile_fk) where acme_pfile_pk is null;";
89 if (pg_num_rows($result) == 0) {
90 echo
"Empty upload_pk $upload_pk\n";
99 $MasterFOSSarray = array();
100 $ToAntelink = array();
102 $PrecheckFileCount = 0;
103 while ($row = pg_fetch_assoc($result)) {
104 $PrecheckFileCount++;
105 $ToAntelink[] = $row;
106 if (count($ToAntelink) >= $MaxBinarySend) {
108 echo
"Precheck $PrecheckFileCount, found $FoundFOSSfiles\n";
110 $FoundFOSSfiles += QueryBinaryServer($ToAntelink, $MasterFOSSarray);
111 $ToAntelink = array();
114 pg_free_result($result);
115 if (count($ToAntelink)) {
116 $FoundFOSSfiles += QueryBinaryServer($ToAntelink, $MasterFOSSarray);
118 echo
"Precheck $PrecheckFileCount, found $FoundFOSSfiles\n";
123 $ToAntelink = array();
124 $TaggedFileCount = 0;
126 foreach ($MasterFOSSarray as $row) {
128 $ToAntelink[] = $row;
129 if (count($ToAntelink) >= $MaxSend) {
130 $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose);
131 $ToAntelink = array();
135 if (count($ToAntelink)) {
136 $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose);
139 echo
"$TaggedFileCount files tagged out of $TotalFileCount files.\n";
150 function QueryBinaryServer($ToAntelink, &$MasterFOSSarray)
153 global $acmeBinaryqueryurl;
158 $sha1array = array();
159 foreach ($ToAntelink as $row) {
160 $sha1array[] = $row[
'pfile_sha1'];
162 $PostData = json_encode($sha1array);
164 $curlch = curl_init($acmeBinaryqueryurl);
165 SetCurlArgs($curlch);
167 curl_setopt($curlch, CURLOPT_POST, TRUE);
168 curl_setopt($curlch,CURLOPT_POSTFIELDS, $PostData);
169 curl_setopt($curlch, CURLOPT_RETURNTRANSFER, TRUE);
172 $curlresponse = curl_exec($curlch);
174 if (curl_errno($curlch)) {
176 echo
"Error " . curl_errno($curlch) .
": " . curl_error($curlch) .
"\n";
183 $response = json_decode($curlresponse);
186 if ($response->error) {
187 echo $response->error .
"\n";
191 if (is_array($response->results)) {
192 foreach ($response->results as $result) {
193 $row = GetRawRow($result->sha1, $ToAntelink);
194 $MasterFOSSarray[] = $row;
210 function QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose)
213 global $acmequeryurl;
219 foreach ($ToAntelink as $row) {
220 $file[
'hash']=$row[
'pfile_sha1'];
221 $file[
'name']=$row[
'ufile_name'];
224 $request[
'files']=$files;
226 $PostData = json_encode($request);
228 $curlch = curl_init($acmequeryurl);
229 SetCurlArgs($curlch);
231 curl_setopt($curlch, CURLOPT_POST,
true);
232 curl_setopt($curlch, CURLOPT_POSTFIELDS, $PostData);
233 curl_setopt($curlch, CURLOPT_RETURNTRANSFER,
true);
236 $response = curl_exec($curlch);
238 if (curl_errno($curlch)) {
240 echo
"Error " . curl_errno($curlch) .
": " . curl_error($curlch) .
"\n";
248 $response = json_decode($response);
253 if ($response->error) {
254 echo $response->error .
"\n";
258 if (is_array($response->results)) {
259 foreach ($response->results as $result) {
260 $row = GetRawRow($result->sha1, $ToAntelink);
271 foreach ($result->projects as $project) {
273 $url = pg_escape_string($PG_CONN, $project->url);
274 $name = pg_escape_string($PG_CONN, $project->name);
275 $acme_project_pk =
'';
276 $sql =
"SELECT acme_project_pk from acme_project where url='$url' and project_name='$name'";
277 $sqlresult = pg_query($PG_CONN, $sql);
279 if (pg_num_rows($sqlresult) > 0) {
280 $projrow = pg_fetch_assoc($sqlresult);
281 $acme_project_pk = $projrow[
'acme_project_pk'];
283 pg_free_result($sqlresult);
285 if (empty($acme_project_pk)) {
287 $acme_project_pk = writeacme_project($project, $Verbose);
291 writeacme_pfile($acme_project_pk, $row[
'pfile_pk']);
295 $sql =
"SELECT * from tag_file where pfile_fk='$row[pfile_pk]' and tag_fk='$tag_pk'";
296 $sqlresult = pg_query($PG_CONN, $sql);
298 if (pg_num_rows($sqlresult) == 0) {
299 $sql =
"insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ($tag_pk, '$row[pfile_pk]', now(), NULL)";
300 $insresult = pg_query($PG_CONN, $sql);
302 pg_free_result($insresult);
305 pg_free_result($sqlresult);
318 function GetRawRow($sha1, $ToAntelink)
321 foreach ($ToAntelink as $row) {
322 if (strcasecmp($row[
'pfile_sha1'], $sha1) == 0) {
334 function SetCurlArgs($ch)
337 curl_setopt($ch,CURLOPT_USERAGENT,
'Curl-php');
338 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
339 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
340 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
342 CURLOPT_HTTPHEADER, array(
"Content-Type: 343 application/json; charset=utf-8",
"Accept:application/json, 344 text/javascript, */*; q=0.01"));
347 $http_proxy = $SysConf[
'FOSSOLOGY'][
'http_proxy'];
348 $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy,
":"));
349 $ProxyPort = substr(strrchr($http_proxy,
":"), 1);
350 if (! empty($ProxyServer)) {
351 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL,
true);
352 curl_setopt($ch, CURLOPT_PROXY, $ProxyServer);
353 if (! empty($ProxyPort)) {
354 curl_setopt($ch, CURLOPT_PROXYPORT, $ProxyPort);
356 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
365 function Usage($argc, $argv)
367 echo
"$argv[0] -v -p -u {upload_pk} -t {tag_pk}\n";
368 echo
" -p prints out raw antepedia info, but do not update the db.\n";
369 echo
" -v prints project found after inserting into db.\n";
378 function writeacme_project($project, $Verbose)
382 $project_name = pg_escape_string($PG_CONN, $project->name);
383 $url = pg_escape_string($PG_CONN, $project->url);
384 $description = pg_escape_string($PG_CONN, $project->description);
388 foreach ($project->licenses as $license) {
389 if (! empty($licenses)) {
392 $licenses .= pg_escape_string($PG_CONN, $license);
396 if (! empty($project->artefacts)) {
397 $artefact = $project->artefacts[0];
398 $projectDate = $artefact->releaseDate;
399 $version = pg_escape_string($PG_CONN, $artefact->version);
401 $content = $project->contents[0];
402 $projectDate = $content->releaseDate;
403 $version = pg_escape_string($PG_CONN, $content->revision);
409 if ($projectDate > 20000000000) {
410 $projectDate = $projectDate / 1000;
412 $releasedate = date(
"Ymd", $projectDate);
415 echo
"Found project: $project_name\n";
419 $sql =
"insert into acme_project (project_name, url, description, licenses, releasedate, version) 420 values ('$project_name', '$url', '$description', '$licenses', '$releasedate', '$version')";
421 $InsResult = pg_query($PG_CONN, $sql);
423 pg_free_result($InsResult);
426 $sql =
"select acme_project_pk from acme_project where project_name='$project_name' and url='$url' and description='$description' and licenses='$licenses' and releasedate='$releasedate' and version='$version' ";
427 $result = pg_query($PG_CONN, $sql);
429 $row = pg_fetch_assoc($result);
430 pg_free_result($result);
431 return $row[
'acme_project_pk'];
439 function writeacme_pfile($acme_project_pk, $pfile_pk)
444 $sql =
"insert into acme_pfile (pfile_fk, acme_project_fk) values ($pfile_pk, $acme_project_pk)";
446 @$InsResult = pg_query($PG_CONN, $sql);
Usage()
Print Usage statement.
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.
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
ConfigInit($sysconfdir, &$SysConf)
Initialize the fossology system after bootstrap().