remote-copy
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
function retrieve($remote_filename, $home_filename, $autocomplete = true)
{
$awnser = '';
$headers = array();
$status = '';
$headerstopped = false;
$fileId = 0;
$size = 0;
$url = explode('/', $remote_filename);
if($url[0] == 'http:' OR $url[0] == 'https'){
$url = array_slice($url, 2);
}
if($autocomplete){
$home_filename .= $url[count($url)-1];
}
if(!$filehandle = fopen($home_filename, 'w')){
trigger_error('Could not create file', E_USER_ERROR);
return false;
}else{
if(!$handle = fsockopen($url[0], 80, $errno, $errstr, 30)) {
trigger_error("$errstr ($errno)", E_USER_ERROR);
fclose($filehandle);
return false;
}else{
$out = "GET /".implode('/', array_slice($url, 1))." HTTP/1.0\r\n";
$out .= "Host: ".$url[0]."\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; nl-NL; rv:1.7.8) Gecko/20050511 Firefox/1.0.4\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($handle, $out);
while (!feof($handle)) {
if(!$headerstopped){
$awnser .= fgets($handle, 8192);
if(stristr($awnser, "\r\n\r\n")){
list($header, $data) = explode("\r\n\r\n", $awnser);
$header = analizeHeader($header);
if(stristr($header['Status'], '200 OK')){
$headerstopped = true;
unset($awnser);
}else{
if(stristr($header['Status'], '400')){
trigger_error('Bad request', E_USER_ERROR);
}
elseif(stristr($header['Status'], '404')){
trigger_error('File not found', E_USER_ERROR);
}
unset($awnser);
fclose($handle);
fclose($filehandle);
return false;
}
}
}else{
$buffer = fgets($handle, 8192);
fwrite($filehandle, $buffer);
$buffer = NULL;
}
}
fclose($handle);
fclose($filehandle);
return $home_filename;
}
}
}
function analizeHeader($header)
{
$headers = array();
foreach(explode("\r\n", $header) as $value){
if(stristr($value, ': ')){
list($key, $value) = explode(': ', $value);
$headers[$key] = $value;
}else{
$headers['Status'] = $value;
}
}
return $headers;
}
?>
function retrieve($remote_filename, $home_filename, $autocomplete = true)
{
$awnser = '';
$headers = array();
$status = '';
$headerstopped = false;
$fileId = 0;
$size = 0;
$url = explode('/', $remote_filename);
if($url[0] == 'http:' OR $url[0] == 'https'){
$url = array_slice($url, 2);
}
if($autocomplete){
$home_filename .= $url[count($url)-1];
}
if(!$filehandle = fopen($home_filename, 'w')){
trigger_error('Could not create file', E_USER_ERROR);
return false;
}else{
if(!$handle = fsockopen($url[0], 80, $errno, $errstr, 30)) {
trigger_error("$errstr ($errno)", E_USER_ERROR);
fclose($filehandle);
return false;
}else{
$out = "GET /".implode('/', array_slice($url, 1))." HTTP/1.0\r\n";
$out .= "Host: ".$url[0]."\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; nl-NL; rv:1.7.8) Gecko/20050511 Firefox/1.0.4\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($handle, $out);
while (!feof($handle)) {
if(!$headerstopped){
$awnser .= fgets($handle, 8192);
if(stristr($awnser, "\r\n\r\n")){
list($header, $data) = explode("\r\n\r\n", $awnser);
$header = analizeHeader($header);
if(stristr($header['Status'], '200 OK')){
$headerstopped = true;
unset($awnser);
}else{
if(stristr($header['Status'], '400')){
trigger_error('Bad request', E_USER_ERROR);
}
elseif(stristr($header['Status'], '404')){
trigger_error('File not found', E_USER_ERROR);
}
unset($awnser);
fclose($handle);
fclose($filehandle);
return false;
}
}
}else{
$buffer = fgets($handle, 8192);
fwrite($filehandle, $buffer);
$buffer = NULL;
}
}
fclose($handle);
fclose($filehandle);
return $home_filename;
}
}
}
function analizeHeader($header)
{
$headers = array();
foreach(explode("\r\n", $header) as $value){
if(stristr($value, ': ')){
list($key, $value) = explode(': ', $value);
$headers[$key] = $value;
}else{
$headers['Status'] = $value;
}
}
return $headers;
}
?>