yeah.
[glcas.git] / plugins / repo.php
1 <?php
2 $URL_HANDLERS["*"] = "GLCASRepo";
3
4
5 class GLCASRepo {
6         function __construct($config)
7         {
8                 $this->config = $config;
9                 if($this->config->getConfigVar("storagelocation") == false) {
10                         global $WEB_ROOT_FS;
11                         $storloc = "$WEB_ROOT_FS/../var/glcas/cache/";
12                         if(!file_exists($storloc)) mkdir($storloc);
13                         $this->config->setConfigVar("storagelocation", realpath($storloc));
14                         $this->config->saveConfig();
15                         error_log("set storage location, $storloc");
16                 }
17         }
18         
19         function go($url)
20         {
21                 error_log("repo:go called");
22                 
23                 // figure out what we're doing 
24                 switch($url) {
25                         case "list":
26                                 GLCASpageBuilder($this, "body");
27                                 break;
28                         default:
29                                 $this->getRepoForUrl($url);
30                 }
31         }
32         
33         function body($url)
34         {
35                 // this is how this will work
36                 //$this->decodeUrl();
37                 if(strncasecmp("list", $url, 4)==0) {
38                         echo "i am the repo list";
39                         return;
40                 }
41                 echo "i am the repo, $url";
42         }
43         
44         
45         // TODO: rework this function
46         /*
47          * What i need to do is have a downloader function
48          * that can cope with lots of different shit
49          * but thats a pipe dream
50          * 
51          * what *THIS* function needs to do is
52          * 1) figure out the repo
53          * 2) figure out the file in the repo
54          * 2.1) if its a directory, go to print directory
55          * 3) if the file exists, give it to the user (if a range is specified give the user the range)
56          * 4) if the file does not exist
57          *    - check if a tmp file exists
58          *    - attempt to get an exclusive flock
59          *    - if flock fails, donwload in progress
60          *    - if flock succeeds, truncate file and re-start download
61          *    - if a range request was made, send the range once available
62          *    - if range not available, sleep for 5 and check again.
63          * 
64          * I dont want to code this from scratch, but i probably need to
65          */
66         function getRepoForUrlNew($url)
67         {
68                 $xurl = split("[/,]", $url);
69                 
70                 // first get the config         
71                 $uconf = unserialize($this->config->getConfigVar("repodata"));
72                 $repostore = $this->config->getConfigVar("storagelocation");
73                 
74                 // preset matched to -1
75                 $matched = -1;
76                 
77                 // first we check for /repo/repoid as a url
78                 $startat = 0;
79                 if($xurl[0] == "repo") {
80                         $repid = $xurl[1];
81                         error_log("trying to get repo for repoid, $repid");
82                         if(isset($uconf[$repid])) {
83                                 $matched = ((int)($repid));
84                                 error_log("set matched, $matched, $repid");
85                                 $startat +=2;
86                         }
87                 }
88                 
89                 // now check for a prefix match
90                 $prematch = false;
91                 if($matched < 0) foreach($uconf as $key => $var) {
92                         $pre = $var["prefix"];
93                         
94                         if($pre!="") {
95                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
96                                 if(strcasecmp($pre, $xurl[0])==0) {
97                                         //echo "Matched pre\n";
98                                         $prematch = true;
99                                         $startat++;
100                                 }
101                         }
102                 }
103                 
104                 // next, check for a short url match
105                 if($matched < 0) foreach($uconf as $key => $var) {
106                         // if we matched a pre, then we check against the second url component
107                         
108                         $short = $var["shorturl"];
109                         
110                         if($short!="") {
111                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
112                                 if(strcasecmp($xurl[$startat], $short)==0) {
113                                         //echo "Matched\n";
114                                         $matched = $key;
115                                         $startat++;
116                                 }
117                         }
118                 }
119                 
120                 // TODO: this deterministic bit
121                 // so far nothing has matched - what this next bit needs to do is try and "Determine" a repo from url
122                 // for eg, if a user gets /fedora/x86_64/os we need to return something appropriate
123                 if($matched < 0) {
124                         echo "No such repo<br>";
125                         header("HTTP/1.0 404 Not Found");
126                         return;
127                 }
128                 
129                 
130                 // something was matched, so now we reconstruct the file component of the url
131                 $file = "/";
132                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
133                         $file .= "/".$xurl[$i];
134                 }
135                 
136                 // so, the ultimate url for the file we need is:
137                 $actualfile = "$repostore/$matched/$file";
138                 error_log("Atcualfile is $actualfile");
139                 
140                 // if its a directory, lets do a print
141                 if(is_dir($actualfile)) {
142                         $this->printDir($actualfile, $file, $url);
143                         return;
144                 }
145                 
146                 // check if the file exists and serve it up
147                 if(file_exists($actualfile)) {
148                         $this->serveUpFile($actualFile, $matched);
149                         return;
150                 } else {
151                         // the file does not exist, we now need to go into "download" mode
152                         $remoteurl = $uconf[$matched]["url"]."/$file";
153                         $this->downloadAndServe($actualFile, $matched, $remoteurl);
154                         return;
155                 }
156         }
157         
158         function serveUpFile($filename, $repoid)
159         {
160                 $uconf = unserialize($this->config->getConfigVar("repodata"));
161                 $repostore = $this->config->getConfigVar("storagelocation");
162                 
163                 // figure out the range header garbage that centos/redhat send
164                 if(isset($_SERVER["HTTP_RANGE"])) {
165                         // we're using ranges - screw you stupid installer
166                         $pr_range = preg_split("/[:\-, ]+/", $_SERVER["HTTP_RANGE"]);
167                         
168                         // cut up ranges
169                         $rangestart = $pr_range[1];
170                         $rangelength = $pr_range[2] - $pr_range[1] +1;
171                         $rangestr = $pr_range[1]."-".$pr_range[2];
172                         error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
173                         
174                         // now spit some headers
175                         header("HTTP/1.1 206 Partial Content");
176                         header("Content-Length: ".$rangelength);
177                         header("Content-Range: bytes $rangesstr/".filesize($actualfile));
178                         
179                         // determine mime type
180                         $type = mime_content_type($actualfile);
181                         
182                         // set mime type header
183                         header("Content-type: $type");
184                         
185                         // open the local file (TODO: error check)
186                         $localfile = fopen($actualfile, "r");
187                         fseek($localfile, $rangestart, SEEK_SET);
188                         
189                         // read in the data, god i hope its not big
190                         $data = fread($localfile, $rangelength);
191                         
192                         // lastly, send data
193                         echo $data;
194                         flush();
195                         
196                         // and close the file
197                         fclose($localfile);
198                         return;
199                 } else {
200
201                         // we're not using range's - good on you installer thingy
202                         header("Content-Length: ".filesize($actualfile));
203
204                         // set the mime type header
205                         $type = mime_content_type($actualfile);
206                         header("Content-type: $type");
207                         
208                         // open the local file                  
209                         $localfile = fopen($actualfile, "r");
210                         
211                         // iterate over its length, send 8k at a time
212                         while(!feof($localfile)) {
213                                 // read and send data
214                                 $data = fread($localfile, 8192);
215                                 echo $data;
216                                 
217                                 // flush so the client sees the data
218                                 flush();
219                         }
220                         
221                         // close the file
222                         fclose($localfile);
223                         return;
224                 }               
225         }
226         
227         // TODO: this is the function im working on
228         function downloadAndServe($filename, $repoid, $remoteurl)
229         {
230                 // this is important so downloads dont die
231                 ignore_user_abort(true);
232                 
233                 // get the configurations we need
234                 $uconf = unserialize($this->config->getConfigVar("repodata"));
235                 $repostore = $this->config->getConfigVar("storagelocation");
236                 
237                 // this is the tricky one for ranges.
238                 
239                 // check if a download exists
240                 if(file_exists("$filename.tmp.data.deleteme")) {
241                         // a download exists, does it still work
242                         $localtmpfh = fopen("$filename.tmp.data.deleteme", "r");
243                         $lockres = flock($localtmpfh, LOCK_EX|LOCK_NB);
244                         if(!$lockres) {
245                                 error_log("flock did fail, all is right with the world a download is in progress");
246                         } else {
247                                 unlink("$filename.tmp.data.deleteme");
248                                 unlink("$filename.tmp.data.deleteme.size");
249                         }
250                 }
251
252                 // open the remote file
253                 $rf = fopen($remoteurl, "r");
254                 
255                 
256                 // get the headers from the remote request and use them to hurt people
257                 $contentlen = 0;
258                 foreach($http_response_header as $key => $val) {
259                         if(preg_match("/HTTP.*30[1-9].*/", $val)) {
260                                 error_log("got a 30x, must be a directory");
261                                 mkdir($filename);
262                                 header("Location: ".$_SERVER["REQUEST_URI"]."/");
263                                 return;
264                         }
265                         // get content length form upstream and print
266                         if(preg_match("/^Content-Length:.*/", $val)) {
267                                 // WARNING, THIS IS NOT RIGHT
268                                 $contentlen = $val;
269                                 header($val);
270                         }
271                         // get content type from upstream and print
272                         if(preg_match("/^Content-Type:.*/", $val)) {
273                                 header($val);   
274                         }
275                 }
276                 
277                 // open the local files
278                 $localfile = fopen($filename.".tmp.data.deleteme", "w");                                
279                 $localsizefile = fopen($filename.".tmp.data.deleteme.size", "w");
280                 
281         }
282         
283         // this is a nightmare
284         function getRepoForUrl($url)
285         {
286                 // the way we breakdown a url is to explode it
287                 $xurl = split("[/,]", $url);
288                 
289                 // we first check if [0] is a prefix
290                 // if now, we check for it being a shorturl (lets just do that for now)
291                 $uconf = unserialize($this->config->getConfigVar("repodata"));
292                 $repostore = $this->config->getConfigVar("storagelocation");
293                 
294                 $matched = -1;
295                 
296                 // first we check for /repo/repoid as a url
297                 $startat = 0;
298                 if($xurl[0] == "repo") {
299                         $repid = $xurl[1];
300                         error_log("trying to get repo for repoid, $repid");
301                         if(isset($uconf[$repid])) {
302                                 $matched = ((int)($repid));
303                                 error_log("set matched, $matched, $repid");
304                                 $startat +=2;
305                         }
306                 }
307                 
308                 
309                 $prematch = false;
310                 if($matched < 0) foreach($uconf as $key => $var) {
311                         $pre = $var["prefix"];
312                         
313                         if($pre!="") {
314                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
315                                 if(strcasecmp($pre, $xurl[0])==0) {
316                                         //echo "Matched pre\n";
317                                         $prematch = true;
318                                         $startat++;
319                                 }
320                         }
321                 }
322                 
323                 
324                 if($matched < 0) foreach($uconf as $key => $var) {
325                         // if we matched a pre, then we check against the second url component
326                         
327                         $short = $var["shorturl"];
328                         
329                         if($short!="") {
330                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
331                                 if(strcasecmp($xurl[$startat], $short)==0) {
332                                         //echo "Matched\n";
333                                         $matched = $key;
334                                         $startat++;
335                                 }
336                         }
337                 }
338                 
339                 if($matched < 0) {
340                         echo "No such repo<br>";
341                         return;
342                 }
343                 
344                 
345                 // now we find an actual file
346                 $file = "/";
347                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
348                         $file .= "/".$xurl[$i];
349                 }
350                 
351                 // now we want to find repostore/$matched/$file;
352                 $actualfile = "$repostore/$matched/$file";
353                 error_log("Atcualfile is $actualfile");
354                 //echo "Start file for $actualfile\n";
355                 
356                 // first check any directories in $file are in existence
357                 $splfile = explode("/", $file);
358                 if(count($splfile) > 1) {
359                         $tomake = "$repostore/$matched/";
360                         for($i = 0; $i < count($splfile)-1; $i++) {
361                                 $tomake .= "/".$splfile[$i];
362                                 //error_log("making directory $tomake");
363                                 if(!is_dir($tomake)) mkdir($tomake);
364                         }
365                 }
366                 
367                 $reqhead = print_r($_REQUEST, true);
368                 $sevhead = print_r($_SERVER, true);
369                 
370                 error_log("req $reqhead");
371                 error_log("sev $sevhead");
372                 
373                 $rangestart = -1;
374                 $rangelength = -1;
375                 $rangesstr = "";
376                 if(isset($_SERVER["HTTP_RANGE"])) {
377                         // oh shit
378                         $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
379                         $rangesb = explode(",", $rangesa[1]);
380                         $rangesstr = $rangesb[0];
381                         $ranges = explode("-", $rangesb[0]);
382                         $rangestart = $ranges[0];
383                         $rangelength = $ranges[1] - $ranges[0] +1; 
384                         error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
385                 }
386                 
387                 // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
388                 if(is_file($actualfile)) {
389                         // file is stored locally, away we go
390                         if($rangelength != -1) {
391                                 header("HTTP/1.1 206 Partial Content");
392                                 header("Content-Length: ".$rangelength);
393                                 header("Content-Range: bytes $rangesstr/".filesize($actualfile));
394                                 //header("Content-Length: ".filesize($actualfile));
395                         } else {
396                                 header("Content-Length: ".filesize($actualfile));
397                         }
398                         $type = mime_content_type($actualfile);
399                         header("Content-type: $type");
400                         $localfile = fopen($actualfile, "r");
401                         if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
402                         while(!feof($localfile)) {
403                                 // cant make this high cause centos is crap
404                                 if($rangelength!=-1) {
405                                         $data = fread($localfile, $rangelength);
406                                         error_log("data size was ".strlen($data));
407                                 } else {
408                                         $data = fread($localfile, 2048);
409                                 }
410                                 
411                                 echo $data;
412                                 flush();
413                                 
414                                 if($rangelength!=-1) {
415                                         fclose($localfile);
416                                         exit(0);
417                                 }
418                         }
419                         fclose($localfile);
420                 } else if(is_dir($actualfile)) {
421                         //echo "in dir for $actualfile\n";
422                         // here we print the contents of the directory
423                         $this->printDir($actualfile, $file, $url);
424                 } else {
425                         // ok, get the file
426                         //echo "in getcheck\n";
427                         $remotefile = $uconf[$matched]["url"]."/$file";
428                         
429                         // TODO: i should get remote contents with fopen/fread/fwrite as
430                         // it should be more memory conservative and we can push to the end client
431                         // straight away
432                         ignore_user_abort(true);
433                         $rf = fopen($remotefile, "r");
434                         error_log("attempting to get remote file $remotefile");
435
436                         
437                         // hopefully this works. if we get a 30x message, it means we tried to get a directory
438                         // i cant think of another way of dealing with it - but this is UGLY
439                         // also get content length and content type
440                         $clen = 0;
441                         foreach($http_response_header as $key => $val) {
442                                 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
443                                         error_log("got a 30x, must be a directory");
444                                         mkdir($actualfile);
445                                         header("Location: ".$_SERVER["REQUEST_URI"]."/");
446                                         return;
447                                 }
448                                 // get content length form upstream and print
449                                 if(preg_match("/^Content-Length:.*/", $val)) {
450                                         $clen = $val;
451                                         header($val);
452                                 }
453                                 // get content type from upstream and print
454                                 if(preg_match("/^Content-Type:.*/", $val)) {
455                                         header($val);   
456                                 }
457                         }
458                         //error_log("repsonse: $http_response_header");
459                         if(!$rf) {
460                                 // return 404
461                                 header("HTTP/1.0 404 Not Found");
462                         } else {
463                                 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");                              
464                                 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
465                                 fwrite($localsizefile, "$clen");
466                                 fclose($localsizefile);         
467                                 while(!feof($rf)) {
468                                         $data = fread($rf, 8192);
469                                         echo $data;
470                                         fwrite($localfile, $data);
471                                         flush();
472                                 }
473                                 fclose($localfile);
474                                 fclose($rf);
475                                 rename($actualfile.".tmp.data.deleteme", $actualfile);
476                                 //error_log("got actualfile, tried to save as $actualfile, did it work?");
477                         }
478                 }
479                 
480                 //echo "got ".$file." for $url which is $actualfile\n";
481                 
482                 //echo "</html></pre>";
483         }
484         
485         function printDir($dir, $localfile, $baseurl)
486         {
487                 $localfile = preg_replace("/\/\/+/", "/", $localfile);
488                 $uri = $_SERVER["REQUEST_URI"];
489                 $content = "";
490                 if(is_dir($dir)) {
491                         $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
492                         $content .= "<table>";
493                         $dh = opendir($dir);
494                         while(($file = readdir($dh))!==false) {
495                                 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
496                         }
497                         $content .= "</table></body></html>";
498                         
499                         GLCASpageBuilder(null, null, $content);
500                         
501                 } else return false;
502         }
503         
504         function getRepoDetailsYum($url, $ismirrorlist=false)
505         {
506                 $actionurl = $url."/repodata/repomd.xml";
507                 
508                 error_log("Getting for action of $actionurl");
509                 
510                 $ld = file_get_contents($actionurl);
511                 
512                 // so here we try and get what this repository provides (os, version, arch), for yum this
513                 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
514                 
515                 if(!$ld) return false;
516                 
517                 // ok, now we tokenize the url and try and guess at the content
518                 $spurl = explode("/", $url);
519                 
520                 // first, find the OS
521                 $kos = getKnownOSList();
522                 $glt["OS"] = "unknown";
523                 $glt["verison"] = "unknown";
524                 $glt["arch"] = "unknown";
525                 $glt["other"] = "unknown";
526                 foreach($spurl as $comp) {
527                         
528                         // find a name
529                         foreach($kos["os"]["short"] as $kosname => $koslong) {
530                                 //error_log("Comparing $kosname and $koslong with $comp");
531                                 if(strcasecmp($kosname, $comp) == 0) {
532                                         //error_log("got $kosname, $koslong for $comp in $url");
533                                         //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
534                                         $glt["OS"] = $koslong;
535                                 }
536                         }
537                         
538                         // find a version, we assume its going to be something [numbers] and a . (optional)
539                         if(preg_match("/^[0-9.]+$/", $comp)>0) {
540                                 //error_log("version match of $comp");
541                                 $glt["version"] = $comp;
542                         }
543                         
544                         // 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
545                         foreach($kos["arch"] as $archinter => $archname ) {
546                                 //error_log("Comparing $archinter, $archname with $comp");
547                                 if(strcasecmp($archname, $comp) == 0) {
548                                         //error_log("arch match of $archname with $comp");
549                                         $glt["arch"] = $archname;
550                                 }
551                         }
552                         
553                         // other is a bt harder, we really have to guess at this one
554                         if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
555                         if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
556                         if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
557                         if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
558                 }
559                 
560                         
561                 return $glt;
562         }
563         
564         function deleteRepo($rkey)
565         {
566                 $uconf = $this->config->getConfigVar("repodata");
567                 $repostore = $this->config->getConfigVar("storagelocation");
568                 
569                 if($uconf !== false) {
570                         $conf = unserialize($uconf);
571                         foreach($conf as $key => $vla) {
572                                 if($key == $rkey) {
573                                         unset($conf["$rkey"]);
574                                         $nconf = serialize($conf);
575                                         system("rm -rf $repostore/$key");
576                                         error_log("remove repo as $rkey");
577                                         $this->config->setConfigVar("repodata", $nconf);
578                                         $this->config->saveConfig();
579                                 }
580                         }
581                 }
582         }
583         
584         function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
585         {
586                 $uconf = $this->config->getConfigVar("repodata");
587                 
588                 $cs["desc"] = $desc;
589                 $cs["os"] = $os;
590                 $cs["version"] = $version;
591                 $cs["arch"] = $arch;
592                 $cs["other"] = $other;
593                 $cs["shorturl"] = $shorturl;
594                 $cs["prefix"] = $prefix;
595                 $cs["url"] = $repurl;
596                 $cs["repotype"] = $repotype;
597                 
598                 
599                 $ckey = 0;
600                 if($uconf !== false) {
601                         $conf = unserialize($uconf);
602                         foreach($conf as $key => $val) {
603                                 $ckey = $key;
604                         }
605                         $ckey++;
606                 }
607                 
608                 $conf[$ckey] = $cs;
609                 
610                 $nconf = serialize($conf);
611                 
612                 error_log("add repo as $ckey");
613                 $this->config->setConfigVar("repodata", $nconf);
614                 $this->config->saveConfig();
615                 
616                 // now create the base structure in the repo
617                 $repostore = $this->config->getConfigVar("storagelocation");
618                 
619                 
620                 // now call update repo
621                 if($init) $this->updateRepoYum($ckey);
622         }
623         
624         function updateRepo($repokey)
625         {
626                 // we only do yum yet
627                 $this->updateRepoYum($repokey);
628         }
629         
630         function updateRepoYum($repokey)
631         {
632                 $repostore = $this->config->getConfigVar("storagelocation");
633                 
634                 $repod = $this->getRepo($repokey);
635                 
636                 $repourl = $repod["url"];
637                 
638                 if(!file_exists("$repostore/$repokey")) {
639                         mkdir("$repostore/$repokey");
640                 }
641                 
642                 if(!file_exists("$repostore/$repokey/repodata")) {
643                         mkdir("$repostore/$repokey/repodata");
644                 }
645                 
646                 //ignore_user_abort(true);
647                 $actionurl = "$repourl/repodata/repomd.xml";
648                 $repomdxml = file_get_contents($actionurl);
649                 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
650                 
651                 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
652                 
653                 
654                 foreach($xml as $key => $var) {
655                         //echo "for key $key has:\n";
656                         //print_r($var);
657                         if($key == "data") {
658                                 $fileloc = $var->location["href"];
659                                 if(!file_exists("$repostore/$repokey/$fileloc")) {
660                                         error_log("getting $fileloc for $repokey on $repourl");
661                                         $dlfile = file_get_contents("$repourl/$fileloc");
662                                         file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
663                                 } else {
664                                         error_log("Not getting $fileloc because we already have it");
665                                 }
666                         }
667                 }
668         }
669         
670         function getRepo($id)
671         {
672                 $uconf = $this->config->getConfigVar("repodata");
673                 if($uconf !== false) {
674                         $lconf = unserialize($uconf);
675                         return $lconf[$id];
676                 } else return false;
677                 
678         }
679         
680         function getRepos()
681         {
682                 $uconf = $this->config->getConfigVar("repodata");
683                 if($uconf !== false) {
684                         return unserialize($uconf);
685                 } else return false;
686                 
687         }
688         
689         private $config;
690 }
691
692 ?>