2 $URL_HANDLERS["*"] = "GLCASRepo";
4 $CRON_CLASSES["GLCASRepo"] = "GLCASRepo";
7 function __construct($config)
9 $this->config = $config;
10 if($this->config->getConfigVar("storagelocation") == false) {
12 $storloc = "$WEB_ROOT_FS/../var/glcas/cache/";
13 if(!file_exists($storloc)) mkdir($storloc);
14 $this->config->setConfigVar("storagelocation", realpath($storloc));
15 $this->config->saveConfig();
16 error_log("set storage location, $storloc");
23 $uconf = unserialize($this->config->getConfigVar("repodata"));
24 $repostore = $this->config->getConfigVar("storagelocation");
28 foreach($uconf as $rkey => $repo) {
29 echo "Repo $rkey: ".$repo["desc"]."<br>";
30 if(!isset($repo["expiretime"])) {
31 echo " - Expire time not set, setting to 2 days by default<br>";
32 $uconf[$rkey]["expiretime"] = 2;
33 $repo["expiretime"] = 2;
34 $this->config->setConfigVar("repodata", serialize($uconf));
35 $this->config->saveConfig();
38 if(!isset($repo["repotype"])) {
39 echo "<font color=\"red\">Repo type not set for repo, setting to yum</font><br>";
40 $uconf[$rkey]["repotype"] = "YUM";
41 $this->config->setConfigVar("repodata", serialize($uconf));
42 $this->config->saveConfig();
43 } else if($repo["repotype"] == "YUM") {
44 if(file_exists("$repostore/$rkey/repodata/repoupdate.lock")) {
45 echo " - <font color=\"red\">Repo locked for update</font><br>";
49 // we still do this next bit, even if its locked because it may be a stray file
50 echo " - Expire time is ".$repo["expiretime"]." days, checking repo<br>";
51 error_log("checking $repostore/$rkey/repodata/repomd.xml");
52 $tdiff = time() - filemtime("$repostore/$rkey/repodata/repomd.xml");
53 $maxtdiff = $repo["expiretime"] * 24 * 3600;
54 if($tdiff > $maxtdiff) {
55 echo " - <font color=\"green\">updating repo</font><br>";
56 $this->updateRepo($rkey);
58 echo " - not updating repo<br>";
60 } else if($repo["repotype"] == "APT") {
61 if(file_exists("$repostore/$rkey/update.lock")) {
62 echo " - <font color=\"red\">Repo locaked for update</font><br>";
64 echo " - Expire time is ".$repo["expiretime"]." days, checking repo<br>";
65 $maxtdiff = $repo["expiretime"] * 24 * 3600;
66 $this->updateRepo($rkey);
73 error_log("repo:go called");
75 // figure out what we're doing
78 GLCASpageBuilder($this, "body");
81 $this->getRepoForUrl($url);
87 // this is how this will work
89 if(strncasecmp("list", $url, 4)==0) {
90 echo "i am the repo list";
93 echo "i am the repo, $url";
97 // TODO: rework this function
99 * What i need to do is have a downloader function
100 * that can cope with lots of different shit
101 * but thats a pipe dream
103 * what *THIS* function needs to do is
104 * 1) figure out the repo
105 * 2) figure out the file in the repo
106 * 2.1) if its a directory, go to print directory
107 * 3) if the file exists, give it to the user (if a range is specified give the user the range)
108 * 4) if the file does not exist
109 * - check if a tmp file exists
110 * - attempt to get an exclusive flock
111 * - if flock fails, donwload in progress
112 * - if flock succeeds, truncate file and re-start download
113 * - if a range request was made, send the range once available
114 * - if range not available, sleep for 5 and check again.
116 * I dont want to code this from scratch, but i probably need to
118 function getRepoForUrl($url)
120 $xurl = split("[/,]", $url);
122 // first get the config
123 $uconf = unserialize($this->config->getConfigVar("repodata"));
124 $repostore = $this->config->getConfigVar("storagelocation");
126 // preset matched to -1
129 // first we check for /repo/repoid as a url
131 if($xurl[0] == "repo") {
133 error_log("trying to get repo for repoid, $repid");
134 if(isset($uconf[$repid])) {
135 $matched = ((int)($repid));
136 error_log("set matched, $matched, $repid");
141 // now check for a prefix match
143 if($matched < 0) foreach($uconf as $key => $var) {
144 $pre = $var["prefix"];
147 //echo "Checking pre $pre against ".$xurl[0]."\n";
148 if(strcasecmp($pre, $xurl[0])==0) {
149 //echo "Matched pre\n";
156 // next, check for a short url match
157 if($matched < 0) foreach($uconf as $key => $var) {
158 // if we matched a pre, then we check against the second url component
160 $short = $var["shorturl"];
163 //echo "Checking short $short against ".$xurl[$startat]."\n";
164 if(strcasecmp($xurl[$startat], $short)==0) {
172 // TODO: this deterministic bit
173 // so far nothing has matched - what this next bit needs to do is try and "Determine" a repo from url
174 // for eg, if a user gets /fedora/x86_64/os we need to return something appropriate
176 echo "No such repo<br>";
177 header("HTTP/1.0 404 Not Found");
183 // something was matched, so now we reconstruct the file component of the url
185 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
186 $file .= "/".$xurl[$i];
189 // so, the ultimate url for the file we need is:
190 $actualfile = "$repostore/$matched/$file";
191 error_log("Atcualfile is $actualfile");
193 // now check for a block in that repo
194 if(isset($uconf[$matched]["blocklist"])) {
195 foreach($uconf[$matched]["blocklist"] as $blockers) {
196 $rfile = ltrim($file, "/");
197 error_log("checking $blockers against $rfile");
198 if(preg_match("/$blockers/", $rfile) > 0) {
199 error_log("should block");
200 header("HTTP/1.0 404 Not Found");
206 // if its a directory, lets do a print
207 if(is_dir($actualfile)) {
208 $this->printDir($actualfile, $file, $url);
212 // check if the file exists and serve it up
213 if(file_exists($actualfile) && !file_exists("$actualfile.size")) {
214 $this->serveUpFile($actualfile, $matched);
217 // the file does not exist, we now need to go into "download" mode
218 $remoteurl = $uconf[$matched]["url"]."/$file";
219 $this->downloadAndServe($actualfile, $matched, $remoteurl);
224 function serveUpFile($actualfile, $repoid)
226 $uconf = unserialize($this->config->getConfigVar("repodata"));
227 $repostore = $this->config->getConfigVar("storagelocation");
229 // figure out the range header garbage that centos/redhat send
230 if(isset($_SERVER["HTTP_RANGE"])) {
231 // we're using ranges - screw you stupid installer
232 $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
235 $rangestart = $pr_range[1];
236 $rangelength = $pr_range[2] - $pr_range[1] +1;
237 $rangestr = $pr_range[1]."-".$pr_range[2];
238 error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
240 // now spit some headers
241 header("HTTP/1.1 206 Partial Content");
242 header("Content-Length: ".$rangelength);
245 header("Content-Range: bytes $rangestr/".filesize($actualfile));
247 // determine mime type
248 $type = mime_content_type($actualfile);
250 // set mime type header
251 header("Content-type: $type");
253 // open the local file (TODO: error check)
254 $localfile = fopen($actualfile, "r");
255 fseek($localfile, $rangestart, SEEK_SET);
257 // read in the data, god i hope its not big
258 $data = fread($localfile, $rangelength);
264 // and close the file
269 // we're not using range's - good on you installer thingy
270 header("Content-Length: ".filesize($actualfile));
272 // set the mime type header
273 $type = mime_content_type($actualfile);
274 header("Content-type: $type");
276 // open the local file
277 $localfile = fopen($actualfile, "r");
279 error_log("normal upload went barf");
283 // iterate over its length, send 8k at a time
284 while(!feof($localfile)) {
285 // read and send data
286 $data = fread($localfile, 32768);
289 // flush so the client sees the data
299 // TODO: this is the function im working on
300 // the alternative to this function is that if a file is in the process of being
301 // downloaded, we simply serve from upstream... not a good idea tho unless we create
302 // a local proxy right here - this function is a race condition waiting to be had
303 // lets hope its a good one!
304 function downloadAndServe($filename, $repoid, $remoteurl)
307 $this->startDownload($filename, $remoteurl);
309 // give the proc a minute to get going
313 // get the configurations we need
314 $uconf = unserialize($this->config->getConfigVar("repodata"));
315 $repostore = $this->config->getConfigVar("storagelocation");
319 // determine if we're ranged
324 if(isset($_SERVER["HTTP_RANGE"])) {
325 // we're using ranges - screw you stupid installer
327 $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
328 error_log("got range ".$_SERVER["HTTP_RANGE"]." and ".print_r($pr_range, true));
331 $rangestart = $pr_range[1];
332 $rangelength = $pr_range[2] - $pr_range[1] +1;
333 $rangestr = $pr_range[1]."-".$pr_range[2];
334 error_log("going ranges at $rangestart, $rangelength, $rangestr");
338 // open the local files
340 // now, lets determine what state we're in
341 // we're either - getting and sending
342 // watching and sending
343 // or a range (Getting and sending)
344 // or a range (watching and sending)
345 // TODO: it may be advicable to start the download as a seperate cli process rather then something goin on here
346 // so it definitely cant be interrupted.
348 // check for a 404 file and wait 2 if it exists - i should really check the timestamp for an updated
349 // file, but thats too much effort for now: TODO: check timestamp on 404 file
351 while(!file_exists("$filename")) {
355 error_log("Sleeping waiting for file");
357 // if 404 file exists, we wait much less time
358 if(file_exists("$filename.404") && $slept > 2) {
359 header("HTTP/1.0 404 Not Found");
363 header("HTTP/1.0 404 Not Found");
369 if(is_dir($filename)) {
371 header("Location: ".$_SERVER["REQUEST_URI"]."/");
376 // first, getting and sending - this is easy.
379 $localfile = fopen($filename, "r");
381 // this is where the fun starts - but this one isnt too bad.
382 error_log("OTHERDOWNLOAD: im another downloader, please work");
383 if(file_exists("$filename.size")) $fsize = file_get_contents("$filename.size");
384 else $fsize = filesize($filename);
385 header("Content-Length: $fsize");
387 while(!feof($localfile)) {
388 $data = fread($localfile, 2048);
390 error_log("dollardata is pair shaped");
392 $sgotten += strlen($data);
393 if($sgotten > $fsize) {
394 error_log("went plop at sgotten, $sgotten, $fsize");
403 // need to think about this in pseudo code.
404 // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
405 $cursize = filesize($filename);
407 $upload_finished = false;
408 while(!$upload_finished) {
409 while($cursize < $fsize && $cursize < ($sgotten+2048)) {
411 error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $fsize");
412 // sleep until the the filesize is greater then what we're up to, or until the file is finished
414 $cursize = filesize($filename);
417 error_log("OTHERDOWNLOAD: continue, $sgotten, $fsize");
418 // reopen local file - if it stopped existing, we need to deal with that
419 $localfile = fopen($filename, "r");
421 // UG, we need to ff, how could i forget that
422 fseek($localfile, $sgotten);
425 error_log("OTHERDOWNLOAD: something went plop");
429 // now loop on the file until we have it at an eof
430 while(!feof($localfile)) {
431 $data = fread($localfile, 512);
433 error_log("OTHERDOWNLOAD: dollar data went plop");
435 $sgotten += strlen($data);
442 if($sgotten >= $fsize) {
443 if($sgotten > $fsize) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
444 $upload_finished = true;
449 error_log("OTHERDOWNLOADER: done with");
458 // and here too, yay, someone else is doing the
459 // download, but we're the retards getting a range
462 $sgatlen = $rangestart+$rangelength;
464 // the problem is here
465 error_log("Downloader: going ranged as other");
467 if(file_exists($filename.".tmp.data.deleteme.size")) $contentlen = file_get_contents($filename.".tmp.data.deleteme.size");
468 else $contentlen = filesize($filename);
469 $contenttype = mime_content_type($filename);
470 header("HTTP/1.1 206 Partial Content");
471 header("Content-Length: $rangelength");
472 header("Content-Range: bytes $rangestr/$contentlen");
473 $contenttype = "Content-Type: application/x-rpm";
475 error_log("$contenttype");
476 header("$contenttype");
482 // first we wait until the file reaches $rangestart
483 while(filesize("$filename") < $rangestart) {
487 // then we open the file and ff to rangestart
488 $localfile = fopen($filename, "r");
489 fseek($localfile, $rangestart);
492 // need to think about this in pseudo code.
493 // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
494 $cursize = filesize($filename);
497 $upload_finished = false;
498 while(!$upload_finished) {
499 while($cursize < $sgatlen && $cursize < ($sgotten+2048)) {
501 error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $contentlen");
502 // sleep until the the filesize is greater then what we're up to, or until the file is finished
504 $cursize = filesize($filename);
507 error_log("OTHERDOWNLOAD: continue, $sgotten, $contentlen");
508 // reopen local file - if it stopped existing, we need to deal with that
509 $localfile = fopen($filename, "r");
511 // UG, we need to ff, how could i forget that
512 fseek($localfile, $sgotten+$rangestart);
515 error_log("OTHERDOWNLOAD: something went plop");
519 // now loop on the file until we have it at sgatlen
520 while(!feof($localfile) && $sgotten < $rangelength) {
521 $left = $rangelength - $sgotten;
522 if($left > 512) $lenget = 512;
523 else $lenget = $left;
524 $data = fread($localfile, $lenget);
526 error_log("OTHERDOWNLOAD: dollar data went plop");
528 $sgotten += strlen($data);
535 if($sgotten >= $rangelength) {
536 if($sgotten > $rangelength) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
537 $upload_finished = true;
542 error_log("OTHERDOWNLOADER: done with");
552 function startDownload($file, $url)
555 error_log("background downloader, start");
556 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
557 if(file_exists("$WEB_ROOT_FS/../bin/downloadfile.php")) {
558 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/downloadfile.php '$url' '$file' > /tmp/dllog 2>&1 &";
561 error_log("cant find download helper... dieing");
565 // this is a nightmare
568 function printDir($dir, $localfile, $baseurl)
570 $localfile = preg_replace("/\/\/+/", "/", $localfile);
571 $uri = $_SERVER["REQUEST_URI"];
574 $content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
575 $content .= "<table>";
579 while(($file = readdir($dh))!==false) {
580 if($file != "." && $file != "..") {
581 if(is_dir("$dir/$file")) {
582 $dirlist[$dirn++] = "$file";
584 $filelist[$filen++] = "$file";
588 if(isset($dirlist)) {
590 foreach($dirlist as $dirs) {
591 $icon = "/icons/folder.png";
592 $content .= "<tr><td><img src=\"$icon\"></td><td><a href=\"$uri/$dirs\">$dirs</a></td><td></td></tr>";
595 if(isset($filelist)) {
597 foreach($filelist as $files) {
598 $fsize = filesize("$dir/$files");
599 $icon = "/icons/text.png";
600 $content .= "<tr><td><img src=\"$icon\"></td><td><a href=\"$uri/$files\">$files</a></td><td>$fsize</td></tr>";
603 $content .= "</table></body></html>";
605 GLCASpageBuilder(null, null, $content);
610 function getRepoDetailsApt($url)
612 $action1 = $url."/dists";
614 // we just want to make sure it exists really
615 error_log("in repo details apt for $url");
616 if(!glcas_isRemoteDir($action1)) {
617 //echo "I cant find any valid APT dists's at $url<br>";
621 // ok, now scan for ubuntu dists as
622 $kos = getKnownOSList();
625 $existing_repo["isrepo"] = true;
626 foreach($kos["apt"] as $key => $val) {
627 //echo "<br>$key, $val<br>";
628 //echo "now check, $action1/$key";
629 if(glcas_isRemoteDir($action1."/$key")) {
630 $existing_repos["knownrepo"][$repos]["name"] = $key;
631 //echo "Found Distro $val<br>";
632 if(glcas_fileExists($action1."/$key/Contents-amd64.gz")) $existing_repos["knownrepo"][$repos]["amd64"] = true;
633 else $existing_repos["knownrepo"][$repos]["amd64"] = false;
634 if(glcas_fileExists($action1."/$key/Contents-i386.gz")) $existing_repos["knownrepo"][$repos]["i386"] = true;
635 else $existing_repos["knownrepo"][$repos]["i386"] = false;
640 $existing_repos["nrepos"] = $repos;
642 // TODO: these need to be "calculated"
643 $existing_repos["distros"] = "Ubuntu, Debian";
644 $existing_repos["versions"] = "8.04LTS, 9.10, 10.04LTS, 10.10, 11.04, 11.10";
645 $existing_repos["arch"] = "x86_64, i386";
648 return $existing_repos;
652 function getRepoDetailsYum($url, $ismirrorlist=false)
654 $actionurl = $url."/repodata/repomd.xml";
656 error_log("Getting for action of $actionurl");
658 $ld = file_get_contents($actionurl);
660 // so here we try and get what this repository provides (os, version, arch), for yum this
661 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
663 if(!$ld) return false;
665 // ok, now we tokenize the url and try and guess at the content
666 $spurl = explode("/", $url);
668 // first, find the OS
669 $kos = getKnownOSList();
670 $glt["OS"] = "unknown";
671 $glt["verison"] = "unknown";
672 $glt["arch"] = "unknown";
673 $glt["other"] = "unknown";
674 foreach($spurl as $comp) {
677 foreach($kos["os"]["short"] as $kosname => $koslong) {
678 //error_log("Comparing $kosname and $koslong with $comp");
679 if(strcasecmp($kosname, $comp) == 0) {
680 //error_log("got $kosname, $koslong for $comp in $url");
681 //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
682 $glt["OS"] = $koslong;
686 // find a version, we assume its going to be something [numbers] and a . (optional)
687 if(preg_match("/^[0-9.]+$/", $comp)>0) {
688 //error_log("version match of $comp");
689 $glt["version"] = $comp;
692 // 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
693 foreach($kos["arch"] as $archinter => $archname ) {
694 //error_log("Comparing $archinter, $archname with $comp");
695 if(strcasecmp($archname, $comp) == 0) {
696 //error_log("arch match of $archname with $comp");
697 $glt["arch"] = $archname;
701 // other is a bt harder, we really have to guess at this one
702 if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
703 if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
704 if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
705 if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
712 function deleteRepo($rkey)
714 $uconf = $this->config->getConfigVar("repodata");
715 $repostore = $this->config->getConfigVar("storagelocation");
717 if($uconf !== false) {
718 $conf = unserialize($uconf);
719 foreach($conf as $key => $vla) {
721 unset($conf["$rkey"]);
722 $nconf = serialize($conf);
723 system("rm -rf $repostore/$key");
724 error_log("remove repo as $rkey");
725 $this->config->setConfigVar("repodata", $nconf);
726 $this->config->saveConfig();
732 function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init, $expiretime, $blocklist=null)
734 $uconf = $this->config->getConfigVar("repodata");
738 $cs["version"] = $version;
740 $cs["other"] = $other;
741 $cs["shorturl"] = $shorturl;
742 $cs["prefix"] = $prefix;
743 $cs["url"] = $repurl;
744 $cs["repotype"] = $repotype;
745 $cs["expiretime"] = $expiretime;
746 if($blocklist != null) {
747 $cs["blocklist"] = $blocklist;
752 if($uconf !== false) {
753 $conf = unserialize($uconf);
754 foreach($conf as $key => $val) {
762 $nconf = serialize($conf);
764 error_log("add repo as $ckey");
765 $this->config->setConfigVar("repodata", $nconf);
766 $this->config->saveConfig();
768 // now create the base structure in the repo
769 $repostore = $this->config->getConfigVar("storagelocation");
772 // now call update repo
773 if($init) $this->updateRepoYum($ckey);
776 function updateRepo($repokey)
778 // we only do yum yet
779 $repod = $this->getRepo($repokey);
781 error_log("in update repo");
783 if($repod["repotype"] == "YUM") $this->updateRepoYum($repokey);
784 if($repod["repotype"] == "APT") $this->updateRepoApt($repokey);
787 function updateRepoApt($repokey)
789 $repostore = $this->config->getConfigVar("storagelocation");
791 $repod = $this->getRepo($repokey);
793 $repourl = $repod["url"];
795 if(!file_exists("$repostore/$repokey")) {
796 mkdir("$repostore/$repokey");
799 error_log("background apt repo update, start");
800 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
801 if(file_exists("$WEB_ROOT_FS/../bin/downloadfile.php")) {
802 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/updateaptrepo.php '$repourl' '$repostore/$repokey/' > /tmp/updateaptrepo.$repokey.log 2>&1 &";
805 error_log("cant find download apt helper... dieing");
809 function updateRepoYum($repokey)
811 $repostore = $this->config->getConfigVar("storagelocation");
813 $repod = $this->getRepo($repokey);
815 $repourl = $repod["url"];
817 if(!file_exists("$repostore/$repokey")) {
818 mkdir("$repostore/$repokey");
821 if(!file_exists("$repostore/$repokey/repodata")) {
822 mkdir("$repostore/$repokey/repodata");
825 error_log("background yum repo update, start");
826 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
827 if(file_exists("$WEB_ROOT_FS/../bin/downloadfile.php")) {
828 $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/updateyumrepo.php '$repourl' '$repostore/$repokey/' > /tmp/updateyumrepo.$repokey.log 2>&1 &";
831 error_log("cant find download yum helper... dieing");
835 //ignore_user_abort(true);
838 function getRepo($id)
840 $uconf = $this->config->getConfigVar("repodata");
841 if($uconf !== false) {
842 $lconf = unserialize($uconf);
850 $uconf = $this->config->getConfigVar("repodata");
851 if($uconf !== false) {
852 return unserialize($uconf);