added a sort to printDir() so it prints dirs first, file second and
[glcas.git] / plugins / repo.php
index a908f03..c201756 100644 (file)
@@ -1,6 +1,7 @@
 <?php
 $URL_HANDLERS["*"] = "GLCASRepo";
-
+global $CRON_CLASSES;
+$CRON_CLASSES["GLCASRepo"] = "GLCASRepo";
 
 class GLCASRepo {
        function __construct($config)
@@ -15,6 +16,49 @@ class GLCASRepo {
                        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</font><br>";
+                       } 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") {
+                               echo "<font color=\"red\">I have no idea how to spawn an update of an APT repo - apt is crap</font><br>";
+                       }
+               }
+       }
 
        function go($url)
        {
@@ -217,7 +261,7 @@ class GLCASRepo {
                        // iterate over its length, send 8k at a time
                        while(!feof($localfile)) {
                                // read and send data
-                               $data = fread($localfile, 8192);
+                               $data = fread($localfile, 32768);
                                echo $data;
 
                                // flush so the client sees the data
@@ -233,7 +277,8 @@ class GLCASRepo {
        // 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
+       // 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)
        {
 
@@ -277,8 +322,34 @@ class GLCASRepo {
                // 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) {
@@ -287,7 +358,8 @@ class GLCASRepo {
                                
                        // this is where the fun starts - but this one isnt too bad.
                        error_log("OTHERDOWNLOAD: im another downloader, please work");
-                       $fsize = file_get_contents("$filename.size");
+                       if(file_exists("$filename.size")) $fsize = file_get_contents("$filename.size");
+                       else $fsize = filesize($filename);
                        header("Content-Length: $fsize");
                        $sgotten = 0;
                        while(!feof($localfile)) {
@@ -461,7 +533,7 @@ class GLCASRepo {
                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' > /dev/null 2>&1 &";
+                       $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");
@@ -679,8 +751,31 @@ class GLCASRepo {
                        $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 != "..") $content .= "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
+                               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>";
                                
@@ -688,6 +783,43 @@ class GLCASRepo {
                                
                } 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] = $key;
+                               $repos++;
+                               //echo "Found Distro $val<br>";
+                       }
+               }
+               $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)
        {
@@ -769,7 +901,7 @@ class GLCASRepo {
                }
        }
 
-       function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
+       function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init, $expiretime)
        {
                $uconf = $this->config->getConfigVar("repodata");
 
@@ -782,6 +914,7 @@ class GLCASRepo {
                $cs["prefix"] = $prefix;
                $cs["url"] = $repurl;
                $cs["repotype"] = $repotype;
+               $cs["expiretime"] = $expiretime;
 
 
                $ckey = 0;
@@ -830,29 +963,18 @@ class GLCASRepo {
                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);
-               $actionurl = "$repourl/repodata/repomd.xml";
-               $repomdxml = file_get_contents($actionurl);
-               file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
-
-               $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
-
-
-               foreach($xml as $key => $var) {
-                       //echo "for key $key has:\n";
-                       //print_r($var);
-                       if($key == "data") {
-                               $fileloc = $var->location["href"];
-                               if(!file_exists("$repostore/$repokey/$fileloc")) {
-                                       error_log("getting $fileloc for $repokey on $repourl");
-                                       $dlfile = file_get_contents("$repourl/$fileloc");
-                                       file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
-                               } else {
-                                       error_log("Not getting $fileloc because we already have it");
-                               }
-                       }
-               }
        }
 
        function getRepo($id)