522eef732c11342087fe9f9841cea3d0396c2a2e
[glcas.git] / plugins / repo.php
1 <?php
2 $URL_HANDLERS["*"] = "GLCASRepo";
3
4
5 class GLCASRepo {
6         function __construct($config)
7         {
8                 $this->config = $config;
9                 if($this->config->getConfigVar("storagelocation") == false) {
10                         global $WEB_ROOT_FS;
11                         $storloc = "$WEB_ROOT_FS/../var/glcas/cache/";
12                         if(!file_exists($storloc)) mkdir($storloc);
13                         $this->config->setConfigVar("storagelocation", realpath($storloc));
14                         $this->config->saveConfig();
15                         error_log("set storage location, $storloc");
16                 }
17         }
18         
19         function go($url)
20         {
21                 error_log("repo:go called");
22                 
23                 // figure out what we're doing 
24                 switch($url) {
25                         case "list":
26                                 GLCASpageBuilder($this, "body");
27                                 break;
28                         default:
29                                 $this->getRepoForUrl($url);
30                 }
31         }
32         
33         function body($url)
34         {
35                 // this is how this will work
36                 //$this->decodeUrl();
37                 if(strncasecmp("list", $url, 4)==0) {
38                         echo "i am the repo list";
39                         return;
40                 }
41                 echo "i am the repo, $url";
42         }
43         
44         
45         // TODO: rework this function
46         /*
47          * What i need to do is have a downloader function
48          * that can cope with lots of different shit
49          * but thats a pipe dream
50          * 
51          * what *THIS* function needs to do is
52          * 1) figure out the repo
53          * 2) figure out the file in the repo
54          * 2.1) if its a directory, go to print directory
55          * 3) if the file exists, give it to the user (if a range is specified give the user the range)
56          * 4) if the file does not exist
57          *    - check if a tmp file exists
58          *    - attempt to get an exclusive flock
59          *    - if flock fails, donwload in progress
60          *    - if flock succeeds, truncate file and re-start download
61          *    - if a range request was made, send the range once available
62          *    - if range not available, sleep for 5 and check again.
63          * 
64          * I dont want to code this from scratch, but i probably need to
65          */
66         function getRepoForUrl($url)
67         {
68                 $xurl = split("[/,]", $url);
69                 
70                 // first get the config         
71                 $uconf = unserialize($this->config->getConfigVar("repodata"));
72                 $repostore = $this->config->getConfigVar("storagelocation");
73                 
74                 // preset matched to -1
75                 $matched = -1;
76                 
77                 // first we check for /repo/repoid as a url
78                 $startat = 0;
79                 if($xurl[0] == "repo") {
80                         $repid = $xurl[1];
81                         error_log("trying to get repo for repoid, $repid");
82                         if(isset($uconf[$repid])) {
83                                 $matched = ((int)($repid));
84                                 error_log("set matched, $matched, $repid");
85                                 $startat +=2;
86                         }
87                 }
88                 
89                 // now check for a prefix match
90                 $prematch = false;
91                 if($matched < 0) foreach($uconf as $key => $var) {
92                         $pre = $var["prefix"];
93                         
94                         if($pre!="") {
95                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
96                                 if(strcasecmp($pre, $xurl[0])==0) {
97                                         //echo "Matched pre\n";
98                                         $prematch = true;
99                                         $startat++;
100                                 }
101                         }
102                 }
103                 
104                 // next, check for a short url match
105                 if($matched < 0) foreach($uconf as $key => $var) {
106                         // if we matched a pre, then we check against the second url component
107                         
108                         $short = $var["shorturl"];
109                         
110                         if($short!="") {
111                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
112                                 if(strcasecmp($xurl[$startat], $short)==0) {
113                                         //echo "Matched\n";
114                                         $matched = $key;
115                                         $startat++;
116                                 }
117                         }
118                 }
119                 
120                 // TODO: this deterministic bit
121                 // so far nothing has matched - what this next bit needs to do is try and "Determine" a repo from url
122                 // for eg, if a user gets /fedora/x86_64/os we need to return something appropriate
123                 if($matched < 0) {
124                         echo "No such repo<br>";
125                         header("HTTP/1.0 404 Not Found");
126                         return;
127                 }
128                 
129                 
130                 // something was matched, so now we reconstruct the file component of the url
131                 $file = "/";
132                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
133                         $file .= "/".$xurl[$i];
134                 }
135                 
136                 // so, the ultimate url for the file we need is:
137                 $actualfile = "$repostore/$matched/$file";
138                 error_log("Atcualfile is $actualfile");
139                 
140                 // if its a directory, lets do a print
141                 if(is_dir($actualfile)) {
142                         $this->printDir($actualfile, $file, $url);
143                         return;
144                 }
145                 
146                 // check if the file exists and serve it up
147                 if(file_exists($actualfile)) {
148                         $this->serveUpFile($actualfile, $matched);
149                         return;
150                 } else {
151                         // the file does not exist, we now need to go into "download" mode
152                         $remoteurl = $uconf[$matched]["url"]."/$file";
153                         $this->downloadAndServe($actualfile, $matched, $remoteurl);
154                         return;
155                 }
156         }
157         
158         function serveUpFile($actualfile, $repoid)
159         {
160                 $uconf = unserialize($this->config->getConfigVar("repodata"));
161                 $repostore = $this->config->getConfigVar("storagelocation");
162                 
163                 // figure out the range header garbage that centos/redhat send
164                 if(isset($_SERVER["HTTP_RANGE"])) {
165                         // we're using ranges - screw you stupid installer
166                         $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
167                         
168                         // cut up ranges
169                         $rangestart = $pr_range[1];
170                         $rangelength = $pr_range[2] - $pr_range[1] +1;
171                         $rangestr = $pr_range[1]."-".$pr_range[2];
172                         error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
173                         
174                         // now spit some headers
175                         header("HTTP/1.1 206 Partial Content");
176                         header("Content-Length: ".$rangelength);
177                         
178
179                         header("Content-Range: bytes $rangesstr/".filesize($actualfile));
180                         
181                         // determine mime type
182                         $type = mime_content_type($actualfile);
183                         
184                         // set mime type header
185                         header("Content-type: $type");
186                         
187                         // open the local file (TODO: error check)
188                         $localfile = fopen($actualfile, "r");
189                         fseek($localfile, $rangestart, SEEK_SET);
190                         
191                         // read in the data, god i hope its not big
192                         $data = fread($localfile, $rangelength);
193                         
194                         // lastly, send data
195                         echo $data;
196                         flush();
197                         
198                         // and close the file
199                         fclose($localfile);
200                         return;
201                 } else {
202
203                         // we're not using range's - good on you installer thingy
204                         header("Content-Length: ".filesize($actualfile));
205
206                         // set the mime type header
207                         $type = mime_content_type($actualfile);
208                         header("Content-type: $type");
209                         
210                         // open the local file                  
211                         $localfile = fopen($actualfile, "r");
212                         if(!$localfile) {
213                                 error_log("normal upload went barf");
214                                 return;
215                         }
216                         
217                         // iterate over its length, send 8k at a time
218                         while(!feof($localfile)) {
219                                 // read and send data
220                                 $data = fread($localfile, 8192);
221                                 echo $data;
222                                 
223                                 // flush so the client sees the data
224                                 flush();
225                         }
226                         
227                         // close the file
228                         fclose($localfile);
229                         return;
230                 }               
231         }
232         
233         // TODO: this is the function im working on
234         // the alternative to this function is that if a file is in the process of being
235         // downloaded, we simply serve from upstream... not a good idea tho unless we create
236         // a local proxy right here
237         function downloadAndServe($filename, $repoid, $remoteurl)
238         {
239                 // this is important so downloads dont die
240                 clearstatcache();
241                 ignore_user_abort(true);
242                 
243                 // get the configurations we need
244                 $uconf = unserialize($this->config->getConfigVar("repodata"));
245                 $repostore = $this->config->getConfigVar("storagelocation");
246                 
247                 // this is the tricky one for ranges.
248                 
249                 // check if a download exists
250                 $otherdownloader = false;
251                 if(file_exists("$filename.tmp.data.deleteme")) {
252                         // a download exists, does it still work
253                         error_log("DOWNLOADER: file exists for current download, hope it works, attempting lock");
254                         $localtmpfh = fopen("$filename.tmp.data.deleteme", "r");
255                         $lockres = flock($localtmpfh, LOCK_EX|LOCK_NB);
256                         if(!$lockres) {
257                                 error_log("flock did fail, all is right with the world a download is in progress");
258                                 $otherdownloader = true;
259                         } else {
260                                 error_log("lock succeeded, dieing in the arse");
261                                 unlink("$filename.tmp.data.deleteme");
262                                 unlink("$filename.tmp.data.deleteme.size");
263                         }
264                 }
265
266                 // open the remote file
267                 $contentlen = 0;
268                 $contenttype = "";
269                 if(!$otherdownloader) {
270                         $remotefile = fopen($remoteurl, "r");
271                         $localfile = fopen($filename.".tmp.data.deleteme", "w");
272                         $lockres = flock($localfile, LOCK_EX);
273                         if(!$localfile) {
274                                 erorr_log("something went plop");
275                                 return;
276                         }
277                         // get the headers from the remote request and use them to hurt people
278                         $contentlen = 0;
279                         foreach($http_response_header as $key => $val) {
280                                 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
281                                         error_log("got a 30x, must be a directory");
282                                         mkdir($filename);
283                                         header("Location: ".$_SERVER["REQUEST_URI"]."/");
284                                         return;
285                                 }
286                                 // get content length form upstream and print
287                                 if(preg_match("/^Content-Length:.*/", $val)) {
288                                         // WARNING, THIS IS NOT RIGHT
289                                         $clentemp = preg_split("/[: ]+/", $val);
290                                         $contentlen = $clentemp[1];
291                                         //header($val);
292                                 }
293                                 // get content type from upstream and print
294                                 if(preg_match("/^Content-Type:.*/", $val)) {
295                                         $contenttype = $val;    
296                                 }
297                                 if(!$remotefile) {
298                                         header("HTTP/1.0 404 Not Found");
299                                         error_log("asked fore file that dont exist");
300                                         return;
301                                 }
302                                  
303                         }
304                         error_log("put contentlen as $contentlen");
305                         file_put_contents($filename.".tmp.data.deleteme.size", $contentlen);
306                 } else {
307                         $localfile = fopen($filename.".tmp.data.deleteme", "r");
308                         if(!$localfile) {
309                                 error_log("something went plop");
310                                 return;
311                         }
312                 }
313                 
314                 
315                 
316                 // determine if we're ranged
317                 $ranged = false;
318                 $rangestart = 0;
319                 $rangelength = 0;
320                 $rangestr="";
321                 if(isset($_SERVER["HTTP_RANGE"])) {
322                         // we're using ranges - screw you stupid installer
323                         
324                         $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
325                         error_log("got range ".$_SERVER["HTTP_RANGE"]." and ".print_r($pr_range, true));
326                         
327                         // cut up ranges
328                         $rangestart = $pr_range[1];
329                         $rangelength = $pr_range[2] - $pr_range[1] +1;
330                         $rangestr = $pr_range[1]."-".$pr_range[2];
331                         error_log("going ranges at $rangestart, $rangelength,");
332                         $ranged = true;
333                 }
334                 
335                 // open the local files
336                 
337                 // now, lets determine what state we're in
338                 // we're either - getting and sending
339                 // watching and sending
340                 // or a range (Getting and sending)
341                 // or a range (watching and sending)
342                 // TODO: it may be advicable to start the download as a seperate cli process rather then something goin on here
343                 // so it definitely cant be interrupted.
344                 
345                 
346                 // first, getting and sending - this is easy.
347                 if(!$ranged && !$otherdownloader) {
348                         while(!feof($remotefile)) {
349                                 $data = fread($remotefile, 2048);
350                                 echo $data;
351                                 flush();
352                                 fwrite($localfile, $data);
353                         }
354                         rename($filename.".tmp.data.deleteme", $filename);
355                         unlink($filename.".tmp.data.deleteme.size");
356                         
357                         // and we're done
358                         return;
359                         
360                         
361                         
362                         
363                 // IT WORKS!!!!!!!
364                 } else if ($otherdownloader && !$ranged) {
365                         // this is where the fun starts - but this one isnt too bad.
366                         error_log("OTHERDOWNLOAD: im another downloader, please work");
367                         $fsize = file_get_contents($filename.".tmp.data.deleteme.size");
368                         header("Content-Length: $fsize");
369                         $sgotten = 0;
370                         while(!feof($localfile)) {      
371                                 error_log("OTHERDOWNLOAD: get");
372                                 $data = fread($localfile, 2048);
373                                 if(!$data) {
374                                         error_log("dollardata is pair shaped");
375                                 } else {
376                                         $sgotten += strlen($data);
377                                         if($sgotten > $fsize) {
378                                                 error_log("went plop at sgotten, $sgotten, $fsize");
379                                                 return;
380                                         }
381                                         echo $data;
382                                         flush();
383                                 }
384                         }
385                         fclose($localfile);
386                         
387                         // need to think about this in pseudo code.
388                         // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
389                         if(file_exists($filename.".tmp.data.deleteme")) $cursize = filesize($filename.".tmp.data.deleteme");
390                         else if(file_exists($filename)) {
391                                 error_log("DOTHERDOWNLOADER: namechange");
392                                 $cursize = filesize($filename);
393                         } else return; // we had to bail
394                         
395                         
396                         $upload_finished = false;
397                         while(!$upload_finished) {
398                                 while($cursize < $fsize && $cursize < ($sgotten+2048)) {
399                                         clearstatcache();
400                                         error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $fsize");
401                                         // sleep until the the filesize is greater then what we're up to, or until the file is finished
402                                         sleep(1);
403                                         if(file_exists($filename.".tmp.data.deleteme")) {
404                                                 error_log("OTHERDOWNLOADER: still same name");
405                                                 $cursize = filesize($filename.".tmp.data.deleteme");
406                                         } else if(file_exists($filename)) {
407                                                 error_log("DOTHERDOWNLOADER: namechange");
408                                                 $cursize = filesize($filename);
409                                         }
410                                         else return; // we had to bail
411                                 }
412                                 
413                                 error_log("OTHERDOWNLOAD: continue, $sgotten, $fsize");
414                                 // reopen local file - if it stopped existing, we need to deal with that
415                                 if(file_exists($filename.".tmp.data.deleteme")) $localfile = fopen($filename.".tmp.data.deleteme", "r");
416                                 else if(file_exists($filename)) $localfile = fopen($filename, "r");
417                                 else return; // we had to bail
418                                 
419                                 // UG, we need to ff, how could i forget that
420                                 fseek($localfile, $sgotten);
421                                 
422                                 if(!$localfile) {
423                                         error_log("OTHERDOWNLOAD: something went plop");
424                                         return;
425                                 }
426                                 
427                                 // now loop on the file until we have it at an eof
428                                 while(!feof($localfile)) {      
429                                         $data = fread($localfile, 512);
430                                         if(!$data) {
431                                                 error_log("OTHERDOWNLOAD: dollar data went plop");
432                                         } else {
433                                                 $sgotten += strlen($data);
434                                                 echo $data;
435                                                 flush();
436                                         }
437                                 }
438                                 fclose($localfile);
439                                 
440                                 if($sgotten >= $fsize) {
441                                         if($sgotten > $fsize) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
442                                         $upload_finished = true;
443                                 }
444                                 // and we're done
445                                 
446                         }
447                         error_log("OTHERDOWNLOADER: done with");
448                         
449                         return;
450                 
451
452                         
453                         
454                 // THIS WAS JUST AWESOME CAUSE IT WORKS 
455                 // Next painful bit
456                 } else if ($ranged && !$otherdownloader) {
457                         
458                         $sgotten = 0;
459                         
460                         
461                         // the problem is here
462                         error_log("Downloader: going ranged as primary");
463                         clearstatcache();
464                         header("HTTP/1.1 206 Partial Content");
465                         header("Content-Length: ".$rangelength);
466                         header("Content-Range: bytes $rangestr/".$contentlen);
467                         header("Content-Type: $contenttype");
468                         header("Connection: close");
469                 
470                         // first, get up to $rangestart
471                         while(!feof($remotefile) && ftell($remotefile) < $rangestart) {
472                                 if(($rangestart - ftell($remotefile)) < 2048) $rsize = $rangestart;
473                                 else $rsize = 2048;
474                                 $data = fread($remotefile, $rsize);
475                                 if(!$data) {
476                                         error_log("dollar data went plop");
477                                 } else {
478                                         $sgotten += strlen($data);
479                                         flush();
480                                         fwrite($localfile, $data);
481                                 }
482                         }
483                         
484                         error_log("should now be at rangestart: ".ftell($remotefile));
485                         
486                         // now start pumping out data until $rangelength
487                         $sgatlen = $rangelength + $rangestart;
488                         while(!feof($remotefile) && ftell($remotefile) < $sgatlen  ) {
489                                 
490                                 // read only 2048
491                                 $rsize = $sgatlen - ftell($remotefile);
492                                 if($rsize > 2048) $rsize = 2048;
493                                 
494                                 $data = fread($remotefile, $rsize);
495                                 if(!$data) {
496                                         error_log("dollar data went plop");
497                                 } else {
498                                         echo $data;
499                                         $sgotten += strlen($data);
500                                         flush();
501                                         fwrite($localfile, $data);
502                                 }
503                         }
504                         
505                         // hopefully this works, the redhat/centos installer really is a terrible
506                         // piece of code.. The reasons for this is that redhat in their minds decided
507                         // to use ranges, and even though you send the range to the client, the client
508                         // just keeps listening. and you can ignore the range request, it ignores you.
509                         
510                         error_log("should now be at rangeend: $sgatlen, $rangestart, ".ftell($remotefile));
511                         flush();
512                         
513                         // now continue on as per normal - totally gunna work...
514                         while(!feof($remotefile)) {
515                                 //error_log("back download");
516                                 $data = fread($remotefile, 2048);
517                                 flush();
518                                 fwrite($localfile, $data);
519                         }
520                         
521                         
522                         rename($filename.".tmp.data.deleteme", $filename);
523                         unlink($filename.".tmp.data.deleteme.size");
524                         
525                         // and we're done
526                         return;
527                         
528                         
529                         
530                         
531                         
532                         // and here
533                 } else if ($ranged && $otherdownloader) {
534                         // and here too, yay, someone else is doing the
535                         // download, but we're the retards getting a range
536                         $sgotten = 0;
537                         
538                         
539                         // the problem is here
540                         error_log("Downloader: going ranged as primary");
541                         clearstatcache();
542                         header("HTTP/1.1 206 Partial Content");
543                         header("Content-Length: ".$rangelength);
544                         header("Content-Range: bytes $rangestr/".$contentlen);
545                         header("Content-Type: $contenttype");
546                         clearstatcache();
547                         
548                         error_log("OTHERDOWNLOAD: im another downloader, please work for ranged");
549                         
550                 }
551                 
552                 
553                 return;
554         }
555         
556         // this is a nightmare
557         function getRepoForUrlOld($url)
558         {
559                 // the way we breakdown a url is to explode it
560                 $xurl = split("[/,]", $url);
561                 
562                 // we first check if [0] is a prefix
563                 // if now, we check for it being a shorturl (lets just do that for now)
564                 $uconf = unserialize($this->config->getConfigVar("repodata"));
565                 $repostore = $this->config->getConfigVar("storagelocation");
566                 
567                 $matched = -1;
568                 
569                 // first we check for /repo/repoid as a url
570                 $startat = 0;
571                 if($xurl[0] == "repo") {
572                         $repid = $xurl[1];
573                         error_log("trying to get repo for repoid, $repid");
574                         if(isset($uconf[$repid])) {
575                                 $matched = ((int)($repid));
576                                 error_log("set matched, $matched, $repid");
577                                 $startat +=2;
578                         }
579                 }
580                 
581                 
582                 $prematch = false;
583                 if($matched < 0) foreach($uconf as $key => $var) {
584                         $pre = $var["prefix"];
585                         
586                         if($pre!="") {
587                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
588                                 if(strcasecmp($pre, $xurl[0])==0) {
589                                         //echo "Matched pre\n";
590                                         $prematch = true;
591                                         $startat++;
592                                 }
593                         }
594                 }
595                 
596                 
597                 if($matched < 0) foreach($uconf as $key => $var) {
598                         // if we matched a pre, then we check against the second url component
599                         
600                         $short = $var["shorturl"];
601                         
602                         if($short!="") {
603                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
604                                 if(strcasecmp($xurl[$startat], $short)==0) {
605                                         //echo "Matched\n";
606                                         $matched = $key;
607                                         $startat++;
608                                 }
609                         }
610                 }
611                 
612                 if($matched < 0) {
613                         echo "No such repo<br>";
614                         return;
615                 }
616                 
617                 
618                 // now we find an actual file
619                 $file = "/";
620                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
621                         $file .= "/".$xurl[$i];
622                 }
623                 
624                 // now we want to find repostore/$matched/$file;
625                 $actualfile = "$repostore/$matched/$file";
626                 error_log("Atcualfile is $actualfile");
627                 //echo "Start file for $actualfile\n";
628                 
629                 // first check any directories in $file are in existence
630                 $splfile = explode("/", $file);
631                 if(count($splfile) > 1) {
632                         $tomake = "$repostore/$matched/";
633                         for($i = 0; $i < count($splfile)-1; $i++) {
634                                 $tomake .= "/".$splfile[$i];
635                                 //error_log("making directory $tomake");
636                                 if(!is_dir($tomake)) mkdir($tomake);
637                         }
638                 }
639                 
640                 $reqhead = print_r($_REQUEST, true);
641                 $sevhead = print_r($_SERVER, true);
642                 
643                 error_log("req $reqhead");
644                 error_log("sev $sevhead");
645                 
646                 $rangestart = -1;
647                 $rangelength = -1;
648                 $rangesstr = "";
649                 if(isset($_SERVER["HTTP_RANGE"])) {
650                         // oh shit
651                         $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
652                         $rangesb = explode(",", $rangesa[1]);
653                         $rangesstr = $rangesb[0];
654                         $ranges = explode("-", $rangesb[0]);
655                         $rangestart = $ranges[0];
656                         $rangelength = $ranges[1] - $ranges[0] +1; 
657                         error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
658                 }
659                 
660                 // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
661                 if(is_file($actualfile)) {
662                         // file is stored locally, away we go
663                         if($rangelength != -1) {
664                                 header("HTTP/1.1 206 Partial Content");
665                                 header("Content-Length: ".$rangelength);
666                                 header("Content-Range: bytes $rangesstr/".filesize($actualfile));
667                                 //header("Content-Length: ".filesize($actualfile));
668                         } else {
669                                 header("Content-Length: ".filesize($actualfile));
670                         }
671                         $type = mime_content_type($actualfile);
672                         header("Content-type: $type");
673                         $localfile = fopen($actualfile, "r");
674                         if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
675                         while(!feof($localfile)) {
676                                 // cant make this high cause centos is crap
677                                 if($rangelength!=-1) {
678                                         $data = fread($localfile, $rangelength);
679                                         error_log("data size was ".strlen($data));
680                                 } else {
681                                         $data = fread($localfile, 2048);
682                                 }
683                                 
684                                 echo $data;
685                                 flush();
686                                 
687                                 if($rangelength!=-1) {
688                                         fclose($localfile);
689                                         exit(0);
690                                 }
691                         }
692                         fclose($localfile);
693                 } else if(is_dir($actualfile)) {
694                         //echo "in dir for $actualfile\n";
695                         // here we print the contents of the directory
696                         $this->printDir($actualfile, $file, $url);
697                 } else {
698                         // ok, get the file
699                         //echo "in getcheck\n";
700                         $remotefile = $uconf[$matched]["url"]."/$file";
701                         
702                         // TODO: i should get remote contents with fopen/fread/fwrite as
703                         // it should be more memory conservative and we can push to the end client
704                         // straight away
705                         ignore_user_abort(true);
706                         $rf = fopen($remotefile, "r");
707                         error_log("attempting to get remote file $remotefile");
708
709                         
710                         // hopefully this works. if we get a 30x message, it means we tried to get a directory
711                         // i cant think of another way of dealing with it - but this is UGLY
712                         // also get content length and content type
713                         $clen = 0;
714                         foreach($http_response_header as $key => $val) {
715                                 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
716                                         error_log("got a 30x, must be a directory");
717                                         mkdir($actualfile);
718                                         header("Location: ".$_SERVER["REQUEST_URI"]."/");
719                                         return;
720                                 }
721                                 // get content length form upstream and print
722                                 if(preg_match("/^Content-Length:.*/", $val)) {
723                                         $clen = $val;
724                                         header($val);
725                                 }
726                                 // get content type from upstream and print
727                                 if(preg_match("/^Content-Type:.*/", $val)) {
728                                         header($val);   
729                                 }
730                         }
731                         //error_log("repsonse: $http_response_header");
732                         if(!$rf) {
733                                 // return 404
734                                 header("HTTP/1.0 404 Not Found");
735                         } else {
736                                 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");                              
737                                 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
738                                 fwrite($localsizefile, "$clen");
739                                 fclose($localsizefile);         
740                                 while(!feof($rf)) {
741                                         $data = fread($rf, 8192);
742                                         echo $data;
743                                         fwrite($localfile, $data);
744                                         flush();
745                                 }
746                                 fclose($localfile);
747                                 fclose($rf);
748                                 rename($actualfile.".tmp.data.deleteme", $actualfile);
749                                 //error_log("got actualfile, tried to save as $actualfile, did it work?");
750                         }
751                 }
752                 
753                 //echo "got ".$file." for $url which is $actualfile\n";
754                 
755                 //echo "</html></pre>";
756         }
757         
758         function printDir($dir, $localfile, $baseurl)
759         {
760                 $localfile = preg_replace("/\/\/+/", "/", $localfile);
761                 $uri = $_SERVER["REQUEST_URI"];
762                 $content = "";
763                 if(is_dir($dir)) {
764                         $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
765                         $content .= "<table>";
766                         $dh = opendir($dir);
767                         while(($file = readdir($dh))!==false) {
768                                 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
769                         }
770                         $content .= "</table></body></html>";
771                         
772                         GLCASpageBuilder(null, null, $content);
773                         
774                 } else return false;
775         }
776         
777         function getRepoDetailsYum($url, $ismirrorlist=false)
778         {
779                 $actionurl = $url."/repodata/repomd.xml";
780                 
781                 error_log("Getting for action of $actionurl");
782                 
783                 $ld = file_get_contents($actionurl);
784                 
785                 // so here we try and get what this repository provides (os, version, arch), for yum this
786                 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
787                 
788                 if(!$ld) return false;
789                 
790                 // ok, now we tokenize the url and try and guess at the content
791                 $spurl = explode("/", $url);
792                 
793                 // first, find the OS
794                 $kos = getKnownOSList();
795                 $glt["OS"] = "unknown";
796                 $glt["verison"] = "unknown";
797                 $glt["arch"] = "unknown";
798                 $glt["other"] = "unknown";
799                 foreach($spurl as $comp) {
800                         
801                         // find a name
802                         foreach($kos["os"]["short"] as $kosname => $koslong) {
803                                 //error_log("Comparing $kosname and $koslong with $comp");
804                                 if(strcasecmp($kosname, $comp) == 0) {
805                                         //error_log("got $kosname, $koslong for $comp in $url");
806                                         //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
807                                         $glt["OS"] = $koslong;
808                                 }
809                         }
810                         
811                         // find a version, we assume its going to be something [numbers] and a . (optional)
812                         if(preg_match("/^[0-9.]+$/", $comp)>0) {
813                                 //error_log("version match of $comp");
814                                 $glt["version"] = $comp;
815                         }
816                         
817                         // now architecture, this can be either i?86 or x86_64 - can also be arm or otherwise, but lets just go with this for now
818                         foreach($kos["arch"] as $archinter => $archname ) {
819                                 //error_log("Comparing $archinter, $archname with $comp");
820                                 if(strcasecmp($archname, $comp) == 0) {
821                                         //error_log("arch match of $archname with $comp");
822                                         $glt["arch"] = $archname;
823                                 }
824                         }
825                         
826                         // other is a bt harder, we really have to guess at this one
827                         if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
828                         if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
829                         if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
830                         if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
831                 }
832                 
833                         
834                 return $glt;
835         }
836         
837         function deleteRepo($rkey)
838         {
839                 $uconf = $this->config->getConfigVar("repodata");
840                 $repostore = $this->config->getConfigVar("storagelocation");
841                 
842                 if($uconf !== false) {
843                         $conf = unserialize($uconf);
844                         foreach($conf as $key => $vla) {
845                                 if($key == $rkey) {
846                                         unset($conf["$rkey"]);
847                                         $nconf = serialize($conf);
848                                         system("rm -rf $repostore/$key");
849                                         error_log("remove repo as $rkey");
850                                         $this->config->setConfigVar("repodata", $nconf);
851                                         $this->config->saveConfig();
852                                 }
853                         }
854                 }
855         }
856         
857         function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
858         {
859                 $uconf = $this->config->getConfigVar("repodata");
860                 
861                 $cs["desc"] = $desc;
862                 $cs["os"] = $os;
863                 $cs["version"] = $version;
864                 $cs["arch"] = $arch;
865                 $cs["other"] = $other;
866                 $cs["shorturl"] = $shorturl;
867                 $cs["prefix"] = $prefix;
868                 $cs["url"] = $repurl;
869                 $cs["repotype"] = $repotype;
870                 
871                 
872                 $ckey = 0;
873                 if($uconf !== false) {
874                         $conf = unserialize($uconf);
875                         foreach($conf as $key => $val) {
876                                 $ckey = $key;
877                         }
878                         $ckey++;
879                 }
880                 
881                 $conf[$ckey] = $cs;
882                 
883                 $nconf = serialize($conf);
884                 
885                 error_log("add repo as $ckey");
886                 $this->config->setConfigVar("repodata", $nconf);
887                 $this->config->saveConfig();
888                 
889                 // now create the base structure in the repo
890                 $repostore = $this->config->getConfigVar("storagelocation");
891                 
892                 
893                 // now call update repo
894                 if($init) $this->updateRepoYum($ckey);
895         }
896         
897         function updateRepo($repokey)
898         {
899                 // we only do yum yet
900                 $this->updateRepoYum($repokey);
901         }
902         
903         function updateRepoYum($repokey)
904         {
905                 $repostore = $this->config->getConfigVar("storagelocation");
906                 
907                 $repod = $this->getRepo($repokey);
908                 
909                 $repourl = $repod["url"];
910                 
911                 if(!file_exists("$repostore/$repokey")) {
912                         mkdir("$repostore/$repokey");
913                 }
914                 
915                 if(!file_exists("$repostore/$repokey/repodata")) {
916                         mkdir("$repostore/$repokey/repodata");
917                 }
918                 
919                 //ignore_user_abort(true);
920                 $actionurl = "$repourl/repodata/repomd.xml";
921                 $repomdxml = file_get_contents($actionurl);
922                 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
923                 
924                 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
925                 
926                 
927                 foreach($xml as $key => $var) {
928                         //echo "for key $key has:\n";
929                         //print_r($var);
930                         if($key == "data") {
931                                 $fileloc = $var->location["href"];
932                                 if(!file_exists("$repostore/$repokey/$fileloc")) {
933                                         error_log("getting $fileloc for $repokey on $repourl");
934                                         $dlfile = file_get_contents("$repourl/$fileloc");
935                                         file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
936                                 } else {
937                                         error_log("Not getting $fileloc because we already have it");
938                                 }
939                         }
940                 }
941         }
942         
943         function getRepo($id)
944         {
945                 $uconf = $this->config->getConfigVar("repodata");
946                 if($uconf !== false) {
947                         $lconf = unserialize($uconf);
948                         return $lconf[$id];
949                 } else return false;
950                 
951         }
952         
953         function getRepos()
954         {
955                 $uconf = $this->config->getConfigVar("repodata");
956                 if($uconf !== false) {
957                         return unserialize($uconf);
958                 } else return false;
959                 
960         }
961         
962         private $config;
963 }
964
965 ?>