1<?php
2/***********************************************************
3 Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library 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 GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this library; if not, write to the Free Software Foundation, Inc.0
16 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
17***********************************************************/
18
19/**
20 * \file bootstrap.php
21 * \brief Fossology system bootstrap
22 * This file may be DUPLICATED in any php utility that needs to
23 * bootstrap itself.  But try to use fo_wrapper instead.
24 */
25
26/**
27 * \brief Bootstrap the fossology php library.
28 *  - Determine SYSCONFDIR
29 *  - parse fossology.conf
30 *  - source template (require_once template-plugin.php)
31 *  - source common files (require_once common.php)
32 *
33 * The following precedence is used to resolve SYSCONFDIR:
34 *  - SYSCONFDIR path passed in ($sysconfdir)
35 *  - environment variable SYSCONFDIR
36 *  - ./fossology.rc
37 *  - SYSCONFDIR  (defined at make time)
38 *
39 * Any errors are fatal.  A text message will be printed followed by an exit(1)
40 *
41 * \param $sysconfdir Typically from the caller's -c command line parameter
42 *
43 * \return the $SysConf array of values.  The first array dimension
44 * is the group, the second is the variable name.
45 * For example:
46 *  -  $SysConf[DIRECTORIES][MODDIR] => "/mymoduledir/
47 *
48 * The global SYSCONFDIR is also set for backward compatibility.
49 *
50 * \Note Since so many files expect directory paths that used to be in pathinclude.php
51 * to be global, this function will define the same globals (everything in the
52 * DIRECTORIES section of fossology.conf).
53 */
54function bootstrap($sysconfdir="")
55{
56  $GLOBALS["PG_CONN"] = NULL;
57  $rcfile = "fossology.rc";
58
59  if (empty($sysconfdir))
60  {
61    $sysconfdir = getenv('SYSCONFDIR');
62    if ($sysconfdir === false)
63    {
64      if (file_exists($rcfile)) $sysconfdir = file_get_contents($rcfile);
65      if ($sysconfdir === false)
66      {
67        $sysconfdir = "{$SYSCONFDIR}";
68      }
69    }
70  }
71
72  $sysconfdir = trim($sysconfdir);
73  $GLOBALS['SYSCONFDIR'] = $sysconfdir;
74
75  /*************  Parse fossology.conf *******************/
76  $ConfFile = "{$sysconfdir}/fossology.conf";
77  if (!file_exists($ConfFile))
78  {
79    $text = _("FATAL! Missing configuration file: $ConfFile");
80    echo "$text\n";
81    exit(1);
82  }
83  $SysConf = parse_ini_file($ConfFile, true);
84  if ($SysConf === false)
85  {
86    $text = _("FATAL! Invalid configuration file: $ConfFile");
87    echo "$text\n";
88    exit(1);
89  }
90
91  /* evaluate all the DIRECTORIES group for variable substitutions.
92   * For example, if PREFIX=/usr/local and BINDIR=PREFIX/bin, we
93   * want BINDIR=/usr/local/bin
94   */
95  foreach($SysConf['DIRECTORIES'] as $var=>$assign)
96  {
97    /* Evaluate the individual variables because they may be referenced
98     * in subsequent assignments.
99     */
100    $toeval = "\$$var = \"$assign\";";
101    eval($toeval);
102
103    /* now reassign the array value with the evaluated result */
104    $SysConf['DIRECTORIES'][$var] = ${$var};
105    $GLOBALS[$var] = ${$var};
106  }
107
108  $moddir = $SysConf['DIRECTORIES']['MODDIR'];
109  if (empty($moddir))
110  {
111    $text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf");
112    echo $text. "\n";
113    exit(1);
114  }
115
116  //require("i18n.php"); DISABLED until i18n infrastructure is set-up.
117  require_once("$moddir/lib/php/common.php");
118  require_once("$moddir/lib/php/Plugin/FO_Plugin.php");
119
120  return $SysConf;
121}
122