FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AdminLicenseFromCSV.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2008-2013 Hewlett-Packard Development Company, L.P.
4  * Copyright (C) 2014 Siemens AG
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  ***********************************************************/
19 
20 namespace Fossology\UI\Page;
21 
28 
33 {
34  const NAME = "admin_license_from_csv";
35  const KEY_UPLOAD_MAX_FILESIZE = 'upload_max_filesize';
36 
37  function __construct()
38  {
39  parent::__construct(self::NAME, array(
40  self::TITLE => "Admin License CSV Import",
41  self::MENU_LIST => "Admin::License Admin::CSV Import",
42  self::REQUIRES_LOGIN => true,
43  self::PERMISSION => Auth::PERM_ADMIN
44  ));
45  }
46 
51  protected function handle (Request $request)
52  {
53  $vars = array();
54 
55  if ($request->isMethod('POST')) {
56  $uploadFile = $request->files->get('file_input');
57  $delimiter = $request->get('delimiter') ?: ',';
58  $enclosure = $request->get('enclosure') ?: '"';
59  $vars['message'] = $this->handleFileUpload($uploadFile, $delimiter,
60  $enclosure);
61  }
62 
63  $vars[self::KEY_UPLOAD_MAX_FILESIZE] = ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
64  $vars['baseUrl'] = $request->getBaseUrl();
65 
66  return $this->render("admin_license_from_csv.html.twig", $this->mergeWithDefault($vars));
67  }
68 
73  protected function handleFileUpload($uploadedFile,$delimiter=',',$enclosure='"')
74  {
75  $errMsg = '';
76  if (! ($uploadedFile instanceof UploadedFile)) {
77  $errMsg = _("No file selected");
78  } elseif ($uploadedFile->getError() !== UPLOAD_ERR_OK) {
79  $errMsg = $uploadedFile->getErrorMessage();
80  } elseif ($uploadedFile->getSize() == 0 && $uploadedFile->getError() == 0) {
81  $errMsg = _("Larger than upload_max_filesize ") .
82  ini_get(self::KEY_UPLOAD_MAX_FILESIZE);
83  } elseif ($uploadedFile->getClientOriginalExtension() != 'csv') {
84  $errMsg = _('Invalid extension ') .
85  $uploadedFile->getClientOriginalExtension() . ' of file ' .
86  $uploadedFile->getClientOriginalName();
87  }
88  if (! empty($errMsg)) {
89  return $errMsg;
90  }
92  $licenseCsvImport = $this->getObject('app.license_csv_import');
93  $licenseCsvImport->setDelimiter($delimiter);
94  $licenseCsvImport->setEnclosure($enclosure);
95  return $licenseCsvImport->handleFile($uploadedFile->getRealPath());
96  }
97 }
98 
99 register_plugin(new AdminLicenseFromCSV());
Upload a file from the users computer using the UI.
render($templateName, $vars=null, $headers=null)