FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SpashtAgent.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Vivek Kumar <vvksindia@gmail.com>
4  Author: Vivek Kumar <vvksindia@gmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 namespace Fossology\Spasht;
9 
16 use GuzzleHttp\Client;
17 
18 include_once (__DIR__ . "/version.php");
19 
26 class SpashtAgent extends Agent
27 {
28 
33  private $uploadDao;
34 
39  private $spashtDao;
40 
45  private $licenseDao;
46 
47  function __construct()
48  {
49  parent::__construct(SPASHT_AGENT_NAME, AGENT_VERSION, AGENT_REV);
50  $this->uploadDao = $this->container->get('dao.upload');
51  $this->spashtDao = $this->container->get('dao.spasht');
52  $this->licenseDao = $this->container->get('dao.license');
53  }
54 
60  function processUploadId($uploadId)
61  {
62  $itemTreeBounds = $this->uploadDao->getParentItemBounds($uploadId);
63  $pfileFileDetails = $this->uploadDao->getPFileDataPerFileName(
64  $itemTreeBounds);
65 
66  $agentId = $this->agentDao->getCurrentAgentId("spasht");
67  $pfileSha1AndpfileId = array();
68 
69  foreach ($pfileFileDetails as $pfileDetail) {
70  $pfileSha1AndpfileId[$pfileDetail['pfile_pk']] = strtolower(
71  $pfileDetail['sha1']);
72  }
73 
74  $uploadAvailable = $this->searchUploadIdInSpasht($uploadId);
75  if ($uploadAvailable === false) {
76  // Nothing to perform
77  return true;
78  }
79 
80  $getNewResult = $this->getInformation($uploadAvailable,
81  $pfileSha1AndpfileId);
82  if (is_string($getNewResult)) {
83  echo "Error: $getNewResult";
84  return false;
85  }
86 
87  $this->insertLicensesSpashtAgentRecord($getNewResult, $agentId);
88  $this->insertCopyrightSpashtAgentRecord($getNewResult, $agentId);
89  return true;
90  }
91 
99  protected function searchUploadIdInSpasht($uploadId)
100  {
101  $result = $this->spashtDao->getComponent($uploadId);
102 
103  if (! empty($result)) {
104  return $result;
105  }
106 
107  return false;
108  }
109 
118  protected function getInformation($details, $pfileSha1AndpfileId)
119  {
120  global $SysConf;
121 
122  $dir = "files";
123 
127  $client = new Client([
128  // Base URI is used with relative requests
129  'base_uri' => $SysConf['SYSCONFIG']["ClearlyDefinedURL"]
130  ]);
131 
132  // uri to definitions section in the api to get scancode details
133  $uri = 'definitions/' . $details->generateUrlString();
134  // Prepare proxy
135  $proxy = [];
136  if (array_key_exists('http_proxy', $SysConf['FOSSOLOGY']) &&
137  ! empty($SysConf['FOSSOLOGY']['http_proxy'])) {
138  $proxy['http'] = $SysConf['FOSSOLOGY']['http_proxy'];
139  }
140  if (array_key_exists('https_proxy', $SysConf['FOSSOLOGY']) &&
141  ! empty($SysConf['FOSSOLOGY']['https_proxy'])) {
142  $proxy['https'] = $SysConf['FOSSOLOGY']['https_proxy'];
143  }
144  if (array_key_exists('no_proxy', $SysConf['FOSSOLOGY']) &&
145  ! empty($SysConf['FOSSOLOGY']['no_proxy'])) {
146  $proxy['no'] = explode(',', $SysConf['FOSSOLOGY']['no_proxy']);
147  }
148 
149  try {
150  $res = $client->request('GET', $uri, ["proxy" => $proxy]);
151  } catch (\Exception $e) {
152  return "Unable to fetch info from $uri. " . $e->getMessage();
153  }
154 
155  if ($res->getStatusCode() == 200) {
156  $body = json_decode($res->getBody()->getContents());
157 
158  if (empty($body)) {
159  return "BodyNotFound";
160  }
161 
162  $newResultBody = array();
163 
164  foreach ($body->$dir as $key) {
165  $searchInUpload = array_search($key->hashes->sha1, $pfileSha1AndpfileId);
166 
167  if (! empty($searchInUpload)) {
168  $temp = array();
169  $temp['pfileId'] = $searchInUpload;
170  $temp['sha1'] = $key->hashes->sha1;
171 
172  if (! empty($key->license)) {
173  $temp['license'] = $this->separateLicenses($key->license);
174  } else {
175  $temp['license'] = [
176  "No_License_Found"
177  ];
178  }
179 
180  if (! empty($key->attributions)) {
181  $temp['attributions'] = $key->attributions;
182  } else {
183  $temp['attributions'] = [
184  "No_Copyright_Found"
185  ];
186  }
187  $newResultBody[] = $temp;
188  }
189  }
190 
191  return $newResultBody;
192  }
193  return "UploadNotFound";
194  }
195 
202  private function separateLicenses($rawLicenses)
203  {
204  $strLicense = array();
205  $checkString = explode(" ", $rawLicenses);
206 
207  foreach ($checkString as $license) {
208  if (strcasecmp($license, "and") === 0 || strcasecmp($license, "or") === 0) {
209  continue;
210  }
211  $strSubLicense = explode("-", $license);
212  $partCount = count($strSubLicense);
213  if ($partCount < 2) {
214  $strLicense[] = $license;
215  continue;
216  }
217 
218  $fossLicense = $license;
219  if ($partCount >= 3 &&
220  strcasecmp($strSubLicense[$partCount - 2], "or") === 0 &&
221  strcasecmp($strSubLicense[$partCount - 1], "later") === 0) {
222  // <license>-or-later
223  $fossLicense = implode("-", array_slice($strSubLicense, 0, -2)) . "+";
224  } elseif (strcasecmp($strSubLicense[$partCount - 1], "only") === 0) {
225  // <license>-only
226  $fossLicense = implode("-", array_slice($strSubLicense, 0, -1));
227  }
228 
229  $strLicense[] = $fossLicense;
230  }
231  return $strLicense;
232  }
233 
239  protected function insertLicensesSpashtAgentRecord($body, $agentId)
240  {
241  foreach ($body as $key) {
242  foreach ($key['license'] as $license) {
243  $l = $this->licenseDao->getLicenseByShortName($license);
244 
245  if ($l != null && ! empty($l->getId())) {
246  $sql = "SELECT fl_pk FROM license_file " .
247  "WHERE agent_fk = $1 AND pfile_fk = $2 AND rf_fk = $3;";
248  $statement = __METHOD__ . ".checkExists";
249  $row = $this->dbManager->getSingleRow($sql, [$agentId,
250  $key['pfileId'], $l->getId()], $statement);
251  if (! empty($row) && ! empty($row['fl_pk'])) {
252  continue;
253  }
254  $this->dbManager->insertTableRow('license_file', [
255  'agent_fk' => $agentId,
256  'pfile_fk' => $key['pfileId'],
257  'rf_fk' => $l->getId()
258  ], __METHOD__ . ".insertLicense");
259  $this->heartbeat(1);
260  }
261  }
262  }
263  }
264 
270  protected function insertCopyrightSpashtAgentRecord($body, $agentId)
271  {
272  foreach ($body as $key) {
273  foreach ($key['attributions'] as $keyCopyright) {
274  if ($keyCopyright == "No_Copyright_Found") {
275  continue;
276  }
277 
278  $hashForCopyright = hash("sha256", $keyCopyright);
279  $sql = "SELECT copyright_spasht_pk FROM copyright_spasht " .
280  "WHERE agent_fk = $1 AND pfile_fk = $2 AND hash = $3 " .
281  "AND clearing_decision_type_fk = 0;";
282  $statement = __METHOD__ . ".checkExists";
283  $row = $this->dbManager->getSingleRow($sql, [$agentId, $key['pfileId'],
284  $hashForCopyright
285  ], $statement);
286  if (! empty($row) && ! empty($row['copyright_spasht_pk'])) {
287  continue;
288  }
289  $this->dbManager->insertTableRow('copyright_spasht', [
290  'agent_fk' => $agentId,
291  'pfile_fk' => $key['pfileId'],
292  'textfinding' => $keyCopyright,
293  'hash' => $hashForCopyright,
294  'clearing_decision_type_fk' => 0
295  ], __METHOD__ . ".insertCopyright");
296  $this->heartbeat(1);
297  }
298  }
299  }
300 }
Structure of an Agent with all required parameters.
Definition: Agent.php:41
heartbeat($newProcessed)
Send hear beat to the scheduler.
Definition: Agent.php:203
insertLicensesSpashtAgentRecord($body, $agentId)
Insert the License Details in Spasht Agent table.
insertCopyrightSpashtAgentRecord($body, $agentId)
Insert the Copyright Details in Spasht Agent table.
processUploadId($uploadId)
Run Spasht Agent for a package.
Definition: SpashtAgent.php:60
getInformation($details, $pfileSha1AndpfileId)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16