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