I updated a centos machine from this code... you ripper, it works!
[glcas.git] / plugins / repo.php
1 <?php
2 $URL_HANDLERS["*"] = "GLCASRepo";
3
4
5 class GLCASRepo {
6         function __construct($config)
7         {
8                 $this->config = $config;
9                 if($this->config->getConfigVar("storagelocation") == false) {
10                         global $WEB_ROOT_FS;
11                         $storloc = "$WEB_ROOT_FS/../var/glcas/cache/";
12                         if(!file_exists($storloc)) mkdir($storloc);
13                         $this->config->setConfigVar("storagelocation", realpath($storloc));
14                         $this->config->saveConfig();
15                         error_log("set storage location, $storloc");
16                 }
17         }
18         
19         function go($url)
20         {
21                 error_log("repo:go called");
22                 
23                 // figure out what we're doing 
24                 switch($url) {
25                         case "list":
26                                 GLCASpageBuilder($this, "body");
27                                 break;
28                         default:
29                                 $this->getRepoForUrl($url);
30                 }
31         }
32         
33         function body($url)
34         {
35                 // this is how this will work
36                 //$this->decodeUrl();
37                 if(strncasecmp("list", $url, 4)==0) {
38                         echo "i am the repo list";
39                         return;
40                 }
41                 echo "i am the repo, $url";
42         }
43         
44         function getRepoForUrl($url)
45         {
46                 // the way we breakdown a url is to explode it
47                 $xurl = split("[/,]", $url);
48                 
49                 // we first check if [0] is a prefix
50                 // if now, we check for it being a shorturl (lets just do that for now)
51                 $uconf = unserialize($this->config->getConfigVar("repodata"));
52                 $repostore = $this->config->getConfigVar("storagelocation");
53                 
54                 $matched = -1;
55                 $startat = 0;
56                 $prematch = false;
57                 foreach($uconf as $key => $var) {
58                         $pre = $var["prefix"];
59                         
60                         if($pre!="") {
61                                 //echo "Checking pre $pre against ".$xurl[0]."\n";
62                                 if(strcasecmp($pre, $xurl[0])==0) {
63                                         //echo "Matched pre\n";
64                                         $prematch = true;
65                                         $startat++;
66                                 }
67                         }
68                 }
69                 
70                 
71                 foreach($uconf as $key => $var) {
72                         // if we matched a pre, then we check against the second url component
73                         
74                         $short = $var["shorturl"];
75                         
76                         if($short!="") {
77                                 //echo "Checking short $short against ".$xurl[$startat]."\n";
78                                 if(strcasecmp($xurl[$startat], $short)==0) {
79                                         //echo "Matched\n";
80                                         $matched = $key;
81                                         $startat++;
82                                 }
83                         }
84                 }
85                 
86                 if($matched > -1) {
87                         //echo "Match on $key\n";
88                 }
89                 
90                 
91                 // now we find an actual file
92                 $file = "/";
93                 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
94                         $file .= "/".$xurl[$i];
95                 }
96                 
97                 // now we want to find repostore/$matched/$file;
98                 $actualfile = "$repostore/$matched/$file";
99                 //echo "Start file for $actualfile\n";
100                 
101                 // first check any directories in $file are in existence
102                 $splfile = explode("/", $file);
103                 if(count($splfile) > 1) {
104                         $tomake = "$repostore/$matched/";
105                         for($i = 0; $i < count($splfile)-1; $i++) {
106                                 $tomake .= "/".$splfile[$i];
107                                 //error_log("making directory $tomake");
108                                 if(!is_dir($tomake)) mkdir($tomake);
109                         }
110                 }
111                 
112                 
113                 if(is_file($actualfile)) {
114                         // file is stored locally, away we go
115                         header("Content-Length: ".filesize($actualfile));
116                         $type = mime_content_type($actualfile);
117                         header("Content-type: $type");
118                         $localfile = fopen($actualfile, "r");
119                         while(!feof($localfile)) {
120                                 $data = fread($localfile, 16384);
121                                 echo $data;
122                                 flush();
123                         }
124                         fclose($localfile);
125                 } else if(is_dir($actualfile)) {
126                         //echo "in dir for $actualfile\n";
127                         // here we print the contents of the directory
128                         $this->printDir($actualfile, $file, $url);
129                 } else {
130                         // ok, get the file
131                         //echo "in getcheck\n";
132                         $remotefile = $uconf[$matched]["url"]."/$file";
133                         
134                         // TODO: i should get remote contents with fopen/fread/fwrite as
135                         // it should be more memory conservative and we can push to the end client
136                         // straight away
137                         $rf = fopen($remotefile, "r");
138                         error_log("attempting to get remote file $remotefile");
139
140                         
141                         // hopefully this works. if we get a 30x message, it means we tried to get a directory
142                         // i cant think of another way of dealing with it - but this is UGLY
143                         // also get content length and content type
144                         $clen = 0;
145                         foreach($http_response_header as $key => $val) {
146                                 if(preg_match("/HTTP.*30[1-9].*/", $val)) {
147                                         error_log("got a 30x, must be a directory");
148                                         mkdir($actualfile);
149                                         header("Location: ".$_SERVER["REQUEST_URI"]."/");
150                                         return;
151                                 }
152                                 // get content length form upstream and print
153                                 if(preg_match("/^Content-Length:.*/", $val)) {
154                                         header($val);
155                                 }
156                                 // get content type from upstream and print
157                                 if(preg_match("/^Content-Type:.*/", $val)) {
158                                         header($val);   
159                                 }
160                         }
161                         //error_log("repsonse: $http_response_header");
162                         if(!$rf) {
163                                 // return 404
164                                 header("HTTP/1.0 404 Not Found");
165                         } else {
166                                 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");                              
167                                 while(!feof($rf)) {
168                                         $data = fread($rf, 8192);
169                                         echo $data;
170                                         fwrite($localfile, $data);
171                                         flush();
172                                 }
173                                 fclose($localfile);
174                                 fclose($rf);
175                                 rename($actualfile.".tmp.data.deleteme", $actualfile);
176                                 error_log("got actualfile, tried to save as $actualfile, did it work?");
177                         }
178                 }
179                 
180                 //echo "got ".$file." for $url which is $actualfile\n";
181                 
182                 //echo "</html></pre>";
183         }
184         
185         function printDir($dir, $localfile, $baseurl)
186         {
187                 $uri = $_SERVER["REQUEST_URI"];
188                 if(is_dir($dir)) {
189                         echo "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
190                         echo "<table>";
191                         $dh = opendir($dir);
192                         while(($file = readdir($dh))!==false) {
193                                 if($file != "." && $file != "..") echo "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
194                         }
195                         echo "</table></body></html>";
196                         
197                 } else return false;
198         }
199         
200         function getRepoDetailsYum($url, $ismirrorlist=false)
201         {
202                 $actionurl = $url."/repodata/repomd.xml";
203                 
204                 error_log("Getting for action of $actionurl");
205                 
206                 $ld = file_get_contents($actionurl);
207                 
208                 // so here we try and get what this repository provides (os, version, arch), for yum this
209                 // should come straight off the url... i.e. centos/6.0/os/x86_64/ (centos, 6.0, base os, 64bit arch)
210                 
211                 if(!$ld) return false;
212                 
213                 // ok, now we tokenize the url and try and guess at the content
214                 $spurl = explode("/", $url);
215                 
216                 // first, find the OS
217                 $kos = getKnownOSList();
218                 $glt["OS"] = "unknown";
219                 $glt["verison"] = "unknown";
220                 $glt["arch"] = "unknown";
221                 $glt["other"] = "unknown";
222                 foreach($spurl as $comp) {
223                         
224                         // find a name
225                         foreach($kos["os"]["short"] as $kosname => $koslong) {
226                                 //error_log("Comparing $kosname and $koslong with $comp");
227                                 if(strcasecmp($kosname, $comp) == 0) {
228                                         //error_log("got $kosname, $koslong for $comp in $url");
229                                         //echo "<pre>inone\n"; print_r($koslong); echo "</pre>";
230                                         $glt["OS"] = $koslong;
231                                 }
232                         }
233                         
234                         // find a version, we assume its going to be something [numbers] and a . (optional)
235                         if(preg_match("/^[0-9.]+$/", $comp)>0) {
236                                 error_log("version match of $comp");
237                                 $glt["version"] = $comp;
238                         }
239                         
240                         // 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
241                         foreach($kos["arch"] as $archinter => $archname ) {
242                                 //error_log("Comparing $archinter, $archname with $comp");
243                                 if(strcasecmp($archname, $comp) == 0) {
244                                         error_log("arch match of $archname with $comp");
245                                         $glt["arch"] = $archname;
246                                 }
247                         }
248                         
249                         // other is a bt harder, we really have to guess at this one
250                         if(strcasecmp("os", $comp) == 0) $glt["other"] = "OS";
251                         if(strcasecmp("update", $comp) == 0) $glt["other"] = "Updates";
252                         if(strcasecmp("updates", $comp) == 0) $glt["other"] = "Updates";
253                         if(strcasecmp("everything", $comp) == 0) $glt["other"] = "OS";
254                 }
255                 
256                         
257                 return $glt;
258         }
259         
260         function deleteRepo($rkey)
261         {
262                 $uconf = $this->config->getConfigVar("repodata");
263                 $repostore = $this->config->getConfigVar("storagelocation");
264                 
265                 if($uconf !== false) {
266                         $conf = unserialize($uconf);
267                         foreach($conf as $key => $vla) {
268                                 if($key == $rkey) {
269                                         unset($conf["$rkey"]);
270                                         $nconf = serialize($conf);
271                                         system("rm -rf $repostore/$key");
272                                         error_log("remove repo as $rkey");
273                                         $this->config->setConfigVar("repodata", $nconf);
274                                         $this->config->saveConfig();
275                                 }
276                         }
277                 }
278         }
279         
280         function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
281         {
282                 $uconf = $this->config->getConfigVar("repodata");
283                 
284                 $cs["desc"] = $desc;
285                 $cs["os"] = $os;
286                 $cs["version"] = $version;
287                 $cs["arch"] = $arch;
288                 $cs["other"] = $other;
289                 $cs["shorturl"] = $shorturl;
290                 $cs["prefix"] = $prefix;
291                 $cs["url"] = $repurl;
292                 $cs["repotype"] = $repotype;
293                 
294                 
295                 $ckey = 0;
296                 if($uconf !== false) {
297                         $conf = unserialize($uconf);
298                         foreach($conf as $key => $val) {
299                                 $ckey = $key;
300                         }
301                         $ckey++;
302                 }
303                 
304                 $conf[$ckey] = $cs;
305                 
306                 $nconf = serialize($conf);
307                 
308                 error_log("add repo as $ckey");
309                 $this->config->setConfigVar("repodata", $nconf);
310                 $this->config->saveConfig();
311                 
312                 // now create the base structure in the repo
313                 $repostore = $this->config->getConfigVar("storagelocation");
314                 
315                 
316                 // now call update repo
317                 if($init) $this->updateRepoYum($ckey);
318         }
319         
320         function updateRepo($repokey)
321         {
322                 // we only do yum yet
323                 $this->updateRepoYum($repokey);
324         }
325         
326         function updateRepoYum($repokey)
327         {
328                 $repostore = $this->config->getConfigVar("storagelocation");
329                 
330                 $repod = $this->getRepo($repokey);
331                 
332                 $repourl = $repod["url"];
333                 
334                 if(!file_exists("$repostore/$repokey")) {
335                         mkdir("$repostore/$repokey");
336                 }
337                 
338                 if(!file_exists("$repostore/$repokey/repodata")) {
339                         mkdir("$repostore/$repokey/repodata");
340                 }
341                 
342                 $actionurl = "$repourl/repodata/repomd.xml";
343                 $repomdxml = file_get_contents($actionurl);
344                 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
345                 
346                 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
347                 
348                 
349                 foreach($xml as $key => $var) {
350                         //echo "for key $key has:\n";
351                         //print_r($var);
352                         if($key == "data") {
353                                 $fileloc = $var->location["href"];
354                                 if(!file_exists("$repostore/$repokey/$fileloc")) {
355                                         error_log("getting $fileloc for $repokey on $repourl");
356                                         $dlfile = file_get_contents("$repourl/$fileloc");
357                                         file_put_contents("$repostore/$repokey/$fileloc", $dlfile);
358                                 } else {
359                                         error_log("Not getting $fileloc because we already have it");
360                                 }
361                         }
362                 }
363         }
364         
365         function getRepo($id)
366         {
367                 $uconf = $this->config->getConfigVar("repodata");
368                 if($uconf !== false) {
369                         $lconf = unserialize($uconf);
370                         return $lconf[$id];
371                 } else return false;
372                 
373         }
374         
375         function getRepos()
376         {
377                 $uconf = $this->config->getConfigVar("repodata");
378                 if($uconf !== false) {
379                         return unserialize($uconf);
380                 } else return false;
381                 
382         }
383         
384         private $config;
385 }
386
387 ?>