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');
105 if (!array_key_exists($aLine[0], $aResult)) {
106 $aResult[$aLine[0]] = false;
115 function showUsage($aSpec, $bExit = false, $sError = false)
118 echo basename($_SERVER['argv'][0]).': '.$sError."\n";
119 echo 'Try `'.basename($_SERVER['argv'][0]).' --help` for more information.'."\n";
122 echo 'Usage: '.basename($_SERVER['argv'][0])."\n";
124 foreach ($aSpec as $aLine) {
125 if (is_array($aLine)) {
132 $aNames[] = '-'.$aLine[1];
135 $aNames[] = '--'.$aLine[0];
137 $sName = join(', ', $aNames);
138 echo ' '.$sName.str_repeat(' ', 30-strlen($sName)).$aLine[7]."\n";
149 echo date('Y-m-d H:i:s == ').$sMsg."\n";
152 $aWarnings = array();
157 $GLOBALS['aWarnings'][] = $sMsg;
158 echo date('Y-m-d H:i:s == ').'WARNING: '.$sMsg."\n";
162 function repeatWarnings()
164 foreach ($GLOBALS['aWarnings'] as $sMsg) {
165 echo ' * ',$sMsg."\n";
170 function setupHTTPProxy()
172 if (!getSettingBool('HTTP_PROXY')) {
176 $sProxy = 'tcp://'.getSetting('HTTP_PROXY_HOST').':'.getSetting('HTTP_PROXY_PROT');
179 $sLogin = getSetting('HTTP_PROXY_LOGIN');
180 $sPassword = getSetting('HTTP_PROXY_PASSWORD');
182 if ($sLogin && $sPassword) {
183 $sAuth = base64_encode($sLogin.':'.$sPassword);
184 $aHeaders = array('Proxy-Authorization: Basic '.$sAuth);
187 $aProxyHeader = array(
189 'request_fulluri' => true,
190 'header' => $aHeaders
193 $aContext = array('http' => $aProxyHeader, 'https' => $aProxyHeader);
194 stream_context_set_default($aContext);