FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
softwareHeritage-plugin.php
1 <?php
2 /***********************************************************
3 Copyright (C) 2019
4 Author: Sandip Kumar Bhuyan<sandipbhyan@gmail.com>
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
20 
25 use \Fossology\Lib\Plugin\DefaultPlugin;
28 use \Symfony\Component\HttpFoundation\Request;
32 
33 
35 {
36  const NAME = "sh-agent";
37 
38  public $AgentName = "agent_softwareHeritage";
39 
40  private $uploadtree_tablename = "";
41 
45  private $uploadDao;
46 
51  private $dbManeger;
52 
57  private $shDao;
58 
60  private $agentDao;
61 
63  protected $agentNames = array('softwareHeritage' => 'SH');
64 
65  public function __construct()
66  {
67  parent::__construct(self::NAME, array(
68  self::TITLE => _("Software Heritage details"),
69  self::PERMISSION => Auth::PERM_READ,
70  self::REQUIRES_LOGIN => false
71  ));
72  $this->Title = _("Software Heritage");
73  $this->dbManeger = $this->container->get('db.manager');
74  $this->uploadDao = $this->container->get('dao.upload');
75  $this->shDao = $this->container->get('dao.softwareHeritage');
76  $this->agentDao = $this->container->get('dao.agent');
77  }
78 
82  function RegisterMenus()
83  {
84  // For all other menus, permit coming back here.
85  $URI = $this->Name . Traceback_parm_keep(array("upload", "item", "show")) . "&flatten=yes";
86 
87  $Item = GetParm("item", PARM_INTEGER);
88  $Upload = GetParm("upload", PARM_INTEGER);
89 
90  if (empty($Item) || empty($Upload)) {
91  return;
92  }
93  $viewLicenseURI = "view-license" . Traceback_parm_keep(array("show", "format", "page", "upload", "item"));
94  $menuName = $this->Title;
95  if (GetParm("mod", PARM_STRING) == self::NAME) {
96  menu_insert("Browse::$menuName", 101);
97  menu_insert("View::$menuName", 101);
98  menu_insert("View-Meta::$menuName", 101);
99  } else {
100  $text = _("Software Heritage");
101  menu_insert("Browse::$menuName", 101, $URI, $text);
102  menu_insert("View::$menuName", 101, $viewLicenseURI, $text);
103  menu_insert("View-Meta::$menuName", 101, $viewLicenseURI, $text);
104  }
105  }
106 
107  public function handle(Request $request)
108  {
109  $upload = intval($request->get("upload"));
110  $groupId = Auth::getGroupId();
111  if (!$this->uploadDao->isAccessible($upload, $groupId)) {
112  return $this->flushContent(_("Permission Denied"));
113  }
114  $item = intval($request->get("item"));
115  $vars['baseuri'] = Traceback_uri();
116  $vars['uploadId'] = $upload;
117  $this->uploadtree_tablename = $this->uploadDao->getUploadtreeTableName($upload);
118  if ($request->get('show')=='quick') {
119  $item = $this->uploadDao->getFatItemId($item,$upload,$this->uploadtree_tablename);
120  }
121  $vars['itemId'] = $item;
122  $vars['micromenu'] = Dir2Browse($this->Name, $item, NULL, $showBox = 0, "Browse", -1,
123  '', '', $this->uploadtree_tablename);
124  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($item, $this->uploadtree_tablename);
125  $left = $itemTreeBounds->getLeft();
126  if (empty($left)) {
127  return $this->flushContent(_("Job unpack/adj2nest hasn't completed."));
128  }
129  $histVars = $this->showUploadHist($itemTreeBounds);
130  if (is_a($histVars, 'Symfony\\Component\\HttpFoundation\\RedirectResponse')) {
131  return $histVars;
132  }
133  $vars = array_merge($vars, $histVars);
134  return $this->render("softwareHeritage.html.twig",$this->mergeWithDefault($vars));
135  }
136 
142  private function showUploadHist(ItemTreeBounds $itemTreeBounds)
143  {
144  $groupId = Auth::getGroupId();
145  $agentId = $this->agentDao->getCurrentAgentId("softwareHeritage");
146 
147  $uploadId = $itemTreeBounds->getUploadId();
148  $scannerAgents = array_keys($this->agentNames);
149  $scanJobProxy = new ScanJobProxy($this->agentDao, $uploadId);
150  $scannerVars = $scanJobProxy->createAgentStatus($scannerAgents);
151  $agentMap = $scanJobProxy->getAgentMap();
152 
153  $vars = array(
154  'agentId' => $agentId,
155  'agentMap' => $agentMap,
156  'scanners'=>$scannerVars
157  );
158 
159  $selectedAgentIds = empty($selectedAgentId) ? $scanJobProxy->getLatestSuccessfulAgentIds() : $agentId;
160 
161  $this->licenseProjector = new LicenseMap($this->getObject('db.manager'),$groupId,LicenseMap::CONCLUSION,true);
162  $dirVars = $this->countFileListing($itemTreeBounds);
163  $childCount = $dirVars['iTotalRecords'];
164  /***************************************
165  * Problem: $ChildCount can be zero if you have a container that does not
166  * unpack to a directory. For example:
167  * file.gz extracts to archive.txt that contains a license.
168  * Same problem seen with .pdf and .Z files.
169  * Solution: if $ChildCount == 0, then just view the license!
170  *
171  * $ChildCount can also be zero if the directory is empty.
172  * **************************************/
173  if ($childCount == 0) {
174  return new RedirectResponse("?mod=view-license" . Traceback_parm_keep(array("upload", "item")));
175  }
176  return array_merge($vars, $dirVars);
177  }
178 
183  private function countFileListing(ItemTreeBounds $itemTreeBounds)
184  {
185  $isFlat = isset($_GET['flatten']);
186  $vars['isFlat'] = $isFlat;
187  $vars['iTotalRecords'] = $this->uploadDao->countNonArtifactDescendants($itemTreeBounds, $isFlat);
188  $uri = Traceback_uri().'?mod='.$this->Name.Traceback_parm_keep(array('upload','folder','show','item'));
189  $vars['fileSwitch'] = $isFlat ? $uri : $uri."&flatten=yes";
190  return $vars;
191  }
192 
198  public function renderString($templateName, $vars)
199  {
200  return $this->renderer->loadTemplate($templateName)->render($vars);
201  }
202 }
203 register_plugin(new softwareHeritagePlugin());
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:274
Traceback_uri()
Get the URI without query to this location.
render($templateName, $vars=null, $headers=null)
showUploadHist(ItemTreeBounds $itemTreeBounds)
Given an $Uploadtree_pk, display:
Wrapper class for license map.
Definition: LicenseMap.php:29
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:57
const PARM_STRING
Definition: common-parm.php:29
const PARM_INTEGER
Definition: common-parm.php:25
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:112
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...
static getGroupId()
Get the current user&#39;s group id.
Definition: Auth.php:78
Traceback_parm_keep($List)
Create a new URI, keeping only these items.