FOSSology  4.4.0
Open Source License Compliance by Open Source Software
change-license-processPost.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018, 2020 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
14 use Symfony\Component\HttpFoundation\JsonResponse;
15 
16 define("TITLE_CHANGELICPROCPOST", _("Private: Change license file post"));
17 
19 {
21  private $clearingDao;
23  private $uploadDao;
25  private $decisionSearch = array(
26  DecisionTypes::IRRELEVANT => "noLicenseKnown",
27  DecisionTypes::NON_FUNCTIONAL => "nonFunctional",
28  DecisionTypes::TO_BE_DISCUSSED => "toBeDiscussed",
29  DecisionTypes::IRRELEVANT => "irrelevant",
30  DecisionTypes::DO_NOT_USE => "doNotUse",
31  DecisionTypes::IDENTIFIED => "identified"
32  );
33 
34  function __construct()
35  {
36  $this->Name = "change-license-processPost";
37  $this->Title = TITLE_CHANGELICPROCPOST;
38  $this->DBaccess = PLUGIN_DB_WRITE;
39  $this->OutputType = 'JSON';
40  $this->OutputToStdout = 1;
41  $this->LoginFlag = 0;
42  $this->NoMenu = 0;
43 
44  parent::__construct();
45 
46  global $container;
47  $this->clearingDao = $container->get('dao.clearing');
48  $this->uploadDao = $container->get('dao.upload');
49  }
50 
54  // TODO: Understand Buckets and modify then
55  function ChangeBuckets()
56  {
57  global $SysConf;
58  global $PG_CONN;
59 
60  $uploadId = GetParm("upload", PARM_STRING);
61  $uploadTreeId = GetParm("item", PARM_STRING);
62 
63  $sql = "SELECT bucketpool_fk from bucket_ars where upload_fk = $uploadId;";
64  $result = pg_query($PG_CONN, $sql);
65  DBCheckResult($result, $sql, __FILE__, __LINE__);
66  $bucketpool_array = pg_fetch_all_columns($result, 0);
67  pg_free_result($result);
68  $buckets_dir = $SysConf['DIRECTORIES']['MODDIR'];
70  foreach ($bucketpool_array as $bucketpool) {
71  $command = "$buckets_dir/buckets/agent/buckets -r -t ".escapeshellarg($uploadTreeId)." -p $bucketpool";
72  exec($command);
73  }
74  }
75 
79  function Output()
80  {
81  $itemArray = array();
82  if ($this->State != PLUGIN_STATE_READY) {
83  return;
84  }
85  $itemId = @$_POST['uploadTreeId'];
86  if (empty($itemId)) {
87  return $this->errorJson("Bad item id");
88  }
89  $itemArray = explode(',', $itemId);
90  $userId = Auth::getUserId();
91  $groupId = Auth::getGroupId();
92  $isRemoval = strtolower(@$_POST['isRemoval']);
93  $isRemoval = $isRemoval == 't' || $isRemoval == 'true';
94  $decisionMark = @$_POST['decisionMark'];
95  $decisionMarkExists = array_search($decisionMark, $this->decisionSearch, true);
96 
97  if ( (!empty($decisionMark)) && $decisionMarkExists !== false ) {
98  foreach ($itemArray as $uploadTreeId) {
99  $responseMsg = $this->doMarkDecisionTypes($uploadTreeId, $groupId,
100  $userId, $decisionMark, $isRemoval);
101  if (! empty($responseMsg)) {
102  return $responseMsg;
103  }
104  }
105  return new JsonResponse(array('result'=>'success'));
106  }
107  return $this->doEdit($userId, $groupId, $itemArray);
108  }
109 
110  function doMarkDecisionTypes($itemId, $groupId, $userId, $decisionMark, $isRemoval)
111  {
112  $itemTableName = $this->uploadDao->getUploadtreeTableName($itemId);
114  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemId, $itemTableName);
115  if ($isRemoval) {
116  $errMsg = $this->clearingDao->deleteDecisionTypeFromDirectory($itemTreeBounds, $groupId, $userId, $decisionMark);
117  } else {
118  $errMsg = $this->clearingDao->markDirectoryAsDecisionType($itemTreeBounds, $groupId, $userId, $decisionMark);
119  }
120  return $errMsg;
121  }
122 
123  function doEdit($userId, $groupId, $itemArray)
124  {
125  $licenses = GetParm("licenseNumbersToBeSubmitted", PARM_RAW);
126  $removed = strtolower(@$_POST['removed']);
127  $removed = $removed == 't' || $removed == 'true';
128 
129  $itemTreeBounds = $this->uploadDao->getItemTreeBounds($itemArray[0]);
130  $uploadId = $itemTreeBounds->getUploadId();
131  $upload = $this->uploadDao->getUpload($uploadId);
132  $uploadName = $upload->getFilename();
133 
134  $jobId = JobAddJob($userId, $groupId, $uploadName, $uploadId);
135  foreach ($itemArray as $itemId) {
136  if (isset($licenses)) {
137  if (! is_array($licenses)) {
138  return $this->errorJson("bad license array");
139  }
140  foreach ($licenses as $licenseId) {
141  if (intval($licenseId) <= 0) {
142  return $this->errorJson("bad license");
143  }
144 
145  $this->clearingDao->insertClearingEvent($itemId, $userId, $groupId,
146  $licenseId, $removed, ClearingEventTypes::USER, $reportInfo = '',
147  $comment = '', $acknowledgement = '', $jobId);
148  }
149  }
150  }
151 
153  $deciderPlugin = plugin_find("agent_deciderjob");
154 
155  $conflictStrategyId = null;
156  $errorMsg = "";
157  $jq_pk = $deciderPlugin->AgentAdd($jobId, $uploadId, $errorMsg, array(), $conflictStrategyId);
158 
161 
168  if (empty($errorMsg) && ($jq_pk>0)) {
169  return new JsonResponse(array("jqid" => $jq_pk));
170  } else {
171  return $this->errorJson($errorMsg, 500);
172  }
173  }
174 
175  private function errorJson($msg, $code = 404)
176  {
177  return new JsonResponse(array("error" => $msg), $code);
178  }
179 }
180 
181 $NewPlugin = new changeLicenseProcessPost;
182 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
__construct()
base constructor. Most plugins will just use this
Output()
Display the loaded menu and plugins.
ChangeBuckets()
change bucket accordingly when change license of one file
ReportCachePurgeAll()
Purge all records from the report cache.
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
const PARM_RAW
Definition: common-parm.php:22
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.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN