FOSSology  4.7.1
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 
14 use Symfony\Component\HttpFoundation\File\UploadedFile;
15 use Symfony\Component\HttpFoundation\Request;
16 use Symfony\Component\HttpFoundation\Response;
17 
22 {
23  const NAME = "admin_license_from_yaml";
24  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
25  const FILE_INPUT_NAME = 'file_input';
26  const FILE_INPUT_NAME_V2 = 'fileInput';
27  function __construct()
28  {
29  parent::__construct(self::NAME, array(
30  self::TITLE => "Admin License Rules Import",
31  self::MENU_LIST => "Admin::License Admin::Rules Import",
32  self::REQUIRES_LOGIN => true,
33  self::PERMISSION => Auth::PERM_ADMIN
34  ));
35  }
39  public function getFileInputName($apiVersion = ApiVersion::V1)
40  {
41  if ($apiVersion == ApiVersion::V2) {
42  return $this::FILE_INPUT_NAME_V2;
43  } else {
44  return $this::FILE_INPUT_NAME;
45  }
46  }
47 
52  protected function handle (Request $request)
53  {
54  $vars = array();
55  if ($request->isMethod('POST')) {
56  $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
57  $vars['message'] = $this->handleFileUpload($uploadFile);
58  }
59  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
60  $vars['baseUrl'] = $request->getBaseUrl();
61  return $this->render("admin_license_from_yaml.html.twig", $this->mergeWithDefault($vars));
62  }
63 
70  public function handleFileUpload($uploadedFile, $fromRest = false)
71  {
72  $errMsg = '';
73  if (! ($uploadedFile instanceof UploadedFile)) {
74  $errMsg = _("No file selected");
75  } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
76  $errMsg = $uploadedFile->getErrorMessage();
77  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
78  $errMsg = _("Larger than upload_max_filesize ") .
79  ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
80  } elseif ($uploadedFile->getClientOriginalExtension() != 'yaml' &&
81  $uploadedFile->getClientOriginalExtension() != 'yml') {
82  $errMsg = _('Invalid extension ') .
83  $uploadedFile->getClientOriginalExtension() . ' of file ' .
84  $uploadedFile->getClientOriginalName();
85  }
86  if (! empty($errMsg)) {
87  if ($fromRest) {
88  return array(false, $errMsg, 400);
89  }
90  return $errMsg;
91  }
93  $licenseYamlImport = $this->getObject('app.license_yaml_import');
94  $msg = $licenseYamlImport->handleFile($uploadedFile->getRealPath());
95  if ($fromRest) {
96  return array(true, $msg, 200);
97  }
98  return $msg;
99  }
100 }
101 
102 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.
getFileInputName($apiVersion=ApiVersion::V1)