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