1<?php 2/*********************************************************** 3 Copyright (C) 2019 Siemens AG 4 Author: Gaurav Mishra <mishra.gaurav@siemens.com> 5 6 This program is free software; you can redistribute it and/or 7 modify it under the terms of the GNU General Public License 8 version 2 as published by the Free Software Foundation. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License along 16 with this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 18 ***********************************************************/ 19 20/** 21 * @file 22 * @brief Common functions required by init scripts 23 */ 24 25 26function guessSysconfdir() 27{ 28 $rcfile = "fossology.rc"; 29 $varfile = dirname(__DIR__).'/variable.list'; 30 $sysconfdir = getenv('SYSCONFDIR'); 31 if ((false===$sysconfdir) && file_exists($rcfile)) 32 { 33 $sysconfdir = file_get_contents($rcfile); 34 } 35 if ((false===$sysconfdir) && file_exists($varfile)) 36 { 37 $ini_array = parse_ini_file($varfile); 38 if($ini_array!==false && array_key_exists('SYSCONFDIR', $ini_array)) 39 { 40 $sysconfdir = $ini_array['SYSCONFDIR']; 41 } 42 } 43 if (false===$sysconfdir) 44 { 45 $text = _("FATAL! System Configuration Error, no SYSCONFDIR."); 46 echo "$text\n"; 47 exit(1); 48 } 49 return $sysconfdir; 50} 51 52 53/** 54 * \brief Determine SYSCONFDIR, parse fossology.conf 55 * 56 * \param $sysconfdir Typically from the caller's -c command line parameter 57 * 58 * \return the $SysConf array of values. The first array dimension 59 * is the group, the second is the variable name. 60 * For example: 61 * - $SysConf[DIRECTORIES][MODDIR] => "/mymoduledir/ 62 * 63 * The global $SYSCONFDIR is also set for backward compatibility. 64 * 65 * \Note Since so many files expect directory paths that used to be in pathinclude.php 66 * to be global, this function will define the same globals (everything in the 67 * DIRECTORIES section of fossology.conf). 68 */ 69function bootstrap($sysconfdir="") 70{ 71 if (empty($sysconfdir)) 72 { 73 $sysconfdir = guessSysconfdir(); 74 echo "assuming SYSCONFDIR=$sysconfdir\n"; 75 } 76 77 $sysconfdir = trim($sysconfdir); 78 $GLOBALS['SYSCONFDIR'] = $sysconfdir; 79 80 /************* Parse fossology.conf *******************/ 81 $ConfFile = "{$sysconfdir}/fossology.conf"; 82 if (!file_exists($ConfFile)) 83 { 84 $text = _("FATAL! Missing configuration file: $ConfFile"); 85 echo "$text\n"; 86 exit(1); 87 } 88 $SysConf = parse_ini_file($ConfFile, true); 89 if ($SysConf === false) 90 { 91 $text = _("FATAL! Invalid configuration file: $ConfFile"); 92 echo "$text\n"; 93 exit(1); 94 } 95 96 /* evaluate all the DIRECTORIES group for variable substitutions. 97 * For example, if PREFIX=/usr/local and BINDIR=$PREFIX/bin, we 98 * want BINDIR=/usr/local/bin 99 */ 100 foreach($SysConf['DIRECTORIES'] as $var=>$assign) 101 { 102 $toeval = "\$$var = \"$assign\";"; 103 eval($toeval); 104 105 /* now reassign the array value with the evaluated result */ 106 $SysConf['DIRECTORIES'][$var] = ${$var}; 107 $GLOBALS[$var] = ${$var}; 108 } 109 110 if (empty($MODDIR)) 111 { 112 $text = _("FATAL! System initialization failure: MODDIR not defined in $SysConf"); 113 echo "$text\n"; 114 exit(1); 115 } 116 117 //require("i18n.php"); DISABLED until i18n infrastructure is set-up. 118 require_once("$MODDIR/lib/php/common.php"); 119 require_once("$MODDIR/lib/php/Plugin/FO_Plugin.php"); 120 return $SysConf; 121} 122 123/** 124 * @brief Using bash's read command, read input from STDIN. 125 * 126 * This function runs a new bash shell and execute read command on it with a 127 * timeout set. 128 * @param integer $seconds Timeout in seconds 129 * @param string $default Default value to return (in case of timeout) 130 * @return string The input read from STDIN or default value. 131 */ 132function readlineTimeout($seconds, $default) 133{ 134 return trim(shell_exec('bash -c ' . 135 escapeshellarg('fossstdin=' . escapeshellarg($default) . 136 ';read -t ' . ((int)$seconds) . ' fossstdin;echo "$fossstdin"'))); 137} 138