FOSSology  4.7.1
Open Source License Compliance by Open Source Software
DetectLicensesFolder.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Rohit Pandey <rohit.pandey4900@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
14 
20 {
22  private $uploadDao;
23 
25  private $searchHelperDao;
26 
28  private $clearingDao;
29 
30  function __construct()
31  {
32  $this->uploadDao = $GLOBALS['container']->get('dao.upload');
33  $this->searchHelperDao = $GLOBALS['container']->get('dao.searchhelperdao');
34  $this->clearingDao = $GLOBALS['container']->get('dao.clearing');
35  }
36 
42  function getDeclearedLicenses($uploadId)
43  {
44  $uploadTreeId = $this->uploadDao->getUploadParent($uploadId);
45 
46  // Search the upload tree for LICENSES directory
47  $Item = $uploadTreeId;
48  $Filename = "LICENSES";
49  $tag = "";
50  $Page = 0;
51  $Limit = 100;
52  $SizeMin = "";
53  $SizeMax = "";
54  $searchtype = "containers";
55  $License = "";
56  $Copyright = "";
57 
58  list($result, $count) = $this->searchHelperDao->GetResults($Item, $Filename, $uploadId, $tag, $Page, $Limit, $SizeMin, $SizeMax, $searchtype, $License, $Copyright, $this->uploadDao, Auth::getGroupId());
59 
60  if ($count==0) {
61  return array();
62  }
63  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($result[0]['uploadtree_pk']);
64  $condition = "(ut.lft BETWEEN $1 AND $2)";
65  $params = array($itemTreeBounds->getLeft(), $itemTreeBounds->getRight());
66  $licenseId = array();
67  $containedItems = $this->uploadDao->getContainedItems($itemTreeBounds, $condition, $params);
68  foreach ($containedItems as $item) {
69  $licenseId[] = $this->cleanLicenseId($item->getFileName());
70  }
71 
72  return $licenseId;
73  }
74 
80  function getLicenseFileDeclaredLicenses($uploadId)
81  {
82  $uploadTreeId = $this->uploadDao->getUploadParent($uploadId);
83  $uploadTreeTableName = GetUploadtreeTableName($uploadId);
84  $rootBounds = $this->uploadDao->getItemTreeBounds($uploadTreeId, $uploadTreeTableName);
85 
86  $condition = "(ut.ufile_name ilike 'LICENSE' OR ut.ufile_name ilike 'LICENSE.txt' OR ut.ufile_name ilike 'LICENSE.md' OR ut.ufile_name ilike 'LICENSE.rst')";
87  $items = $this->uploadDao->getContainedItems($rootBounds, $condition);
88 
89  if (empty($items)) {
90  return array();
91  }
92 
93  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($items[0]->getId(), $uploadTreeTableName);
94 
95  $clearedLicenses = $this->clearingDao->getClearedLicenses($itemTreeBounds, Auth::getGroupId());
96  $licenseIds = array();
97  foreach ($clearedLicenses as $licenseRef) {
98  $licenseIds[] = $licenseRef->getShortName();
99  }
100 
101  return array_unique($licenseIds);
102  }
103 
109  public function cleanLicenseId($licenseFileName)
110  {
111  return preg_replace("/(.*)\\.txt$/i", "$1", $licenseFileName);
112  }
113 }
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
Detects licenses declard inside LICENSES directory.
getLicenseFileDeclaredLicenses($uploadId)
Get licenses declared in root LICENSE file.
getDeclearedLicenses($uploadId)
Get licenses decleared in LICENSES folder.
cleanLicenseId($licenseFileName)
Truncate .txt from licenseFileName.
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414
Contains business rules for FOSSology.