FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdminLicenseFromCSV.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014 Siemens AG
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_license_from_csv";
24  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
25  const FILE_INPUT_NAME = 'file_input';
26 
27  function __construct()
28  {
29  parent::__construct(self::NAME, array(
30  self::TITLE => "Admin License CSV Import",
31  self::MENU_LIST => "Admin::License Admin::CSV Import",
32  self::REQUIRES_LOGIN => true,
33  self::PERMISSION => Auth::PERM_ADMIN
34  ));
35  }
36 
41  protected function handle (Request $request)
42  {
43  $vars = array();
44 
45  if ($request->isMethod('POST')) {
46  $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
47  $delimiter = $request->get('delimiter') ?: ',';
48  $enclosure = $request->get('enclosure') ?: '"';
49  $vars['message'] = $this->handleFileUpload($uploadFile, $delimiter,
50  $enclosure)[1];
51  }
52 
53  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
54  $vars['baseUrl'] = $request->getBaseUrl();
55  $vars['license_csv_import'] = true;
56 
57  return $this->render("admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
58  }
59 
60 
65  public function handleFileUpload($uploadedFile,$delimiter=',',$enclosure='"')
66  {
67  $errMsg = '';
68  if (! ($uploadedFile instanceof UploadedFile)) {
69  $errMsg = _("No file selected");
70  } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
71  $errMsg = $uploadedFile->getErrorMessage();
72  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
73  $errMsg = _("Larger than upload_max_filesize ") .
74  ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
75  } elseif ($uploadedFile->getClientOriginalExtension() != 'csv') {
76  $errMsg = _('Invalid extension ') .
77  $uploadedFile->getClientOriginalExtension() . ' of file ' .
78  $uploadedFile->getClientOriginalName();
79  }
80  if (! empty($errMsg)) {
81  return array(false, $errMsg,400);
82  }
84  $licenseCsvImport = $this->getObject('app.license_csv_import');
85  $licenseCsvImport->setDelimiter($delimiter);
86  $licenseCsvImport->setEnclosure($enclosure);
87 
88  return array(true,$licenseCsvImport->handleFile($uploadedFile->getRealPath()),200);
89  }
90 
94  public function getFileInputName()
95  {
96  return $this::FILE_INPUT_NAME;
97  }
98 }
99 
100 register_plugin(new AdminLicenseFromCSV());
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.