FOSSology  4.7.1
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 
22 if (!function_exists('resolve_config_value')) {
23  function resolve_config_value($value, array $scope)
24  {
25  return preg_replace_callback('/\$(?:([A-Za-z_][A-Za-z0-9_]*)|\{([A-Za-z_][A-Za-z0-9_]*)\})/', static function ($matches) use ($scope) {
26  $name = !empty($matches[1]) ? $matches[1] : $matches[2];
27  return array_key_exists($name, $scope) ? $scope[$name] : $matches[0];
28  }, $value);
29  }
30 }
31 
32 
33 function guessSysconfdir()
34 {
35  $rcfile = "fossology.rc";
36  $varfile = dirname(__DIR__).'/variable.list';
37  $sysconfdir = getenv('SYSCONFDIR');
38  if ((false===$sysconfdir) && file_exists($rcfile))
39  {
40  $sysconfdir = file_get_contents($rcfile);
41  }
42  if ((false===$sysconfdir) && file_exists($varfile))
43  {
44  $ini_array = parse_ini_file($varfile);
45  if($ini_array!==false && array_key_exists('SYSCONFDIR', $ini_array))
46  {
47  $sysconfdir = $ini_array['SYSCONFDIR'];
48  }
49  }
50  if (false===$sysconfdir)
51  {
52  $text = _("FATAL! System Configuration Error, no SYSCONFDIR.");
53  echo "$text\n";
54  exit(1);
55  }
56  return $sysconfdir;
57 }
58 
59 
76 function bootstrap($sysconfdir="")
77 {
78  if (empty($sysconfdir))
79  {
80  $sysconfdir = guessSysconfdir();
81  echo "assuming SYSCONFDIR=$sysconfdir\n";
82  }
83 
84  $sysconfdir = trim($sysconfdir);
85  $GLOBALS['SYSCONFDIR'] = $sysconfdir;
86 
87  /************* Parse fossology.conf *******************/
88  $ConfFile = "{$sysconfdir}/fossology.conf";
89  if (!file_exists($ConfFile))
90  {
91  $text = _("FATAL! Missing configuration file: $ConfFile");
92  echo "$text\n";
93  exit(1);
94  }
95  $SysConf = parse_ini_file($ConfFile, true);
96  if ($SysConf === false)
97  {
98  $text = _("FATAL! Invalid configuration file: $ConfFile");
99  echo "$text\n";
100  exit(1);
101  }
102 
103  /* evaluate all the DIRECTORIES group for variable substitutions.
104  * For example, if PREFIX=/usr/local and BINDIR=$PREFIX/bin, we
105  * want BINDIR=/usr/local/bin
106  */
107  $resolvedValues = array();
108  foreach($SysConf['DIRECTORIES'] as $var=>$assign)
109  {
110  $resolvedValue = resolve_config_value($assign, $resolvedValues);
111  $resolvedValues[$var] = $resolvedValue;
112 
113  /* now reassign the array value with the evaluated result */
114  $SysConf['DIRECTORIES'][$var] = $resolvedValue;
115  ${$var} = $resolvedValue;
116  $GLOBALS[$var] = $resolvedValue;
117  }
118 
119  if (empty($MODDIR))
120  {
121  $text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf");
122  echo "$text\n";
123  exit(1);
124  }
125 
126  //require("i18n.php"); DISABLED until i18n infrastructure is set-up.
127  require_once("$MODDIR/lib/php/common.php");
128  require_once("$MODDIR/lib/php/Plugin/FO_Plugin.php");
129  return $SysConf;
130 }
131 
141 function readlineTimeout($seconds, $default)
142 {
143  return trim(shell_exec('bash -c ' .
144  escapeshellarg('fossstdin=' . escapeshellarg($default) .
145  ';read -t ' . ((int)$seconds) . ' fossstdin;echo "$fossstdin"')));
146 }
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.
if(!function_exists('resolve_config_value')) guessSysconfdir()