13 if($url == "" || $file == "") {
14 echo "need a url and file\n";
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);
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);
37 error_log("flock did fail, all is right with the world a download is in progress");
40 error_log("lock succeeded, dieing in the arse");
46 $remotefile = fopen($url, "r");
48 // check if ther mote file started
50 foreach($http_response_header as $key => $val) {
51 if(preg_match("/HTTP.*404.*/", $val)) {
52 echo "got a 404 touching 404 file\n";
58 if(file_exists("$file.404")) {
63 $localfile = fopen($file, "w");
64 $lockres = flock($localfile, LOCK_EX);
66 echo "something went plop\n";
71 // get the headers from the remote request and use them to hurt people
74 foreach($http_response_header as $key => $val) {
75 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
81 // get content length form upstream and print
82 if(preg_match("/^Content-Length:.*/", $val)) {
83 $clentemp = preg_split("/[: ]+/", $val);
84 $contentlen = $clentemp[1];
87 // get content type from upstream and print
88 if(preg_match("/^Content-Type:.*/", $val)) {
97 file_put_contents("$file.size", $contentlen);
99 while(!feof($remotefile)) {
100 $data = fread($remotefile, 2048);
101 fwrite($localfile, $data);
104 //rename("$file.tmp.data.deleteme", $file);
105 unlink("$file.size");