FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdminLicenseFromYAML.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2024 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\UI\Page;
9 
13 use Symfony\Component\HttpFoundation\File\UploadedFile;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 
21 {
22  const NAME = "admin_license_from_yaml";
23  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
24  function __construct()
25  {
26  parent::__construct(self::NAME, array(
27  self::TITLE => "Admin License Rules Import",
28  self::MENU_LIST => "Admin::License Admin::Rules Import",
29  self::REQUIRES_LOGIN => true,
30  self::PERMISSION => Auth::PERM_ADMIN
31  ));
32  }
37  protected function handle (Request $request)
38  {
39  $vars = array();
40  if ($request->isMethod('POST')) {
41  $uploadFile = $request->files->get('file_input');
42  $vars['message'] = $this->handleFileUpload($uploadFile);
43  }
44  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
45  $vars['baseUrl'] = $request->getBaseUrl();
46  return $this->render("admin_license_from_yaml.html.twig", $this->mergeWithDefault($vars));
47  }
48 
53  protected function handleFileUpload($uploadedFile)
54  {
55  $errMsg = '';
56  if (! ($uploadedFile instanceof UploadedFile)) {
57  $errMsg = _("No file selected");
58  } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
59  $errMsg = $uploadedFile->getErrorMessage();
60  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
61  $errMsg = _("Larger than upload_max_filesize ") .
62  ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
63  } elseif ($uploadedFile->getClientOriginalExtension() != 'yaml' &&
64  $uploadedFile->getClientOriginalExtension() != 'yml') {
65  $errMsg = _('Invalid extension ') .
66  $uploadedFile->getClientOriginalExtension() . ' of file ' .
67  $uploadedFile->getClientOriginalName();
68  }
69  if (! empty($errMsg)) {
70  return $errMsg;
71  }
73  $licenseYamlImport = $this->getObject('app.license_yaml_import');
74  return $licenseYamlImport->handleFile($uploadedFile->getRealPath());
75  }
76 }
77 
78 register_plugin(new AdminLicenseFromYAML());
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
render($templateName, $vars=null, $headers=null)
Upload a file from the users computer using the UI.