31a257f06c55ab6e263e48b3d225379847562823
[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                 $uconf = unserialize($this->config->getConfigVar("repodata"));
231                 $repostore = $this->config->getConfigVar("storagelocation");
232                 
233                 // this is the tricky one for ranges.
234                 
235                 // check if a download exists
236                 if(file_exists("$actualfile.tmp.data.deleteme")) {
237                         // a download exists, does it still work
238                         $localtmpfh = fopen("$actualfile.tmp.data.deleteme", "r");
239                         $lockres = flock($localtmpfh, LOCK_EX|LOCK_NB);
240                         if(!$lockres) {
241                                 error_log("flock did fail, all is right with the world a download is in progress");
242                         } else {
243                                 unlink("$actualfile.tmp.data.deleteme");
244                                 unlink("$actualfile.tmp.data.deleteme.size");
245                         }
246                 }
247
248                 
249                 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");                              
250                 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
251                 
252                 
253                 // get the headers from the remote request and use them to hurt people
254                 foreach($http_response_header as $key => $val) {
255                         if(preg_match("/HTTP.*30[1-9].*/", $val)) {
256                                 error_log("got a 30x, must be a directory");
257                                 mkdir($actualfile);
258                                 header("Location: ".$_SERVER["REQUEST_URI"]."/");
259                                 return;
260                         }
261                         // get content length form upstream and print
262                         if(preg_match("/^Content-Length:.*/", $val)) {
263                                 $clen = $val;
264                                 header($val);
265                         }
266                         // get content type from upstream and print
267                         if(preg_match("/^Content-Type:.*/", $val)) {
268                                 header($val);   
269                         }
270                 }
271         }
272         
273         // this is a nightmare
274         function getRepoForUrl($url)
275         {
276                 // the way we breakdown a url is to explode it
277                 $xurl = split("[/,]", $url);
278                 
279                 // we first check if [0] is a prefix
280                 // if now, we check for it being a shorturl (lets just do that for now)
281                 $uconf = unserialize($this->config->getConfigVar("repodata"));
282                 $repostore = $this->config->getConfigVar("storagelocation");
283                 
284                 $matched = -1;
285                 
286                 // first we check for /repo/repoid as a url
287                 $startat = 0;
288                 if($xurl[0] == "repo") {
289                         $repid = $xurl[1];
290                         error_log("trying to get repo for repoid, $repid");
291                         if(isset($uconf[$repid])) {
292                                 $matched = ((int)($repid));
293                                 error_log("set matched, $matched, $repid");
294                                 $startat +=2;
295                         }
296                 }
297                 
298                 
299                 $prematch = false;
300                 if($matched < 0) foreach($uconf as $key => $var) {
301                         $pre = $var["prefix"];
302                         
303                         if($pre!="") {
304                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
305                                 if(strcasecmp($pre, $xurl[0])==0) {
306                                         //echo "Matched pre\n";
307                                         $prematch = true;
308                                         $startat++;
309                                 }
310                         }
311                 }
312                 
313                 
314                 if($matched < 0) foreach($uconf as $key => $var) {
315                         // if we matched a pre, then we check against the second url component
316                         
317                         $short = $var["shorturl"];
318                         
319                         if($short!="") {
320                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
321                                 if(strcasecmp($xurl[$startat], $short)==0) {
322                                         //echo "Matched\n";
323                                         $matched = $key;
324                                         $startat++;
325                                 }
326                         }
327                 }
328                 
329                 if($matched < 0) {
330                         echo "No such repo<br>";
331                         return;
332                 }
333                 
334                 
335                 // now we find an actual file
336                 $file = "/";
337                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
338                         $file .= "/".$xurl[$i];
339                 }
340                 
341                 // now we want to find repostore/$matched/$file;
342                 $actualfile = "$repostore/$matched/$file";
343                 error_log("Atcualfile is $actualfile");
344                 //echo "Start file for $actualfile\n";
345                 
346                 // first check any directories in $file are in existence
347                 $splfile = explode("/", $file);
348                 if(count($splfile) > 1) {
349                         $tomake = "$repostore/$matched/";
350                         for($i = 0; $i < count($splfile)-1; $i++) {
351                                 $tomake .= "/".$splfile[$i];
352                                 //error_log("making directory $tomake");
353                                 if(!is_dir($tomake)) mkdir($tomake);
354                         }
355                 }
356                 
357                 $reqhead = print_r($_REQUEST, true);
358                 $sevhead = print_r($_SERVER, true);
359                 
360                 error_log("req $reqhead");
361                 error_log("sev $sevhead");
362                 
363                 $rangestart = -1;
364                 $rangelength = -1;
365                 $rangesstr = "";
366                 if(isset($_SERVER["HTTP_RANGE"])) {
367                         // oh shit
368                         $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
369                         $rangesb = explode(",", $rangesa[1]);
370                         $rangesstr = $rangesb[0];
371                         $ranges = explode("-", $rangesb[0]);
372                         $rangestart = $ranges[0];
373                         $rangelength = $ranges[1] - $ranges[0] +1; 
374                         error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
375                 }
376                 
377                 // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
378                 if(is_file($actualfile)) {
379                         // file is stored locally, away we go
380                         if($rangelength != -1) {
381                                 header("HTTP/1.1 206 Partial Content");
382                                 header("Content-Length: ".$rangelength);
383                                 header("Content-Range: bytes $rangesstr/".filesize($actualfile));
384                                 //header("Content-Length: ".filesize($actualfile));
385                         } else {
386                                 header("Content-Length: ".filesize($actualfile));
387                         }
388                         $type = mime_content_type($actualfile);
389                         header("Content-type: $type");
390                         $localfile = fopen($actualfile, "r");
391                         if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
392                         while(!feof($localfile)) {
393                                 // cant make this high cause centos is crap
394                                 if($rangelength!=-1) {
395                                         $data = fread($localfile, $rangelength);
396                                         error_log("data size was ".strlen($data));
397                                 } else {
398                                         $data = fread($localfile, 2048);
399                                 }
400                                 
401                                 echo $data;
402                                 flush();
403                                 
404                                 if($rangelength!=-1) {
405                                         fclose($localfile);
406                                         exit(0);
407                                 }
408                         }
409                         fclose($localfile);
410                 } else if(is_dir($actualfile)) {
411                         //echo "in dir for $actualfile\n";
412                         // here we print the contents of the directory
413                         $this->printDir($actualfile, $file, $url);
414                 } else {
415                         // ok, get the file
416                         //echo "in getcheck\n";
417                         $remotefile = $uconf[$matched]["url"]."/$file";
418                         
419                         // TODO: i should get remote contents with fopen/fread/fwrite as
420                         // it should be more memory conservative and we can push to the end client
421                         // straight away
422                         ignore_user_abort(true);
423                         $rf = fopen($remotefile, "r");
424                         error_log("attempting to get remote file $remotefile");
425
426                         
427                         // hopefully this works. if we get a 30x message, it means we tried to get a directory
428                         // i cant think of another way of dealing with it - but this is UGLY
429                         // also get content length and content type
430                         $clen = 0;
431                         foreach($http_response_header as $key => $val) {
432                                 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
433                                         error_log("got a 30x, must be a directory");
434                                         mkdir($actualfile);
435                                         header("Location: ".$_SERVER["REQUEST_URI"]."/");
436                                         return;
437                                 }
438                                 // get content length form upstream and print
439                                 if(preg_match("/^Content-Length:.*/", $val)) {
440                                         $clen = $val;
441                                         header($val);
442                                 }
443                                 // get content type from upstream and print
444                                 if(preg_match("/^Content-Type:.*/", $val)) {
445                                         header($val);   
446                                 }
447                         }
448                         //error_log("repsonse: $http_response_header");
449                         if(!$rf) {
450                                 // return 404
451                                 header("HTTP/1.0 404 Not Found");
452                         } else {
453                                 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");                              
454                                 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
455                                 fwrite($localsizefile, "$clen");
456                                 fclose($localsizefile);         
457                                 while(!feof($rf)) {
458                                         $data = fread($rf, 8192);
459                                         echo $data;
460                                         fwrite($localfile, $data);
461                                         flush();
462                                 }
463                                 fclose($localfile);
464                                 fclose($rf);
465                                 rename($actualfile.".tmp.data.deleteme", $actualfile);
466                                 //error_log("got actualfile, tried to save as $actualfile, did it work?");
467                         }
468                 }
469                 
470                 //echo "got ".$file." for $url which is $actualfile\n";
471                 
472                 //echo "</html></pre>";
473         }
474         
475         function printDir($dir, $localfile, $baseurl)
476         {
477                 $localfile = preg_replace("/\/\/+/", "/", $localfile);
478                 $uri = $_SERVER["REQUEST_URI"];
479                 $content = "";
480                 if(is_dir($dir)) {
481                         $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
482                         $content .= "<table>";
483                         $dh = opendir($dir);
484                         while(($file = readdir($dh))!==false) {
485                                 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
486                         }
487                         $content .= "</table></body></html>";
488                         
489                         GLCASpageBuilder(null, null, $content);
490                         
491                 } else return false;
492         }
493         
494         function getRepoDetailsYum($url, $ismirrorlist=false)
495         {
496                 $actionurl = $url."/repodata/repomd.xml";
497                 
498                 error_log("Getting for action of $actionurl");
499                 
500                 $ld = file_get_contents($actionurl);
501                 
502                 // so here we try and get what this repository provides (os, version, arch), for yum this
503                 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
504                 
505                 if(!$ld) return false;
506                 
507                 // ok, now we tokenize the url and try and guess at the content
508                 $spurl = explode("/", $url);
509                 
510                 // first, find the OS
511                 $kos = getKnownOSList();
512                 $glt["OS"] = "unknown";
513                 $glt["verison"] = "unknown";
514                 $glt["arch"] = "unknown";
515                 $glt["other"] = "unknown";
516                 foreach($spurl as $comp) {
517                         
518                         // find a name
519                         foreach($kos["os"]["short"] as $kosname => $koslong) {
520                                 //error_log("Comparing $kosname and $koslong with $comp");
521                                 if(strcasecmp($kosname, $comp) == 0) {
522                                         //error_log("got $kosname, $koslong for $comp in $url");
523                                         //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
524                                         $glt["OS"] = $koslong;
525                                 }
526                         }
527                         
528                         // find a version, we assume its going to be something [numbers] and a . (optional)
529                         if(preg_match("/^[0-9.]+$/", $comp)>0) {
530                                 //error_log("version match of $comp");
531                                 $glt["version"] = $comp;
532                         }
533                         
534                         // 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
535                         foreach($kos["arch"] as $archinter => $archname ) {
536                                 //error_log("Comparing $archinter, $archname with $comp");
537                                 if(strcasecmp($archname, $comp) == 0) {
538                                         //error_log("arch match of $archname with $comp");
539                                         $glt["arch"] = $archname;
540                                 }
541                         }
542                         
543                         // other is a bt harder, we really have to guess at this one
544                         if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
545                         if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
546                         if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
547                         if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
548                 }
549                 
550                         
551                 return $glt;
552         }
553         
554         function deleteRepo($rkey)
555         {
556                 $uconf = $this->config->getConfigVar("repodata");
557                 $repostore = $this->config->getConfigVar("storagelocation");
558                 
559                 if($uconf !== false) {
560                         $conf = unserialize($uconf);
561                         foreach($conf as $key => $vla) {
562                                 if($key == $rkey) {
563                                         unset($conf["$rkey"]);
564                                         $nconf = serialize($conf);
565                                         system("rm -rf $repostore/$key");
566                                         error_log("remove repo as $rkey");
567                                         $this->config->setConfigVar("repodata", $nconf);
568                                         $this->config->saveConfig();
569                                 }
570                         }
571                 }
572         }
573         
574         function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
575         {
576                 $uconf = $this->config->getConfigVar("repodata");
577                 
578                 $cs["desc"] = $desc;
579                 $cs["os"] = $os;
580                 $cs["version"] = $version;
581                 $cs["arch"] = $arch;
582                 $cs["other"] = $other;
583                 $cs["shorturl"] = $shorturl;
584                 $cs["prefix"] = $prefix;
585                 $cs["url"] = $repurl;
586                 $cs["repotype"] = $repotype;
587                 
588                 
589                 $ckey = 0;
590                 if($uconf !== false) {
591                         $conf = unserialize($uconf);
592                         foreach($conf as $key => $val) {
593                                 $ckey = $key;
594                         }
595                         $ckey++;
596                 }
597                 
598                 $conf[$ckey] = $cs;
599                 
600                 $nconf = serialize($conf);
601                 
602                 error_log("add repo as $ckey");
603                 $this->config->setConfigVar("repodata", $nconf);
604                 $this->config->saveConfig();
605                 
606                 // now create the base structure in the repo
607                 $repostore = $this->config->getConfigVar("storagelocation");
608                 
609                 
610                 // now call update repo
611                 if($init) $this->updateRepoYum($ckey);
612         }
613         
614         function updateRepo($repokey)
615         {
616                 // we only do yum yet
617                 $this->updateRepoYum($repokey);
618         }
619         
620         function updateRepoYum($repokey)
621         {
622                 $repostore = $this->config->getConfigVar("storagelocation");
623                 
624                 $repod = $this->getRepo($repokey);
625                 
626                 $repourl = $repod["url"];
627                 
628                 if(!file_exists("$repostore/$repokey")) {
629                         mkdir("$repostore/$repokey");
630                 }
631                 
632                 if(!file_exists("$repostore/$repokey/repodata")) {
633                         mkdir("$repostore/$repokey/repodata");
634                 }
635                 
636                 //ignore_user_abort(true);
637                 $actionurl = "$repourl/repodata/repomd.xml";
638                 $repomdxml = file_get_contents($actionurl);
639                 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
640                 
641                 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
642                 
643                 
644                 foreach($xml as $key => $var) {
645                         //echo "for key $key has:\n";
646                         //print_r($var);
647                         if($key == "data") {
648                                 $fileloc = $var->location["href"];
649                                 if(!file_exists("$repostore/$repokey/$fileloc")) {
650                                         error_log("getting $fileloc for $repokey on $repourl");
651                                         $dlfile = file_get_contents("$repourl/$fileloc");
652                                         file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
653                                 } else {
654                                         error_log("Not getting $fileloc because we already have it");
655                                 }
656                         }
657                 }
658         }
659         
660         function getRepo($id)
661         {
662                 $uconf = $this->config->getConfigVar("repodata");
663                 if($uconf !== false) {
664                         $lconf = unserialize($uconf);
665                         return $lconf[$id];
666                 } else return false;
667                 
668         }
669         
670         function getRepos()
671         {
672                 $uconf = $this->config->getConfigVar("repodata");
673                 if($uconf !== false) {
674                         return unserialize($uconf);
675                 } else return false;
676                 
677         }
678         
679         private $config;
680 }
681
682 ?>