3 require_once(CONST_LibDir.'/Shell.php');
5 function getCmdOpt($aArg, $aSpec, &$aResult, $bExitOnError = false, $bExitOnUnknown = false)
10 foreach ($aSpec as $aLine) {
11 if (is_array($aLine)) {
13 $aQuick['--'.$aLine[0]] = $aLine;
16 $aQuick['-'.$aLine[1]] = $aLine;
18 $aCounts[$aLine[0]] = 0;
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]]++;
30 if ($aLine[4] == $aLine[5]) {
33 for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
35 if ($i >= $iSize || $aArg[$i][0] == '-') {
36 showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
41 $xVal[] = realpath($aArg[$i]);
44 $sPath = realpath(dirname($aArg[$i]));
46 $xVal[] = $sPath . '/' . basename($aArg[$i]);
52 $xVal[] = (bool)$aArg[$i];
55 $xVal[] = (int)$aArg[$i];
58 $xVal[] = (float)$aArg[$i];
72 fail('Variable numbers of params not yet supported');
76 if (!array_key_exists($aLine[0], $aResult)) {
77 $aResult[$aLine[0]] = array();
79 $aResult[$aLine[0]][] = $xVal;
81 $aResult[$aLine[0]] = $xVal;
84 $bUnknown = $aArg[$i];
88 if (array_key_exists('help', $aResult)) {
91 if ($bUnknown && $bExitOnUnknown) {
92 showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
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');
100 if ($aCounts[$aLine[0]] > $aLine[3]) {
101 showUsage($aSpec, $bExitOnError, 'Option \''.$aLine[0].'\' is pressent too many times');
103 if ($aLine[6] == 'bool' && !array_key_exists($aLine[0], $aResult)) {
104 $aResult[$aLine[0]] = false;
111 function showUsage($aSpec, $bExit = false, $sError = false)
114 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
115 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
118 echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
120 foreach ($aSpec as $aLine) {
121 if (is_array($aLine)) {
128 $aNames[] = '-'.$aLine[1];
131 $aNames[] = '--'.$aLine[0];
133 $sName = join(', ', $aNames);
134 echo ' '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
145 echo date('Y-m-d H:i:s == ').$sMsg."\n";
148 $aWarnings = array();
153 $GLOBALS['aWarnings'][] = $sMsg;
154 echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
158 function repeatWarnings()
160 foreach ($GLOBALS['aWarnings'] as $sMsg) {
161 echo ' * ',$sMsg."\n";
166 function setupHTTPProxy()
168 if (!getSettingBool('HTTP_PROXY')) {
172 $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
175 $sLogin = getSetting('HTTP_PROXY_LOGIN');
176 $sPassword = getSetting('HTTP_PROXY_PASSWORD');
178 if ($sLogin && $sPassword) {
179 $sAuth = base64_encode($sLogin.':'.$sPassword);
180 $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
183 $aProxyHeader = array(
185 'request_fulluri' => true,
186 'header' => $aHeaders
189 $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
190 stream_context_set_default($aContext);