FOSSology  4.7.1
Open Source License Compliance by Open Source Software
CycloneDXGeneratorUi.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2023 Sushant Kumar <sushantmishra02102002@gmail.com>
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\CycloneDX\UI;
9 
10 use Exception;
16 use Symfony\Component\HttpFoundation\Request;
17 
19 {
20  const NAME = 'ui_cyclonedx';
21  const DEFAULT_OUTPUT_FORMAT = "cyclonedx_json";
23  protected $outputFormat = self::DEFAULT_OUTPUT_FORMAT;
24 
25  function __construct()
26  {
27  $possibleOutputFormat = trim(GetParm("outputFormat",PARM_STRING));
28  if (strcmp($possibleOutputFormat,"") !== 0 &&
29  strcmp($possibleOutputFormat,self::DEFAULT_OUTPUT_FORMAT) !== 0 &&
30  ctype_alnum($possibleOutputFormat)) {
31  $this->outputFormat = $possibleOutputFormat;
32  }
33  parent::__construct(self::NAME, array(
34  self::TITLE => _("CycloneDX generation"),
35  self::PERMISSION => Auth::PERM_WRITE,
36  self::REQUIRES_LOGIN => true
37  ));
38  }
39 
40  function preInstall()
41  {
42  $text = _("Generate CycloneDX report");
43  menu_insert("Browse-Pfile::Export&nbsp;CycloneDX Report", 0, self::NAME, $text);
44  menu_insert("UploadMulti::Generate&nbsp;CycloneDX Report", 0, self::NAME, $text);
45 
46  }
47 
48  protected function handle(Request $request)
49  {
50 
51  $groupId = Auth::getGroupId();
52  $uploadIds = $request->get('uploads') ?: array();
53  $uploadIds[] = intval($request->get('upload'));
54  $addUploads = array();
55  foreach ($uploadIds as $uploadId) {
56  if (empty($uploadId)) {
57  continue;
58  }
59  try
60  {
61  $addUploads[$uploadId] = $this->getUpload($uploadId, $groupId);
62  }
63  catch(Exception $e)
64  {
65  return $this->flushContent($e->getMessage());
66  }
67  }
68  $folderId = $request->get('folder');
69  if (!empty($folderId)) {
70  /* @var $folderDao FolderDao */
71  $folderDao = $this->getObject('dao.folder');
72  $folderUploads = $folderDao->getFolderUploads($folderId, $groupId);
73  foreach ($folderUploads as $uploadProgress) {
74  $addUploads[$uploadProgress->getId()] = $uploadProgress;
75  }
76  }
77  if (empty($addUploads)) {
78  return $this->flushContent(_('No upload selected'));
79  }
80  $upload = array_pop($addUploads);
81  try
82  {
83  list($jobId,$jobQueueId) = $this->getJobAndJobqueue($groupId, $upload, $addUploads);
84  }
85  catch (Exception $ex) {
86  return $this->flushContent($ex->getMessage());
87  }
88 
89  $vars = array('jqPk' => $jobQueueId,
90  'downloadLink' => Traceback_uri(). "?mod=download&report=".$jobId,
91  'showJobsLink' => Traceback_uri(). "?mod=showjobs&upload=".$upload->getId(),
92  'reportType' => $this->outputFormat);
93  $text = sprintf(_("Generating ". $this->outputFormat . " report for '%s'"), $upload->getFilename());
94  $vars['content'] = "<h2>".$text."</h2>";
95  $content = $this->renderer->load("report.html.twig")->render($vars);
96  $message = '<h3 id="jobResult"></h3>';
97  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
98  $showJobsPlugin = \plugin_find('showjobs');
99  $showJobsPlugin->OutputOpen();
100  return $showJobsPlugin->getResponse();
101  }
102 
103  protected function uploadsAdd($uploads)
104  {
105  if (count($uploads) == 0) {
106  return '';
107  }
108  return '--uploadsAdd='. implode(',', array_keys($uploads));
109  }
110 
111  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
112  {
113  $uploadId = $upload->getId();
114  $cyclonedxAgent = plugin_find('agent_cyclonedx');
115  $userId = Auth::getUserId();
116  $jqCmdArgs = $this->uploadsAdd($addUploads);
117 
118  $dbManager = $this->getObject('db.manager');
119  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
120  . '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';
121  $params = array($cyclonedxAgent->AgentName,$uploadId,$userId,$groupId);
122  $log = __METHOD__;
123  if ($jqCmdArgs) {
124  $sql .= ' AND jq_cmd_args=$5';
125  $params[] = $jqCmdArgs;
126  $log .= '.args';
127  } else {
128  $sql .= ' AND jq_cmd_args IS NULL';
129  }
130  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
131  if (!empty($scheduled)) {
132  return array($scheduled['job_pk'],$scheduled['jq_pk']);
133  }
134  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
135  $error = "";
136  $jobQueueId = $cyclonedxAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
137  if ($jobQueueId<0) {
138  throw new Exception(_("Cannot schedule").": ".$error);
139  }
140  return array ($jobId, $jobQueueId);
141  }
142 
143  protected function getUpload($uploadId, $groupId)
144  {
145  if ($uploadId <=0) {
146  throw new Exception(_("parameter error: $uploadId"));
147  }
148  /* @var $uploadDao UploadDao */
149  $uploadDao = $this->getObject('dao.upload');
150  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
151  throw new Exception(_("permission denied"));
152  }
154  $upload = $uploadDao->getUpload($uploadId);
155  if ($upload === null) {
156  throw new Exception(_('cannot find uploadId'));
157  }
158  return $upload;
159  }
160 
170  public function scheduleAgent($groupId, $upload, $addUploads = array())
171  {
172  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
173  }
174 }
175 
176 register_plugin(new CycloneDxGeneratorUi());
scheduleAgent($groupId, $upload, $addUploads=array())
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
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