FOSSology  4.7.1
Open Source License Compliance by Open Source Software
fo_dbcheck.php
Go to the documentation of this file.
1 #!/usr/bin/php
2 <?php
3 /*
4  SPDX-FileCopyrightText: © 2011 Hewlett-Packard Development Company, L.P.
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
16 /* Initialize the program configuration variables */
17 $SysConf = array(); // fo system configuration variables
18 $PG_CONN = 0; // Database connection
19 $Plugins = array();
20 
21 /* Note: php 5 getopt() ignores options not specified in the function call, so add
22  * dummy options in order to catch invalid options.
23  */
24 $AllPossibleOpts = "abc:defghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
25 
26 /* defaults */
27 $Verbose = false;
28 $DatabaseName = "fossology";
29 $UpdateLiceneseRef = false;
30 $sysconfdir = '';
31 
32 
40 if (!function_exists('resolve_config_value')) {
41  function resolve_config_value($value, array $scope)
42  {
43  return preg_replace_callback('/\$(?:([A-Za-z_][A-Za-z0-9_]*)|\{([A-Za-z_][A-Za-z0-9_]*)\})/', static function ($matches) use ($scope) {
44  $name = !empty($matches[1]) ? $matches[1] : $matches[2];
45  return array_key_exists($name, $scope) ? $scope[$name] : $matches[0];
46  }, $value);
47  }
48 }
49 
50 /* command-line options */
51 $Options = getopt($AllPossibleOpts);
52 foreach($Options as $Option => $OptVal)
53 {
54  switch($Option)
55  {
56  case 'c': /* set SYSCONFIDR */
57  $sysconfdir = $OptVal;
58  break;
59  default:
60  echo "Invalid Option \"$Option\".\n";
61  Usage();
62  }
63 }
64 
65 /* Set SYSCONFDIR and set global (for backward compatibility) */
66 $SysConf = bootstrap($sysconfdir);
67 
68 /* Initialize global system configuration variables $SysConfig[] */
69 ConfigInit($SYSCONFDIR, $SysConf);
70 
71 $rv = DBconnect($sysconfdir, "", false);
72 
73 if ($rv === false) exit(1);
74 
75 exit(0);
76 
77 
81 function Usage()
82 {
83  global $argv;
84 
85  $usage = "Usage: " . basename($argv[0]) . " [options]
86  Update FOSSology database. Options are:
87  -c fossology system configuration directory
88  -h this help usage";
89  print "$usage\n";
90  exit(0);
91 }
92 
93 
94 /************************************************/
95 /****** From src/lib/php/bootstrap.php ********/
124 function bootstrap($sysconfdir="")
125 {
126  $rcfile = "fossology.rc";
127 
128  if (empty($sysconfdir))
129  {
130  $sysconfdir = getenv('SYSCONFDIR');
131  if ($sysconfdir === false)
132  {
133  if (file_exists($rcfile)) $sysconfdir = file_get_contents($rcfile);
134  if ($sysconfdir === false)
135  {
136  /* NO SYSCONFDIR specified */
137  $text = _("FATAL! System Configuration Error, no SYSCONFDIR.");
138  echo "$text\n";
139  exit(1);
140  }
141  }
142  }
143 
144  $sysconfdir = trim($sysconfdir);
145  $GLOBALS['SYSCONFDIR'] = $sysconfdir;
146 
147  /************* Parse fossology.conf *******************/
148  $ConfFile = "{$sysconfdir}/fossology.conf";
149  if (!file_exists($ConfFile))
150  {
151  $text = _("FATAL! Missing configuration file: $ConfFile");
152  echo "$text\n";
153  exit(1);
154  }
155  $SysConf = parse_ini_file($ConfFile, true);
156  if ($SysConf === false)
157  {
158  $text = _("FATAL! Invalid configuration file: $ConfFile");
159  echo "$text\n";
160  exit(1);
161  }
162 
163  /* evaluate all the DIRECTORIES group for variable substitutions.
164  * For example, if PREFIX=/usr/local and BINDIR=$PREFIX/bin, we
165  * want BINDIR=/usr/local/bin
166  */
167  $resolvedValues = array();
168  foreach($SysConf['DIRECTORIES'] as $var=>$assign)
169  {
170  $resolvedValue = resolve_config_value($assign, $resolvedValues);
171  $resolvedValues[$var] = $resolvedValue;
172 
173  /* now reassign the array value with the evaluated result */
174  $SysConf['DIRECTORIES'][$var] = $resolvedValue;
175  ${$var} = $resolvedValue;
176  $GLOBALS[$var] = $resolvedValue;
177  }
178 
179  if (empty($MODDIR))
180  {
181  $text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf");
182  echo $text. "\n";
183  exit(1);
184  }
185 
186  //require("i18n.php"); DISABLED until i18n infrastructure is set-up.
187  //require_once("$MODDIR/www/ui/template/template-plugin.php"); DISABLED as don't needed
188  require_once("$MODDIR/lib/php/common.php");
189  return $SysConf;
190 }
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:33
if(!function_exists('resolve_sysconfig_value')) ConfigInit($sysconfdir, &$SysConf, $exitOnDbFail=true)
Initialize the fossology system after bootstrap().
bootstrap($sysconfdir="")
Bootstrap the fossology php library.
Definition: fo_dbcheck.php:124
Usage()
Print Usage statement.
Definition: fo_dbcheck.php:81
if(!function_exists('resolve_config_value')) $Options
Definition: fo_dbcheck.php:51
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN