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 // this is important so downloads dont die
231 ignore_user_abort(true);
233 // get the configurations we need
234 $uconf = unserialize($this->config->getConfigVar("repodata"));
235 $repostore = $this->config->getConfigVar("storagelocation");
237 // this is the tricky one for ranges.
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);
245 error_log("flock did fail, all is right with the world a download is in progress");
247 unlink("$filename.tmp.data.deleteme");
248 unlink("$filename.tmp.data.deleteme.size");
252 // open the remote file
253 $rf = fopen($remoteurl, "r");
256 // get the headers from the remote request and use them to hurt people
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");
262 header("Location: ".$_SERVER["REQUEST_URI"]."/");
265 // get content length form upstream and print
266 if(preg_match("/^Content-Length:.*/", $val)) {
267 // WARNING, THIS IS NOT RIGHT
271 // get content type from upstream and print
272 if(preg_match("/^Content-Type:.*/", $val)) {
277 // open the local files
278 $localfile = fopen($filename.".tmp.data.deleteme", "w");
279 $localsizefile = fopen($filename.".tmp.data.deleteme.size", "w");
283 // this is a nightmare
284 function getRepoForUrl($url)
286 // the way we breakdown a url is to explode it
287 $xurl = split("[/,]", $url);
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");
296 // first we check for /repo/repoid as a url
298 if($xurl[0] == "repo") {
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");
310 if($matched < 0) foreach($uconf as $key => $var) {
311 $pre = $var["prefix"];
314 //echo "Checking pre $pre against ".$xurl[0]."\n";
315 if(strcasecmp($pre, $xurl[0])==0) {
316 //echo "Matched pre\n";
324 if($matched < 0) foreach($uconf as $key => $var) {
325 // if we matched a pre, then we check against the second url component
327 $short = $var["shorturl"];
330 //echo "Checking short $short against ".$xurl[$startat]."\n";
331 if(strcasecmp($xurl[$startat], $short)==0) {
340 echo "No such repo<br>";
345 // now we find an actual file
347 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
348 $file .= "/".$xurl[$i];
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";
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);
367 $reqhead = print_r($_REQUEST, true);
368 $sevhead = print_r($_SERVER, true);
370 error_log("req $reqhead");
371 error_log("sev $sevhead");
376 if(isset($_SERVER["HTTP_RANGE"])) {
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]);
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));
396 header("Content-Length: ".filesize($actualfile));
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));
408 $data = fread($localfile, 2048);
414 if($rangelength!=-1) {
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);
426 //echo "in getcheck\n";
427 $remotefile = $uconf[$matched]["url"]."/$file";
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
432 ignore_user_abort(true);
433 $rf = fopen($remotefile, "r");
434 error_log("attempting to get remote file $remotefile");
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
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");
445 header("Location: ".$_SERVER["REQUEST_URI"]."/");
448 // get content length form upstream and print
449 if(preg_match("/^Content-Length:.*/", $val)) {
453 // get content type from upstream and print
454 if(preg_match("/^Content-Type:.*/", $val)) {
458 //error_log("repsonse: $http_response_header");
461 header("HTTP/1.0 404 Not Found");
463 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
464 $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
465 fwrite($localsizefile, "$clen");
466 fclose($localsizefile);
468 $data = fread($rf, 8192);
470 fwrite($localfile, $data);
475 rename($actualfile.".tmp.data.deleteme", $actualfile);
476 //error_log("got actualfile, tried to save as $actualfile, did it work?");
480 //echo "got ".$file." for $url which is $actualfile\n";
482 //echo "</html></pre>";
485 function printDir($dir, $localfile, $baseurl)
487 $localfile = preg_replace("/\/\/+/", "/", $localfile);
488 $uri = $_SERVER["REQUEST_URI"];
491 $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
492 $content .= "<table>";
494 while(($file = readdir($dh))!==false) {
495 if($file != "." && $file != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
497 $content .= "</table></body></html>";
499 GLCASpageBuilder(null, null, $content);
504 function getRepoDetailsYum($url, $ismirrorlist=false)
506 $actionurl = $url."/repodata/repomd.xml";
508 error_log("Getting for action of $actionurl");
510 $ld = file_get_contents($actionurl);
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)
515 if(!$ld) return false;
517 // ok, now we tokenize the url and try and guess at the content
518 $spurl = explode("/", $url);
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) {
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;
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;
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;
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";
564 function deleteRepo($rkey)
566 $uconf = $this->config->getConfigVar("repodata");
567 $repostore = $this->config->getConfigVar("storagelocation");
569 if($uconf !== false) {
570 $conf = unserialize($uconf);
571 foreach($conf as $key => $vla) {
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();
584 function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
586 $uconf = $this->config->getConfigVar("repodata");
590 $cs["version"] = $version;
592 $cs["other"] = $other;
593 $cs["shorturl"] = $shorturl;
594 $cs["prefix"] = $prefix;
595 $cs["url"] = $repurl;
596 $cs["repotype"] = $repotype;
600 if($uconf !== false) {
601 $conf = unserialize($uconf);
602 foreach($conf as $key => $val) {
610 $nconf = serialize($conf);
612 error_log("add repo as $ckey");
613 $this->config->setConfigVar("repodata", $nconf);
614 $this->config->saveConfig();
616 // now create the base structure in the repo
617 $repostore = $this->config->getConfigVar("storagelocation");
620 // now call update repo
621 if($init) $this->updateRepoYum($ckey);
624 function updateRepo($repokey)
626 // we only do yum yet
627 $this->updateRepoYum($repokey);
630 function updateRepoYum($repokey)
632 $repostore = $this->config->getConfigVar("storagelocation");
634 $repod = $this->getRepo($repokey);
636 $repourl = $repod["url"];
638 if(!file_exists("$repostore/$repokey")) {
639 mkdir("$repostore/$repokey");
642 if(!file_exists("$repostore/$repokey/repodata")) {
643 mkdir("$repostore/$repokey/repodata");
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);
651 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
654 foreach($xml as $key => $var) {
655 //echo "for key $key has:\n";
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);
664 error_log("Not getting $fileloc because we already have it");
670 function getRepo($id)
672 $uconf = $this->config->getConfigVar("repodata");
673 if($uconf !== false) {
674 $lconf = unserialize($uconf);
682 $uconf = $this->config->getConfigVar("repodata");
683 if($uconf !== false) {
684 return unserialize($uconf);