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 getRepoForUrlNew($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)) {
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($filename, $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);
177 header("Content-Range: bytes $rangesstr/".filesize($actualfile));
179 // determine mime type
180 $type = mime_content_type($actualfile);
182 // set mime type header
183 header("Content-type: $type");
185 // open the local file (TODO: error check)
186 $localfile = fopen($actualfile, "r");
187 fseek($localfile, $rangestart, SEEK_SET);
189 // read in the data, god i hope its not big
190 $data = fread($localfile, $rangelength);
196 // and close the file
201 // we're not using range's - good on you installer thingy
202 header("Content-Length: ".filesize($actualfile));
204 // set the mime type header
205 $type = mime_content_type($actualfile);
206 header("Content-type: $type");
208 // open the local file
209 $localfile = fopen($actualfile, "r");
211 // iterate over its length, send 8k at a time
212 while(!feof($localfile)) {
213 // read and send data
214 $data = fread($localfile, 8192);
217 // flush so the client sees the data
227 // TODO: this is the function im working on
228 function downloadAndServe($filename, $repoid, $remoteurl)
230 $uconf = unserialize($this->config->getConfigVar("repodata"));
231 $repostore = $this->config->getConfigVar("storagelocation");
233 // this is the tricky one for ranges.
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);
241 error_log("flock did fail, all is right with the world a download is in progress");
243 unlink("$actualfile.tmp.data.deleteme");
244 unlink("$actualfile.tmp.data.deleteme.size");
249 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
250 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
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");
258 header("Location: ".$_SERVER["REQUEST_URI"]."/");
261 // get content length form upstream and print
262 if(preg_match("/^Content-Length:.*/", $val)) {
266 // get content type from upstream and print
267 if(preg_match("/^Content-Type:.*/", $val)) {
273 // this is a nightmare
274 function getRepoForUrl($url)
276 // the way we breakdown a url is to explode it
277 $xurl = split("[/,]", $url);
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");
286 // first we check for /repo/repoid as a url
288 if($xurl[0] == "repo") {
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");
300 if($matched < 0) foreach($uconf as $key => $var) {
301 $pre = $var["prefix"];
304 //echo "Checking pre $pre against ".$xurl[0]."\n";
305 if(strcasecmp($pre, $xurl[0])==0) {
306 //echo "Matched pre\n";
314 if($matched < 0) foreach($uconf as $key => $var) {
315 // if we matched a pre, then we check against the second url component
317 $short = $var["shorturl"];
320 //echo "Checking short $short against ".$xurl[$startat]."\n";
321 if(strcasecmp($xurl[$startat], $short)==0) {
330 echo "No such repo<br>";
335 // now we find an actual file
337 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
338 $file .= "/".$xurl[$i];
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";
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);
357 $reqhead = print_r($_REQUEST, true);
358 $sevhead = print_r($_SERVER, true);
360 error_log("req $reqhead");
361 error_log("sev $sevhead");
366 if(isset($_SERVER["HTTP_RANGE"])) {
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]);
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));
386 header("Content-Length: ".filesize($actualfile));
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));
398 $data = fread($localfile, 2048);
404 if($rangelength!=-1) {
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);
416 //echo "in getcheck\n";
417 $remotefile = $uconf[$matched]["url"]."/$file";
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
422 ignore_user_abort(true);
423 $rf = fopen($remotefile, "r");
424 error_log("attempting to get remote file $remotefile");
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
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");
435 header("Location: ".$_SERVER["REQUEST_URI"]."/");
438 // get content length form upstream and print
439 if(preg_match("/^Content-Length:.*/", $val)) {
443 // get content type from upstream and print
444 if(preg_match("/^Content-Type:.*/", $val)) {
448 //error_log("repsonse: $http_response_header");
451 header("HTTP/1.0 404 Not Found");
453 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
454 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
455 fwrite($localsizefile, "$clen");
456 fclose($localsizefile);
458 $data = fread($rf, 8192);
460 fwrite($localfile, $data);
465 rename($actualfile.".tmp.data.deleteme", $actualfile);
466 //error_log("got actualfile, tried to save as $actualfile, did it work?");
470 //echo "got ".$file." for $url which is $actualfile\n";
472 //echo "</html></pre>";
475 function printDir($dir, $localfile, $baseurl)
477 $localfile = preg_replace("/\/\/+/", "/", $localfile);
478 $uri = $_SERVER["REQUEST_URI"];
481 $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
482 $content .= "<table>";
484 while(($file = readdir($dh))!==false) {
485 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
487 $content .= "</table></body></html>";
489 GLCASpageBuilder(null, null, $content);
494 function getRepoDetailsYum($url, $ismirrorlist=false)
496 $actionurl = $url."/repodata/repomd.xml";
498 error_log("Getting for action of $actionurl");
500 $ld = file_get_contents($actionurl);
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)
505 if(!$ld) return false;
507 // ok, now we tokenize the url and try and guess at the content
508 $spurl = explode("/", $url);
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) {
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;
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;
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;
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";
554 function deleteRepo($rkey)
556 $uconf = $this->config->getConfigVar("repodata");
557 $repostore = $this->config->getConfigVar("storagelocation");
559 if($uconf !== false) {
560 $conf = unserialize($uconf);
561 foreach($conf as $key => $vla) {
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();
574 function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
576 $uconf = $this->config->getConfigVar("repodata");
580 $cs["version"] = $version;
582 $cs["other"] = $other;
583 $cs["shorturl"] = $shorturl;
584 $cs["prefix"] = $prefix;
585 $cs["url"] = $repurl;
586 $cs["repotype"] = $repotype;
590 if($uconf !== false) {
591 $conf = unserialize($uconf);
592 foreach($conf as $key => $val) {
600 $nconf = serialize($conf);
602 error_log("add repo as $ckey");
603 $this->config->setConfigVar("repodata", $nconf);
604 $this->config->saveConfig();
606 // now create the base structure in the repo
607 $repostore = $this->config->getConfigVar("storagelocation");
610 // now call update repo
611 if($init) $this->updateRepoYum($ckey);
614 function updateRepo($repokey)
616 // we only do yum yet
617 $this->updateRepoYum($repokey);
620 function updateRepoYum($repokey)
622 $repostore = $this->config->getConfigVar("storagelocation");
624 $repod = $this->getRepo($repokey);
626 $repourl = $repod["url"];
628 if(!file_exists("$repostore/$repokey")) {
629 mkdir("$repostore/$repokey");
632 if(!file_exists("$repostore/$repokey/repodata")) {
633 mkdir("$repostore/$repokey/repodata");
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);
641 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
644 foreach($xml as $key => $var) {
645 //echo "for key $key has:\n";
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);
654 error_log("Not getting $fileloc because we already have it");
660 function getRepo($id)
662 $uconf = $this->config->getConfigVar("repodata");
663 if($uconf !== false) {
664 $lconf = unserialize($uconf);
672 $uconf = $this->config->getConfigVar("repodata");
673 if($uconf !== false) {
674 return unserialize($uconf);