1<?php
2/***********************************************************
3 Copyright (C) 2015 Siemens AG
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17***********************************************************/
18
19use Fossology\Lib\Dao\UserDao;
20
21require_once("$MODDIR/lib/php/common-cli.php");
22cli_Init();
23require_once("$MODDIR/lib/php/common-users.php");
24
25error_reporting(E_ALL);
26
27$usage = "Usage: " . basename($argv[0]) . " [options]
28  --username  = admin/user with license-admin permissions
29  --password  = password
30  --delimiter = delimiter, default is ','
31  --enclosure = enclosure, default is '\"'
32  --csv       = csv file to import
33";
34$opts = getopt("h", array('username:', 'password:', 'delimiter:', 'enclosure:', "csv:"));
35
36if (array_key_exists('h',$opts)) {
37  print "$usage\n";
38  return 0;
39}
40
41if (!array_key_exists('csv',$opts)) {
42  print "no input file given\n";
43  print "$usage\n";
44  return 0;
45} else {
46  $filename = $opts['csv'];
47}
48
49$username = array_key_exists("username", $opts) ? $opts["username"] : null;
50$passwd = array_key_exists("password", $opts) ? $opts["password"] : null;
51
52$delimiter = array_key_exists("delimiter", $opts) ? $opts["delimiter"] : ',';
53$enclosure = array_key_exists("enclosure", $opts) ? $opts["enclosure"] : '"';
54
55if (!account_check($username, $passwd, $group)) {
56  print "Fossology login failure\n";
57  return 2;
58} else {
59  print "Logged in as user $username\n";
60}
61
62/** @var UserDao */
63$userDao = $GLOBALS['container']->get("dao.user");
64$adminRow = $userDao->getUserByName($username);
65if ($adminRow["user_perm"] < PLUGIN_DB_ADMIN) {
66  print "You have no permission to admin the licenses\n";
67  return 1;
68}
69
70print "importing\n";
71/** @var LicenseCsvImport */
72$licenseCsvImport = $GLOBALS['container']->get('app.license_csv_import');
73$licenseCsvImport->setDelimiter($delimiter);
74$licenseCsvImport->setEnclosure($enclosure);
75$import = $licenseCsvImport->handleFile($filename);
76
77if ($import !== null) {
78  print $import;
79  print "\n";
80}
81
82print "done\n";
83