FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
fo_import_licenses.php
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 
20 
21 require_once("$MODDIR/lib/php/common-cli.php");
22 cli_Init();
23 require_once("$MODDIR/lib/php/common-users.php");
24 
25 error_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 
36 if (array_key_exists('h',$opts)) {
37  print "$usage\n";
38  return 0;
39 }
40 
41 if (!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 
55 if (!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 
63 $userDao = $GLOBALS['container']->get("dao.user");
64 $adminRow = $userDao->getUserByName($username);
65 if ($adminRow["user_perm"] < PLUGIN_DB_ADMIN) {
66  print "You have no permission to admin the licenses\n";
67  return 1;
68 }
69 
70 print "importing\n";
72 $licenseCsvImport = $GLOBALS['container']->get('app.license_csv_import');
73 $licenseCsvImport->setDelimiter($delimiter);
74 $licenseCsvImport->setEnclosure($enclosure);
75 $import = $licenseCsvImport->handleFile($filename);
76 
77 if ($import !== null) {
78  print $import;
79  print "\n";
80 }
81 
82 print "done\n";
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:87
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:51
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:36