FOSSology  4.4.0
Open Source License Compliance by Open Source Software
fossinit-common.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
15 function guessSysconfdir()
16 {
17  $rcfile = "fossology.rc";
18  $varfile = dirname(__DIR__).'/variable.list';
19  $sysconfdir = getenv('SYSCONFDIR');
20  if ((false===$sysconfdir) && file_exists($rcfile))
21  {
22  $sysconfdir = file_get_contents($rcfile);
23  }
24  if ((false===$sysconfdir) && file_exists($varfile))
25  {
26  $ini_array = parse_ini_file($varfile);
27  if($ini_array!==false && array_key_exists('SYSCONFDIR', $ini_array))
28  {
29  $sysconfdir = $ini_array['SYSCONFDIR'];
30  }
31  }
32  if (false===$sysconfdir)
33  {
34  $text = _("FATAL! System Configuration Error, no SYSCONFDIR.");
35  echo "$text\n";
36  exit(1);
37  }
38  return $sysconfdir;
39 }
40 
41 
58 function bootstrap($sysconfdir="")
59 {
60  if (empty($sysconfdir))
61  {
62  $sysconfdir = guessSysconfdir();
63  echo "assuming SYSCONFDIR=$sysconfdir\n";
64  }
65 
66  $sysconfdir = trim($sysconfdir);
67  $GLOBALS['SYSCONFDIR'] = $sysconfdir;
68 
69  /************* Parse fossology.conf *******************/
70  $ConfFile = "{$sysconfdir}/fossology.conf";
71  if (!file_exists($ConfFile))
72  {
73  $text = _("FATAL! Missing configuration file: $ConfFile");
74  echo "$text\n";
75  exit(1);
76  }
77  $SysConf = parse_ini_file($ConfFile, true);
78  if ($SysConf === false)
79  {
80  $text = _("FATAL! Invalid configuration file: $ConfFile");
81  echo "$text\n";
82  exit(1);
83  }
84 
85  /* evaluate all the DIRECTORIES group for variable substitutions.
86  * For example, if PREFIX=/usr/local and BINDIR=$PREFIX/bin, we
87  * want BINDIR=/usr/local/bin
88  */
89  foreach($SysConf['DIRECTORIES'] as $var=>$assign)
90  {
91  $toeval = "\$$var = \"$assign\";";
92  eval($toeval);
93 
94  /* now reassign the array value with the evaluated result */
95  $SysConf['DIRECTORIES'][$var] = ${$var};
96  $GLOBALS[$var] = ${$var};
97  }
98 
99  if (empty($MODDIR))
100  {
101  $text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf");
102  echo "$text\n";
103  exit(1);
104  }
105 
106  //require("i18n.php"); DISABLED until i18n infrastructure is set-up.
107  require_once("$MODDIR/lib/php/common.php");
108  require_once("$MODDIR/lib/php/Plugin/FO_Plugin.php");
109  return $SysConf;
110 }
111 
121 function readlineTimeout($seconds, $default)
122 {
123  return trim(shell_exec('bash -c ' .
124  escapeshellarg('fossstdin=' . escapeshellarg($default) .
125  ';read -t ' . ((int)$seconds) . ' fossstdin;echo "$fossstdin"')));
126 }
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
bootstrap($sysconfdir="")
Determine SYSCONFDIR, parse fossology.conf.
readlineTimeout($seconds, $default)
Using bash's read command, read input from STDIN.