18 require_once
"/usr/local/share/fossology/lib/php/bootstrap.php";
21 $acmekey = file_get_contents(
"acme.key");
23 $acmebaseurl =
'https://api.antepedia.com/acme/v3';
25 $acmeBinaryqueryurl = $acmebaseurl .
"/bquery/$acmekey";
26 $acmequeryurl = $acmebaseurl .
"/squery/$acmekey";
27 $acmekeycheckurl = $acmebaseurl .
"/checkey/$acmekey";
39 $ch = curl_init($acmekeycheckurl);
41 $contents = curl_exec( $ch );
42 $response=json_decode($contents);
44 if (! $response->authorized) {
45 echo
"Invalid antelink acme key.\n";
56 $Options = getopt(
"vpt:u:");
57 if ( array_key_exists(
't', $Options)
58 && array_key_exists(
'u', $Options)
60 $tag_pk = $Options[
't'];
61 $upload_pk = $Options[
'u'];
63 echo
"Fatal: Missing parameter\n";
68 $PrintOnly = ( array_key_exists(
'p', $Options)) ?
true :
false;
69 $Verbose = ( array_key_exists(
'v', $Options)) ?
true :
false;
72 $sql =
"SELECT pfile_pk, pfile_sha1, ufile_name, acme_pfile_pk FROM (SELECT distinct(pfile_fk) AS PF, ufile_name FROM uploadtree
73 WHERE upload_fk='$upload_pk' and (ufile_mode&x'10000000'::int)=0) as SS
74 inner join pfile on (PF=pfile_pk)
75 left join acme_pfile on (PF=acme_pfile.pfile_fk) where acme_pfile_pk is null;";
78 if (pg_num_rows($result) == 0) {
79 echo
"Empty upload_pk $upload_pk\n";
88 $MasterFOSSarray = array();
89 $ToAntelink = array();
91 $PrecheckFileCount = 0;
92 while ($row = pg_fetch_assoc($result)) {
95 if (count($ToAntelink) >= $MaxBinarySend) {
97 echo
"Precheck $PrecheckFileCount, found $FoundFOSSfiles\n";
99 $FoundFOSSfiles += QueryBinaryServer($ToAntelink, $MasterFOSSarray);
100 $ToAntelink = array();
103 pg_free_result($result);
104 if (count($ToAntelink)) {
105 $FoundFOSSfiles += QueryBinaryServer($ToAntelink, $MasterFOSSarray);
107 echo
"Precheck $PrecheckFileCount, found $FoundFOSSfiles\n";
112 $ToAntelink = array();
113 $TaggedFileCount = 0;
115 foreach ($MasterFOSSarray as $row) {
117 $ToAntelink[] = $row;
118 if (count($ToAntelink) >= $MaxSend) {
119 $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose);
120 $ToAntelink = array();
124 if (count($ToAntelink)) {
125 $TaggedFileCount += QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose);
128 echo
"$TaggedFileCount files tagged out of $TotalFileCount files.\n";
139 function QueryBinaryServer($ToAntelink, &$MasterFOSSarray)
142 global $acmeBinaryqueryurl;
147 $sha1array = array();
148 foreach ($ToAntelink as $row) {
149 $sha1array[] = $row[
'pfile_sha1'];
151 $PostData = json_encode($sha1array);
153 $curlch = curl_init($acmeBinaryqueryurl);
154 SetCurlArgs($curlch);
156 curl_setopt($curlch, CURLOPT_POST, TRUE);
157 curl_setopt($curlch,CURLOPT_POSTFIELDS, $PostData);
158 curl_setopt($curlch, CURLOPT_RETURNTRANSFER, TRUE);
161 $curlresponse = curl_exec($curlch);
163 if (curl_errno($curlch)) {
165 echo
"Error " . curl_errno($curlch) .
": " . curl_error($curlch) .
"\n";
172 $response = json_decode($curlresponse);
175 if ($response->error) {
176 echo $response->error .
"\n";
180 if (is_array($response->results)) {
181 foreach ($response->results as $result) {
182 $row = GetRawRow($result->sha1, $ToAntelink);
183 $MasterFOSSarray[] = $row;
199 function QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose)
202 global $acmequeryurl;
208 foreach ($ToAntelink as $row) {
209 $file[
'hash']=$row[
'pfile_sha1'];
210 $file[
'name']=$row[
'ufile_name'];
213 $request[
'files']=$files;
215 $PostData = json_encode($request);
217 $curlch = curl_init($acmequeryurl);
218 SetCurlArgs($curlch);
220 curl_setopt($curlch, CURLOPT_POST,
true);
221 curl_setopt($curlch, CURLOPT_POSTFIELDS, $PostData);
222 curl_setopt($curlch, CURLOPT_RETURNTRANSFER,
true);
225 $response = curl_exec($curlch);
227 if (curl_errno($curlch)) {
229 echo
"Error " . curl_errno($curlch) .
": " . curl_error($curlch) .
"\n";
237 $response = json_decode($response);
242 if ($response->error) {
243 echo $response->error .
"\n";
247 if (is_array($response->results)) {
248 foreach ($response->results as $result) {
249 $row = GetRawRow($result->sha1, $ToAntelink);
260 foreach ($result->projects as $project) {
262 $url = pg_escape_string(
$PG_CONN, $project->url);
263 $name = pg_escape_string(
$PG_CONN, $project->name);
264 $acme_project_pk =
'';
265 $sql =
"SELECT acme_project_pk from acme_project where url='$url' and project_name='$name'";
266 $sqlresult = pg_query(
$PG_CONN, $sql);
268 if (pg_num_rows($sqlresult) > 0) {
269 $projrow = pg_fetch_assoc($sqlresult);
270 $acme_project_pk = $projrow[
'acme_project_pk'];
272 pg_free_result($sqlresult);
274 if (empty($acme_project_pk)) {
276 $acme_project_pk = writeacme_project($project, $Verbose);
280 writeacme_pfile($acme_project_pk, $row[
'pfile_pk']);
284 $sql =
"SELECT * from tag_file where pfile_fk='$row[pfile_pk]' and tag_fk='$tag_pk'";
285 $sqlresult = pg_query(
$PG_CONN, $sql);
287 if (pg_num_rows($sqlresult) == 0) {
288 $sql =
"insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ($tag_pk, '$row[pfile_pk]', now(), NULL)";
289 $insresult = pg_query(
$PG_CONN, $sql);
291 pg_free_result($insresult);
294 pg_free_result($sqlresult);
307 function GetRawRow($sha1, $ToAntelink)
310 foreach ($ToAntelink as $row) {
311 if (strcasecmp($row[
'pfile_sha1'], $sha1) == 0) {
323 function SetCurlArgs($ch)
326 curl_setopt($ch,CURLOPT_USERAGENT,
'Curl-php');
327 curl_setopt($ch, CURLOPT_RETURNTRANSFER,
true);
328 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,
false);
329 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
331 CURLOPT_HTTPHEADER, array(
"Content-Type:
332 application/json; charset=utf-8",
"Accept:application/json,
333 text/javascript, */*; q=0.01"));
336 $http_proxy = $SysConf[
'FOSSOLOGY'][
'http_proxy'];
337 $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy,
":"));
338 $ProxyPort = substr(strrchr($http_proxy,
":"), 1);
339 if (! empty($ProxyServer)) {
340 curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL,
true);
341 curl_setopt($ch, CURLOPT_PROXY, $ProxyServer);
342 if (! empty($ProxyPort)) {
343 curl_setopt($ch, CURLOPT_PROXYPORT, $ProxyPort);
345 curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
354 function Usage($argc, $argv)
356 echo
"$argv[0] -v -p -u {upload_pk} -t {tag_pk}\n";
357 echo
" -p prints out raw antepedia info, but do not update the db.\n";
358 echo
" -v prints project found after inserting into db.\n";
367 function writeacme_project($project, $Verbose)
371 $project_name = pg_escape_string(
$PG_CONN, $project->name);
372 $url = pg_escape_string(
$PG_CONN, $project->url);
373 $description = pg_escape_string(
$PG_CONN, $project->description);
377 foreach ($project->licenses as $license) {
378 if (! empty($licenses)) {
381 $licenses .= pg_escape_string(
$PG_CONN, $license);
385 if (! empty($project->artefacts)) {
386 $artefact = $project->artefacts[0];
387 $projectDate = $artefact->releaseDate;
388 $version = pg_escape_string(
$PG_CONN, $artefact->version);
390 $content = $project->contents[0];
391 $projectDate = $content->releaseDate;
392 $version = pg_escape_string(
$PG_CONN, $content->revision);
398 if ($projectDate > 20000000000) {
399 $projectDate /= 1000;
401 $releasedate = date(
"Ymd", $projectDate);
404 echo
"Found project: $project_name\n";
408 $sql =
"insert into acme_project (project_name, url, description, licenses, releasedate, version)
409 values ('$project_name', '$url', '$description', '$licenses', '$releasedate', '$version')";
410 $InsResult = pg_query(
$PG_CONN, $sql);
412 pg_free_result($InsResult);
415 $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' ";
418 $row = pg_fetch_assoc($result);
419 pg_free_result($result);
420 return $row[
'acme_project_pk'];
428 function writeacme_pfile($acme_project_pk, $pfile_pk)
433 $sql =
"insert into acme_pfile (pfile_fk, acme_project_fk) values ($pfile_pk, $acme_project_pk)";
435 @$InsResult = pg_query(
$PG_CONN, $sql);
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
ConfigInit($sysconfdir, &$SysConf, $exitOnDbFail=true)
Initialize the fossology system after bootstrap().
Usage()
Print Usage statement.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
bootstrap($sysconfdir="")
Bootstrap the fossology php library.