hello
[glcas.git] / plugins / admin.php
1 <?php 
2
3 error_log("admin loaded");
4
5 global $URL_HANDLERS;
6 $URL_HANDLERS["admin.*"] = "GLCASAdmin";
7 global $CRON_CLASSES;
8 $CRON_CLASSES["GLCASAdmin"] = "GLCASAdmin";
9
10 class GLCASAdmin {
11         function __construct($config)
12         {
13                 $this->config = $config;
14                 error_log("constructor for GLCASAdmin");
15                 
16         }
17         
18         function go($url)
19         {
20                 error_log("repo:go called");
21                 
22                 if(isset($_REQUEST["action"])) {
23                         switch($_REQUEST["action"]) {
24                                 case "updaterepo":
25                                         error_log("in updaterepo");
26                                         GLCASpageBuilder($this,"doUpdateRepo");
27                                         return;
28                                 case "reponext":
29                                         error_log("in reponext");
30                                         GLCASpageBuilder($this,"doRepoNextForm");
31                                         return;
32                                         break;
33                                 case "addrepoyum":
34                                         GLCASpageBuilder($this,"doAddRepoYum");
35                                         return;
36                                 case "deleterepo":
37                                         error_log("call delete repo");
38                                         GLCASpageBuilder($this, "doRemoveRepo");
39                                         return;
40                                 case "setstorage":
41                                         error_log("call setstorage");
42                                         GLCASpageBuilder($this, "setStorage");
43                                         return;
44                                 case "scanrepo":
45                                         break;
46                         }
47                 }
48                 GLCASpageBuilder($this, "body");
49         }
50         
51         function cron()
52         {
53                 echo "ADMIN CRON: nothing to see here<br>";
54                 // TODO: touch cronstamp file here
55                 
56                 // make a backup of the config.. but.. errr. how to tell if theres an old backup?
57                 // backups will occur... weekly and be called $configpath.DDMMYYYY
58                 $configpath = glcas_getWebConfigPath();
59                 $basepath = dirname($configpath);
60                 echo "ADMIN CRON: backing up config, $configpath, $basepath<br>";
61                 $nowtime = time();
62                 $oneday = 3600 * 24; // 1 hour in seconds, by 24 hours
63                 for($i = 0; $i < 7; $i ++) {
64                         $cdatestr = strftime("%d-%h-%Y", $nowtime - ($oneday*$i));
65                         $backupfile = "$configpath.$cdatestr";
66                         error_log("checking for backup of config as $backupfile");
67                         if(file_exists($backupfile)) {
68                                 error_log("backupconfig exists as $backupfile, exit");
69                                 echo "ADMIN CRON: backup of config is $backupfile, created $i days ago, not creating new one<br>";
70                                 return;
71                         }
72                 }
73                 $nowdatestr = strftime("%d-%h-%Y");
74                 $backupfile = "$configpath.$nowdatestr";
75                 copy($configpath, $backupfile);
76                 echo "ADMIN CRON: createing backup of config as $backupfile<br>";
77         }
78         
79         function body($url)
80         {
81                 // for the main admin body, we hae the following components
82                 
83                 // 1. list of currently available repos and stuff
84                 
85                 // 2. way to add a repo direct
86                 
87                 // 3. way to scan for a repo given a url
88                 
89                 // so first, lets do the main body
90                 $this->mainBody($url);
91         }
92         
93         function doRemoveRepo($url)
94         {
95                 $repo = $_REQUEST["repo"];
96                 $myRep = new GLCASRepo($this->config);
97                 
98                 
99                 error_log("called delete repo on $repo");
100                 $myRep->deleteRepo($repo);
101                 
102                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
103                 header("Location: $BASE_URL/admin/");
104         }
105         
106         
107         function setStorage($url)
108         {
109                 
110         }
111         
112         function doRepoNextForm($url)
113         {
114                 $myRep = new GLCASRepo($this->config);
115                 
116                 $wasyum = false;
117                 $wasapt = false;
118                 if($_REQUEST["repotype"] == "yumbase") {
119                         $glt = $myRep->getRepoDetailsYum($_REQUEST["repourl"]);
120                         $wasyum = true;
121                 } else if($_REQUEST["repotype"] == "yummirrorlist") {
122                         $glt = $myRep->getRepoDetailsYum($_REQUEST["repourl"], true);
123                         $wasyum = true;
124                 } else if($_REQUEST["repotype"] == "apt") {
125                         $glt = $myRep->getRepoDetailsApt($_REQUEST["repourl"]);
126                         $wasapt = true;
127                 }
128                 
129                 if($wasapt) {
130                         error_log("in wasapt");
131                         $repourl = $_REQUEST["repourl"];
132                         
133                         if($glt === false) {
134                                 echo "Could not find any apt repository at $repourl<br>";
135                                 return;
136                         }
137                         
138                         $extratext = "";
139                         $provides = "";
140                         if($glt["nrepos"] > 0) {
141                                 $kos = getKnownOSList();
142                                 foreach($glt["knownrepo"] as $key => $val) {
143                                         $reponame = $kos["apt"][$val];
144                                         //echo "Fount a repo, $val which is $reponame<br>";
145                                         $provides .= "$reponame\n";
146                                 }
147                                 $extratext = " for ".$glt["distros"];
148                         } else {
149                                 //echo "Looks like an apt repo, but I cant find any known distro's<br>";
150                         }
151                         
152                         echo "<form method=\"post\" action=\"?action=addrepoapt\">";
153                         echo "<input type=\"hidden\" name=\"repourl\" value=\"$repourl\">";
154                         echo "<table>";
155                         echo "<tr><td>Description</td><td><input type=\"text\" name=\"desc\" value=\"APT Repo$extratext\"></td>";
156                         echo "<tr><td>Provides</td><td><textarea name=\"provides\">$provides</text area></td></tr>";
157                         echo "<tr><td>Short URL</td><td><input type=\"text\" name=\"shorturl\"></td></tr>";
158                         echo "<tr><td>URL Prefix (blank for none)</td><td><input type=\"text\" name=\"prefix\"></td></tr>";
159                         echo "<tr><td>Expire time for Meta Data</td><td><input type=\"text\" name=\"expiretime\"> days</td></tr>";
160                         echo "</table>";
161                         echo "</form>";
162                         return true;
163                 }
164                 
165                 if($wasyum) {
166                         $os = $glt["OS"];
167                         $version = $glt["version"];
168                         $arch = $glt["arch"];
169                         $other = $glt["other"];
170                         $repourl = $_REQUEST["repourl"];
171                         echo "<form method=\"post\" action=\"?action=addrepoyum\">";
172                         echo "<input type=\"hidden\" name=\"repourl\" value=\"$repourl\">";
173                         echo "<table>";
174                         echo "<tr><td>Description</td><td><input type=\"text\" name=\"desc\" value=\"$os, $version, $arch - $other\"></td></tr>";
175                         echo "<tr><td>OS</td><td><input type=\"text\" name=\"OS\" value=\"$os\"></td></tr>";
176                         echo "<tr><td>Version</td><td><input type=\"text\" name=\"version\" value=\"$version\"></td></tr>";
177                         echo "<tr><td>Architecture</td><td><input type=\"text\" name=\"arch\" value=\"$arch\"></td></tr>";
178                         echo "<tr><td>Other (OS, Updates, etc)</td><td><input type=\"text\" name=\"other\" value=\"$other\"></td></tr>";
179                         echo "<tr><td>Short URL</td><td><input type=\"text\" name=\"shorturl\"></td></tr>";
180                         echo "<tr><td>URL Prefix (blank for none)</td><td><input type=\"text\" name=\"prefix\"></td></tr>";
181                         echo "<tr><td>Do Initial Update (can take a while, but done in background)</td><td><input type=\"checkbox\" name=\"initial\" checked></td></tr>";
182                         echo "<tr><td>Expire time for Meta Data</td><td><input type=\"text\" name=\"expiretime\"> days</td></tr>";
183                         echo "<tr><td><input type=\"submit\" name=\"Add\" value=\"Add\"></td></tr>";
184                         echo "</table>";
185                         echo "</form>";
186                 }
187         }
188         
189         function doAddRepoYum($url)
190         {
191                 $repo = new GLCASRepo($this->config);
192                 
193                 $desc = $_REQUEST["desc"];
194                 $OS = $_REQUEST["OS"];
195                 $version = $_REQUEST["version"];
196                 $arch = $_REQUEST["arch"];
197                 $other = $_REQUEST["other"];
198                 $shorturl = $_REQUEST["shorturl"];
199                 $prefix = $_REQUEST["prefix"];
200                 $repurl = $_REQUEST["repourl"];
201                 $expiretime = $REQUEST["expiretime"];
202                 $init = false;
203                 if(isset($_REQUEST["initial"])) $init = true;
204                 
205                 
206                 
207                 $repo->addRepo($desc, $OS, $version, $arch, $other, $shorturl, $prefix, $repurl, "YUM", $init, $expiretime);
208                 
209                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
210                 header("Location: $BASE_URL/admin/");
211         }
212         
213         function doUpdateRepo($url)
214         {
215                 $rkey = $_REQUEST["repo"];
216                 
217                 $repo = new GLCASRepo($this->config);
218                 
219                 $repo->updateRepo($rkey);
220                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
221                 header("Location: $BASE_URL/admin/");
222         }
223         
224         function mainBody($url)
225         {
226                 global $WEB_ROOT_FS, $URL_HANDLERS, $BASE_URL;
227                 
228                 // first, list available repos
229                 echo "<h3>Repositories</h3>";
230                 echo "<br><table border=\"1\">";
231                 echo "<tr><th>Name</th><th>Type</th><th>OS</th><th>Version</th><th>Architecture</th><th>Other</th><th>Prefix</th><th>Short URL</th><th>Browse</th><th>Control</th></tr>";
232                 
233                 // now iterate thru the repos and print them
234                 $repo = new GLCASRepo($this->config);
235                 $repos = $repo->getRepos();
236                 
237                 foreach($repos as $rkey => $rval) {
238                         $desc = $rval["desc"];
239                         $os = $rval["os"];
240                         $version = $rval["version"];
241                         $arch = $rval["arch"];
242                         $other = $rval["other"];
243                         $repotype = $rval["repotype"];
244                         $prefix = $rval["prefix"];
245                         $shorturl = $rval["shorturl"];
246                         if($prefix == "") $prefix = "-";
247                         if($shorturl == "") $shorturl = "-";
248                         echo "<tr><td>$desc</td><td>$repotype</td><td>$os</td><td>$version</td><td>$arch</td><td>$other</td><td>$prefix</td><td>$shorturl</td>";
249                         
250                         // get url
251                         $browseurl = "$BASE_URL/repo/$rkey";
252                         if($shorturl!="-") {
253                                 $brurl = $shorturl;
254                                 if($prefix != "-") $brurl = "$prefix/$shorturl";
255                                 $browseurl = "$BASE_URL/$brurl/";
256                         }
257                         
258                         echo "<td><a href=\"$browseurl\">Browse</td><td>";
259                         
260                         // Edit
261                         echo "<a href=\"?action=editrepo&repo=$rkey\">Edit</a> ";
262                         // update
263                         echo "<a href=\"?action=updaterepo&repo=$rkey\">Update</a> ";
264                         // freeze
265                         echo "<a href=\"?action=freezerepo&repo=$rkey\">Freeze</a> ";
266                         // deactivate
267                         echo "<a href=\"?action=disablerepo&repo=$rkey\">Disable</a> ";
268                         // clean
269                         echo "<a href=\"?action=cleanrepo&repo=$rkey\">Clean</a> ";
270                         // Remove
271                         echo "<a href=\"?action=deleterepo&repo=$rkey\">Delete</a>";
272                         
273                         echo "</td>";
274                         echo "</tr>";
275                 }
276                 
277                 echo "</table><br><hr>";
278                 //echo "<pre>";
279                 //if($repos !== false) print_r($repos);
280                 //echo "</pre>";
281                 
282                 // wrap all this in a table
283                 echo "<table><tr><td valign=\"top\">";
284                 // now, add a repo
285                 echo "<h3>Add A Repo</h3>";
286                 echo "<form method=\"post\" action=\"?action=reponext\">";
287                 echo "Type <select name=\"repotype\">";
288                         echo "<option value=\"yumbase\">YUM (Base URL)</option>";
289                         echo "<option value=\"yummirrorlist\">YUM (Mirror List) - not implemented</option>";
290                         echo "<option value=\"apt\">APT - not implemented</option>";
291                         echo "</select><br>";
292                 echo "URL <input type=\"text\" name=\"repourl\"><br>";
293                 echo "<input type=\"submit\" name=\"Add\" value=\"Add\"><br>";
294                 echo "</form>";
295                 
296                 echo "</td><td valign=\"top\">";
297                 
298                 // now scan for a repo
299                 echo "<h3>Scan For Repos - not implemented</h3>";
300                 echo "<form method=\"post\" action=\"?action=scanrepo\">";
301                 echo "Hint <select name=\"repohint\">";
302                         echo "<option value=\"fedora\">Fedora</option>";
303                         echo "<option value=\"centos\">Centos</option>";
304                         echo "<option value=\"ubuntu\">Ubuntu</option>";
305                         echo "</select><br>";
306                 echo "URL <input type=\"text\" name=\"repourl\"><br>";
307                 echo "<input type=\"submit\" name=\"Scan\" value=\"Scan\"><br>";
308                 echo "</form>";
309                 
310                 echo "</td></tr></table><hr>";
311                 
312                 // repo storage location
313                 echo "<h3>Storage<h3><br>";
314                 echo "<form method=\"post\" action=\"?action=setstorage\">";
315                 $storloc = $this->config->getConfigVar("storagelocation");
316                 echo "<input type=\"text\" name=\"storageloc\" value=\"$storloc\" size=\"100\">";
317                 echo "<input type=\"submit\" name=\"Set\" value=\"Set\">";
318                 echo "</form>";
319         }
320
321         private $config;
322 }
323
324 ?>