FOSSology  4.7.1
Open Source License Compliance by Open Source Software
AdminCustomTextImport.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2025 Harshit Gandhi <gandhiharshit716@gmail.com>
4  SPDX-FileCopyrightText: © Fossology contributors
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\UI\Page;
10 
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_custom_text_import";
24  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
25  const FILE_INPUT_NAME = 'file_input';
26  const MAX_IMPORT_BYTES = 5242880; // 5 MB
27 
29  protected $customTextImport;
30 
31  function __construct()
32  {
33  parent::__construct(self::NAME, array(
34  self::TITLE => "Import Custom Text (CSV/JSON)",
35  self::MENU_LIST => "Admin::Text Management::Import::CSV/JSON Import",
36  self::REQUIRES_LOGIN => true,
37  self::PERMISSION => Auth::PERM_ADMIN
38  ));
40  $this->customTextImport = $GLOBALS['container']->get('app.custom_text_import');
41  }
42 
47  protected function handle(Request $request)
48  {
49  $vars = array();
50 
51  if ($request->isMethod('POST')) {
52  $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
53  $delimiter = $request->get('delimiter') ?: ',';
54  $enclosure = $request->get('enclosure') ?: '"';
55  $vars['message'] = $this->handleFileUpload($uploadFile, $delimiter, $enclosure)[1];
56  }
57 
58  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
59  $vars['baseUrl'] = $request->getBaseUrl();
60  $vars['custom_text_import'] = true;
61 
62  return $this->render("admin_custom_text_import.html.twig", $this->mergeWithDefault($vars));
63  }
64 
69  public function handleFileUpload($uploadedFile, $delimiter = ',', $enclosure = '"')
70  {
71  $errMsg = '';
72  if (!($uploadedFile instanceof UploadedFile)) {
73  $errMsg = _("No file selected");
74  } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
75  $errMsg = $uploadedFile->getErrorMessage();
76  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
77  $errMsg = _("Larger than upload_max_filesize ") .
78  ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
79  } elseif ($uploadedFile->getSize() > self::MAX_IMPORT_BYTES) {
80  $errMsg = _("File exceeds maximum allowed size of 5 MB");
81  } elseif ($uploadedFile->getClientOriginalExtension() != 'csv'
82  && $uploadedFile->getClientOriginalExtension() != 'json') {
83  $errMsg = _('Invalid file extension ') .
84  $uploadedFile->getClientOriginalExtension() . ' of file ' .
85  $uploadedFile->getClientOriginalName();
86  }
87  if (!empty($errMsg)) {
88  return array(false, $errMsg, 400);
89  }
90  $this->customTextImport->setDelimiter($delimiter);
91  $this->customTextImport->setEnclosure($enclosure);
92 
93  return array(true, $this->customTextImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension()), 200);
94  }
95 }
96 
97 register_plugin(new AdminCustomTextImport());
Import custom text phrases from CSV/JSON.
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.
handleFileUpload($uploadedFile, $delimiter=',', $enclosure='"')