2 $URL_HANDLERS["*"] = "GLCASRepo";
6 function __construct($config)
8 $this->config = $config;
9 if($this->config->getConfigVar("storagelocation") == false) {
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");
21 error_log("repo:go called");
23 // figure out what we're doing
26 GLCASpageBuilder($this, "body");
29 $this->getRepoForUrl($url);
35 // this is how this will work
37 if(strncasecmp("list", $url, 4)==0) {
38 echo "i am the repo list";
41 echo "i am the repo, $url";
45 // TODO: rework this function
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
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.
64 * I dont want to code this from scratch, but i probably need to
66 function getRepoForUrl($url)
68 $xurl = split("[/,]", $url);
70 // first get the config
71 $uconf = unserialize($this->config->getConfigVar("repodata"));
72 $repostore = $this->config->getConfigVar("storagelocation");
74 // preset matched to -1
77 // first we check for /repo/repoid as a url
79 if($xurl[0] == "repo") {
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");
89 // now check for a prefix match
91 if($matched < 0) foreach($uconf as $key => $var) {
92 $pre = $var["prefix"];
95 //echo "Checking pre $pre against ".$xurl[0]."\n";
96 if(strcasecmp($pre, $xurl[0])==0) {
97 //echo "Matched pre\n";
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
108 $short = $var["shorturl"];
111 //echo "Checking short $short against ".$xurl[$startat]."\n";
112 if(strcasecmp($xurl[$startat], $short)==0) {
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
124 echo "No such repo<br>";
125 header("HTTP/1.0 404 Not Found");
130 // something was matched, so now we reconstruct the file component of the url
132 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
133 $file .= "/".$xurl[$i];
136 // so, the ultimate url for the file we need is:
137 $actualfile = "$repostore/$matched/$file";
138 error_log("Atcualfile is $actualfile");
140 // if its a directory, lets do a print
141 if(is_dir($actualfile)) {
142 $this->printDir($actualfile, $file, $url);
146 // check if the file exists and serve it up
147 if(file_exists($actualfile) && !file_exists("$actualfile.size")) {
148 $this->serveUpFile($actualfile, $matched);
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);
158 function serveUpFile($actualfile, $repoid)
160 $uconf = unserialize($this->config->getConfigVar("repodata"));
161 $repostore = $this->config->getConfigVar("storagelocation");
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"]);
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]);
174 // now spit some headers
175 header("HTTP/1.1 206 Partial Content");
176 header("Content-Length: ".$rangelength);
179 header("Content-Range: bytes $rangestr/".filesize($actualfile));
181 // determine mime type
182 $type = mime_content_type($actualfile);
184 // set mime type header
185 header("Content-type: $type");
187 // open the local file (TODO: error check)
188 $localfile = fopen($actualfile, "r");
189 fseek($localfile, $rangestart, SEEK_SET);
191 // read in the data, god i hope its not big
192 $data = fread($localfile, $rangelength);
198 // and close the file
203 // we're not using range's - good on you installer thingy
204 header("Content-Length: ".filesize($actualfile));
206 // set the mime type header
207 $type = mime_content_type($actualfile);
208 header("Content-type: $type");
210 // open the local file
211 $localfile = fopen($actualfile, "r");
213 error_log("normal upload went barf");
217 // iterate over its length, send 8k at a time
218 while(!feof($localfile)) {
219 // read and send data
220 $data = fread($localfile, 32768);
223 // flush so the client sees the data
233 // TODO: this is the function im working on
234 // the alternative to this function is that if a file is in the process of being
235 // downloaded, we simply serve from upstream... not a good idea tho unless we create
236 // a local proxy right here - this function is a race condition waiting to be had
237 // lets hope its a good one!
238 function downloadAndServe($filename, $repoid, $remoteurl)
241 $this->startDownload($filename, $remoteurl);
243 // give the proc a minute to get going
247 // get the configurations we need
248 $uconf = unserialize($this->config->getConfigVar("repodata"));
249 $repostore = $this->config->getConfigVar("storagelocation");
253 // determine if we're ranged
258 if(isset($_SERVER["HTTP_RANGE"])) {
259 // we're using ranges - screw you stupid installer
261 $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
262 error_log("got range ".$_SERVER["HTTP_RANGE"]." and ".print_r($pr_range, true));
265 $rangestart = $pr_range[1];
266 $rangelength = $pr_range[2] - $pr_range[1] +1;
267 $rangestr = $pr_range[1]."-".$pr_range[2];
268 error_log("going ranges at $rangestart, $rangelength, $rangestr");
272 // open the local files
274 // now, lets determine what state we're in
275 // we're either - getting and sending
276 // watching and sending
277 // or a range (Getting and sending)
278 // or a range (watching and sending)
279 // TODO: it may be advicable to start the download as a seperate cli process rather then something goin on here
280 // so it definitely cant be interrupted.
284 // first, getting and sending - this is easy.
287 $localfile = fopen($filename, "r");
289 // this is where the fun starts - but this one isnt too bad.
290 error_log("OTHERDOWNLOAD: im another downloader, please work");
291 if(file_exists("$filename.size")) $fsize = file_get_contents("$filename.size");
292 else $fsize = filesize($filename);
293 header("Content-Length: $fsize");
295 while(!feof($localfile)) {
296 $data = fread($localfile, 2048);
298 error_log("dollardata is pair shaped");
300 $sgotten += strlen($data);
301 if($sgotten > $fsize) {
302 error_log("went plop at sgotten, $sgotten, $fsize");
311 // need to think about this in pseudo code.
312 // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
313 $cursize = filesize($filename);
315 $upload_finished = false;
316 while(!$upload_finished) {
317 while($cursize < $fsize && $cursize < ($sgotten+2048)) {
319 error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $fsize");
320 // sleep until the the filesize is greater then what we're up to, or until the file is finished
322 $cursize = filesize($filename);
325 error_log("OTHERDOWNLOAD: continue, $sgotten, $fsize");
326 // reopen local file - if it stopped existing, we need to deal with that
327 $localfile = fopen($filename, "r");
329 // UG, we need to ff, how could i forget that
330 fseek($localfile, $sgotten);
333 error_log("OTHERDOWNLOAD: something went plop");
337 // now loop on the file until we have it at an eof
338 while(!feof($localfile)) {
339 $data = fread($localfile, 512);
341 error_log("OTHERDOWNLOAD: dollar data went plop");
343 $sgotten += strlen($data);
350 if($sgotten >= $fsize) {
351 if($sgotten > $fsize) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
352 $upload_finished = true;
357 error_log("OTHERDOWNLOADER: done with");
366 // and here too, yay, someone else is doing the
367 // download, but we're the retards getting a range
370 $sgatlen = $rangestart+$rangelength;
372 // the problem is here
373 error_log("Downloader: going ranged as other");
375 if(file_exists($filename.".tmp.data.deleteme.size")) $contentlen = file_get_contents($filename.".tmp.data.deleteme.size");
376 else $contentlen = filesize($filename);
377 $contenttype = mime_content_type($filename);
378 header("HTTP/1.1 206 Partial Content");
379 header("Content-Length: $rangelength");
380 header("Content-Range: bytes $rangestr/$contentlen");
381 $contenttype = "Content-Type: application/x-rpm";
383 error_log("$contenttype");
384 header("$contenttype");
390 // first we wait until the file reaches $rangestart
391 while(filesize("$filename") < $rangestart) {
395 // then we open the file and ff to rangestart
396 $localfile = fopen($filename, "r");
397 fseek($localfile, $rangestart);
400 // need to think about this in pseudo code.
401 // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
402 $cursize = filesize($filename);
405 $upload_finished = false;
406 while(!$upload_finished) {
407 while($cursize < $sgatlen && $cursize < ($sgotten+2048)) {
409 error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $contentlen");
410 // sleep until the the filesize is greater then what we're up to, or until the file is finished
412 $cursize = filesize($filename);
415 error_log("OTHERDOWNLOAD: continue, $sgotten, $contentlen");
416 // reopen local file - if it stopped existing, we need to deal with that
417 $localfile = fopen($filename, "r");
419 // UG, we need to ff, how could i forget that
420 fseek($localfile, $sgotten+$rangestart);
423 error_log("OTHERDOWNLOAD: something went plop");
427 // now loop on the file until we have it at sgatlen
428 while(!feof($localfile) && $sgotten < $rangelength) {
429 $left = $rangelength - $sgotten;
430 if($left > 512) $lenget = 512;
431 else $lenget = $left;
432 $data = fread($localfile, $lenget);
434 error_log("OTHERDOWNLOAD: dollar data went plop");
436 $sgotten += strlen($data);
443 if($sgotten >= $rangelength) {
444 if($sgotten > $rangelength) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
445 $upload_finished = true;
450 error_log("OTHERDOWNLOADER: done with");
460 function startDownload($file, $url)
463 error_log("background downloader, start");
464 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
465 if(file_exists("$WEB_ROOT_FS/../bin/downloadfile.php")) {
466 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/downloadfile.php '$url' '$file' > /dev/null 2>&1 &";
469 error_log("cant find download helper... dieing");
473 // this is a nightmare
474 function getRepoForUrlOld($url)
476 // the way we breakdown a url is to explode it
477 $xurl = split("[/,]", $url);
479 // we first check if [0] is a prefix
480 // if now, we check for it being a shorturl (lets just do that for now)
481 $uconf = unserialize($this->config->getConfigVar("repodata"));
482 $repostore = $this->config->getConfigVar("storagelocation");
486 // first we check for /repo/repoid as a url
488 if($xurl[0] == "repo") {
490 error_log("trying to get repo for repoid, $repid");
491 if(isset($uconf[$repid])) {
492 $matched = ((int)($repid));
493 error_log("set matched, $matched, $repid");
500 if($matched < 0) foreach($uconf as $key => $var) {
501 $pre = $var["prefix"];
504 //echo "Checking pre $pre against ".$xurl[0]."\n";
505 if(strcasecmp($pre, $xurl[0])==0) {
506 //echo "Matched pre\n";
514 if($matched < 0) foreach($uconf as $key => $var) {
515 // if we matched a pre, then we check against the second url component
517 $short = $var["shorturl"];
520 //echo "Checking short $short against ".$xurl[$startat]."\n";
521 if(strcasecmp($xurl[$startat], $short)==0) {
530 echo "No such repo<br>";
535 // now we find an actual file
537 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
538 $file .= "/".$xurl[$i];
541 // now we want to find repostore/$matched/$file;
542 $actualfile = "$repostore/$matched/$file";
543 error_log("Atcualfile is $actualfile");
544 //echo "Start file for $actualfile\n";
546 // first check any directories in $file are in existence
547 $splfile = explode("/", $file);
548 if(count($splfile) > 1) {
549 $tomake = "$repostore/$matched/";
550 for($i = 0; $i < count($splfile)-1; $i++) {
551 $tomake .= "/".$splfile[$i];
552 //error_log("making directory $tomake");
553 if(!is_dir($tomake)) mkdir($tomake);
557 $reqhead = print_r($_REQUEST, true);
558 $sevhead = print_r($_SERVER, true);
560 error_log("req $reqhead");
561 error_log("sev $sevhead");
566 if(isset($_SERVER["HTTP_RANGE"])) {
568 $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
569 $rangesb = explode(",", $rangesa[1]);
570 $rangesstr = $rangesb[0];
571 $ranges = explode("-", $rangesb[0]);
572 $rangestart = $ranges[0];
573 $rangelength = $ranges[1] - $ranges[0] +1;
574 error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
577 // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
578 if(is_file($actualfile)) {
579 // file is stored locally, away we go
580 if($rangelength != -1) {
581 header("HTTP/1.1 206 Partial Content");
582 header("Content-Length: ".$rangelength);
583 header("Content-Range: bytes $rangesstr/".filesize($actualfile));
584 //header("Content-Length: ".filesize($actualfile));
586 header("Content-Length: ".filesize($actualfile));
588 $type = mime_content_type($actualfile);
589 header("Content-type: $type");
590 $localfile = fopen($actualfile, "r");
591 if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
592 while(!feof($localfile)) {
593 // cant make this high cause centos is crap
594 if($rangelength!=-1) {
595 $data = fread($localfile, $rangelength);
596 error_log("data size was ".strlen($data));
598 $data = fread($localfile, 2048);
604 if($rangelength!=-1) {
610 } else if(is_dir($actualfile)) {
611 //echo "in dir for $actualfile\n";
612 // here we print the contents of the directory
613 $this->printDir($actualfile, $file, $url);
616 //echo "in getcheck\n";
617 $remotefile = $uconf[$matched]["url"]."/$file";
619 // TODO: i should get remote contents with fopen/fread/fwrite as
620 // it should be more memory conservative and we can push to the end client
622 ignore_user_abort(true);
623 $rf = fopen($remotefile, "r");
624 error_log("attempting to get remote file $remotefile");
627 // hopefully this works. if we get a 30x message, it means we tried to get a directory
628 // i cant think of another way of dealing with it - but this is UGLY
629 // also get content length and content type
631 foreach($http_response_header as $key => $val) {
632 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
633 error_log("got a 30x, must be a directory");
635 header("Location: ".$_SERVER["REQUEST_URI"]."/");
638 // get content length form upstream and print
639 if(preg_match("/^Content-Length:.*/", $val)) {
643 // get content type from upstream and print
644 if(preg_match("/^Content-Type:.*/", $val)) {
648 //error_log("repsonse: $http_response_header");
651 header("HTTP/1.0 404 Not Found");
653 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
654 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
655 fwrite($localsizefile, "$clen");
656 fclose($localsizefile);
658 $data = fread($rf, 8192);
660 fwrite($localfile, $data);
665 rename($actualfile.".tmp.data.deleteme", $actualfile);
666 //error_log("got actualfile, tried to save as $actualfile, did it work?");
670 //echo "got ".$file." for $url which is $actualfile\n";
672 //echo "</html></pre>";
675 function printDir($dir, $localfile, $baseurl)
677 $localfile = preg_replace("/\/\/+/", "/", $localfile);
678 $uri = $_SERVER["REQUEST_URI"];
681 $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
682 $content .= "<table>";
684 while(($file = readdir($dh))!==false) {
685 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
687 $content .= "</table></body></html>";
689 GLCASpageBuilder(null, null, $content);
694 function getRepoDetailsYum($url, $ismirrorlist=false)
696 $actionurl = $url."/repodata/repomd.xml";
698 error_log("Getting for action of $actionurl");
700 $ld = file_get_contents($actionurl);
702 // so here we try and get what this repository provides (os, version, arch), for yum this
703 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
705 if(!$ld) return false;
707 // ok, now we tokenize the url and try and guess at the content
708 $spurl = explode("/", $url);
710 // first, find the OS
711 $kos = getKnownOSList();
712 $glt["OS"] = "unknown";
713 $glt["verison"] = "unknown";
714 $glt["arch"] = "unknown";
715 $glt["other"] = "unknown";
716 foreach($spurl as $comp) {
719 foreach($kos["os"]["short"] as $kosname => $koslong) {
720 //error_log("Comparing $kosname and $koslong with $comp");
721 if(strcasecmp($kosname, $comp) == 0) {
722 //error_log("got $kosname, $koslong for $comp in $url");
723 //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
724 $glt["OS"] = $koslong;
728 // find a version, we assume its going to be something [numbers] and a . (optional)
729 if(preg_match("/^[0-9.]+$/", $comp)>0) {
730 //error_log("version match of $comp");
731 $glt["version"] = $comp;
734 // 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
735 foreach($kos["arch"] as $archinter => $archname ) {
736 //error_log("Comparing $archinter, $archname with $comp");
737 if(strcasecmp($archname, $comp) == 0) {
738 //error_log("arch match of $archname with $comp");
739 $glt["arch"] = $archname;
743 // other is a bt harder, we really have to guess at this one
744 if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
745 if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
746 if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
747 if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
754 function deleteRepo($rkey)
756 $uconf = $this->config->getConfigVar("repodata");
757 $repostore = $this->config->getConfigVar("storagelocation");
759 if($uconf !== false) {
760 $conf = unserialize($uconf);
761 foreach($conf as $key => $vla) {
763 unset($conf["$rkey"]);
764 $nconf = serialize($conf);
765 system("rm -rf $repostore/$key");
766 error_log("remove repo as $rkey");
767 $this->config->setConfigVar("repodata", $nconf);
768 $this->config->saveConfig();
774 function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
776 $uconf = $this->config->getConfigVar("repodata");
780 $cs["version"] = $version;
782 $cs["other"] = $other;
783 $cs["shorturl"] = $shorturl;
784 $cs["prefix"] = $prefix;
785 $cs["url"] = $repurl;
786 $cs["repotype"] = $repotype;
790 if($uconf !== false) {
791 $conf = unserialize($uconf);
792 foreach($conf as $key => $val) {
800 $nconf = serialize($conf);
802 error_log("add repo as $ckey");
803 $this->config->setConfigVar("repodata", $nconf);
804 $this->config->saveConfig();
806 // now create the base structure in the repo
807 $repostore = $this->config->getConfigVar("storagelocation");
810 // now call update repo
811 if($init) $this->updateRepoYum($ckey);
814 function updateRepo($repokey)
816 // we only do yum yet
817 $this->updateRepoYum($repokey);
820 function updateRepoYum($repokey)
822 $repostore = $this->config->getConfigVar("storagelocation");
824 $repod = $this->getRepo($repokey);
826 $repourl = $repod["url"];
828 if(!file_exists("$repostore/$repokey")) {
829 mkdir("$repostore/$repokey");
832 if(!file_exists("$repostore/$repokey/repodata")) {
833 mkdir("$repostore/$repokey/repodata");
836 //ignore_user_abort(true);
837 $actionurl = "$repourl/repodata/repomd.xml";
838 $repomdxml = file_get_contents($actionurl);
839 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
841 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
844 foreach($xml as $key => $var) {
845 //echo "for key $key has:\n";
848 $fileloc = $var->location["href"];
849 if(!file_exists("$repostore/$repokey/$fileloc")) {
850 error_log("getting $fileloc for $repokey on $repourl");
851 $dlfile = file_get_contents("$repourl/$fileloc");
852 file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
854 error_log("Not getting $fileloc because we already have it");
860 function getRepo($id)
862 $uconf = $this->config->getConfigVar("repodata");
863 if($uconf !== false) {
864 $lconf = unserialize($uconf);
872 $uconf = $this->config->getConfigVar("repodata");
873 if($uconf !== false) {
874 return unserialize($uconf);