', $url); $u= parse_url($url); $fd= fsockopen($u['host'], isset($u['port']) ? $u['port'] : 80, $errno, $errstr); if (!$fd) { println('*** Failed (#', $errno, ': ', $errstr, ' ***'); return FALSE; } // Send HTTP request fputs($fd, sprintf( "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", $u['path'].'.ar', $u['host'] )); // Read HTTP response $status= fgets($fd, 1024); sscanf($status, "HTTP/1.%*d %d %[^\r]", $code, $message); if (200 != $code) { println('*** Failed (HTTP ', $code, ': ', $message, ' ***'); return FALSE; } do { if (FALSE === ($header= fgets($fd, 1024))) return FALSE; if ('' === ($header= rtrim($header))) break; } while ($header); while ($line= fgets($fd, 0xFF)) { if (2 != sscanf($line, '--%d:%[^:]--', $length, $filename)) continue; printf( '---> %s (%.2f kB) [%s]%s', $filename, $length / 1024, str_repeat('.', $pw), str_repeat("\x08", $pw+ 1) ); $ft= fopen($targetdir.DIRECTORY_SEPARATOR.$filename, 'wb'); if (!$ft) { println('*** I/O Error w/ ', $filename, ' ***'); return FALSE; } $s= 0; $c= 0; while ($s < $length) { $s+= fwrite($ft, fread($fd, min(0x1000, $length- $s))); // Update progress $d= ceil(($s / $length) * $pw); if ($d == $c) continue; echo str_repeat('#', $d- $c); flush(); $c= $d; } fclose($ft); println(); } fclose($fd); return TRUE; } // }}} // {{{ which(string command [, bool ext]) function which($command, $ext= FALSE) { $search= explode(PATH_SEPARATOR, getenv('PATH')); $extensions= array(''); $ext && $extensions+= explode(PATH_SEPARATOR, getenv('PATHEXT')); foreach ($search as $path) { foreach ($extensions as $ext) { if (file_exists($q= $path.DIRECTORY_SEPARATOR.$command.$ext)) return realpath($q); } } return NULL; } // }}} // {{{ main error_reporting(E_ALL); $uname= php_uname(); println('===> Setup for ', VERSION, ' @ ', $uname); // Download dependencies ardl(BASE_URL.VERSION.'/depend', TARGET_DIR) || exit(1); $depend= parse_ini_file(TARGET_DIR.'depend.ini', TRUE); // Check for PHP executable if (!($exe= which('php5', TRUE))) { if (!($exe= which('php', TRUE))) { println('*** PHP command not in path (', getenv('PATH'), ') ***'); exit(1); } } // Found it, execute $extensions= array(); $proc= proc_open('"'.$exe.'"', array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w')), $pipes); if (!$proc) { println('*** PHP executable ', $exe, ' could not be forked ***'); exit(1); } // Gather information fwrite($pipes[0], ' array_flip(array_map("strtolower", get_loaded_extensions())), "ini" => ini_get_all() )); ?>'); fclose($pipes[0]); // Scan until we find a NUL character. This is the beginning of our output while (fgetc($pipes[1]) != "\0"); fscanf($pipes[1], '%s %s', $version, $sapi); // Read the rest for ($out= ''; !feof($pipes[1]); ) $out.= fread($pipes[1], 0x1000); for ($err= ''; !feof($pipes[2]); ) $err.= fread($pipes[2], 0x1000); fclose($pipes[1]); fclose($pipes[2]); $exitcode= proc_close($proc); $config= unserialize($out); // Verify it worked if (0 != $exitcode || '' != $err || !is_array($config)) { println('*** PHP executable ', $exe, ' exited with unexpected exit code ', $exitcode, ' (', $err, ') ***'); exit(1); } println('---> System PHP ', $version, ' SAPI ', $sapi, ' @ ', $exe); // Verify version foreach ($depend['php'] as $op => $compare) { if (!version_compare($version, $compare, $op)) { println('*** PHP Version ', $op, ' ', $compare, ' required ***'); exit(1); } } // Check extensions foreach ($depend['ext.required'] as $ext => $usage) { if (!isset($config['ext'][$ext])) { println('*** PHP Extension ', $ext, ' required for ', $usage, ' ***'); exit(1); } } foreach ($depend['ext.conflict'] as $ext => $usage) { if (isset($config['ext'][$ext])) { println('*** PHP Extension ', $ext, ' conflicts (', $usage, ') ***'); exit(1); } } foreach ($depend['ext.optional'] as $ext => $usage) { if (!isset($config['ext'][$ext])) { println('---> PHP Extension ', $ext, ' not found, needed for ', $usage, ' (ignoring)'); } } // Check configuration foreach ($depend['ini'] as $setting => $match) { $value= @$config['ext'][$setting]['local_value']; if (!preg_match($match, $value)) { println('*** PHP .ini setting ', $setting, ' needs to match ', $match, ' (but is '.$value.') ***'); exit(1); } } unlink(TARGET_DIR.'depend.ini'); println('===> Dependencies OK'); // Create directories $target= TARGET_DIR.DIRECTORY_SEPARATOR.VERSION.DIRECTORY_SEPARATOR; println('---> Target: ', $target); foreach (array($target, $target.'bin', $target.'lib') as $dir) { if (is_dir($dir)) continue; if (mkdir($dir)) continue; println('*** Failed to create directory ', $dir, ' ***'); exit(1); } // Download : // * base (lang.base.php) // * META-INF (boot.pth, ChangeLog) // * Libraries (xp-rt, net.xp_framework XAR) println('===> Base'); ardl(BASE_URL.VERSION.'/base', $target) || exit(2); println('===> META-INF'); ardl(BASE_URL.VERSION.'/meta-inf', $target) || exit(2); println('===> Libraries'); ardl(BASE_URL.VERSION.'/lib', $target) || exit(2); // Detect environment println('===> Environment'); switch (TRUE) { case NULL != which('cygwin1.dll'): println('---> Found cygwin1.dll, assuming cygwin environment'); $runners= 'cygwin'; break; case TRUE == preg_match('/^Windows/i', $uname): println('---> Windows'); $runners= 'windows'; break; default: println('---> Un*x'); $runners= 'unix'; break; } // Download runners ardl(BASE_URL.'bin/'.$runners, $target.'bin') || exit(3); // Ensure u+rwx $d= opendir($target.'bin'); while ($e= readdir($d)) { if (!is_file($f= $target.'bin'.DIRECTORY_SEPARATOR.$e)) continue; chmod($f, fileperms($f) | 0700); } closedir($d); // Check to see if it works println('===> Configuration:'); system('"'.$target.'bin/xp" -v', $exitcode); if (0 != $exitcode) { println('*** Failed to launch `', $target.'bin/xp -v`, exitcode ', $exitcode); exit(4); } println('===> Done, you might want to add "', $target.'bin', '" to your PATH'); // }}} ?>