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 = [
58 'maxtime' => intval($this->
sysconfig[
'LicenseDBSleep']),
70 if ($apiVersion == ApiVersion::V2) {
71 return $this::FILE_INPUT_NAME_V2;
73 return $this::FILE_INPUT_NAME;
81 protected function handle(Request $request)
84 if (!$request->isMethod(
'POST')) {
87 if ($request->isMethod(
'POST')) {
88 if ($request->get(
'importFrom') ===
'licensedb') {
89 $startTime = microtime(
true);
91 $fetchLicenseTime = microtime(
true) - $startTime;
92 $this->fileLogger->debug(
"Fetching License and Check if exist took: " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTime));
93 $this->fileLogger->debug(
"****************** Message From LicenseDB import [" . date(
'Y-m-d H:i:s') .
"] ******************");
94 $this->fileLogger->debug($vars[
"message"]);
95 $this->fileLogger->debug(
"****************** End Message From LicenseDB import ******************");
97 $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
98 $delimiter = $request->get(
'delimiter') ?:
',';
99 $enclosure = $request->get(
'enclosure') ?:
'"';
104 $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
105 $vars[
'baseUrl'] = $request->getBaseUrl();
106 $vars[
'license_csv_import'] =
true;
108 if (!empty(
trim($this->configuration[
'url']))) {
109 $vars[
'baseURL'] = !empty($this->configuration[
'uri']);
110 $vars[
'authToken'] = !empty($this->configuration[
'token']);
111 $vars[
'exportEndpoint'] = !empty($this->configuration[
'content']);
112 return $this->
render(
"admin_license_from_licensedb.html.twig", $this->mergeWithDefault($vars));
114 return $this->
render(
"admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
129 $finalURL = $this->configuration[
'url'] . $this->configuration[
'uri'] . $this->configuration[
'content'];
131 $startTimeReq = microtime(
true);
132 $response = $this->guzzleClient->get($finalURL);
133 $fetchLicenseTimeReq = microtime(
true) - $startTimeReq;
134 $this->fileLogger->debug(
"LicenseDB req:' took " . sprintf(
"%0.3fms", 1000 * $fetchLicenseTimeReq));
135 if ($response->getStatusCode() == 200) {
136 $data = json_decode($response->getBody()->getContents());
139 if ($data ===
null && json_last_error() !== JSON_ERROR_NONE) {
140 return $msg .
"Error decoding JSON: " . json_last_error_msg() .
"\n";
142 return $this->licenseCsvImport->importJsonData($data, $msg);
144 }
catch (RequestException|GuzzleException $e) {
145 return $msg . _(
'Something Went Wrong, check if host is accessible') .
': ' . $e->getMessage();
156 if (!($uploadedFile instanceof UploadedFile)) {
157 $errMsg = _(
"No file selected");
158 } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
159 $errMsg = $uploadedFile->getErrorMessage();
160 } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
161 $errMsg = _(
"Larger than upload_max_filesize ") .
162 ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
163 } elseif ($uploadedFile->getClientOriginalExtension() !=
'csv'
164 && $uploadedFile->getClientOriginalExtension() !=
'json') {
165 $errMsg = _(
'Invalid file extension ') .
166 $uploadedFile->getClientOriginalExtension() .
' of file ' .
167 $uploadedFile->getClientOriginalName();
169 if (!empty($errMsg)) {
170 return array(
false, $errMsg, 400);
172 $this->licenseCsvImport->setDelimiter($delimiter);
173 $this->licenseCsvImport->setEnclosure($enclosure);
175 return array(
true, $this->licenseCsvImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension()), 200);
189 $getHealth = $this->configuration[
'url'] . $this->configuration[
'uri'] .
"/health";
191 $response = $this->guzzleClient->get($getHealth);
192 if ($response->getStatusCode() === 200) {
195 }
catch (RequestException|GuzzleException $e) {
203 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.