function __construct($config)
{
$this->config = $config;
- if($this->config->getConfigVar("storagelocation") == false) {
+ if($this->config->getConfig("storagelocation") == false) {
global $WEB_ROOT_FS;
$storloc = "$WEB_ROOT_FS/../var/glcas/cache/";
if(!file_exists($storloc)) mkdir($storloc);
- $this->config->setConfigVar("storagelocation", realpath($storloc));
- $this->config->saveConfig();
+ $this->config->setConfig("storagelocation", realpath($storloc));
error_log("set storage location, $storloc");
}
}
+
+ // with the move to the sqlite db, this is going to get painful
function cron()
{
//echo "<pre>";
- $uconf = unserialize($this->config->getConfigVar("repodata"));
- $repostore = $this->config->getConfigVar("storagelocation");
+ $uconf = unserialize($this->config->getConfig("repodata"));
+ $repostore = $this->config->getConfig("storagelocation");
//print_r($uconf);
//echo "</pre>";
foreach($uconf as $rkey => $repo) {
echo "Repo $rkey: ".$repo["desc"]."<br>";
+ if(isset($repo["frozen"])) {
+ error_log("repo froze set");
+ $repofrozen = $repo["frozen"];
+ } else {
+ error_log("repo froze not set");
+ $repofrozen = false;
+ }
if(!isset($repo["expiretime"])) {
echo " - Expire time not set, setting to 2 days by default<br>";
$uconf[$rkey]["expiretime"] = 2;
$repo["expiretime"] = 2;
- $this->config->setConfigVar("repodata", serialize($uconf));
+ $this->config->setConfig("repodata", serialize($uconf));
$this->config->saveConfig();
}
- if(file_exists("$repostore/$rkey/repodata/repoupdate.lock")) {
- echo " - <font color=\"red\">Repo locked for update</font><br>";
-
- }
-
- // we still do this next bit, even if its locked because it may be a stray file
- echo " - Expire time is ".$repo["expiretime"]." days, checking repo<br>";
- error_log("checking $repostore/$rkey/repodata/repomd.xml");
- $tdiff = time() - filemtime("$repostore/$rkey/repodata/repomd.xml");
- $maxtdiff = $repo["expiretime"] * 24 * 3600;
- if($tdiff > $maxtdiff) {
- echo " - <font color=\"green\">updating repo</font><br>";
- $this->updateRepo($rkey);
- } else {
- echo " - not updating repo<br>";
+ if(!isset($repo["repotype"])) {
+ echo "<font color=\"red\">Repo type not set for repo, setting to yum</font><br>";
+ $uconf[$rkey]["repotype"] = "YUM";
+ $this->config->setConfig("repodata", serialize($uconf));
+ $this->config->saveConfig();
+ } else if($repo["repotype"] == "YUM") {
+ if($repofrozen) {
+ error_log("repo frozen");
+ echo " - <font color=\"blue\">Repo Frozen</font><br>";
+ } else {
+ if(file_exists("$repostore/$rkey/repodata/repoupdate.lock")) {
+ echo " - <font color=\"red\">Repo locked for update</font><br>";
+
+ }
+
+ // we still do this next bit, even if its locked because it may be a stray file
+ echo " - Expire time is ".$repo["expiretime"]." days, checking repo<br>";
+ error_log("checking $repostore/$rkey/repodata/repomd.xml");
+ $tdiff = time() - filemtime("$repostore/$rkey/repodata/repomd.xml");
+ $maxtdiff = $repo["expiretime"] * 24 * 3600;
+ if($tdiff > $maxtdiff) {
+ echo " - <font color=\"green\">updating repo</font><br>";
+ $this->updateRepo($rkey);
+ } else {
+ echo " - not updating repo<br>";
+ }
+ }
+ } else if($repo["repotype"] == "APT") {
+ if($repofrozen) {
+ error_log("repo frozen");
+ echo " - <font color=\"blue\">Repo Frozen</font><br>";
+ } else {
+ if(file_exists("$repostore/$rkey/update.lock")) {
+ echo " - <font color=\"red\">Repo locaked for update</font><br>";
+ }
+ echo " - Expire time is ".$repo["expiretime"]." days, checking repo<br>";
+ $maxtdiff = $repo["expiretime"] * 24 * 3600;
+ // now we check for outdated release files
+ $dh = opendir("$repostore/$rkey/dists/");
+ $needsupdate = false;
+ while(($fn = readdir($dh))!==false) {
+ if($fn != "." && $fn != "..") {
+ if(file_exists("$repostore/$rkey/dists/$fn/Release")) {
+ $tdiff = time() - filemtime("$repostore/$rkey/dists/$fn/Release");
+ if($tdiff > $maxtdiff) $needsupdate = true;
+ }
+ }
+ }
+ if($needsupdate) {
+ echo " - Spawn updater<br>";
+ $this->updateRepo($rkey);
+ } else {
+ echo " - Up to date<br>";
+ }
+ }
}
}
}
$xurl = split("[/,]", $url);
// first get the config
- $uconf = unserialize($this->config->getConfigVar("repodata"));
- $repostore = $this->config->getConfigVar("storagelocation");
+ $uconf = unserialize($this->config->getConfig("repodata"));
+ $repostore = $this->config->getConfig("storagelocation");
// preset matched to -1
$matched = -1;
header("HTTP/1.0 404 Not Found");
return;
}
+
// something was matched, so now we reconstruct the file component of the url
// so, the ultimate url for the file we need is:
$actualfile = "$repostore/$matched/$file";
error_log("Atcualfile is $actualfile");
-
+
+ // now check for a block in that repo
+ if(isset($uconf[$matched]["blocklist"])) {
+ foreach($uconf[$matched]["blocklist"] as $blockers) {
+ $rfile = ltrim($file, "/");
+ error_log("checking $blockers against $rfile");
+ if(preg_match("/$blockers/", $rfile) > 0) {
+ error_log("should block");
+ header("HTTP/1.0 404 Not Found");
+ return;
+ }
+ }
+ }
+
// if its a directory, lets do a print
if(is_dir($actualfile)) {
$this->printDir($actualfile, $file, $url);
function serveUpFile($actualfile, $repoid)
{
- $uconf = unserialize($this->config->getConfigVar("repodata"));
- $repostore = $this->config->getConfigVar("storagelocation");
+ $uconf = unserialize($this->config->getConfig("repodata"));
+ $repostore = $this->config->getConfig("storagelocation");
// figure out the range header garbage that centos/redhat send
if(isset($_SERVER["HTTP_RANGE"])) {
clearstatcache();
// get the configurations we need
- $uconf = unserialize($this->config->getConfigVar("repodata"));
- $repostore = $this->config->getConfigVar("storagelocation");
+ $uconf = unserialize($this->config->getConfig("repodata"));
+ $repostore = $this->config->getConfig("storagelocation");
}
// this is a nightmare
- function getRepoForUrlOld($url)
- {
- // the way we breakdown a url is to explode it
- $xurl = split("[/,]", $url);
-
- // we first check if [0] is a prefix
- // if now, we check for it being a shorturl (lets just do that for now)
- $uconf = unserialize($this->config->getConfigVar("repodata"));
- $repostore = $this->config->getConfigVar("storagelocation");
-
- $matched = -1;
-
- // first we check for /repo/repoid as a url
- $startat = 0;
- if($xurl[0] == "repo") {
- $repid = $xurl[1];
- error_log("trying to get repo for repoid, $repid");
- if(isset($uconf[$repid])) {
- $matched = ((int)($repid));
- error_log("set matched, $matched, $repid");
- $startat +=2;
- }
- }
-
- $prematch = false;
- if($matched < 0) foreach($uconf as $key => $var) {
- $pre = $var["prefix"];
-
- if($pre!="") {
- //echo "Checking pre $pre against ".$xurl[0]."\n";
- if(strcasecmp($pre, $xurl[0])==0) {
- //echo "Matched pre\n";
- $prematch = true;
- $startat++;
- }
- }
- }
-
-
- if($matched < 0) foreach($uconf as $key => $var) {
- // if we matched a pre, then we check against the second url component
-
- $short = $var["shorturl"];
-
- if($short!="") {
- //echo "Checking short $short against ".$xurl[$startat]."\n";
- if(strcasecmp($xurl[$startat], $short)==0) {
- //echo "Matched\n";
- $matched = $key;
- $startat++;
- }
- }
- }
-
- if($matched < 0) {
- echo "No such repo<br>";
- return;
- }
-
-
- // now we find an actual file
- $file = "/";
- if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
- $file .= "/".$xurl[$i];
- }
-
- // now we want to find repostore/$matched/$file;
- $actualfile = "$repostore/$matched/$file";
- error_log("Atcualfile is $actualfile");
- //echo "Start file for $actualfile\n";
-
- // first check any directories in $file are in existence
- $splfile = explode("/", $file);
- if(count($splfile) > 1) {
- $tomake = "$repostore/$matched/";
- for($i = 0; $i < count($splfile)-1; $i++) {
- $tomake .= "/".$splfile[$i];
- //error_log("making directory $tomake");
- if(!is_dir($tomake)) mkdir($tomake);
- }
- }
-
- $reqhead = print_r($_REQUEST, true);
- $sevhead = print_r($_SERVER, true);
-
- error_log("req $reqhead");
- error_log("sev $sevhead");
-
- $rangestart = -1;
- $rangelength = -1;
- $rangesstr = "";
- if(isset($_SERVER["HTTP_RANGE"])) {
- // oh shit
- $rangesa = explode("=", $_SERVER["HTTP_RANGE"]);
- $rangesb = explode(",", $rangesa[1]);
- $rangesstr = $rangesb[0];
- $ranges = explode("-", $rangesb[0]);
- $rangestart = $ranges[0];
- $rangelength = $ranges[1] - $ranges[0] +1;
- error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
- }
-
- // i have to support http_range cause REDHAT/CENTOS IS annoying as all hell. christ, why do this?
- if(is_file($actualfile)) {
- // file is stored locally, away we go
- if($rangelength != -1) {
- header("HTTP/1.1 206 Partial Content");
- header("Content-Length: ".$rangelength);
- header("Content-Range: bytes $rangesstr/".filesize($actualfile));
- //header("Content-Length: ".filesize($actualfile));
- } else {
- header("Content-Length: ".filesize($actualfile));
- }
- $type = mime_content_type($actualfile);
- header("Content-type: $type");
- $localfile = fopen($actualfile, "r");
- if($rangestart!=-1) fseek($localfile, $rangestart, SEEK_SET);
- while(!feof($localfile)) {
- // cant make this high cause centos is crap
- if($rangelength!=-1) {
- $data = fread($localfile, $rangelength);
- error_log("data size was ".strlen($data));
- } else {
- $data = fread($localfile, 2048);
- }
-
- echo $data;
- flush();
-
- if($rangelength!=-1) {
- fclose($localfile);
- exit(0);
- }
- }
- fclose($localfile);
- } else if(is_dir($actualfile)) {
- //echo "in dir for $actualfile\n";
- // here we print the contents of the directory
- $this->printDir($actualfile, $file, $url);
- } else {
- // ok, get the file
- //echo "in getcheck\n";
- $remotefile = $uconf[$matched]["url"]."/$file";
-
- // TODO: i should get remote contents with fopen/fread/fwrite as
- // it should be more memory conservative and we can push to the end client
- // straight away
- ignore_user_abort(true);
- $rf = fopen($remotefile, "r");
- error_log("attempting to get remote file $remotefile");
-
-
- // hopefully this works. if we get a 30x message, it means we tried to get a directory
- // i cant think of another way of dealing with it - but this is UGLY
- // also get content length and content type
- $clen = 0;
- foreach($http_response_header as $key => $val) {
- if(preg_match("/HTTP.*30[1-9].*/", $val)) {
- error_log("got a 30x, must be a directory");
- mkdir($actualfile);
- header("Location: ".$_SERVER["REQUEST_URI"]."/");
- return;
- }
- // get content length form upstream and print
- if(preg_match("/^Content-Length:.*/", $val)) {
- $clen = $val;
- header($val);
- }
- // get content type from upstream and print
- if(preg_match("/^Content-Type:.*/", $val)) {
- header($val);
- }
- }
- //error_log("repsonse: $http_response_header");
- if(!$rf) {
- // return 404
- header("HTTP/1.0 404 Not Found");
- } else {
- $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
- $localsizefile = fopen($actualfile.".tmp.data.deleteme.size", "w");
- fwrite($localsizefile, "$clen");
- fclose($localsizefile);
- while(!feof($rf)) {
- $data = fread($rf, 8192);
- echo $data;
- fwrite($localfile, $data);
- flush();
- }
- fclose($localfile);
- fclose($rf);
- rename($actualfile.".tmp.data.deleteme", $actualfile);
- //error_log("got actualfile, tried to save as $actualfile, did it work?");
- }
- }
-
- //echo "got ".$file." for $url which is $actualfile\n";
-
- //echo "</html></pre>";
- }
function printDir($dir, $localfile, $baseurl)
{
$content .= "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
$content .= "<table>";
$dh = opendir($dir);
+ $dirn = 0;
+ $filen = 0;
while(($file = readdir($dh))!==false) {
if($file != "." && $file != "..") {
if(is_dir("$dir/$file")) {
- $fsize = "";
- $icon = "/icons/folder.png";
+ $dirlist[$dirn++] = "$file";
} else {
- $fsize = filesize("$dir/$file");
- $icon = "/icons/text.png";
+ $filelist[$filen++] = "$file";
}
- $content .= "<tr><td><img src=\"$icon\"></td><td><a href=\"$uri/$file\">$file</a></td><td>$fsize</td></tr>";
}
}
+ if(isset($dirlist)) {
+ sort($dirlist);
+ foreach($dirlist as $dirs) {
+ $icon = "/icons/folder.png";
+ $content .= "<tr><td><img src=\"$icon\"></td><td><a href=\"$uri/$dirs\">$dirs</a></td><td></td></tr>";
+ }
+ }
+ if(isset($filelist)) {
+ sort($filelist);
+ foreach($filelist as $files) {
+ $fsize = filesize("$dir/$files");
+ $icon = "/icons/text.png";
+ $content .= "<tr><td><img src=\"$icon\"></td><td><a href=\"$uri/$files\">$files</a></td><td>$fsize</td></tr>";
+ }
+ }
$content .= "</table></body></html>";
GLCASpageBuilder(null, null, $content);
//echo "<br>$key, $val<br>";
//echo "now check, $action1/$key";
if(glcas_isRemoteDir($action1."/$key")) {
- $existing_repos["knownrepo"][$repos] = $key;
- $repos++;
+ $existing_repos["knownrepo"][$repos]["name"] = $key;
//echo "Found Distro $val<br>";
+ if(glcas_fileExists($action1."/$key/Contents-amd64.gz")) $existing_repos["knownrepo"][$repos]["amd64"] = true;
+ else $existing_repos["knownrepo"][$repos]["amd64"] = false;
+ if(glcas_fileExists($action1."/$key/Contents-i386.gz")) $existing_repos["knownrepo"][$repos]["i386"] = true;
+ else $existing_repos["knownrepo"][$repos]["i386"] = false;
+ $repos++;
+
}
}
$existing_repos["nrepos"] = $repos;
return $glt;
}
+ function freezeRepo($rkey)
+ {
+
+ $uconf = $this->config->getConfig("repodata");
+ $repostore = $this->config->getConfig("storagelocation");
+
+ if($uconf !== false) {
+ $conf = unserialize($uconf);
+ foreach($conf as $key => $vla) {
+ if($key == $rkey) {
+ if(isset($conf["$rkey"]["frozen"])) {
+ if($conf["$rkey"]["frozen"]) {
+ $conf["$rkey"]["frozen"] = false;
+ error_log("Unfreeze repo");
+ } else {
+ $conf["$rkey"]["frozen"] = true;
+ error_log("freeze repo");
+ }
+ } else {
+ $conf["$rkey"]["frozen"] = true;
+ }
+ $nconf = serialize($conf);
+ error_log("freeze repo as $rkey");
+ $this->config->setConfig("repodata", $nconf);
+ $this->config->saveConfig();
+ }
+ }
+ }
+ }
+
function deleteRepo($rkey)
{
- $uconf = $this->config->getConfigVar("repodata");
- $repostore = $this->config->getConfigVar("storagelocation");
+ $uconf = $this->config->getConfig("repodata");
+ $repostore = $this->config->getConfig("storagelocation");
if($uconf !== false) {
$conf = unserialize($uconf);
$nconf = serialize($conf);
system("rm -rf $repostore/$key");
error_log("remove repo as $rkey");
- $this->config->setConfigVar("repodata", $nconf);
+ $this->config->setConfig("repodata", $nconf);
$this->config->saveConfig();
}
}
}
}
- function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init, $expiretime)
+ function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init, $expiretime, $blocklist=null)
{
- $uconf = $this->config->getConfigVar("repodata");
+ $uconf = $this->config->getConfig("repodata");
$cs["desc"] = $desc;
$cs["os"] = $os;
$cs["url"] = $repurl;
$cs["repotype"] = $repotype;
$cs["expiretime"] = $expiretime;
+ if($blocklist != null) {
+ $cs["blocklist"] = $blocklist;
+ }
$ckey = 0;
$nconf = serialize($conf);
error_log("add repo as $ckey");
- $this->config->setConfigVar("repodata", $nconf);
+ $this->config->setConfig("repodata", $nconf);
$this->config->saveConfig();
// now create the base structure in the repo
- $repostore = $this->config->getConfigVar("storagelocation");
+ $repostore = $this->config->getConfig("storagelocation");
// now call update repo
function updateRepo($repokey)
{
// we only do yum yet
- $this->updateRepoYum($repokey);
+ $repod = $this->getRepo($repokey);
+ if(isset($repod["frozen"])) if($repod["frozen"]) {
+ error_log("in update repo for frozen repo, ignore update");
+ return;
+ }
+
+ error_log("in update repo");
+
+ if($repod["repotype"] == "YUM") $this->updateRepoYum($repokey);
+ if($repod["repotype"] == "APT") $this->updateRepoApt($repokey);
+ }
+
+ function updateRepoApt($repokey)
+ {
+ $repostore = $this->config->getConfig("storagelocation");
+
+ $repod = $this->getRepo($repokey);
+
+ $repourl = $repod["url"];
+
+ if(!file_exists("$repostore/$repokey")) {
+ mkdir("$repostore/$repokey");
+ }
+
+ if(!file_exists("$repostore/$repokey/dists/")) {
+ mkdir("$repostore/$repokey/dists/");
+ // no point trying to update if we have no dists directory
+ return;
+ }
+
+
+ error_log("background apt repo update, start");
+ global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+ if(file_exists("$WEB_ROOT_FS/../bin/downloadfile.php")) {
+ $scall = "/usr/bin/php $WEB_ROOT_FS/../bin/updateaptrepo.php '$repourl' '$repostore/$repokey/' > /tmp/updateaptrepo.$repokey.log 2>&1 &";
+ system($scall);
+ } else {
+ error_log("cant find download apt helper... dieing");
+ }
}
function updateRepoYum($repokey)
{
- $repostore = $this->config->getConfigVar("storagelocation");
+ $repostore = $this->config->getConfig("storagelocation");
$repod = $this->getRepo($repokey);
function getRepo($id)
{
- $uconf = $this->config->getConfigVar("repodata");
+ $uconf = $this->config->getConfig("repodata");
if($uconf !== false) {
$lconf = unserialize($uconf);
return $lconf[$id];
function getRepos()
{
- $uconf = $this->config->getConfigVar("repodata");
+ $uconf = $this->config->getConfig("repodata");
if($uconf !== false) {
return unserialize($uconf);
} else return false;