2 $URL_HANDLERS["*"] = "GLCASRepo";
6 function __construct($config)
8 $this->config = $config;
9 if($this->config->getConfigVar("storagelocation") == false) {
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");
21 error_log("repo:go called");
23 // figure out what we're doing
26 GLCASpageBuilder($this, "body");
29 $this->getRepoForUrl($url);
35 // this is how this will work
37 if(strncasecmp("list", $url, 4)==0) {
38 echo "i am the repo list";
41 echo "i am the repo, $url";
44 function getRepoForUrl($url)
46 // the way we breakdown a url is to explode it
47 $xurl = split("[/,]", $url);
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");
57 foreach($uconf as $key => $var) {
58 $pre = $var["prefix"];
61 //echo "Checking pre $pre against ".$xurl[0]."\n";
62 if(strcasecmp($pre, $xurl[0])==0) {
63 //echo "Matched pre\n";
71 foreach($uconf as $key => $var) {
72 // if we matched a pre, then we check against the second url component
74 $short = $var["shorturl"];
77 //echo "Checking short $short against ".$xurl[$startat]."\n";
78 if(strcasecmp($xurl[$startat], $short)==0) {
87 //echo "Match on $key\n";
91 // now we find an actual file
93 if(count($xurl) > $startat) for($i=$startat; $i < count($xurl); $i++) {
94 $file .= "/".$xurl[$i];
97 // now we want to find repostore/$matched/$file;
98 $actualfile = "$repostore/$matched/$file";
99 //echo "Start file for $actualfile\n";
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);
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);
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);
131 //echo "in getcheck\n";
132 $remotefile = $uconf[$matched]["url"]."/$file";
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
137 $rf = fopen($remotefile, "r");
138 error_log("attempting to get remote file $remotefile");
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
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");
149 header("Location: ".$_SERVER["REQUEST_URI"]."/");
152 // get content length form upstream and print
153 if(preg_match("/^Content-Length:.*/", $val)) {
156 // get content type from upstream and print
157 if(preg_match("/^Content-Type:.*/", $val)) {
161 //error_log("repsonse: $http_response_header");
164 header("HTTP/1.0 404 Not Found");
166 $localfile = fopen($actualfile.".tmp.data.deleteme", "w");
168 $data = fread($rf, 8192);
170 fwrite($localfile, $data);
175 rename($actualfile.".tmp.data.deleteme", $actualfile);
176 error_log("got actualfile, tried to save as $actualfile, did it work?");
180 //echo "got ".$file." for $url which is $actualfile\n";
182 //echo "</html></pre>";
185 function printDir($dir, $localfile, $baseurl)
187 $uri = $_SERVER["REQUEST_URI"];
189 echo "<html><head><title>Index of $localfile</title></head><body><h1>Index of $localfile</h1>";
192 while(($file = readdir($dh))!==false) {
193 if($file != "." && $file != "..") echo "<tr><td><a href=\"$uri/$file\">$file</a></td></tr>";
195 echo "</table></body></html>";
200 function getRepoDetailsYum($url, $ismirrorlist=false)
202 $actionurl = $url."/repodata/repomd.xml";
204 error_log("Getting for action of $actionurl");
206 $ld = file_get_contents($actionurl);
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)
211 if(!$ld) return false;
213 // ok, now we tokenize the url and try and guess at the content
214 $spurl = explode("/", $url);
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) {
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;
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;
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;
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";
260 function deleteRepo($rkey)
262 $uconf = $this->config->getConfigVar("repodata");
263 $repostore = $this->config->getConfigVar("storagelocation");
265 if($uconf !== false) {
266 $conf = unserialize($uconf);
267 foreach($conf as $key => $vla) {
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();
280 function addRepo($desc, $os, $version, $arch, $other, $shorturl, $prefix, $repurl, $repotype, $init)
282 $uconf = $this->config->getConfigVar("repodata");
286 $cs["version"] = $version;
288 $cs["other"] = $other;
289 $cs["shorturl"] = $shorturl;
290 $cs["prefix"] = $prefix;
291 $cs["url"] = $repurl;
292 $cs["repotype"] = $repotype;
296 if($uconf !== false) {
297 $conf = unserialize($uconf);
298 foreach($conf as $key => $val) {
306 $nconf = serialize($conf);
308 error_log("add repo as $ckey");
309 $this->config->setConfigVar("repodata", $nconf);
310 $this->config->saveConfig();
312 // now create the base structure in the repo
313 $repostore = $this->config->getConfigVar("storagelocation");
316 // now call update repo
317 if($init) $this->updateRepoYum($ckey);
320 function updateRepo($repokey)
322 // we only do yum yet
323 $this->updateRepoYum($repokey);
326 function updateRepoYum($repokey)
328 $repostore = $this->config->getConfigVar("storagelocation");
330 $repod = $this->getRepo($repokey);
332 $repourl = $repod["url"];
334 if(!file_exists("$repostore/$repokey")) {
335 mkdir("$repostore/$repokey");
338 if(!file_exists("$repostore/$repokey/repodata")) {
339 mkdir("$repostore/$repokey/repodata");
342 $actionurl = "$repourl/repodata/repomd.xml";
343 $repomdxml = file_get_contents($actionurl);
344 file_put_contents("$repostore/$repokey/repodata/repomd.xml", $repomdxml);
346 $xml = simplexml_load_file("$repostore/$repokey/repodata/repomd.xml");
349 foreach($xml as $key => $var) {
350 //echo "for key $key has:\n";
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);
359 error_log("Not getting $fileloc because we already have it");
365 function getRepo($id)
367 $uconf = $this->config->getConfigVar("repodata");
368 if($uconf !== false) {
369 $lconf = unserialize($uconf);
377 $uconf = $this->config->getConfigVar("repodata");
378 if($uconf !== false) {
379 return unserialize($uconf);