/** * @brief Query the Antelink public server and tag the results. * @param $ToAntelink array of pfile_fk, pfile_sha1, ufile_name records * @param $tag_pk * @param $PrintOnly print the raw antelink data, do not update the db. Used for debugging. * @parma $Verbose print project name. * @return number of tagged files. **/ function QueryTag($ToAntelink, $tag_pk, $PrintOnly, $Verbose) { global $PG_CONN; global $acmequeryurl; $numTagged = 0; /* construct array of arrays of name and sha1's */ $files = array(); foreach ($ToAntelink as $row) { $file['hash'] = $row['pfile_sha1']; $file['name'] = $row['ufile_name']; $files[] = $file; } $request['files'] = $files; $PostData = json_encode($request); $curlch = curl_init($acmequeryurl); SetCurlArgs($curlch); curl_setopt($curlch, CURLOPT_POST, TRUE); curl_setopt($curlch, CURLOPT_POSTFIELDS, $PostData); curl_setopt($curlch, CURLOPT_RETURNTRANSFER, TRUE); //getting response from server $response = curl_exec($curlch); if (curl_errno($curlch)) { // Fatal: display curl errors echo "Error " . curl_errno($curlch) . ": " . curl_error($curlch) . "\n"; return 0; // exit; } //closing the curl curl_close($curlch); $response = json_decode($response); //echo "response\n"; //print_r($response); // print any errors if ($response->error) { echo $response->error . "\n"; } /* Add tag or print */ if (is_array($response->results)) { foreach ($response->results as $result) { $row = GetRawRow($result->sha1, $ToAntelink); if ($PrintOnly) { if (!empty($row)) { print_r($row); } // echo $row['ufile_name'] . "\n"; print_r($result); continue; } foreach ($result->projects as $project) { /* check if acme_project already exists (check if the url is unique) */ $url = pg_escape_string($PG_CONN, $project->url); $name = pg_escape_string($PG_CONN, $project->name); $acme_project_pk = ''; $sql = "SELECT acme_project_pk from acme_project where url='{$url}' and project_name='{$name}'"; $sqlresult = pg_query($PG_CONN, $sql); DBCheckResult($sqlresult, $sql, __FILE__, __LINE__); if (pg_num_rows($sqlresult) > 0) { $projrow = pg_fetch_assoc($sqlresult); $acme_project_pk = $projrow['acme_project_pk']; } pg_free_result($sqlresult); if (empty($acme_project_pk)) { /* this is a new acme_project, so write the acme_project record */ $acme_project_pk = writeacme_project($project, $Verbose); } /* write the acme_pfile record */ writeacme_pfile($acme_project_pk, $row['pfile_pk']); /* Tag the pfile (update tag_file table) */ /* There is no constraint preventing duplicate tags so do a precheck */ $sql = "SELECT * from tag_file where pfile_fk='{$row['pfile_pk']}' and tag_fk='{$tag_pk}'"; $sqlresult = pg_query($PG_CONN, $sql); DBCheckResult($sqlresult, $sql, __FILE__, __LINE__); if (pg_num_rows($sqlresult) == 0) { $sql = "insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ({$tag_pk}, '{$row['pfile_pk']}', now(), NULL)"; $insresult = pg_query($PG_CONN, $sql); DBCheckResult($insresult, $sql, __FILE__, __LINE__); pg_free_result($insresult); $numTagged++; } pg_free_result($sqlresult); } } } return $numTagged; }
/** * @brief Query the Antelink public server and tag the results. * @param $ToAntelink array of pfile_fk, pfile_sha1, ufile_name records * @param $tag_pk * @param $PrintOnly print the ufile_name, do not update the db. Used for debugging. * @return number of tagged files. **/ function QueryTag($ToAntelink, $tag_pk, $PrintOnly) { global $PG_CONN; global $SysConf; $AntepediaServer = "https://api.antepedia.com/acme/v3/bquery/"; /* parse http_proxy server and port */ $http_proxy = $SysConf['FOSSOLOGY']['http_proxy']; $ProxyServer = substr($http_proxy, 0, strrpos($http_proxy, ":")); $ProxyPort = substr(strrchr($http_proxy, ":"), 1); /* construct array of just sha1's */ $sha1array = array(); foreach ($ToAntelink as $row) { $sha1array[] = $row['pfile_sha1']; } $PostData = json_encode($sha1array); $curlch = curl_init($AntepediaServer); //turning off the server and peer verification(TrustManager Concept). curl_setopt($curlch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curlch, CURLOPT_SSL_VERIFYHOST, 2); curl_setopt($curlch, CURLOPT_POST, TRUE); curl_setopt($curlch, CURLOPT_POSTFIELDS, $PostData); curl_setopt($curlch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curlch, CURLOPT_USERAGENT, 'Curl-php'); if (!empty($ProxyServer)) { curl_setopt($curlch, CURLOPT_HTTPPROXYTUNNEL, TRUE); curl_setopt($curlch, CURLOPT_PROXY, $ProxyServer); if (!empty($ProxyPort)) { curl_setopt($curlch, CURLOPT_PROXYPORT, $ProxyPort); } curl_setopt($curlch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP); } curl_setopt($curlch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'charset=utf-8', 'Accept:application/json, text/javascript, */*; q=0.01')); //getting response from server $response = curl_exec($curlch); if (curl_errno($curlch)) { // Fatal: display curl errors echo "Error " . curl_errno($curlch) . ": " . curl_error($curlch); exit; } //closing the curl curl_close($curlch); $response = json_decode($response); // print any errors if ($response->error) { echo $response->error . "\n"; } //echo "response\n"; //print_r($response); /* Add tag or print */ foreach ($response->results as $result) { $row = GetRawRow($result->sha1, $ToAntelink); if ($PrintOnly) { echo $row['ufile_name'] . "\n"; continue; } /* Tag the pfile (update tag_file table) */ /* There is no constraint preventing duplicate tags so do a precheck */ $sql = "SELECT * from tag_file where pfile_fk='{$row['pfile_fk']}' and tag_fk='{$tag_pk}'"; $sqlresult = pg_query($PG_CONN, $sql); DBCheckResult($sqlresult, $sql, __FILE__, __LINE__); if (pg_num_rows($sqlresult) == 0) { $sql = "insert into tag_file (tag_fk, pfile_fk, tag_file_date, tag_file_text) values ({$tag_pk}, '{$row['pfile_fk']}', now(), NULL)"; $InsResult = pg_query($PG_CONN, $sql); DBCheckResult($InsResult, $sql, __FILE__, __LINE__); } pg_free_result($sqlresult); } return; }