FOSSology  4.5.0-rc1
Open Source License Compliance by Open Source Software
SpdxThreeGeneratorUi.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Abhishek Kumar
4  Author: Abhishek Kumar <akumar17871@gmail.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Spdx\UI;
10 
15 use Symfony\Component\HttpFoundation\Request;
16 
22 {
23  const NAME = 'ui_spdx3';
24  const DEFAULT_OUTPUT_FORMAT = "spdx3jsonld";
29 
30  function __construct()
31  {
32  $possibleOutputFormat = trim(GetParm("outputFormat",PARM_STRING));
33  if (strcmp($possibleOutputFormat,"") !== 0 &&
34  strcmp($possibleOutputFormat,self::DEFAULT_OUTPUT_FORMAT) !== 0 &&
35  ctype_alnum($possibleOutputFormat)) {
36  $this->outputFormat = $possibleOutputFormat;
37  }
38  parent::__construct(self::NAME, array(
39  self::TITLE => _("SPDX3 generation"),
40  self::PERMISSION => Auth::PERM_WRITE,
41  self::REQUIRES_LOGIN => true
42  ));
43  }
44 
49  function preInstall()
50  {
51  $text = _("Generate SPDX3.0 report in JSON-LD format");
52  menu_insert("Browse-Pfile::Export&nbsp;SPDX3.0&nbsp;JSON-LD&nbsp;report", 0, self::NAME . '&outputFormat=spdx3jsonld', $text);
53  menu_insert("UploadMulti::Generate&nbsp;SPDX3.0", 0, self::NAME, $text);
54 
55  $text = _("Generate SPDX3.0 report in JSON format");
56  menu_insert("Browse-Pfile::Export&nbsp;SPDX3.0&nbsp;JSON&nbsp;report", 0, self::NAME . '&outputFormat=spdx3json', $text);
57 
58  $text = _("Generate SPDX3.0 report in RDF format");
59  menu_insert("Browse-Pfile::Export&nbsp;SPDX3.0&nbsp;RDF&nbsp;report", 0, self::NAME . '&outputFormat=spdx3rdf', $text);
60 
61  $text = _("Generate SPDX3.0 report in tag/value format");
62  menu_insert("Browse-Pfile::Export&nbsp;SPDX3.0&nbsp;tag/value&nbsp;report", 0, self::NAME . '&outputFormat=spdx3tv', $text);
63  }
64 
69  protected function handle(Request $request)
70  {
71 
72  $groupId = Auth::getGroupId();
73  $uploadIds = $request->get('uploads') ?: array();
74  $uploadIds[] = intval($request->get('upload'));
75  $addUploads = array();
76  foreach ($uploadIds as $uploadId) {
77  if (empty($uploadId)) {
78  continue;
79  }
80  try {
81  $addUploads[$uploadId] = $this->getUpload($uploadId, $groupId);
82  } catch(Exception $e) {
83  return $this->flushContent($e->getMessage());
84  }
85  }
86  $folderId = $request->get('folder');
87  if (!empty($folderId)) {
88  /* @var $folderDao FolderDao */
89  $folderDao = $this->getObject('dao.folder');
90  $folderUploads = $folderDao->getFolderUploads($folderId, $groupId);
91  foreach ($folderUploads as $uploadProgress) {
92  $addUploads[$uploadProgress->getId()] = $uploadProgress;
93  }
94  }
95  if (empty($addUploads)) {
96  return $this->flushContent(_('No upload selected'));
97  }
98  $upload = array_pop($addUploads);
99  try {
100  list($jobId,$jobQueueId) = $this->getJobAndJobqueue($groupId, $upload, $addUploads);
101  } catch (\Exception $ex) {
102  return $this->flushContent($ex->getMessage());
103  }
104 
105  $vars = array('jqPk' => $jobQueueId,
106  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
107  'reportType' => $this->outputFormat);
108  $text = sprintf(_("Generating ". $this->outputFormat . " report for '%s'"), $upload->getFilename());
109  $vars['content'] = "<h2>".$text."</h2>";
110  $content = $this->renderer->load("report.html.twig")->render($vars);
111  $message = '<h3 id="jobResult"></h3>';
112  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
113  $showJobsPlugin = \plugin_find('showjobs');
114  $showJobsPlugin->OutputOpen();
115  return $showJobsPlugin->getResponse();
116  }
117 
123  protected function uploadsAdd($uploads)
124  {
125  if (count($uploads) == 0) {
126  return '';
127  }
128  return '--uploadsAdd='. implode(',', array_keys($uploads));
129  }
130 
139  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
140  {
141  $uploadId = $upload->getId();
142  $SpdxThreeAgent = plugin_find('agent_'.$this->outputFormat);
143  $userId = Auth::getUserId();
144  $jqCmdArgs = $this->uploadsAdd($addUploads);
145 
146  $dbManager = $this->getObject('db.manager');
147  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
148  . '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';
149  $params = array($SpdxThreeAgent->AgentName,$uploadId,$userId,$groupId);
150  $log = __METHOD__;
151  if ($jqCmdArgs) {
152  $sql .= ' AND jq_cmd_args=$5';
153  $params[] = $jqCmdArgs;
154  $log .= '.args';
155  } else {
156  $sql .= ' AND jq_cmd_args IS NULL';
157  }
158  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
159  if (!empty($scheduled)) {
160  return array($scheduled['job_pk'],$scheduled['jq_pk']);
161  }
162  if (empty($jqCmdArgs)) {
163  $jobName = $upload->getFilename();
164  } else {
165  $jobName = "Multi File SPDX3";
166  }
167  $jobId = JobAddJob($userId, $groupId, $jobName, $uploadId);
168  $error = "";
169  $jobQueueId = $SpdxThreeAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
170  if ($jobQueueId < 0) {
171  throw new \Exception(_("Cannot schedule").": ".$error);
172  }
173  return array($jobId,$jobQueueId, $error);
174  }
175 
183  protected function getUpload($uploadId, $groupId)
184  {
185  if ($uploadId <= 0) {
186  throw new \Exception(_("parameter error: $uploadId"));
187  }
188  /* @var $uploadDao UploadDao */
189  $uploadDao = $this->getObject('dao.upload');
190  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
191  throw new \Exception(_("permission denied"));
192  }
194  $upload = $uploadDao->getUpload($uploadId);
195  if ($upload === null) {
196  throw new \Exception(_('cannot find uploadId'));
197  }
198  return $upload;
199  }
200 
211  public function scheduleAgent($groupId, $upload,
212  $outputFormat = self::DEFAULT_OUTPUT_FORMAT, $addUploads = array())
213  {
214  $this->outputFormat = $outputFormat;
215  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
216  }
217 }
218 
219 register_plugin(new SpdxThreeGeneratorUi());
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 SPDX3 agent to generate report from UI.
getJobAndJobqueue($groupId, $upload, $addUploads)
Get the Job ID and Job queue ID.
const DEFAULT_OUTPUT_FORMAT
Default report format.
scheduleAgent($groupId, $upload, $outputFormat=self::DEFAULT_OUTPUT_FORMAT, $addUploads=array())
uploadsAdd($uploads)
Add multiple uploads to the report.
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
list_t type structure used to keep various lists. (e.g. there are multiple lists).
Definition: nomos.h:308