FOSSology  4.4.0
Open Source License Compliance by Open Source Software
softwareHeritage-plugin.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Sandip Kumar Bhuyan <sandipbhuyan@gmail.com>
4  Author: Sandip Kumar Bhuyan<sandipbhyan@gmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 namespace Fossology\SoftwareHeritage\UI;
9 
14 use \Fossology\Lib\Plugin\DefaultPlugin;
16 use Symfony\Component\HttpFoundation\RedirectResponse;
17 use \Symfony\Component\HttpFoundation\Request;
21 
22 
24 {
25  const NAME = "sh-agent";
26 
27  public $AgentName = "agent_softwareHeritage";
28 
29  private $uploadtree_tablename = "";
30 
34  private $uploadDao;
35 
40  private $dbManeger;
41 
46  private $shDao;
47 
49  private $agentDao;
50 
52  protected $agentNames = array('softwareHeritage' => 'SH');
53 
54  public function __construct()
55  {
56  parent::__construct(self::NAME, array(
57  self::TITLE => _("Software Heritage details"),
58  self::PERMISSION => Auth::PERM_READ,
59  self::REQUIRES_LOGIN => false
60  ));
61  $this->Title = _("Software Heritage");
62  $this->dbManeger = $this->container->get('db.manager');
63  $this->uploadDao = $this->container->get('dao.upload');
64  $this->shDao = $this->container->get('dao.softwareHeritage');
65  $this->agentDao = $this->container->get('dao.agent');
66  }
67 
71  function RegisterMenus()
72  {
73  // For all other menus, permit coming back here.
74  $URI = $this->Name . Traceback_parm_keep(array("upload", "item", "show")) . "&flatten=yes";
75 
76  $Item = GetParm("item", PARM_INTEGER);
77  $Upload = GetParm("upload", PARM_INTEGER);
78 
79  if (empty($Item) || empty($Upload)) {
80  return;
81  }
82  $viewLicenseURI = "view-license" . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
83  $menuName = $this->Title;
84  if (GetParm("mod", PARM_STRING) == self::NAME) {
85  menu_insert("Browse::$menuName", 101);
86  menu_insert("View::$menuName", 101);
87  menu_insert("View-Meta::$menuName", 101);
88  } else {
89  $text = _("Software Heritage");
90  menu_insert("Browse::$menuName", 101, $URI, $text);
91  menu_insert("View::$menuName", 101, $viewLicenseURI, $text);
92  menu_insert("View-Meta::$menuName", 101, $viewLicenseURI, $text);
93  }
94  }
95 
96  public function handle(Request $request)
97  {
98  $upload = intval($request->get("upload"));
99  $groupId = Auth::getGroupId();
100  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
101  return $this->flushContent(_("Permission Denied"));
102  }
103  $item = intval($request->get("item"));
104  $vars['baseuri'] = Traceback_uri();
105  $vars['uploadId'] = $upload;
106  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
107  if ($request->get('show')=='quick') {
108  $item = $this->uploadDao->getFatItemId($item,$upload,$this->uploadtree_tablename);
109  }
110  $vars['itemId'] = $item;
111  $vars['micromenu'] = Dir2Browse($this->Name, $item, NULL, $showBox = 0, "Browse", -1,
112  '', '', $this->uploadtree_tablename);
113  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
114  $left = $itemTreeBounds->getLeft();
115  if (empty($left)) {
116  return $this->flushContent(_("Job unpack/adj2nest hasn't completed."));
117  }
118  $histVars = $this->showUploadHist($itemTreeBounds);
119  if (is_a($histVars, 'Symfony\\Component\\HttpFoundation\\RedirectResponse')) {
120  return $histVars;
121  }
122  $vars = array_merge($vars, $histVars);
123  return $this->render("softwareHeritage.html.twig",$this->mergeWithDefault($vars));
124  }
125 
131  private function showUploadHist(ItemTreeBounds $itemTreeBounds)
132  {
133  $groupId = Auth::getGroupId();
134  $agentId = $this->agentDao->getCurrentAgentId("softwareHeritage");
135 
136  $uploadId = $itemTreeBounds->getUploadId();
137  $scannerAgents = array_keys($this->agentNames);
138  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
139  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
140  $agentMap = $scanJobProxy->getAgentMap();
141 
142  $vars = array(
143  'agentId' => $agentId,
144  'agentMap' => $agentMap,
145  'scanners'=>$scannerVars
146  );
147 
148  $selectedAgentIds = empty($selectedAgentId) ? $scanJobProxy->getLatestSuccessfulAgentIds() : $agentId;
149 
150  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
151  $dirVars = $this->countFileListing($itemTreeBounds);
152  $childCount = $dirVars['iTotalRecords'];
153  /***************************************
154  * Problem: $ChildCount can be zero if you have a container that does not
155  * unpack to a directory. For example:
156  * file.gz extracts to archive.txt that contains a license.
157  * Same problem seen with .pdf and .Z files.
158  * Solution: if $ChildCount == 0, then just view the license!
159  *
160  * $ChildCount can also be zero if the directory is empty.
161  * **************************************/
162  if ($childCount == 0) {
163  return new RedirectResponse("?mod=view-license" . Traceback_parm_keep(array("upload", "item")));
164  }
165  return array_merge($vars, $dirVars);
166  }
167 
172  private function countFileListing(ItemTreeBounds $itemTreeBounds)
173  {
174  $isFlat = isset($_GET['flatten']);
175  $vars['isFlat'] = $isFlat;
176  $vars['iTotalRecords'] = $this->uploadDao->countNonArtifactDescendants($itemTreeBounds, $isFlat);
177  $uri = Traceback_uri().'?mod='.$this->Name.Traceback_parm_keep(array('upload','folder','show','item'));
178  $vars['fileSwitch'] = $isFlat ? $uri : $uri."&flatten=yes";
179  return $vars;
180  }
181 
187  public function renderString($templateName, $vars)
188  {
189  return $this->renderer->load($templateName)->render($vars);
190  }
191 }
192 register_plugin(new softwareHeritagePlugin());
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
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
Wrapper class for license map.
Definition: LicenseMap.php:19
render($templateName, $vars=null, $headers=null)
showUploadHist(ItemTreeBounds $itemTreeBounds)
Given an $Uploadtree_pk, display:
Dir2Browse($Mod, $UploadtreePk, $LinkLast=NULL, $ShowBox=1, $ShowMicro=NULL, $Enumerate=-1, $PreText='', $PostText='', $uploadtree_tablename="uploadtree")
Get an html linked string of a file browse path.
Definition: common-dir.php:263
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
const PARM_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
Traceback_parm_keep($List)
Create a new URI, keeping only these items.