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