9 namespace Fossology\UI\Page;
16 use GuzzleHttp\Client;
17 use GuzzleHttp\Exception\GuzzleException;
18 use GuzzleHttp\Exception\RequestException;
19 use Symfony\Component\HttpFoundation\File\UploadedFile;
20 use Symfony\Component\HttpFoundation\Request;
21 use Symfony\Component\HttpFoundation\Response;
28 const NAME =
"admin_license_from_csv";
29 const KEY_UPLOAD_MAX_FILESIZE =
'upload_max_filesize';
30 const FILE_INPUT_NAME =
'file_input';
31 const FILE_INPUT_NAME_V2 =
'fileInput';
36 private $guzzleClient;
42 function __construct()
45 parent::__construct(self::NAME, array(
46 self::TITLE =>
"Admin License Import",
47 self::MENU_LIST =>
"Admin::License Admin::License Import",
48 self::REQUIRES_LOGIN =>
true,
52 $this->licenseCsvImport = $GLOBALS[
'container']->get(
'app.license_csv_import');
54 $this->configuration = [
69 if ($apiVersion == ApiVersion::V2) {
70 return $this::FILE_INPUT_NAME_V2;
72 return $this::FILE_INPUT_NAME;
80 protected function handle(Request $request)
83 if (!$request->isMethod(
'POST')) {
86 if ($request->isMethod(
'POST')) {
87 if ($request->get(
'importFrom') ===
'licensedb') {
88 $startTime = microtime(
true);
90 $fetchLicenseTime = microtime(
true) - $startTime;
91 $this->fileLogger->debug(
"Fetching License and Check if exist took: " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTime));
92 $this->fileLogger->debug(
"****************** Message From LicenseDB import [" . date(
'Y-m-d H:i:s') .
"] ******************");
93 $this->fileLogger->debug($vars[
"message"]);
94 $this->fileLogger->debug(
"****************** End Message From LicenseDB import ******************");
96 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
97 $delimiter = $request->get(
'delimiter') ?:
',';
98 $enclosure = $request->get(
'enclosure') ?:
'"';
103 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
104 $vars[
'baseUrl'] = $request->getBaseUrl();
105 $vars[
'license_csv_import'] =
true;
107 if (!empty(
trim($this->configuration[
'url']))) {
108 $vars[
'baseURL'] = !empty($this->configuration[
'uri']);
109 $vars[
'authToken'] = !empty($this->configuration[
'token']);
110 $vars[
'exportEndpoint'] = !empty($this->configuration[
'content']);
111 return $this->
render(
"admin_license_from_licensedb.html.twig", $this->mergeWithDefault($vars));
113 return $this->
render(
"admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
128 $finalURL = $this->configuration[
'url'] . $this->configuration[
'uri'] . $this->configuration[
'content'];
130 $startTimeReq = microtime(
true);
131 $response = $this->guzzleClient->get($finalURL);
132 $fetchLicenseTimeReq = microtime(
true) - $startTimeReq;
133 $this->fileLogger->debug(
"LicenseDB req:' took " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTimeReq));
134 if ($response->getStatusCode() == 200) {
135 $data = json_decode($response->getBody()->getContents());
138 if ($data ===
null && json_last_error() !== JSON_ERROR_NONE) {
139 return $msg .
"Error decoding JSON: " . json_last_error_msg() .
"\n";
141 return $this->licenseCsvImport->importJsonData($data, $msg);
143 }
catch (RequestException|GuzzleException $e) {
144 return $msg . _(
'Something Went Wrong, check if host is accessible') .
': ' . $e->getMessage();
155 if (!($uploadedFile instanceof UploadedFile)) {
156 $errMsg = _(
"No file selected");
157 } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
158 $errMsg = $uploadedFile->getErrorMessage();
159 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
160 $errMsg = _(
"Larger than upload_max_filesize ") .
161 ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
162 } elseif ($uploadedFile->getClientOriginalExtension() !=
'csv'
163 && $uploadedFile->getClientOriginalExtension() !=
'json') {
164 $errMsg = _(
'Invalid file extension ') .
165 $uploadedFile->getClientOriginalExtension() .
' of file ' .
166 $uploadedFile->getClientOriginalName();
168 if (!empty($errMsg)) {
169 return array(
false, $errMsg, 400);
171 $this->licenseCsvImport->setDelimiter($delimiter);
172 $this->licenseCsvImport->setEnclosure($enclosure);
174 return array(
true, $this->licenseCsvImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension()), 200);
188 $getHealth = $this->configuration[
'url'] . $this->configuration[
'uri'] .
"/health";
190 $response = $this->guzzleClient->get($getHealth);
191 if ($response->getStatusCode() === 200) {
194 }
catch (RequestException|GuzzleException $e) {
202 register_plugin(
new AdminLicenseFromCSV());
Import licenses from CSV.
Contains the constants and helpers for authentication of user.
render($templateName, $vars=null, $headers=null)
static getGuzzleClient(array $SysConf, string $baseUri, string $token="")
Upload a file from the users computer using the UI.
getFileInputName($apiVersion=ApiVersion::V1)
handleFileUpload($uploadedFile, $delimiter=',', $enclosure='"')
char * trim(char *ptext)
Trimming whitespace.