added a apt repo updater... that doesnt work
[glcas.git] / plugins / repo.php
index 804a5ce..9d1de84 100644 (file)
 <?php
 $URL_HANDLERS["*"] = "GLCASRepo";
+global $CRON_CLASSES;
+$CRON_CLASSES["GLCASRepo"] = "GLCASRepo";
 
 class GLCASRepo {
        function __construct($config)
        {
                $this->config = $config;
+               if($this->config->getConfigVar("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();
+                       error_log("set storage location, $storloc");
+               }
        }
        
+       function cron()
+       {
+               //echo "<pre>";
+               $uconf = unserialize($this->config->getConfigVar("repodata"));
+               $repostore = $this->config->getConfigVar("storagelocation");
+               //print_r($uconf);
+               //echo "</pre>";
+               
+               foreach($uconf as $rkey => $repo) {
+                       echo "Repo $rkey: ".$repo["desc"]."<br>";
+                       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->saveConfig();                            
+                       }
+                       
+                       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->setConfigVar("repodata", serialize($uconf));
+                               $this->config->saveConfig();                            
+                       } else if($repo["repotype"] == "YUM") {
+                               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(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;
+                               $this->updateRepo($rkey);
+                       }
+               }
+       }
+
        function go($url)
        {
-               echo "i am the repo man, i repo the repo";
+               error_log("repo:go called");
+
+               // figure out what we're doing
+               switch($url) {
+                       case "list":
+                               GLCASpageBuilder($this, "body");
+                               break;
+                       default:
+                               $this->getRepoForUrl($url);
+               }
+       }
+
+       function body($url)
+       {
+               // this is how this will work
+               //$this->decodeUrl();
+               if(strncasecmp("list", $url, 4)==0) {
+                       echo "i am the repo list";
+                       return;
+               }
+               echo "i am the repo, $url";
+       }
+
+
+       // TODO: rework this function
+       /*
+       * What i need to do is have a downloader function
+       * that can cope with lots of different shit
+       * but thats a pipe dream
+       *
+       * what *THIS* function needs to do is
+       * 1) figure out the repo
+       * 2) figure out the file in the repo
+       * 2.1) if its a directory, go to print directory
+       * 3) if the file exists, give it to the user (if a range is specified give the user the range)
+       * 4) if the file does not exist
+       *    - check if a tmp file exists
+       *    - attempt to get an exclusive flock
+       *    - if flock fails, donwload in progress
+       *    - if flock succeeds, truncate file and re-start download
+       *    - if a range request was made, send the range once available
+       *    - if range not available, sleep for 5 and check again.
+       *
+       * I dont want to code this from scratch, but i probably need to
+       */
+       function getRepoForUrl($url)
+       {
+               $xurl = split("[/,]", $url);
+
+               // first get the config
+               $uconf = unserialize($this->config->getConfigVar("repodata"));
+               $repostore = $this->config->getConfigVar("storagelocation");
+
+               // preset matched to -1
+               $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;
+                       }
+               }
+
+               // now check for a prefix match
+               $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++;
+                               }
+                       }
+               }
+
+               // next, check for a short url match
+               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++;
+                               }
+                       }
+               }
+
+               // TODO: this deterministic bit
+               // so far nothing has matched - what this next bit needs to do is try and "Determine" a repo from url
+               // for eg, if a user gets /fedora/x86_64/os we need to return something appropriate
+               if($matched < 0) {
+                       echo "No such repo<br>";
+                       header("HTTP/1.0 404 Not Found");
+                       return;
+               }
+               
+
+
+               // something was matched, so now we reconstruct the file component of the url
+               $file = "/";
+               if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
+                       $file .= "/".$xurl[$i];
+               }
+
+               // 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);
+                       return;
+               }
+
+               // check if the file exists and serve it up
+               if(file_exists($actualfile) && !file_exists("$actualfile.size")) {
+                       $this->serveUpFile($actualfile, $matched);
+                       return;
+               } else {
+                       // the file does not exist, we now need to go into "download" mode
+                       $remoteurl = $uconf[$matched]["url"]."/$file";
+                       $this->downloadAndServe($actualfile, $matched, $remoteurl);
+                       return;
+               }
+       }
+
+       function serveUpFile($actualfile, $repoid)
+       {
+               $uconf = unserialize($this->config->getConfigVar("repodata"));
+               $repostore = $this->config->getConfigVar("storagelocation");
+
+               // figure out the range header garbage that centos/redhat send
+               if(isset($_SERVER["HTTP_RANGE"])) {
+                       // we're using ranges - screw you stupid installer
+                       $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
+                               
+                       // cut up ranges
+                       $rangestart = $pr_range[1];
+                       $rangelength = $pr_range[2] - $pr_range[1] +1;
+                       $rangestr = $pr_range[1]."-".$pr_range[2];
+                       error_log("going ranges at $rangestart, $rangelength,".$rangesa[1].",".$rangesb[0]);
+                               
+                       // now spit some headers
+                       header("HTTP/1.1 206 Partial Content");
+                       header("Content-Length: ".$rangelength);
+                               
+
+                       header("Content-Range: bytes $rangestr/".filesize($actualfile));
+                               
+                       // determine mime type
+                       $type = mime_content_type($actualfile);
+                               
+                       // set mime type header
+                       header("Content-type: $type");
+                               
+                       // open the local file (TODO: error check)
+                       $localfile = fopen($actualfile, "r");
+                       fseek($localfile, $rangestart, SEEK_SET);
+                               
+                       // read in the data, god i hope its not big
+                       $data = fread($localfile, $rangelength);
+                               
+                       // lastly, send data
+                       echo $data;
+                       flush();
+                               
+                       // and close the file
+                       fclose($localfile);
+                       return;
+               } else {
+
+                       // we're not using range's - good on you installer thingy
+                       header("Content-Length: ".filesize($actualfile));
+
+                       // set the mime type header
+                       $type = mime_content_type($actualfile);
+                       header("Content-type: $type");
+                               
+                       // open the local file
+                       $localfile = fopen($actualfile, "r");
+                       if(!$localfile) {
+                               error_log("normal upload went barf");
+                               return;
+                       }
+                               
+                       // iterate over its length, send 8k at a time
+                       while(!feof($localfile)) {
+                               // read and send data
+                               $data = fread($localfile, 32768);
+                               echo $data;
+
+                               // flush so the client sees the data
+                               flush();
+                       }
+                               
+                       // close the file
+                       fclose($localfile);
+                       return;
+               }
+       }
+
+       // TODO: this is the function im working on
+       // the alternative to this function is that if a file is in the process of being
+       // downloaded, we simply serve from upstream... not a good idea tho unless we create
+       // a local proxy right here - this function is a race condition waiting to be had
+       // lets hope its a good one!
+       function downloadAndServe($filename, $repoid, $remoteurl)
+       {
+
+               $this->startDownload($filename, $remoteurl);
+
+               // give the proc a minute to get going
+               sleep(2);
+               clearstatcache();
+
+               // get the configurations we need
+               $uconf = unserialize($this->config->getConfigVar("repodata"));
+               $repostore = $this->config->getConfigVar("storagelocation");
+
+
+
+               // determine if we're ranged
+               $ranged = false;
+               $rangestart = 0;
+               $rangelength = 0;
+               $rangestr="";
+               if(isset($_SERVER["HTTP_RANGE"])) {
+                       // we're using ranges - screw you stupid installer
+                               
+                       $pr_range = preg_split("/[:\-=, ]+/", $_SERVER["HTTP_RANGE"]);
+                       error_log("got range ".$_SERVER["HTTP_RANGE"]." and ".print_r($pr_range, true));
+                               
+                       // cut up ranges
+                       $rangestart = $pr_range[1];
+                       $rangelength = $pr_range[2] - $pr_range[1] +1;
+                       $rangestr = $pr_range[1]."-".$pr_range[2];
+                       error_log("going ranges at $rangestart, $rangelength, $rangestr");
+                       $ranged = true;
+               }
+
+               // open the local files
+
+               // now, lets determine what state we're in
+               // we're either - getting and sending
+               // watching and sending
+               // or a range (Getting and sending)
+               // or a range (watching and sending)
+               // TODO: it may be advicable to start the download as a seperate cli process rather then something goin on here
+               // so it definitely cant be interrupted.
+               
+               // check for a 404 file and wait 2 if it exists - i should really check the timestamp for an updated
+               // file, but thats too much effort for now: TODO: check timestamp on 404 file
+               $slept = 0;
+               while(!file_exists("$filename")) {
+                       clearstatcache();
+                       sleep(1);
+                       $slept += 1;
+                       error_log("Sleeping waiting for file");
+                       
+                       // if 404 file exists, we wait much less time
+                       if(file_exists("$filename.404") && $slept > 2) {
+                               header("HTTP/1.0 404 Not Found");
+                               return;
+                       }
+                       if($slept > 10) {
+                               header("HTTP/1.0 404 Not Found");
+                               return;
+                       } 
+               }
+               
+               clearstatcache();
+               if(is_dir($filename)) {
+                       
+                       header("Location: ".$_SERVER["REQUEST_URI"]."/");
+                       return;
+               }
+                       
+
+               // first, getting and sending - this is easy.
+               if (!$ranged) {
+                               
+                       $localfile = fopen($filename, "r");
+                               
+                       // this is where the fun starts - but this one isnt too bad.
+                       error_log("OTHERDOWNLOAD: im another downloader, please work");
+                       if(file_exists("$filename.size")) $fsize = file_get_contents("$filename.size");
+                       else $fsize = filesize($filename);
+                       header("Content-Length: $fsize");
+                       $sgotten = 0;
+                       while(!feof($localfile)) {
+                               $data = fread($localfile, 2048);
+                               if(!$data) {
+                                       error_log("dollardata is pair shaped");
+                               } else {
+                                       $sgotten += strlen($data);
+                                       if($sgotten > $fsize) {
+                                               error_log("went plop at sgotten, $sgotten, $fsize");
+                                               return;
+                                       }
+                                       echo $data;
+                                       flush();
+                               }
+                       }
+                       fclose($localfile);
+                               
+                       // need to think about this in pseudo code.
+                       // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
+                       $cursize = filesize($filename);                         
+                               
+                       $upload_finished = false;
+                       while(!$upload_finished) {
+                               while($cursize < $fsize && $cursize < ($sgotten+2048)) {
+                                       clearstatcache();
+                                       error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $fsize");
+                                       // sleep until the the filesize is greater then what we're up to, or until the file is finished
+                                       sleep(1);
+                                       $cursize = filesize($filename);
+                               }
+
+                               error_log("OTHERDOWNLOAD: continue, $sgotten, $fsize");
+                               // reopen local file - if it stopped existing, we need to deal with that
+                               $localfile = fopen($filename, "r");
+
+                               // UG, we need to ff, how could i forget that
+                               fseek($localfile, $sgotten);
+
+                               if(!$localfile) {
+                                       error_log("OTHERDOWNLOAD: something went plop");
+                                       return;
+                               }
+
+                               // now loop on the file until we have it at an eof
+                               while(!feof($localfile)) {
+                                       $data = fread($localfile, 512);
+                                       if(!$data) {
+                                               error_log("OTHERDOWNLOAD: dollar data went plop");
+                                       } else {
+                                               $sgotten += strlen($data);
+                                               echo $data;
+                                               flush();
+                                       }
+                               }
+                               fclose($localfile);
+
+                               if($sgotten >= $fsize) {
+                                       if($sgotten > $fsize) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
+                                       $upload_finished = true;
+                               }
+                               // and we're done
+
+                       }
+                       error_log("OTHERDOWNLOADER: done with");
+                               
+                       return;
+
+
+                               
+                               
+                       // Next painful bit
+               } else {
+                       // and here too, yay, someone else is doing the
+                       // download, but we're the retards getting a range
+                       $sgotten = 0;
+                               
+                       $sgatlen = $rangestart+$rangelength;
+                               
+                       // the problem is here
+                       error_log("Downloader: going ranged as other");
+                       clearstatcache();
+                       if(file_exists($filename.".tmp.data.deleteme.size")) $contentlen = file_get_contents($filename.".tmp.data.deleteme.size");
+                       else $contentlen = filesize($filename);
+                       $contenttype = mime_content_type($filename);
+                       header("HTTP/1.1 206 Partial Content");
+                       header("Content-Length: $rangelength");
+                       header("Content-Range: bytes $rangestr/$contentlen");
+                       $contenttype = "Content-Type: application/x-rpm";
+                       
+                       error_log("$contenttype");
+                       header("$contenttype");
+                       
+                       
+                       clearstatcache();
+                               
+                               
+                       // first we wait until the file reaches $rangestart
+                       while(filesize("$filename") < $rangestart) {
+                               sleep(1);
+                       }
+                               
+                       // then we open the file and ff to rangestart
+                       $localfile = fopen($filename, "r");
+                       fseek($localfile, $rangestart);
+                               
+                       $sgotten = 0;
+                       // need to think about this in pseudo code.
+                       // 1. close the file and wait for it to get to $sgotten + 2048 or $fsize
+                       $cursize = filesize($filename);
+                               
+                               
+                       $upload_finished = false;
+                       while(!$upload_finished) {
+                               while($cursize < $sgatlen && $cursize < ($sgotten+2048)) {
+                                       clearstatcache();
+                                       error_log("OTHERDOWNLOAD: halt, $cursize, $sgotten, $contentlen");
+                                       // sleep until the the filesize is greater then what we're up to, or until the file is finished
+                                       sleep(1);
+                                       $cursize = filesize($filename);
+                               }
+
+                               error_log("OTHERDOWNLOAD: continue, $sgotten, $contentlen");
+                               // reopen local file - if it stopped existing, we need to deal with that
+                               $localfile = fopen($filename, "r");
+
+                               // UG, we need to ff, how could i forget that
+                               fseek($localfile, $sgotten+$rangestart);
+
+                               if(!$localfile) {
+                                       error_log("OTHERDOWNLOAD: something went plop");
+                                       return;
+                               }
+
+                               // now loop on the file until we have it at sgatlen
+                               while(!feof($localfile) && $sgotten < $rangelength) {
+                                       $left = $rangelength - $sgotten;
+                                       if($left > 512) $lenget = 512;
+                                       else $lenget = $left;
+                                       $data = fread($localfile, $lenget);
+                                       if(!$data) {
+                                               error_log("OTHERDOWNLOAD: dollar data went plop");
+                                       } else {
+                                               $sgotten += strlen($data);
+                                               echo $data;
+                                               flush();
+                                       }
+                               }
+                               fclose($localfile);
+
+                               if($sgotten >= $rangelength) {
+                                       if($sgotten > $rangelength) error_log("OTHERDOWNLOADER: finished but $sgotten, $fsize doesnt make senze");
+                                       $upload_finished = true;
+                               }
+                               // and we're done
+
+                       }
+                       error_log("OTHERDOWNLOADER: done with");
+                               
+                       return;
+                               
+               }
+
+
+               return;
+       }
+
+       function startDownload($file, $url)
+       {
+               
+               error_log("background downloader, 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/downloadfile.php '$url' '$file' > /tmp/dllog 2>&1 &";
+                       system($scall);
+               } else {
+                       error_log("cant find download helper... dieing");
+               }
+       }
+
+       // this is a nightmare
+
+
+       function printDir($dir, $localfile, $baseurl)
+       {
+               $localfile = preg_replace("/\/\/+/", "/", $localfile);
+               $uri = $_SERVER["REQUEST_URI"];
+               $content = "";
+               if(is_dir($dir)) {
+                       $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")) {
+                                               $dirlist[$dirn++] = "$file";
+                                       } else {
+                                               $filelist[$filen++] = "$file";
+                                       }
+                               }
+                       }
+                       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);
+                               
+               } else return false;
        }
        
+       function getRepoDetailsApt($url)
+       {
+               $action1 = $url."/dists";
+               
+               // we just want to make sure it exists really
+               error_log("in repo details apt for $url");
+               if(!glcas_isRemoteDir($action1)) {
+                       //echo "I cant find any valid APT dists's at $url<br>";
+                       return false;
+               }
+               
+               // ok, now scan for ubuntu dists as
+               $kos = getKnownOSList();
+               
+               $repos = 0;
+               $existing_repo["isrepo"] = true;
+               foreach($kos["apt"] as $key => $val) {
+                       //echo "<br>$key, $val<br>";
+                       //echo "now check, $action1/$key";
+                       if(glcas_isRemoteDir($action1."/$key")) {
+                               $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;
+               
+               // TODO: these need to be "calculated"
+               $existing_repos["distros"] = "Ubuntu, Debian";
+               $existing_repos["versions"] = "8.04LTS, 9.10, 10.04LTS, 10.10, 11.04, 11.10";
+               $existing_repos["arch"] = "x86_64, i386";
+               
+               
+               return $existing_repos;
+
+       }
+
+       function getRepoDetailsYum($url, $ismirrorlist=false)
+       {
+               $actionurl = $url."/repodata/repomd.xml";
+
+               error_log("Getting for action of $actionurl");
+
+               $ld = file_get_contents($actionurl);
+
+               // so here we try and get what this repository provides (os, version, arch), for yum this
+               // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
+
+               if(!$ld) return false;
+
+               // ok, now we tokenize the url and try and guess at the content
+               $spurl = explode("/", $url);
+
+               // first, find the OS
+               $kos = getKnownOSList();
+               $glt["OS"] = "unknown";
+               $glt["verison"] = "unknown";
+               $glt["arch"] = "unknown";
+               $glt["other"] = "unknown";
+               foreach($spurl as $comp) {
+                               
+                       // find a name
+                       foreach($kos["os"]["short"] as $kosname => $koslong) {
+                               //error_log("Comparing $kosname and $koslong with $comp");
+                               if(strcasecmp($kosname, $comp) == 0) {
+                                       //error_log("got $kosname, $koslong for $comp in $url");
+                                       //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
+                                       $glt["OS"] = $koslong;
+                               }
+                       }
+                               
+                       // find a version, we assume its going to be something [numbers] and a . (optional)
+                       if(preg_match("/^[0-9.]+$/", $comp)>0) {
+                               //error_log("version match of $comp");
+                               $glt["version"] = $comp;
+                       }
+                               
+                       // 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
+                       foreach($kos["arch"] as $archinter => $archname ) {
+                               //error_log("Comparing $archinter, $archname with $comp");
+                               if(strcasecmp($archname, $comp) == 0) {
+                                       //error_log("arch match of $archname with $comp");
+                                       $glt["arch"] = $archname;
+                               }
+                       }
+                               
+                       // other is a bt harder, we really have to guess at this one
+                       if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
+                       if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
+                       if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
+                       if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
+               }
+
+                       
+               return $glt;
+       }
+
+       function deleteRepo($rkey)
+       {
+               $uconf = $this->config->getConfigVar("repodata");
+               $repostore = $this->config->getConfigVar("storagelocation");
+
+               if($uconf !== false) {
+                       $conf = unserialize($uconf);
+                       foreach($conf as $key => $vla) {
+                               if($key == $rkey) {
+                                       unset($conf["$rkey"]);
+                                       $nconf = serialize($conf);
+                                       system("rm -rf $repostore/$key");
+                                       error_log("remove repo as $rkey");
+                                       $this->config->setConfigVar("repodata", $nconf);
+                                       $this->config->saveConfig();
+                               }
+                       }
+               }
+       }
+
+       function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init, $expiretime, $blocklist=null)
+       {
+               $uconf = $this->config->getConfigVar("repodata");
+
+               $cs["desc"] = $desc;
+               $cs["os"] = $os;
+               $cs["version"] = $version;
+               $cs["arch"] = $arch;
+               $cs["other"] = $other;
+               $cs["shorturl"] = $shorturl;
+               $cs["prefix"] = $prefix;
+               $cs["url"] = $repurl;
+               $cs["repotype"] = $repotype;
+               $cs["expiretime"] = $expiretime;
+               if($blocklist != null) {
+                       $cs["blocklist"] = $blocklist;
+               }
+
+
+               $ckey = 0;
+               if($uconf !== false) {
+                       $conf = unserialize($uconf);
+                       foreach($conf as $key => $val) {
+                               $ckey = $key;
+                       }
+                       $ckey++;
+               }
+
+               $conf[$ckey] = $cs;
+
+               $nconf = serialize($conf);
+
+               error_log("add repo as $ckey");
+               $this->config->setConfigVar("repodata", $nconf);
+               $this->config->saveConfig();
+
+               // now create the base structure in the repo
+               $repostore = $this->config->getConfigVar("storagelocation");
+
+
+               // now call update repo
+               if($init) $this->updateRepoYum($ckey);
+       }
+
+       function updateRepo($repokey)
+       {
+               // we only do yum yet
+               $repod = $this->getRepo($repokey);
+               
+               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->getConfigVar("storagelocation");
+
+               $repod = $this->getRepo($repokey);
+
+               $repourl = $repod["url"];
+
+               if(!file_exists("$repostore/$repokey")) {
+                       mkdir("$repostore/$repokey");
+               }
+
+               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");
+
+               $repod = $this->getRepo($repokey);
+
+               $repourl = $repod["url"];
+
+               if(!file_exists("$repostore/$repokey")) {
+                       mkdir("$repostore/$repokey");
+               }
+
+               if(!file_exists("$repostore/$repokey/repodata")) {
+                       mkdir("$repostore/$repokey/repodata");
+               }
+               
+               error_log("background yum 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/updateyumrepo.php '$repourl' '$repostore/$repokey/' > /tmp/updateyumrepo.$repokey.log 2>&1 &";
+                       system($scall);
+               } else {
+                       error_log("cant find download yum helper... dieing");
+               }
+               
+
+               //ignore_user_abort(true);
+       }
+
+       function getRepo($id)
+       {
+               $uconf = $this->config->getConfigVar("repodata");
+               if($uconf !== false) {
+                       $lconf = unserialize($uconf);
+                       return $lconf[$id];
+               } else return false;
+
+       }
+
+       function getRepos()
+       {
+               $uconf = $this->config->getConfigVar("repodata");
+               if($uconf !== false) {
+                       return unserialize($uconf);
+               } else return false;
+
+       }
+
        private $config;
 }