1#!/usr/bin/php -q
2<?php
3/***********************************************************
4 Copyright (C) 2011-2012 Hewlett-Packard Development Company, L.P.
5 Copyright (C) 2015 Siemens AG
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 version 2 as published by the Free Software Foundation.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 ***********************************************************/
20//error_reporting( E_ALL | E_STRICT );
21/**
22 * @file fo_wrapper.php
23 * @brief Bootstrap into the fossology system and then
24 *        include arg[0].  You must be in group fossy.
25 *
26 * For example, symlink fo_schema_export to fo_wrapper.php
27 * fo_wrapper.php will bootstrap followed by including the
28 * code from fo_schema_export.php
29 *
30 * @return 0 for success, 1 for failure.
31 **/
32
33/* Initialize the program configuration variables */
34$SysConf = array();  // fo system configuration variables
35$PG_CONN = 0;   // Database connection
36$Plugins = array();
37
38/* Set SYSCONFDIR and set global (for backward compatibility) */
39/** get sysconfdir */
40$sysconfdir = "";
41for ($i = 1;$i < $argc;$i++)
42{
43  $arg = $argv[$i];
44  if ("-c" === $arg) {
45    $sysconfdir = $argv[$i + 1];
46    break;
47  }
48}
49$SysConf = bootstrap($sysconfdir);
50
51/* Initialize global system configuration variables $SysConfig[] */
52ConfigInit($SYSCONFDIR, $SysConf);
53
54/* User must be in group fossy or what configured in fossology.conf */
55$projectGroup = $SysConf['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy';
56$gInfo = posix_getgrnam($projectGroup);
57posix_setgid($gInfo['gid']);
58$groups = `groups`;
59if (!preg_match("/\s$projectGroup\s/",$groups) && (posix_getgid() != $gInfo['gid']))
60{
61  print "FATAL: You must be in group '$projectGroup'.\n";
62  exit(1);
63}
64
65require_once("$MODDIR/lib/php/libschema.php");
66
67/* Locations where the target .php file might exist */
68$basename = basename($argv[0]);
69$dirname = dirname($argv[0]);
70$FO_phpLocations = "$BINDIR"
71                    . PATH_SEPARATOR . "$dirname"
72                    . PATH_SEPARATOR . "$SBINDIR"
73                    . PATH_SEPARATOR . "$LIBEXECDIR";
74$IncludePath = set_include_path(get_include_path() . PATH_SEPARATOR . $FO_phpLocations);
75
76try {
77  require("$basename" . ".php");
78}
79catch (Exception $e ) {
80  print $e->getMessage();
81  var_dump($e->getTraceAsString());
82}
83
84exit(0);
85
86/**
87 * \brief Bootstrap the fossology php library.
88 *  - Determine SYSCONFDIR
89 *  - parse fossology.conf
90 *  - source template (require_once template-plugin.php)
91 *  - source common files (require_once common.php)
92 *
93 * The following precedence is used to resolve SYSCONFDIR:
94 *  - $SYSCONFDIR path passed in ($sysconfdir)
95 *  - environment variable SYSCONFDIR
96 *  - ./fossology.rc
97 *
98 * Any errors are fatal.  A text message will be printed followed by an exit(1)
99 *
100 * \param $sysconfdir Typically from the caller's -c command line parameter
101 *
102 * \return the $SysConf array of values.  The first array dimension
103 * is the group, the second is the variable name.
104 * For example:
105 *  -  $SysConf[DIRECTORIES][MODDIR] => "/mymoduledir/
106 *
107 * The global $SYSCONFDIR is also set for backward compatibility.
108 *
109 * \Note Since so many files expect directory paths that used to be in pathinclude.php
110 * to be global, this function will define the same globals (everything in the
111 * DIRECTORIES section of fossology.conf).
112 */
113function bootstrap($sysconfdir="")
114{
115  error_reporting(E_ERROR | E_WARNING | E_PARSE);
116
117  $rcfile = "fossology.rc";
118  if (empty($sysconfdir))
119  {
120    $sysconfdir = getenv('SYSCONFDIR');
121    if ($sysconfdir === false)
122    {
123      if (file_exists($rcfile)) $sysconfdir = file_get_contents($rcfile);
124      if ($sysconfdir === false)
125      {
126        /** set the sysconfdir with default */
127        $sysconfdir = "{$SYSCONFDIR}";
128      }
129    }
130  }
131
132  $sysconfdir = trim($sysconfdir);
133  $GLOBALS['SYSCONFDIR'] = $sysconfdir;
134
135  /*************  Parse fossology.conf *******************/
136  $ConfFile = "{$sysconfdir}/fossology.conf";
137  if (!file_exists($ConfFile))
138  {
139    $text = _("FATAL! Missing configuration file: $ConfFile");
140    echo "$text\n";
141    exit(1);
142  }
143  $SysConf = parse_ini_file($ConfFile, true);
144  if ($SysConf === false)
145  {
146    $text = _("FATAL! Invalid configuration file: $ConfFile");
147    echo "$text\n";
148    exit(1);
149  }
150
151  /* evaluate all the DIRECTORIES group for variable substitutions.
152   * For example, if PREFIX=/usr/local and BINDIR=$PREFIX/bin, we
153   * want BINDIR=/usr/local/bin
154   */
155  foreach($SysConf['DIRECTORIES'] as $var=>$assign)
156  {
157    /* Evaluate the individual variables because they may be referenced
158     * in subsequent assignments.
159     */
160    $toeval = "\$$var = \"$assign\";";
161    eval($toeval);
162
163    /* now reassign the array value with the evaluated result */
164    $SysConf['DIRECTORIES'][$var] = ${$var};
165    $GLOBALS[$var] = ${$var};
166  }
167
168  if (empty($MODDIR))
169  {
170    $text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf");
171    echo $text. "\n";
172    exit(1);
173  }
174
175  require_once("$MODDIR/lib/php/common.php");
176  require_once("$MODDIR/lib/php/Plugin/FO_Plugin.php");
177  return $SysConf;
178}
179