5ec7a09448fd8f3fafe0b0dafd89b12f8a062364
[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) && !file_exists("$actualfile.size")) {
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 $rangestr/".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
240                 $this->startDownload($filename, $remoteurl);
241
242                 // give the proc a minute to get going
243                 sleep(2);
244                 clearstatcache();
245
246                 // get the configurations we need
247                 $uconf = unserialize($this->config->getConfigVar("repodata"));
248                 $repostore = $this->config->getConfigVar("storagelocation");
249
250
251
252                 // determine if we're ranged
253                 $ranged = false;
254                 $rangestart = 0;
255                 $rangelength = 0;
256                 $rangestr="";
257                 if(isset($_SERVER["HTTP_RANGE"])) {
258                         // we're using ranges - screw you stupid installer
259                                 
260                         $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
261                         error_log("got range ".$_SERVER["HTTP_RANGE"]." and ".print_r($pr_range, true));
262                                 
263                         // cut up ranges
264                         $rangestart = $pr_range[1];
265                         $rangelength = $pr_range[2] - $pr_range[1] +1;
266                         $rangestr = $pr_range[1]."-".$pr_range[2];
267                         error_log("going ranges at $rangestart, $rangelength, $rangestr");
268                         $ranged = true;
269                 }
270
271                 // open the local files
272
273                 // now, lets determine what state we're in
274                 // we're either - getting and sending
275                 // watching and sending
276                 // or a range (Getting and sending)
277                 // or a range (watching and sending)
278                 // TODO: it may be advicable to start the download as a seperate cli process rather then something goin on here
279                 // so it definitely cant be interrupted.
280
281
282
283                 // first, getting and sending - this is easy.
284                 if (!$ranged) {
285                                 
286                         $localfile = fopen($filename, "r");
287                                 
288                         // this is where the fun starts - but this one isnt too bad.
289                         error_log("OTHERDOWNLOAD: im another downloader, please work");
290                         $fsize = file_get_contents("$filename.size");
291                         header("Content-Length: $fsize");
292                         $sgotten = 0;
293                         while(!feof($localfile)) {
294                                 error_log("OTHERDOWNLOAD: get");
295                                 $data = fread($localfile, 2048);
296                                 if(!$data) {
297                                         error_log("dollardata is pair shaped");
298                                 } else {
299                                         $sgotten += strlen($data);
300                                         if($sgotten > $fsize) {
301                                                 error_log("went plop at sgotten, $sgotten, $fsize");
302                                                 return;
303                                         }
304                                         echo $data;
305                                         flush();
306                                 }
307                         }
308                         fclose($localfile);
309                                 
310                         // need to think about this in pseudo code.
311                         // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
312                         $cursize = filesize($filename);                         
313                                 
314                         $upload_finished = false;
315                         while(!$upload_finished) {
316                                 while($cursize < $fsize && $cursize < ($sgotten+2048)) {
317                                         clearstatcache();
318                                         error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $fsize");
319                                         // sleep until the the filesize is greater then what we're up to, or until the file is finished
320                                         sleep(1);
321                                         $cursize = filesize($filename);
322                                 }
323
324                                 error_log("OTHERDOWNLOAD: continue, $sgotten, $fsize");
325                                 // reopen local file - if it stopped existing, we need to deal with that
326                                 $localfile = fopen($filename, "r");
327
328                                 // UG, we need to ff, how could i forget that
329                                 fseek($localfile, $sgotten);
330
331                                 if(!$localfile) {
332                                         error_log("OTHERDOWNLOAD: something went plop");
333                                         return;
334                                 }
335
336                                 // now loop on the file until we have it at an eof
337                                 while(!feof($localfile)) {
338                                         $data = fread($localfile, 512);
339                                         if(!$data) {
340                                                 error_log("OTHERDOWNLOAD: dollar data went plop");
341                                         } else {
342                                                 $sgotten += strlen($data);
343                                                 echo $data;
344                                                 flush();
345                                         }
346                                 }
347                                 fclose($localfile);
348
349                                 if($sgotten >= $fsize) {
350                                         if($sgotten > $fsize) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
351                                         $upload_finished = true;
352                                 }
353                                 // and we're done
354
355                         }
356                         error_log("OTHERDOWNLOADER: done with");
357                                 
358                         return;
359
360
361                                 
362                                 
363                         // Next painful bit
364                 } else {
365                         // and here too, yay, someone else is doing the
366                         // download, but we're the retards getting a range
367                         $sgotten = 0;
368                                 
369                         $sgatlen = $rangestart+$rangelength;
370                                 
371                         // the problem is here
372                         error_log("Downloader: going ranged as other");
373                         clearstatcache();
374                         $contentlen = file_get_contents($filename.".tmp.data.deleteme.size");
375                         $contenttype = mime_content_type($filename);
376                         header("HTTP/1.1 206 Partial Content");
377                         header("Content-Length: $rangelength");
378                         header("Content-Range: bytes $rangestr/$contentlen");
379                         $contenttype = "Content-Type: application/x-rpm";
380                         
381                         error_log("$contenttype");
382                         header("$contenttype");
383                         
384                         
385                         clearstatcache();
386                                 
387                         error_log("OTHERDOWNLOAD: im another downloader, please work for ranged");
388                                 
389
390                         error_log("OTHERDOWNLOAD: im another downloader, please work");
391                                 
392                         // first we wait until the file reaches $rangestart
393                         while(filesize("$filename") < $rangestart) {
394                                 sleep(1);
395                         }
396                                 
397                         // then we open the file and ff to rangestart
398                         $localfile = fopen($filename, "r");
399                         fseek($localfile, $rangestart);
400                                 
401                         $sgotten = 0;
402                         // need to think about this in pseudo code.
403                         // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
404                         $cursize = filesize($filename.".tmp.data.deleteme");
405                                 
406                                 
407                         $upload_finished = false;
408                         while(!$upload_finished) {
409                                 while($cursize < $sgatlen && $cursize < ($sgotten+2048)) {
410                                         clearstatcache();
411                                         error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $contentlen");
412                                         // sleep until the the filesize is greater then what we're up to, or until the file is finished
413                                         sleep(1);
414                                         $cursize = filesize($filename);
415                                 }
416
417                                 error_log("OTHERDOWNLOAD: continue, $sgotten, $contentlen");
418                                 // reopen local file - if it stopped existing, we need to deal with that
419                                 $localfile = fopen($filename, "r");
420
421                                 // UG, we need to ff, how could i forget that
422                                 fseek($localfile, $sgotten+$rangestart);
423
424                                 if(!$localfile) {
425                                         error_log("OTHERDOWNLOAD: something went plop");
426                                         return;
427                                 }
428
429                                 // now loop on the file until we have it at sgatlen
430                                 while(!feof($localfile) && $sgotten < $rangelength) {
431                                         $left = $rangelength - $sgotten;
432                                         if($left > 512) $lenget = 512;
433                                         else $lenget = $left;
434                                         $data = fread($localfile, $lenget);
435                                         if(!$data) {
436                                                 error_log("OTHERDOWNLOAD: dollar data went plop");
437                                         } else {
438                                                 $sgotten += strlen($data);
439                                                 echo $data;
440                                                 flush();
441                                         }
442                                 }
443                                 fclose($localfile);
444
445                                 if($sgotten >= $rangelength) {
446                                         if($sgotten > $rangelength) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
447                                         $upload_finished = true;
448                                 }
449                                 // and we're done
450
451                         }
452                         error_log("OTHERDOWNLOADER: done with");
453                                 
454                         return;
455                                 
456                 }
457
458
459                 return;
460         }
461
462         function startDownload($file, $url)
463         {
464                 
465                 error_log("background downloader, start");
466                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
467                 if(file_exists("$WEB_ROOT_FS/../bin/downloadfile.php")) {
468                         $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/downloadfile.php '$url' '$file' > /dev/null 2>&1 &";
469                         system($scall);
470                         error_log("started as v, $v, $scall");
471                 } else {
472                         error_log("cant find download helper... dieing");
473                 }
474         }
475
476         // this is a nightmare
477         function getRepoForUrlOld($url)
478         {
479                 // the way we breakdown a url is to explode it
480                 $xurl = split("[/,]", $url);
481
482                 // we first check if [0] is a prefix
483                 // if now, we check for it being a shorturl (lets just do that for now)
484                 $uconf = unserialize($this->config->getConfigVar("repodata"));
485                 $repostore = $this->config->getConfigVar("storagelocation");
486
487                 $matched = -1;
488
489                 // first we check for /repo/repoid as a url
490                 $startat = 0;
491                 if($xurl[0] == "repo") {
492                         $repid = $xurl[1];
493                         error_log("trying to get repo for repoid, $repid");
494                         if(isset($uconf[$repid])) {
495                                 $matched = ((int)($repid));
496                                 error_log("set matched, $matched, $repid");
497                                 $startat +=2;
498                         }
499                 }
500
501
502                 $prematch = false;
503                 if($matched < 0) foreach($uconf as $key => $var) {
504                         $pre = $var["prefix"];
505                                 
506                         if($pre!="") {
507                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
508                                 if(strcasecmp($pre, $xurl[0])==0) {
509                                         //echo "Matched pre\n";
510                                         $prematch = true;
511                                         $startat++;
512                                 }
513                         }
514                 }
515
516
517                 if($matched < 0) foreach($uconf as $key => $var) {
518                         // if we matched a pre, then we check against the second url component
519                                 
520                         $short = $var["shorturl"];
521                                 
522                         if($short!="") {
523                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
524                                 if(strcasecmp($xurl[$startat], $short)==0) {
525                                         //echo "Matched\n";
526                                         $matched = $key;
527                                         $startat++;
528                                 }
529                         }
530                 }
531
532                 if($matched < 0) {
533                         echo "No such repo<br>";
534                         return;
535                 }
536
537
538                 // now we find an actual file
539                 $file = "/";
540                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
541                         $file .= "/".$xurl[$i];
542                 }
543
544                 // now we want to find repostore/$matched/$file;
545                 $actualfile = "$repostore/$matched/$file";
546                 error_log("Atcualfile is $actualfile");
547                 //echo "Start file for $actualfile\n";
548
549                 // first check any directories in $file are in existence
550                 $splfile = explode("/", $file);
551                 if(count($splfile) > 1) {
552                         $tomake = "$repostore/$matched/";
553                         for($i = 0; $i < count($splfile)-1; $i++) {
554                                 $tomake .= "/".$splfile[$i];
555                                 //error_log("making directory $tomake");
556                                 if(!is_dir($tomake)) mkdir($tomake);
557                         }
558                 }
559
560                 $reqhead = print_r($_REQUEST, true);
561                 $sevhead = print_r($_SERVER, true);
562
563                 error_log("req $reqhead");
564                 error_log("sev $sevhead");
565
566                 $rangestart = -1;
567                 $rangelength = -1;
568                 $rangesstr = "";
569                 if(isset($_SERVER["HTTP_RANGE"])) {
570                         // oh shit
571                         $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
572                         $rangesb = explode(",", $rangesa[1]);
573                         $rangesstr = $rangesb[0];
574                         $ranges = explode("-", $rangesb[0]);
575                         $rangestart = $ranges[0];
576                         $rangelength = $ranges[1] - $ranges[0] +1;
577                         error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
578                 }
579
580                 // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
581                 if(is_file($actualfile)) {
582                         // file is stored locally, away we go
583                         if($rangelength != -1) {
584                                 header("HTTP/1.1 206 Partial Content");
585                                 header("Content-Length: ".$rangelength);
586                                 header("Content-Range: bytes $rangesstr/".filesize($actualfile));
587                                 //header("Content-Length: ".filesize($actualfile));
588                         } else {
589                                 header("Content-Length: ".filesize($actualfile));
590                         }
591                         $type = mime_content_type($actualfile);
592                         header("Content-type: $type");
593                         $localfile = fopen($actualfile, "r");
594                         if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
595                         while(!feof($localfile)) {
596                                 // cant make this high cause centos is crap
597                                 if($rangelength!=-1) {
598                                         $data = fread($localfile, $rangelength);
599                                         error_log("data size was ".strlen($data));
600                                 } else {
601                                         $data = fread($localfile, 2048);
602                                 }
603
604                                 echo $data;
605                                 flush();
606
607                                 if($rangelength!=-1) {
608                                         fclose($localfile);
609                                         exit(0);
610                                 }
611                         }
612                         fclose($localfile);
613                 } else if(is_dir($actualfile)) {
614                         //echo "in dir for $actualfile\n";
615                         // here we print the contents of the directory
616                         $this->printDir($actualfile, $file, $url);
617                 } else {
618                         // ok, get the file
619                         //echo "in getcheck\n";
620                         $remotefile = $uconf[$matched]["url"]."/$file";
621                                 
622                         // TODO: i should get remote contents with fopen/fread/fwrite as
623                         // it should be more memory conservative and we can push to the end client
624                         // straight away
625                         ignore_user_abort(true);
626                         $rf = fopen($remotefile, "r");
627                         error_log("attempting to get remote file $remotefile");
628
629                                 
630                         // hopefully this works. if we get a 30x message, it means we tried to get a directory
631                         // i cant think of another way of dealing with it - but this is UGLY
632                         // also get content length and content type
633                         $clen = 0;
634                         foreach($http_response_header as $key => $val) {
635                                 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
636                                         error_log("got a 30x, must be a directory");
637                                         mkdir($actualfile);
638                                         header("Location: ".$_SERVER["REQUEST_URI"]."/");
639                                         return;
640                                 }
641                                 // get content length form upstream and print
642                                 if(preg_match("/^Content-Length:.*/", $val)) {
643                                         $clen = $val;
644                                         header($val);
645                                 }
646                                 // get content type from upstream and print
647                                 if(preg_match("/^Content-Type:.*/", $val)) {
648                                         header($val);
649                                 }
650                         }
651                         //error_log("repsonse: $http_response_header");
652                         if(!$rf) {
653                                 // return 404
654                                 header("HTTP/1.0 404 Not Found");
655                         } else {
656                                 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
657                                 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
658                                 fwrite($localsizefile, "$clen");
659                                 fclose($localsizefile);
660                                 while(!feof($rf)) {
661                                         $data = fread($rf, 8192);
662                                         echo $data;
663                                         fwrite($localfile, $data);
664                                         flush();
665                                 }
666                                 fclose($localfile);
667                                 fclose($rf);
668                                 rename($actualfile.".tmp.data.deleteme", $actualfile);
669                                 //error_log("got actualfile, tried to save as $actualfile, did it work?");
670                         }
671                 }
672
673                 //echo "got ".$file." for $url which is $actualfile\n";
674
675                 //echo "</html></pre>";
676         }
677
678         function printDir($dir, $localfile, $baseurl)
679         {
680                 $localfile = preg_replace("/\/\/+/", "/", $localfile);
681                 $uri = $_SERVER["REQUEST_URI"];
682                 $content = "";
683                 if(is_dir($dir)) {
684                         $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
685                         $content .= "<table>";
686                         $dh = opendir($dir);
687                         while(($file = readdir($dh))!==false) {
688                                 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
689                         }
690                         $content .= "</table></body></html>";
691                                 
692                         GLCASpageBuilder(null, null, $content);
693                                 
694                 } else return false;
695         }
696
697         function getRepoDetailsYum($url, $ismirrorlist=false)
698         {
699                 $actionurl = $url."/repodata/repomd.xml";
700
701                 error_log("Getting for action of $actionurl");
702
703                 $ld = file_get_contents($actionurl);
704
705                 // so here we try and get what this repository provides (os, version, arch), for yum this
706                 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
707
708                 if(!$ld) return false;
709
710                 // ok, now we tokenize the url and try and guess at the content
711                 $spurl = explode("/", $url);
712
713                 // first, find the OS
714                 $kos = getKnownOSList();
715                 $glt["OS"] = "unknown";
716                 $glt["verison"] = "unknown";
717                 $glt["arch"] = "unknown";
718                 $glt["other"] = "unknown";
719                 foreach($spurl as $comp) {
720                                 
721                         // find a name
722                         foreach($kos["os"]["short"] as $kosname => $koslong) {
723                                 //error_log("Comparing $kosname and $koslong with $comp");
724                                 if(strcasecmp($kosname, $comp) == 0) {
725                                         //error_log("got $kosname, $koslong for $comp in $url");
726                                         //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
727                                         $glt["OS"] = $koslong;
728                                 }
729                         }
730                                 
731                         // find a version, we assume its going to be something [numbers] and a . (optional)
732                         if(preg_match("/^[0-9.]+$/", $comp)>0) {
733                                 //error_log("version match of $comp");
734                                 $glt["version"] = $comp;
735                         }
736                                 
737                         // 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
738                         foreach($kos["arch"] as $archinter => $archname ) {
739                                 //error_log("Comparing $archinter, $archname with $comp");
740                                 if(strcasecmp($archname, $comp) == 0) {
741                                         //error_log("arch match of $archname with $comp");
742                                         $glt["arch"] = $archname;
743                                 }
744                         }
745                                 
746                         // other is a bt harder, we really have to guess at this one
747                         if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
748                         if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
749                         if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
750                         if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
751                 }
752
753                         
754                 return $glt;
755         }
756
757         function deleteRepo($rkey)
758         {
759                 $uconf = $this->config->getConfigVar("repodata");
760                 $repostore = $this->config->getConfigVar("storagelocation");
761
762                 if($uconf !== false) {
763                         $conf = unserialize($uconf);
764                         foreach($conf as $key => $vla) {
765                                 if($key == $rkey) {
766                                         unset($conf["$rkey"]);
767                                         $nconf = serialize($conf);
768                                         system("rm -rf $repostore/$key");
769                                         error_log("remove repo as $rkey");
770                                         $this->config->setConfigVar("repodata", $nconf);
771                                         $this->config->saveConfig();
772                                 }
773                         }
774                 }
775         }
776
777         function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
778         {
779                 $uconf = $this->config->getConfigVar("repodata");
780
781                 $cs["desc"] = $desc;
782                 $cs["os"] = $os;
783                 $cs["version"] = $version;
784                 $cs["arch"] = $arch;
785                 $cs["other"] = $other;
786                 $cs["shorturl"] = $shorturl;
787                 $cs["prefix"] = $prefix;
788                 $cs["url"] = $repurl;
789                 $cs["repotype"] = $repotype;
790
791
792                 $ckey = 0;
793                 if($uconf !== false) {
794                         $conf = unserialize($uconf);
795                         foreach($conf as $key => $val) {
796                                 $ckey = $key;
797                         }
798                         $ckey++;
799                 }
800
801                 $conf[$ckey] = $cs;
802
803                 $nconf = serialize($conf);
804
805                 error_log("add repo as $ckey");
806                 $this->config->setConfigVar("repodata", $nconf);
807                 $this->config->saveConfig();
808
809                 // now create the base structure in the repo
810                 $repostore = $this->config->getConfigVar("storagelocation");
811
812
813                 // now call update repo
814                 if($init) $this->updateRepoYum($ckey);
815         }
816
817         function updateRepo($repokey)
818         {
819                 // we only do yum yet
820                 $this->updateRepoYum($repokey);
821         }
822
823         function updateRepoYum($repokey)
824         {
825                 $repostore = $this->config->getConfigVar("storagelocation");
826
827                 $repod = $this->getRepo($repokey);
828
829                 $repourl = $repod["url"];
830
831                 if(!file_exists("$repostore/$repokey")) {
832                         mkdir("$repostore/$repokey");
833                 }
834
835                 if(!file_exists("$repostore/$repokey/repodata")) {
836                         mkdir("$repostore/$repokey/repodata");
837                 }
838
839                 //ignore_user_abort(true);
840                 $actionurl = "$repourl/repodata/repomd.xml";
841                 $repomdxml = file_get_contents($actionurl);
842                 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
843
844                 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
845
846
847                 foreach($xml as $key => $var) {
848                         //echo "for key $key has:\n";
849                         //print_r($var);
850                         if($key == "data") {
851                                 $fileloc = $var->location["href"];
852                                 if(!file_exists("$repostore/$repokey/$fileloc")) {
853                                         error_log("getting $fileloc for $repokey on $repourl");
854                                         $dlfile = file_get_contents("$repourl/$fileloc");
855                                         file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
856                                 } else {
857                                         error_log("Not getting $fileloc because we already have it");
858                                 }
859                         }
860                 }
861         }
862
863         function getRepo($id)
864         {
865                 $uconf = $this->config->getConfigVar("repodata");
866                 if($uconf !== false) {
867                         $lconf = unserialize($uconf);
868                         return $lconf[$id];
869                 } else return false;
870
871         }
872
873         function getRepos()
874         {
875                 $uconf = $this->config->getConfigVar("repodata");
876                 if($uconf !== false) {
877                         return unserialize($uconf);
878                 } else return false;
879
880         }
881
882         private $config;
883 }
884
885 ?>