]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/cmd.php
Merge pull request #3373 from lonvia/restrict-man-made
[nominatim.git] / lib-php / cmd.php
1 <?php
2 /**
3  * SPDX-License-Identifier: GPL-2.0-only
4  *
5  * This file is part of Nominatim. (https://nominatim.org)
6  *
7  * Copyright (C) 2022 by the Nominatim developer community.
8  * For a full list of authors see the git log.
9  */
10
11 require_once(CONST_LibDir.'/Shell.php');
12
13 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
14 {
15     $aQuick = array();
16     $aCounts = array();
17
18     foreach ($aSpec as $aLine) {
19         if (is_array($aLine)) {
20             if ($aLine[0]) {
21                 $aQuick['--'.$aLine[0]] = $aLine;
22             }
23             if ($aLine[1]) {
24                 $aQuick['-'.$aLine[1]] = $aLine;
25             }
26             $aCounts[$aLine[0]] = 0;
27         }
28     }
29
30     $aResult = array();
31     $bUnknown = false;
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]]++;
37             $xVal = null;
38             if ($aLine[4] == $aLine[5]) {
39                 if ($aLine[4]) {
40                     $xVal = array();
41                     for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
42                         $i++;
43                         if ($i >= $iSize || $aArg[$i][0] == '-') {
44                             showUsage($aSpec, $bExitOnError, 'Parameter of  \''.$aLine[0].'\' is missing');
45                         }
46
47                         switch ($aLine[6]) {
48                             case 'realpath':
49                                 $xVal[] = realpath($aArg[$i]);
50                                 break;
51                             case 'realdir':
52                                 $sPath = realpath(dirname($aArg[$i]));
53                                 if ($sPath) {
54                                     $xVal[] = $sPath . '/' . basename($aArg[$i]);
55                                 } else {
56                                     $xVal[] = $sPath;
57                                 }
58                                 break;
59                             case 'bool':
60                                 $xVal[] = (bool)$aArg[$i];
61                                 break;
62                             case 'int':
63                                 $xVal[] = (int)$aArg[$i];
64                                 break;
65                             case 'float':
66                                 $xVal[] = (float)$aArg[$i];
67                                 break;
68                             default:
69                                 $xVal[] = $aArg[$i];
70                                 break;
71                         }
72                     }
73                     if ($aLine[4] == 1) {
74                         $xVal = $xVal[0];
75                     }
76                 } else {
77                     $xVal = true;
78                 }
79             } else {
80                 fail('Variable numbers of params not yet supported');
81             }
82
83             if ($aLine[3] > 1) {
84                 if (!array_key_exists($aLine[0], $aResult)) {
85                     $aResult[$aLine[0]] = array();
86                 }
87                 $aResult[$aLine[0]][] = $xVal;
88             } else {
89                 $aResult[$aLine[0]] = $xVal;
90             }
91         } else {
92             $bUnknown = $aArg[$i];
93         }
94     }
95
96     if (array_key_exists('help', $aResult)) {
97         showUsage($aSpec);
98     }
99     if ($bUnknown && $bExitOnUnknown) {
100         showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
101     }
102
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');
107             }
108             if ($aCounts[$aLine[0]] > $aLine[3]) {
109                 showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is present too many times');
110             }
111             if ($aLine[6] == 'bool' && !array_key_exists($aLine[0], $aResult)) {
112                 $aResult[$aLine[0]] = false;
113             }
114         }
115     }
116     return $bUnknown;
117 }
118
119 function showUsage($aSpec, $bExit = false, $sError = false)
120 {
121     if ($sError) {
122         echo basename($_SERVER['argv'][0]).': '.$sError."\n";
123         echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
124         exit;
125     }
126     echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
127     $bFirst = true;
128     foreach ($aSpec as $aLine) {
129         if (is_array($aLine)) {
130             if ($bFirst) {
131                 $bFirst = false;
132                 echo "\n";
133             }
134             $aNames = array();
135             if ($aLine[1]) {
136                 $aNames[] = '-'.$aLine[1];
137             }
138             if ($aLine[0]) {
139                 $aNames[] = '--'.$aLine[0];
140             }
141             $sName = join(', ', $aNames);
142             echo '  '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
143         } else {
144             echo $aLine."\n";
145         }
146     }
147     echo "\n";
148     exit;
149 }
150
151 function info($sMsg)
152 {
153     echo date('Y-m-d H:i:s == ').$sMsg."\n";
154 }
155
156 $aWarnings = array();
157
158
159 function warn($sMsg)
160 {
161     $GLOBALS['aWarnings'][] = $sMsg;
162     echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
163 }
164
165
166 function repeatWarnings()
167 {
168     foreach ($GLOBALS['aWarnings'] as $sMsg) {
169         echo '  * ',$sMsg."\n";
170     }
171 }
172
173
174 function setupHTTPProxy()
175 {
176     if (!getSettingBool('HTTP_PROXY')) {
177         return;
178     }
179
180     $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
181     $aHeaders = array();
182
183     $sLogin = getSetting('HTTP_PROXY_LOGIN');
184     $sPassword = getSetting('HTTP_PROXY_PASSWORD');
185
186     if ($sLogin && $sPassword) {
187         $sAuth = base64_encode($sLogin.':'.$sPassword);
188         $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
189     }
190
191     $aProxyHeader = array(
192                      'proxy' => $sProxy,
193                      'request_fulluri' => true,
194                      'header' => $aHeaders
195                     );
196
197     $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
198     stream_context_set_default($aContext);
199 }