FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AdminObligationFromCSV.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2008-2013 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2014-2017 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_obligation_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 Obligation CSV Import",
31  self::MENU_LIST => "Admin::Obligation 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,$enclosure);
50  }
51 
52  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
53  $vars['baseUrl'] = $request->getBaseUrl();
54  $vars['license_csv_import'] = false;
55 
56  return $this->render("admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
57  }
58 
63  public function handleFileUpload($uploadedFile,$delimiter=',',$enclosure='"', $fromRest=false)
64  {
65  $errMsg = '';
66  if (! ($uploadedFile instanceof UploadedFile)) {
67  $errMsg = _("No file selected");
68  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
69  $errMsg = _("Larger than upload_max_filesize ") . ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
70  } elseif ($uploadedFile->getClientOriginalExtension()!='csv') {
71  $errMsg = _('Invalid extension ') .
72  $uploadedFile->getClientOriginalExtension() . ' of file ' .
73  $uploadedFile->getClientOriginalName();
74  }
75  if (! empty($errMsg)) {
76  if ($fromRest) {
77  return array(false, $errMsg, 400);
78  }
79  return $errMsg;
80  }
82  $obligationCsvImport = $this->getObject('app.obligation_csv_import');
83  $obligationCsvImport->setDelimiter($delimiter);
84  $obligationCsvImport->setEnclosure($enclosure);
85 
86  $res = $obligationCsvImport->handleFile($uploadedFile->getRealPath());
87  if ($fromRest) {
88  return array(true, $res, 200);
89  }
90  return $res;
91  }
92 
96  public function getFileInputName()
97  {
98  return $this::FILE_INPUT_NAME;
99  }
100 }
101 
102 register_plugin(new AdminObligationFromCSV());
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.