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)) {
12 if ($aLine[0]) $aQuick['--'.$aLine[0]] = $aLine;
13 if ($aLine[1]) $aQuick['-'.$aLine[1]] = $aLine;
14 $aCounts[$aLine[0]] = 0;
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]]++;
26 if ($aLine[4] == $aLine[5]) {
29 for ($n = $aLine[4]; $i < $iSize && $n; $n--) {
31 if ($i >= $iSize || $aArg[$i][0] == '-') showUsage($aSpec, $bExitOnError, 'Parameter of \''.$aLine[0].'\' is missing');
35 $xVal[] = realpath($aArg[$i]);
38 $sPath = realpath(dirname($aArg[$i]));
40 $xVal[] = $sPath . '/' . basename($aArg[$i]);
46 $xVal[] = (bool)$aArg[$i];
49 $xVal[] = (int)$aArg[$i];
52 $xVal[] = (float)$aArg[$i];
59 if ($aLine[4] == 1) $xVal = $xVal[0];
64 fail('Variable numbers of params not yet supported');
68 if (!array_key_exists($aLine[0], $aResult)) $aResult[$aLine[0]] = array();
69 $aResult[$aLine[0]][] = $xVal;
71 $aResult[$aLine[0]] = $xVal;
74 $bUnknown = $aArg[$i];
78 if (array_key_exists('help', $aResult)) showUsage($aSpec);
79 if ($bUnknown && $bExitOnUnknown) showUsage($aSpec, $bExitOnError, 'Unknown option \''.$bUnknown.'\'');
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');
87 if (!array_key_exists($aLine[0], $aResult))
88 $aResult[$aLine[0]] = false;
96 function showUsage($aSpec, $bExit = false, $sError = false)
99 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
100 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
103 echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
105 foreach ($aSpec as $aLine) {
106 if (is_array($aLine)) {
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";
126 echo date('Y-m-d H:i:s == ').$sMsg."\n";
129 $aWarnings = array();
134 $GLOBALS['aWarnings'][] = $sMsg;
135 echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
139 function repeatWarnings()
141 foreach ($GLOBALS['aWarnings'] as $sMsg) {
142 echo ' * ',$sMsg."\n";
147 function setupHTTPProxy()
149 if (!getSettingBool('HTTP_PROXY')) {
153 $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
156 $sLogin = getSetting('HTTP_PROXY_LOGIN');
157 $sPassword = getSetting('HTTP_PROXY_PASSWORD');
159 if ($sLogin && $sPassword) {
160 $sAuth = base64_encode($sLogin.':'.$sPassword);
161 $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
164 $aProxyHeader = array(
166 'request_fulluri' => true,
167 'header' => $aHeaders
170 $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
171 stream_context_set_default($aContext);