c0cbec8ccee407c4656ed056b9778d4f947f2f13
[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 = "$repostore/$matched/";
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                 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 foreach($http_response_header as $key => $val) {
75         if(preg_match("/HTTP.*30[1-9].*/", $val)) {
76                 fclose($localfile);
77                 unlink($file);
78                 mkdir($file);
79                 exit(0);
80         }
81         // get content length form upstream and print
82         if(preg_match("/^Content-Length:.*/", $val)) {
83                 $clentemp = preg_split("/[: ]+/", $val);
84                 $contentlen = $clentemp[1];
85                 //header($val);
86         }
87         // get content type from upstream and print
88         if(preg_match("/^Content-Type:.*/", $val)) {
89                 $contenttype = $val;    
90         }
91         if(!$remotefile) {
92                 return;
93         }
94          
95 }
96
97 file_put_contents("$file.size", $contentlen);
98
99 while(!feof($remotefile)) {
100         $data = fread($remotefile, 2048);
101         fwrite($localfile, $data);
102 }
103
104 //rename("$file.tmp.data.deleteme", $file);
105 unlink("$file.size");
106
107 ?>