FOSSology  4.4.0
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  'reportType' => $this->outputFormat);
92  $text = sprintf(_("Generating ". $this->outputFormat . " report for '%s'"), $upload->getFilename());
93  $vars['content'] = "<h2>".$text."</h2>";
94  $content = $this->renderer->load("report.html.twig")->render($vars);
95  $message = '<h3 id="jobResult"></h3>';
96  $request->duplicate(array('injectedMessage'=>$message,'injectedFoot'=>$content,'mod'=>'showjobs'))->overrideGlobals();
97  $showJobsPlugin = \plugin_find('showjobs');
98  $showJobsPlugin->OutputOpen();
99  return $showJobsPlugin->getResponse();
100  }
101 
102  protected function uploadsAdd($uploads)
103  {
104  if (count($uploads) == 0) {
105  return '';
106  }
107  return '--uploadsAdd='. implode(',', array_keys($uploads));
108  }
109 
110  protected function getJobAndJobqueue($groupId, $upload, $addUploads)
111  {
112  $uploadId = $upload->getId();
113  $cyclonedxAgent = plugin_find('agent_cyclonedx');
114  $userId = Auth::getUserId();
115  $jqCmdArgs = $this->uploadsAdd($addUploads);
116 
117  $dbManager = $this->getObject('db.manager');
118  $sql = 'SELECT jq_pk,job_pk FROM jobqueue, job '
119  . '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';
120  $params = array($cyclonedxAgent->AgentName,$uploadId,$userId,$groupId);
121  $log = __METHOD__;
122  if ($jqCmdArgs) {
123  $sql .= ' AND jq_cmd_args=$5';
124  $params[] = $jqCmdArgs;
125  $log .= '.args';
126  } else {
127  $sql .= ' AND jq_cmd_args IS NULL';
128  }
129  $scheduled = $dbManager->getSingleRow($sql,$params,$log);
130  if (!empty($scheduled)) {
131  return array($scheduled['job_pk'],$scheduled['jq_pk']);
132  }
133  $jobId = JobAddJob($userId, $groupId, $upload->getFilename(), $uploadId);
134  $error = "";
135  $jobQueueId = $cyclonedxAgent->AgentAdd($jobId, $uploadId, $error, array(), $jqCmdArgs);
136  if ($jobQueueId<0) {
137  throw new Exception(_("Cannot schedule").": ".$error);
138  }
139  return array ($jobId, $jobQueueId);
140  }
141 
142  protected function getUpload($uploadId, $groupId)
143  {
144  if ($uploadId <=0) {
145  throw new Exception(_("parameter error: $uploadId"));
146  }
147  /* @var $uploadDao UploadDao */
148  $uploadDao = $this->getObject('dao.upload');
149  if (!$uploadDao->isAccessible($uploadId, $groupId)) {
150  throw new Exception(_("permission denied"));
151  }
153  $upload = $uploadDao->getUpload($uploadId);
154  if ($upload === null) {
155  throw new Exception(_('cannot find uploadId'));
156  }
157  return $upload;
158  }
159 
169  public function scheduleAgent($groupId, $upload, $addUploads = array())
170  {
171  return $this->getJobAndJobqueue($groupId, $upload, $addUploads);
172  }
173 }
174 
175 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