modified the way the directory detection works... its not foolproof,
[glcas.git] / bin / downloadfile.php
1 <?php
2
3 $url = "";
4 $file = "";
5 if(isset($argv[1])) {
6         $url = $argv[1];
7 }
8
9 if(isset($argv[2])) {
10         $file = $argv[2];
11 }
12
13 if($url == "" || $file == "") {
14         echo "need a url and file\n";
15         exit(0);
16 }
17
18 // first check any directories in $file are in existence
19 $splfile = explode("/", $file);
20 if(count($splfile) > 1) {
21         $tomake = "";
22         for($i = 0; $i < count($splfile)-1; $i++) {
23                 $tomake .= "/".$splfile[$i];
24                 echo("making directory $tomake");
25                 if(!is_dir($tomake)) mkdir($tomake);
26         }
27 }
28
29
30 // ok, we kick off a download
31 if(file_exists("$file")) {
32         // a download exists, does it still work
33         error_log("DOWNLOADER: file exists for current download, hope it works, attempting lock");
34         $localtmpfh = fopen("$file", "r");
35         $lockres = flock($localtmpfh, LOCK_EX|LOCK_NB);
36         if(!$lockres) {
37                 error_log("flock did fail, all is right with the world a download is in progress");
38                 exit(0);
39         } else {
40                 error_log("lock succeeded, dieing in the arse");
41                 unlink("$file");
42                 if(file_exists("$file.size")) unlink("$file.size");
43         }
44 }
45
46 $remotefile = fopen($url, "r");
47
48 // check if ther mote file started
49 if(!$remotefile) {
50         foreach($http_response_header as $key => $val) {
51                 if(preg_match("/HTTP.*404.*/", $val)) {
52                         echo "got a 404 touching 404 file\n";
53                         touch("$file.404");
54                         exit(0);
55                 }
56         }
57 } else {
58         if(file_exists("$file.404")) {
59                 unlink("$file.404");
60         }
61 }
62
63 $localfile = fopen($file, "w");
64 $lockres = flock($localfile, LOCK_EX);
65 if(!$localfile) {
66         echo "something went plop\n";
67         return;
68 }
69
70
71 // get the headers from the remote request and use them to hurt people
72 $contentlen = 0;
73 $contenttype = "";
74
75 // detecting a remote directory on a web session is near impossible, this is the best i can come up with
76 // - check for a redirection header, then look for a trailing / on the url... best of luck to me
77 foreach($http_response_header as $key => $val) {
78         if(preg_match("/.*Location:.*/", $val)) {
79                 echo "got location as $val\n";
80                 $realloc = preg_replace("/.*[: ]/", "", $val);
81                 if(preg_match("/.*\/$/", $realloc)) {
82                         echo "matched\n";
83                         fclose($localfile);
84                         unlink($file);
85                         mkdir($file);
86                         exit(0);
87                 }
88         }
89         // get content length form upstream and print
90         if(preg_match("/^Content-Length:.*/", $val)) {
91                 $clentemp = preg_split("/[: ]+/", $val);
92                 $contentlen = $clentemp[1];
93                 //header($val);
94         }
95         // get content type from upstream and print
96         if(preg_match("/^Content-Type:.*/", $val)) {
97                 $contenttype = $val;    
98         }
99         if(!$remotefile) {
100                 return;
101         }
102          
103 }
104
105 file_put_contents("$file.size", $contentlen);
106
107 while(!feof($remotefile)) {
108         $data = fread($remotefile, 2048);
109         fwrite($localfile, $data);
110 }
111
112 //rename("$file.tmp.data.deleteme", $file);
113 unlink("$file.size");
114
115 ?>