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