9 namespace Fossology\UI\Page;
17 use GuzzleHttp\Client;
18 use GuzzleHttp\Exception\GuzzleException;
19 use GuzzleHttp\Exception\RequestException;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
23 use League\OAuth2\Client\Provider\GenericProvider;
30 const NAME =
"admin_obligation_from_csv";
31 const KEY_UPLOAD_MAX_FILESIZE =
'upload_max_filesize';
32 const FILE_INPUT_NAME =
'file_input';
33 const FILE_INPUT_NAME_V2 =
'fileInput';
38 private $oidcProvider;
45 function __construct()
48 parent::__construct(self::NAME, array(
49 self::TITLE =>
"Admin Obligation Import",
50 self::MENU_LIST =>
"Admin::Obligation Admin::Obligation Import",
51 self::REQUIRES_LOGIN =>
true,
55 $this->obligationsCsvImport = $GLOBALS[
'container']->get(
'app.obligation_csv_import');
57 $this->configuration = [
58 'uri' =>
trim($this->
sysconfig[
'SYSCONFIG'][
'LicenseDBBaseURL']),
59 'content' =>
trim($this->
sysconfig[
'SYSCONFIG'][
'LicenseDBContentObligations']),
60 'health' =>
trim($this->
sysconfig[
'SYSCONFIG'][
'LicenseDBHealth']),
61 'token' => empty(
trim($this->
sysconfig[
'SYSCONFIG'][
'LicenseDBToken'])) ? null :
trim($this->
sysconfig[
'SYSCONFIG'][
'LicenseDBToken']),
64 if ($this->configuration[
'token'] ==
null) {
65 $this->oidcProvider =
new GenericProvider([
66 "clientId" =>
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcAppId']),
67 "clientSecret" =>
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcSecret']),
68 "redirectUri" =>
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcRedirectURL']),
69 "urlAuthorize" =>
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcAuthorizeURL']),
70 "urlAccessToken" =>
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcAccessTokenURL']),
71 "urlResourceOwnerDetails" =>
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcResourceURL']),
74 if (isset($this->
sysconfig[
'SYSCONFIG'][
'OidcScope'])) {
75 $this->configuration[
'scope'] =
trim($this->
sysconfig[
'SYSCONFIG'][
'OidcScope']);
78 $this->oidcProvider =
null;
87 if ($apiVersion == ApiVersion::V2) {
88 return $this::FILE_INPUT_NAME_V2;
90 return $this::FILE_INPUT_NAME;
98 protected function handle(Request $request)
101 if (!$request->isMethod(
'POST')) {
102 $getHealth = $this->configuration[
'uri'] . $this->configuration[
'health'];
106 if ($request->isMethod(
'POST')) {
107 if ($request->get(
'importFrom') ===
'licensedb') {
108 $startTime = microtime(
true);
110 $fetchLicenseTime = microtime(
true) - $startTime;
111 $this->fileLogger->debug(
"Fetching Obligations and Check time took: " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTime));
112 $this->fileLogger->debug(
"****************** Message From LicenseDB import [" . date(
'Y-m-d H:i:s') .
"] ******************");
113 $this->fileLogger->debug($vars[
"message"]);
114 $this->fileLogger->debug(
"****************** End Message From LicenseDB import ******************");
116 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
117 $delimiter = $request->get(
'delimiter') ?:
',';
118 $enclosure = $request->get(
'enclosure') ?:
'"';
120 $vars[
'message'] = is_array($result) ? $result[1] : $result;
124 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
125 $vars[
'baseUrl'] = $request->getBaseUrl();
126 $vars[
'license_csv_import'] =
false;
128 if (!empty(
trim($this->configuration[
'uri']))) {
129 $vars[
'baseURL'] = !empty($this->configuration[
'uri']);
130 $vars[
'tokenConfig'] = !empty($this->configuration[
'token']) || $this->oidcProvider !=
null;
131 $vars[
'exportEndpoint'] = !empty($this->configuration[
'content']);
132 return $this->
render(
"admin_license_from_licensedb.html.twig", $this->mergeWithDefault($vars));
134 return $this->
render(
"admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
149 $finalURL = $this->configuration[
'uri'] . $this->configuration[
'content'];
151 $startTimeReq = microtime(
true);
154 if ($this->configuration[
'token'] !=
null) {
155 $accessToken = $this->configuration[
'token'];
158 if (isset($this->configuration[
'scope'])) {
159 $options[
'scope'] = $this->configuration[
'scope'];
161 $accessToken = $this->oidcProvider->getAccessToken(
'client_credentials', $options);
164 $response = $guzzleClient->get($finalURL);
165 $fetchLicenseTimeReq = microtime(
true) - $startTimeReq;
166 $this->fileLogger->debug(
"LicenseDB req:' took " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTimeReq));
169 return $this->obligationsCsvImport->importJsonData($data, $msg);
171 return $msg . $e->getMessage();
172 }
catch (RequestException | GuzzleException $e) {
173 return $msg . _(
'Something Went Wrong, check if host is accessible') .
': ' . $e->getMessage();
181 public function handleFileUpload($uploadedFile, $delimiter =
',', $enclosure =
'"', $fromRest =
false)
184 if (!($uploadedFile instanceof UploadedFile)) {
185 $errMsg = _(
"No file selected");
186 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
187 $errMsg = _(
"Larger than upload_max_filesize ") . ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
189 $uploadedFile->getClientOriginalExtension() !=
'csv'
190 && $uploadedFile->getClientOriginalExtension() !=
'json'
192 $errMsg = _(
'Invalid extension ') .
193 $uploadedFile->getClientOriginalExtension() .
' of file ' .
194 $uploadedFile->getClientOriginalName();
196 if (!empty($errMsg)) {
198 return array(
false, $errMsg, 400);
202 $this->obligationsCsvImport->setDelimiter($delimiter);
203 $this->obligationsCsvImport->setEnclosure($enclosure);
205 return array(
true, $this->obligationsCsvImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension()), 200);
209 register_plugin(
new AdminObligationFromCSV());
Helper class for Obligation CSV Import.
Contains the constants and helpers for authentication of user.
Exception when Guzzle client response is not as expected.
render($templateName, $vars=null, $headers=null)
static processHttpResponse($response)
static getGuzzleClient(array $SysConf, string $baseUri, string $token="")
static checkLicenseDBHealth(string $getHealth, $guzzleClient)
Upload a file from the users computer using the UI.
getFileInputName($apiVersion=ApiVersion::V1)
handleFileUpload($uploadedFile, $delimiter=',', $enclosure='"', $fromRest = false)
handleLicenseDbObligationImport()
char * trim(char *ptext)
Trimming whitespace.