added a apt repo updater... that doesnt work
[glcas.git] / plugins / admin.php
index 4a99fb7..492ed29 100644 (file)
@@ -4,6 +4,8 @@ error_log("admin loaded");
 
 global $URL_HANDLERS;
 $URL_HANDLERS["admin.*"] = "GLCASAdmin";
+global $CRON_CLASSES;
+$CRON_CLASSES["GLCASAdmin"] = "GLCASAdmin";
 
 class GLCASAdmin {
        function __construct($config)
@@ -28,6 +30,9 @@ class GLCASAdmin {
                                        GLCASpageBuilder($this,"doRepoNextForm");
                                        return;
                                        break;
+                               case "addrepoapt":
+                                       GLCASpageBuilder($this,"doAddRepoApt");
+                                       return;
                                case "addrepoyum":
                                        GLCASpageBuilder($this,"doAddRepoYum");
                                        return;
@@ -46,6 +51,34 @@ class GLCASAdmin {
                GLCASpageBuilder($this, "body");
        }
        
+       function cron()
+       {
+               echo "ADMIN CRON: nothing to see here<br>";
+               // TODO: touch cronstamp file here
+               
+               // make a backup of the config.. but.. errr. how to tell if theres an old backup?
+               // backups will occur... weekly and be called $configpath.DDMMYYYY
+               $configpath = glcas_getWebConfigPath();
+               $basepath = dirname($configpath);
+               echo "ADMIN CRON: backing up config, $configpath, $basepath<br>";
+               $nowtime = time();
+               $oneday = 3600 * 24; // 1 hour in seconds, by 24 hours
+               for($i = 0; $i < 7; $i ++) {
+                       $cdatestr = strftime("%d-%h-%Y", $nowtime - ($oneday*$i));
+                       $backupfile = "$configpath.$cdatestr";
+                       error_log("checking for backup of config as $backupfile");
+                       if(file_exists($backupfile)) {
+                               error_log("backupconfig exists as $backupfile, exit");
+                               echo "ADMIN CRON: backup of config is $backupfile, created $i days ago, not creating new one<br>";
+                               return;
+                       }
+               }
+               $nowdatestr = strftime("%d-%h-%Y");
+               $backupfile = "$configpath.$nowdatestr";
+               copy($configpath, $backupfile);
+               echo "ADMIN CRON: createing backup of config as $backupfile<br>";
+       }
+       
        function body($url)
        {
                // for the main admin body, we hae the following components
@@ -83,7 +116,10 @@ class GLCASAdmin {
        {
                $myRep = new GLCASRepo($this->config);
                
+               $kos = getKnownOSList();
+               
                $wasyum = false;
+               $wasapt = false;
                if($_REQUEST["repotype"] == "yumbase") {
                        $glt = $myRep->getRepoDetailsYum($_REQUEST["repourl"]);
                        $wasyum = true;
@@ -92,8 +128,77 @@ class GLCASAdmin {
                        $wasyum = true;
                } else if($_REQUEST["repotype"] == "apt") {
                        $glt = $myRep->getRepoDetailsApt($_REQUEST["repourl"]);
+                       $wasapt = true;
                }
                
+               if($wasapt) {
+                       error_log("in wasapt");
+                       $repourl = $_REQUEST["repourl"];
+                       
+                       if($glt === false) {
+                               echo "Could not find any apt repository at $repourl<br>";
+                               return;
+                       }
+                       
+                       $extratext = "";
+                       $provides = "";
+                       if($glt["nrepos"] > 0) {
+                               
+                               foreach($glt["knownrepo"] as $key => $val) {
+                                       $reponame = $kos["apt"][$val];
+                                       //echo "Fount a repo, $val which is $reponame<br>";
+                                       $provides .= "$reponame\n";
+                               }
+                               $extratext = " for ".$glt["distros"];
+                       } else {
+                               //echo "Looks like an apt repo, but I cant find any known distro's<br>";
+                       }
+                       //$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";
+                       
+                       
+                       $distros = $glt["distros"];
+                       $version = $glt["versions"];
+                       $archs = $glt["arch"];
+                       
+                       //echo "<pre>";
+                       //print_r($glt);
+                       //echo "</pre>";
+                       
+                       echo "The joy of apt is that there could be a million distro's under the root of an apt mirror";
+                       echo "if it doesn't list here, but you know its there, its still usable for glcas";
+                       echo "<form method=\"post\" action=\"?action=addrepoapt\">";
+                       echo "<input type=\"hidden\" name=\"repourl\" value=\"$repourl\">";
+                       echo "<input type=\"hidden\" name=\"distro\" value=\"$distros\">";
+                       echo "<input type=\"hidden\" name=\"provides\" value=\"Multiple\">";
+                       echo "<input type=\"hidden\" name=\"arch\" value=\"$archs\">";
+                       echo "<table>";
+                       echo "<tr><td>Description</td><td><input type=\"text\" name=\"desc\" value=\"APT Repo$extratext\"></td>";
+                       
+                       // TODO: deal with block bit
+                       echo "<tr><td>Provides (tick those you wish to BLOCK - not implemented)</td><td>";
+                       foreach($glt["knownrepo"] as $key => $val) {
+                               $reponame = $kos["apt"][$val["name"]];
+                               $a = "";
+                               $b = "";
+                               if($glt["knownrepo"][$key]["i386"]) $a = "i386";
+                               if($glt["knownrepo"][$key]["amd64"]) $b = "x86_64";
+                               if($a != "" && $b != "") $archs = "$a/$b";
+                               else $archs = "$a$b"; 
+                               echo "<input type=\"checkbox\" name=\"blocked-".$val["name"]."\"> $reponame ($archs)<br>";
+                       }
+                       
+                       echo "</td></tr>";
+                       //echo "<tr><td>Provides</td><td><textarea name=\"provides\" rows=\"".$glt["nrepos"]."\" cols=\"100\">$provides</textarea></td></tr>";
+                       echo "<tr><td>Short URL</td><td><input type=\"text\" name=\"shorturl\"></td></tr>";
+                       echo "<tr><td>URL Prefix (blank for none)</td><td><input type=\"text\" name=\"prefix\"></td></tr>";
+                       echo "<tr><td>Expire time for Meta Data</td><td><input type=\"text\" name=\"expiretime\" value=\"3\"> days</td></tr>";
+                       echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr>";
+                       echo "</table>";
+                       echo "</form>";
+                       return true;
+               }
                
                if($wasyum) {
                        $os = $glt["OS"];
@@ -112,14 +217,52 @@ class GLCASAdmin {
                        echo "<tr><td>Short URL</td><td><input type=\"text\" name=\"shorturl\"></td></tr>";
                        echo "<tr><td>URL Prefix (blank for none)</td><td><input type=\"text\" name=\"prefix\"></td></tr>";
                        echo "<tr><td>Do Initial Update (can take a while, but done in background)</td><td><input type=\"checkbox\" name=\"initial\" checked></td></tr>";
-                       echo "<tr><td>Expire time for Meta Data</td><td><input type=\"text\" name=\"expiretime\"> days</td></tr>";
+                       echo "<tr><td>Expire time for Meta Data</td><td><input type=\"text\" name=\"expiretime\" value=\"3\"> days</td></tr>";
                        echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr>";
                        echo "</table>";
                        echo "</form>";
-               } else {
-                       // apt is much tricker cause one repo can provide multiple versions, OS's and architectures.
                }
        }
+
+       function doAddRepoApt($url)
+       {
+               $repo = new GLCASRepo($this->config);
+               
+               $desc = $_REQUEST["desc"];
+               $OS = $_REQUEST["distro"];
+               $version = $_REQUEST["provides"];
+               $arch = $_REQUEST["arch"];
+               $shorturl = $_REQUEST["shorturl"];
+               $prefix = $_REQUEST["prefix"];
+               $repurl = $_REQUEST["repourl"];
+               $expiretime = $REQUEST["expiretime"];
+               $init = false;
+               if(isset($_REQUEST["initial"])) $init = true;
+               
+               $blocklist = null;
+               $bn = 0;
+               foreach($_REQUEST as $key => $val) {
+                       if(preg_match("/^blocked-*/", $key)) {
+                               $blocked = preg_replace("/^blocked-/", "", $key);
+                               //echo "<br>got block for $blocked<br>";
+                               $blocklist[$bn] = "dists\/$blocked.*";
+                               $bn++;
+                       }
+               }
+               
+               //echo "<pre>";
+               //print_r($_REQUEST);
+               //echo "</pre>";
+               
+               //exit(0);
+               
+               $repo->addRepo($desc, $OS, $version, $arch, "-", $shorturl, $prefix, $repurl, "APT", $init, $expiretime, $blocklist);
+               
+               // next we need to deal with that "blocked" bit
+               
+               global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
+               header("Location: $BASE_URL/admin/");
+       }
        
        function doAddRepoYum($url)
        {
@@ -148,6 +291,8 @@ class GLCASAdmin {
        function doUpdateRepo($url)
        {
                $rkey = $_REQUEST["repo"];
+
+               error_log("would update for repo");
                
                $repo = new GLCASRepo($this->config);
                
@@ -215,14 +360,13 @@ class GLCASAdmin {
                //echo "</pre>";
                
                // wrap all this in a table
-               echo "<table><tr><td valign=\"top\">";
+               echo "<table border=\"1\"><tr><td valign=\"top\">";
                // now, add a repo
                echo "<h3>Add A Repo</h3>";
                echo "<form method=\"post\" action=\"?action=reponext\">";
                echo "Type <select name=\"repotype\">";
                        echo "<option value=\"yumbase\">YUM (Base URL)</option>";
-                       echo "<option value=\"yummirrorlist\">YUM (Mirror List) - not implemented</option>";
-                       echo "<option value=\"apt\">APT - not implemented</option>";
+                       echo "<option value=\"apt\">APT</option>";
                        echo "</select><br>";
                echo "URL <input type=\"text\" name=\"repourl\"><br>";
                echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
@@ -236,19 +380,37 @@ class GLCASAdmin {
                echo "Hint <select name=\"repohint\">";
                        echo "<option value=\"fedora\">Fedora</option>";
                        echo "<option value=\"centos\">Centos</option>";
-                       echo "<option value=\"ubuntu\">Ubuntu</option>";
                        echo "</select><br>";
                echo "URL <input type=\"text\" name=\"repourl\"><br>";
                echo "<input type=\"submit\" name=\"Scan\" value=\"Scan\"><br>";
                echo "</form>";
                
-               echo "</td></tr></table><hr>";
+               echo "</td><td valign=\"top\">";
+               
+               // now for mirror lists
+               echo "<h3>Via Mirrolist - not implemented</h3>";
+               echo "<form method=\"post\" action=\"?action=mirrorlist\">";
+               echo "Distro <select name=\"mirrordistro\">";
+                       echo "<option value=\"fedora\">Fedora</option>";
+                       echo "<option value=\"centos\">Centos</option>";
+                       echo "</select><br>";
+               echo "Architecture <select name=\"arch\">";
+                       echo "<option value=\"x86_64\">64 bit</option>";
+                       echo "<option value=\"i386\">32 bit</option>";
+                       echo "</select><br>";
+               echo "Version <input type=\"text\" name=\"version\"><br>";
+               echo "Get updates too <input type=\"checkbox\" name=\"getupdates\"><br>";
+               echo "<input type=\"submit\" name=\"Scan\" value=\"Scan\"><br>";
+               echo "</form>";
+               
+               
+               echo "</td></tr></table>";
                
                // repo storage location
                echo "<h3>Storage<h3><br>";
                echo "<form method=\"post\" action=\"?action=setstorage\">";
                $storloc = $this->config->getConfigVar("storagelocation");
-               echo "<input type=\"text\" name=\"storageloc\" value=\"$storloc\" size=\"100\">";
+               echo "<input class=\"storage\" type=\"text\" name=\"storageloc\" value=\"$storloc\" size=\"100\">";
                echo "<input type=\"submit\" name=\"Set\" value=\"Set\">";
                echo "</form>";
        }