FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fo_import_licenses.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
10 require_once("$MODDIR/lib/php/common-cli.php");
11 cli_Init();
12 require_once("$MODDIR/lib/php/common-users.php");
13 
14 error_reporting(E_ALL);
15 
16 $usage = "Usage: " . basename($argv[0]) . " [options]
17  --username = admin/user with license-admin permissions
18  --password = password
19  --delimiter = delimiter, default is ','
20  --enclosure = enclosure, default is '\"'
21  --csv = csv file to import
22 ";
23 $opts = getopt("h", array('username:', 'password:', 'delimiter:', 'enclosure:', "csv:"));
24 
25 if (array_key_exists('h',$opts)) {
26  print "$usage\n";
27  return 0;
28 }
29 
30 if (!array_key_exists('csv',$opts)) {
31  print "no input file given\n";
32  print "$usage\n";
33  return 0;
34 } else {
35  $filename = $opts['csv'];
36 }
37 
38 $username = array_key_exists("username", $opts) ? $opts["username"] : null;
39 $passwd = array_key_exists("password", $opts) ? $opts["password"] : null;
40 
41 $delimiter = array_key_exists("delimiter", $opts) ? $opts["delimiter"] : ',';
42 $enclosure = array_key_exists("enclosure", $opts) ? $opts["enclosure"] : '"';
43 
44 if (!account_check($username, $passwd, $group)) {
45  print "Fossology login failure\n";
46  return 2;
47 } else {
48  print "Logged in as user $username\n";
49 }
50 
52 $userDao = $GLOBALS['container']->get("dao.user");
53 $adminRow = $userDao->getUserByName($username);
54 if ($adminRow["user_perm"] < PLUGIN_DB_ADMIN) {
55  print "You have no permission to admin the licenses\n";
56  return 1;
57 }
58 
59 print "importing\n";
61 $licenseCsvImport = $GLOBALS['container']->get('app.license_csv_import');
62 $licenseCsvImport->setDelimiter($delimiter);
63 $licenseCsvImport->setEnclosure($enclosure);
64 $import = $licenseCsvImport->handleFile($filename);
65 
66 if ($import !== null) {
67  print $import;
68  print "\n";
69 }
70 
71 print "done\n";
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:75
cli_Init()
Initialize the fossology environment for CLI use. This routine loads the plugins so they can be use b...
Definition: common-cli.php:25
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39