FOSSology  4.4.0
Open Source License Compliance by Open Source Software
SpdxTwoGeneratorUi.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015-2018 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\SpdxTwo\UI;
9 
14 use Symfony\Component\HttpFoundation\Request;
15 
21 {
22  const NAME = 'ui_spdx2';
23  const DEFAULT_OUTPUT_FORMAT = "spdx2";
28 
29  function __construct()
30  {
31  $possibleOutputFormat = trim(GetParm("outputFormat",PARM_STRING));
32  if (strcmp($possibleOutputFormat,"") !== 0 &&
33  strcmp($possibleOutputFormat,self::DEFAULT_OUTPUT_FORMAT) !== 0 &&
34  ctype_alnum($possibleOutputFormat)) {
35  $this->outputFormat = $possibleOutputFormat;
36  }
37  parent::__construct(self::NAME, array(
38  self::TITLE => _(strtoupper($this->outputFormat) . " generation"),
39  self::PERMISSION => Auth::PERM_WRITE,
40  self::REQUIRES_LOGIN => true
41  ));
42  }
43 
48  function preInstall()
49  {
50  $text = _("Generate SPDX report in RDF format");
51  menu_insert("Browse-Pfile::Export&nbsp;SPDX&nbsp;RDF&nbsp;report", 0, self::NAME, $text);
52  menu_insert("UploadMulti::Generate&nbsp;SPDX", 0, self::NAME, $text);
53 
54  $text = _("Generate SPDX report in tag:value format");
55  menu_insert("Browse-Pfile::Export&nbsp;SPDX&nbsp;tag:value&nbsp;report", 0, self::NAME . '&outputFormat=spdx2tv', $text);
56 
57  $text = _("Generate CSV report (with SPDX IDs)");
58  menu_insert("Browse-Pfile::Export&nbsp;CSV&nbsp;report&nbsp;(SPDX)", 0, self::NAME . '&outputFormat=spdx2csv', $text);
59 
60  $text = _("Generate Debian Copyright file");
61  menu_insert("Browse-Pfile::Export&nbsp;DEP5&nbsp;report", 0, self::NAME . '&outputFormat=dep5', $text);
62  }
63 
68  protected function handle(Request $request)
69  {
70 
71  $groupId = Auth::getGroupId();
72  $uploadIds = $request->get('uploads') ?: array();
73  $uploadIds[] = intval($request->get('upload'));
74  $addUploads = array();
75  foreach ($uploadIds as $uploadId) {
76  if (empty($uploadId)) {
77  continue;
78  }
79  try {
80  $addUploads[$uploadId] = $this->getUpload($uploadId, $groupId);
81  } catch(Exception $e) {
82  return $this->flushContent($e->getMessage());
83  }
84  }
85  $folderId = $request->get('folder');
86  if (!empty($folderId)) {
87  /* @var $folderDao FolderDao */
88  $folderDao = $this->getObject('dao.folder');
89  $folderUploads = $folderDao->getFolderUploads($folderId, $groupId);
90  foreach ($folderUploads as $uploadProgress) {
91  $addUploads[$uploadProgress->getId()] = $uploadProgress;
92  }
93  }
94  if (empty($addUploads)) {
95  return $this->flushContent(_('No upload selected'));
96  }
97  $upload = array_pop($addUploads);
98  try {
99  list($jobId,$jobQueueId) = $this->getJobAndJobqueue($groupId, $upload, $addUploads);
100  } catch (\Exception $ex) {
101  return $this->flushContent($ex->getMessage());
102  }
103 
104  $vars = array('jqPk' => $jobQueueId,
105  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
106  'reportType' => $this->outputFormat);
107  $text = sprintf(_("Generating ". $this->outputFormat . " report for '%s'"), $upload->getFilename());
108  $vars['content'] = "<h2>".$text."</h2>";
109  $content = $this->renderer->load("report.html.twig")->render($vars);
110  $message = '<h3 id="jobResult"></h3>';
111  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
112  $showJobsPlugin = \plugin_find('showjobs');
113  $showJobsPlugin->OutputOpen();
114  return $showJobsPlugin->getResponse();
115  }
116 
122  protected function uploadsAdd($uploads)
123  {
124  if (count($uploads) == 0) {
125  return '';
126  }
127  return '--uploadsAdd='. implode(',', array_keys($uploads));
128  }
129 
138  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
139  {
140  $uploadId = $upload->getId();
141  $spdxTwoAgent = plugin_find('agent_'.$this->outputFormat);
142  $userId = Auth::getUserId();
143  $jqCmdArgs = $this->uploadsAdd($addUploads);
144 
145  $dbManager = $this->getObject('db.manager');
146  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
147  . 'WHERE jq_job_fk=job_pk AND jq_type=$1 AND job_group_fk=$4 AND job_user_fk=$3 AND jq_args=$2 AND jq_endtime IS NULL';
148  $params = array($spdxTwoAgent->AgentName,$uploadId,$userId,$groupId);
149  $log = __METHOD__;
150  if ($jqCmdArgs) {
151  $sql .= ' AND jq_cmd_args=$5';
152  $params[] = $jqCmdArgs;
153  $log .= '.args';
154  } else {
155  $sql .= ' AND jq_cmd_args IS NULL';
156  }
157  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
158  if (!empty($scheduled)) {
159  return array($scheduled['job_pk'],$scheduled['jq_pk']);
160  }
161  if (empty($jqCmdArgs)) {
162  $jobName = $upload->getFilename();
163  } else {
164  $jobName = "Multi File SPDX2";
165  }
166  $jobId = JobAddJob($userId, $groupId, $jobName, $uploadId);
167  $error = "";
168  $jobQueueId = $spdxTwoAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
169  if ($jobQueueId < 0) {
170  throw new \Exception(_("Cannot schedule").": ".$error);
171  }
172  return array($jobId,$jobQueueId, $error);
173  }
174 
182  protected function getUpload($uploadId, $groupId)
183  {
184  if ($uploadId <= 0) {
185  throw new \Exception(_("parameter error: $uploadId"));
186  }
187  /* @var $uploadDao UploadDao */
188  $uploadDao = $this->getObject('dao.upload');
189  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
190  throw new \Exception(_("permission denied"));
191  }
193  $upload = $uploadDao->getUpload($uploadId);
194  if ($upload === null) {
195  throw new \Exception(_('cannot find uploadId'));
196  }
197  return $upload;
198  }
199 
210  public function scheduleAgent($groupId, $upload,
211  $outputFormat = self::DEFAULT_OUTPUT_FORMAT, $addUploads = array())
212  {
213  $this->outputFormat = $outputFormat;
214  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
215  }
216 }
217 
218 register_plugin(new SpdxTwoGeneratorUi());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
static getUserId()
Get the current user's id.
Definition: Auth.php:68
static getGroupId()
Get the current user's group id.
Definition: Auth.php:80
Call SPDX2 agent to generate report from UI.
getJobAndJobqueue($groupId, $upload, $addUploads)
Get the Job ID and Job queue ID.
scheduleAgent($groupId, $upload, $outputFormat=self::DEFAULT_OUTPUT_FORMAT, $addUploads=array())
uploadsAdd($uploads)
Add multiple uploads to the report.
const DEFAULT_OUTPUT_FORMAT
Default report format.
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_STRING
Definition: common-parm.php:18
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
FUNCTION char * strtoupper(char *s)
Helper function to upper case a string.
Definition: utils.c:39
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308