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 
15 use Symfony\Component\HttpFoundation\File\UploadedFile;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpFoundation\Response;
18 
23 {
24  const NAME = "admin_obligation_from_csv";
25  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
26  const FILE_INPUT_NAME = 'file_input';
27  const FILE_INPUT_NAME_V2 = 'fileInput';
28 
29  function __construct()
30  {
31  parent::__construct(self::NAME, array(
32  self::TITLE => "Admin Obligation Import",
33  self::MENU_LIST => "Admin::Obligation Admin::Obligation Import",
34  self::REQUIRES_LOGIN => true,
35  self::PERMISSION => Auth::PERM_ADMIN
36  ));
37  }
38 
43  protected function handle (Request $request)
44  {
45  $vars = array();
46 
47  if ($request->isMethod('POST')) {
48  $uploadFile = $request->files->get(self::FILE_INPUT_NAME);
49  $delimiter = $request->get('delimiter')?:',';
50  $enclosure = $request->get('enclosure')?:'"';
51  $vars['message'] = $this->handleFileUpload($uploadFile,$delimiter,$enclosure);
52  }
53 
54  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
55  $vars['baseUrl'] = $request->getBaseUrl();
56  $vars['license_csv_import'] = false;
57 
58  return $this->render("admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
59  }
60 
65  public function handleFileUpload($uploadedFile,$delimiter=',',$enclosure='"', $fromRest=false)
66  {
67  $errMsg = '';
68  if (! ($uploadedFile instanceof UploadedFile)) {
69  $errMsg = _("No file selected");
70  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
71  $errMsg = _("Larger than upload_max_filesize ") . ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
72  } elseif ($uploadedFile->getClientOriginalExtension() != 'csv'
73  && $uploadedFile->getClientOriginalExtension() != 'json') {
74  $errMsg = _('Invalid extension ') .
75  $uploadedFile->getClientOriginalExtension() . ' of file ' .
76  $uploadedFile->getClientOriginalName();
77  }
78  if (! empty($errMsg)) {
79  if ($fromRest) {
80  return array(false, $errMsg, 400);
81  }
82  return $errMsg;
83  }
85  $obligationCsvImport = $this->getObject('app.obligation_csv_import');
86  $obligationCsvImport->setDelimiter($delimiter);
87  $obligationCsvImport->setEnclosure($enclosure);
88 
89  $res = $obligationCsvImport->handleFile($uploadedFile->getRealPath(), $uploadedFile->getClientOriginalExtension());
90  if ($fromRest) {
91  return array(true, $res, 200);
92  }
93  return $res;
94  }
95 
99  public function getFileInputName($apiVersion = ApiVersion::V1)
100  {
101  if ($apiVersion == ApiVersion::V2) {
102  return $this::FILE_INPUT_NAME_V2;
103  } else {
104  return $this::FILE_INPUT_NAME;
105  }
106  }
107 }
108 
109 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.