3 * SPDX-License-Identifier: GPL-2.0-only
5 * This file is part of Nominatim. (https://nominatim.org)
7 * Copyright (C) 2022 by the Nominatim developer community.
8 * For a full list of authors see the git log.
11 require_once(CONST_LibDir.'/Shell.php');
13 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
18 foreach ($aSpec as $aLine) {
19 if (is_array($aLine)) {
21 $aQuick['--'.$aLine[0]] = $aLine;
24 $aQuick['-'.$aLine[1]] = $aLine;
26 $aCounts[$aLine[0]] = 0;
32 $iSize = count($aArg);
33 for ($i = 1; $i < $iSize; $i++) {
34 if (isset($aQuick[$aArg[$i]])) {
35 $aLine = $aQuick[$aArg[$i]];
36 $aCounts[$aLine[0]]++;
38 if ($aLine[4] == $aLine[5]) {
41 for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
43 if ($i >= $iSize || $aArg[$i][0] == '-') {
44 showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
49 $xVal[] = realpath($aArg[$i]);
52 $sPath = realpath(dirname($aArg[$i]));
54 $xVal[] = $sPath . '/' . basename($aArg[$i]);
60 $xVal[] = (bool)$aArg[$i];
63 $xVal[] = (int)$aArg[$i];
66 $xVal[] = (float)$aArg[$i];
80 fail('Variable numbers of params not yet supported');
84 if (!array_key_exists($aLine[0], $aResult)) {
85 $aResult[$aLine[0]] = array();
87 $aResult[$aLine[0]][] = $xVal;
89 $aResult[$aLine[0]] = $xVal;
92 $bUnknown = $aArg[$i];
96 if (array_key_exists('help', $aResult)) {
99 if ($bUnknown && $bExitOnUnknown) {
100 showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
103 foreach ($aSpec as $aLine) {
104 if (is_array($aLine)) {
105 if ($aCounts[$aLine[0]] < $aLine[2]) {
106 showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is missing');
108 if ($aCounts[$aLine[0]] > $aLine[3]) {
109 showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
111 if ($aLine[6] == 'bool' && !array_key_exists($aLine[0], $aResult)) {
112 $aResult[$aLine[0]] = false;
119 function showUsage($aSpec, $bExit = false, $sError = false)
122 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
123 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
126 echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
128 foreach ($aSpec as $aLine) {
129 if (is_array($aLine)) {
136 $aNames[] = '-'.$aLine[1];
139 $aNames[] = '--'.$aLine[0];
141 $sName = join(', ', $aNames);
142 echo ' '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
153 echo date('Y-m-d H:i:s == ').$sMsg."\n";
156 $aWarnings = array();
161 $GLOBALS['aWarnings'][] = $sMsg;
162 echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
166 function repeatWarnings()
168 foreach ($GLOBALS['aWarnings'] as $sMsg) {
169 echo ' * ',$sMsg."\n";
174 function setupHTTPProxy()
176 if (!getSettingBool('HTTP_PROXY')) {
180 $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
183 $sLogin = getSetting('HTTP_PROXY_LOGIN');
184 $sPassword = getSetting('HTTP_PROXY_PASSWORD');
186 if ($sLogin && $sPassword) {
187 $sAuth = base64_encode($sLogin.':'.$sPassword);
188 $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
191 $aProxyHeader = array(
193 'request_fulluri' => true,
194 'header' => $aHeaders
197 $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
198 stream_context_set_default($aContext);