Busybox: Upgrade to 1.21.1 (stable). lsof active.
[tomato.git] / release / src / router / php / pear / fetch.php
blob01d223392e48d54997dcb841e0a50db48ba16bd0
1 <?php
3 function usage($argv) {
4 echo "Usage:\n";
5 printf("\tphp %s <http://example.com/file> <localfile>\n", $argv[0]);
6 exit(1);
9 function stream_notification_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max) {
10 static $filesize = null;
12 switch($notification_code) {
13 case STREAM_NOTIFY_RESOLVE:
14 case STREAM_NOTIFY_AUTH_REQUIRED:
15 case STREAM_NOTIFY_COMPLETED:
16 case STREAM_NOTIFY_FAILURE:
17 case STREAM_NOTIFY_AUTH_RESULT:
18 /* Ignore */
19 break;
21 case STREAM_NOTIFY_REDIRECTED:
22 echo "Being redirected to: ", $message, "\n";
23 break;
25 case STREAM_NOTIFY_CONNECT:
26 echo "Connected...\n";
27 break;
29 case STREAM_NOTIFY_FILE_SIZE_IS:
30 $filesize = $bytes_max;
31 echo "Filesize: ", $filesize, "\n";
32 break;
34 case STREAM_NOTIFY_MIME_TYPE_IS:
35 echo "Mime-type: ", $message, "\n";
36 break;
38 case STREAM_NOTIFY_PROGRESS:
39 if ($bytes_transferred > 0) {
40 if (!isset($filesize)) {
41 printf("\rUnknown filesize.. %2d kb done..", $bytes_transferred/1024);
42 } else {
43 $length = (int)(($bytes_transferred/$filesize)*100);
44 printf("\r[%-100s] %d%% (%2d/%2d kb)", str_repeat("=", $length). ">", $length, ($bytes_transferred/1024), $filesize/1024);
47 break;
51 isset($argv[1], $argv[2]) or usage($argv);
53 if (!isset($_ENV['http_proxy'])) {
54 $copt = null;
55 } else {
56 $copt = array(
57 'http' => array(
58 'proxy' => preg_replace('/^http/i', 'tcp', $_ENV['http_proxy']),
59 'request_fulluri' => true,
64 $ctx = stream_context_create($copt, array("notification" => "stream_notification_callback"));
66 $fp = fopen($argv[1], "r", false, $ctx);
67 if (is_resource($fp) && file_put_contents($argv[2], $fp)) {
68 echo "\nDone!\n";
69 exit(0);
72 $err = error_get_last();
73 echo "\nError..\n", $err["message"], "\n";
74 exit(1);