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