2c78f60acf8d578f09afe32a01ef30fa0b70c025
1 <?php
3 class faiPackage extends plugin
4 {
5 /* CLI vars */
6 var $cli_summary = "Manage server basic objects";
7 var $cli_description = "Some longer text\nfor help";
8 var $cli_parameters = array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10 /* attribute list for save action */
11 var $ignore_account = TRUE;
13 /* Attributes for this Object */
14 var $attributes = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection","FAIdebianMirror");
16 /* ObjectClasses for this Object*/
17 var $objectclasses = array("top","FAIclass","FAIpackageList","FAIrepository");
19 /* Class name of the Ldap ObjectClass for the Sub Object */
20 var $subClass = "";
21 var $subClasses = array("top","FAIclass","FAIscriptEntry");
23 /* Class name of the php class which allows us to edit a Sub Object */
24 var $subClassName = "";
26 /* Attributes to initialise for each subObject */
27 var $subAttributes = array("cn","description");
28 var $sub64coded = array();
30 var $ConfiguredPackages = array();
32 /* Specific attributes */
33 var $cn = ""; // The class name for this object
34 var $description = ""; // The description for this set of partitions
35 var $is_dialog = false; // specifies which buttons will be shown to save or abort
36 var $dialog = NULL; // a dialog, e.g. new disk dialog
37 var $SubObjects = array(); // All leafobjects of this object
39 var $FAIdebianRelease = ""; // The selected release
40 var $FAIdebianSection = ""; // selected section
41 var $FAIdebianMirror = ""; // selected mirror
42 var $mirror = ""; // selected mirror
44 var $servers = array(); // All available servers
45 var $releases = array(); // All possible releases
46 var $sections = array(); // All section types
48 var $list = NULL;
50 var $mirrors = array(); // The combination of server/release/section
51 var $confDir = "";
52 var $usedPackages = array();
53 var $buffer = NULL;
54 var $strID ="";
55 var $newDialogShown =false;
57 function faiPackage ($config, $dn= NULL)
58 {
59 /* Load Attributes */
60 plugin::plugin ($config, $dn);
62 /* If "dn==new" we try to create a new entry
63 * Else we must read all objects from ldap which belong to this entry.
64 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
65 */
66 if($dn != "new"){
67 $this->dn =$dn;
68 }
70 if(isset($this->attrs['FAIpackage'])){
71 unset($this->attrs['FAIpackage']['count']);
72 foreach($this->attrs['FAIpackage'] as $pkg){
73 $this->usedPackages[$pkg] = $pkg;
74 }
75 ksort($this->usedPackages);
76 }else{
77 $this->usedPackages = array();
78 }
80 $ldap = $this->config->get_ldap_link();
81 foreach($this->usedPackages as $name){
82 $ldap->search("(&(objectClass=FAIdebconfInfo)(FAIpackage=".$name."))");
83 while($attr = $ldap->fetch()){
85 $tmp =array();
86 $tmp['Name'] = $attr['FAIvariable'][0];
87 $tmp['Type'] = $attr['FAIvariableType'][0];
89 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
90 $tmp['Value'] = $attr['FAIvariableContent'][0];
91 }else{
92 $content = array();
93 unset($attr['FAIvariableContent']['count']);
94 foreach($attr['FAIvariableContent'] as $attr){
95 $tmp['Value'][] = $attr;
96 }
97 }
98 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
99 }
100 }
102 if (isset($this->attrs['FAIdebianSection']['count'])){
103 unset($this->attrs['FAIdebianSection']['count']);
104 }
105 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
106 $this->FAIdebianSection = array();
107 foreach($this->attrs['FAIdebianSection'] as $sec){
108 $this->FAIdebianSection[$sec]=$sec;
109 }
110 }
112 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
113 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
114 }
115 $this->confDir = CONFIG_DIR."/fai/";
116 $this->FAIpackage = array();
118 /* Extract mirror location */
119 $this->mirror= preg_replace("!^[^/]+//([^/]+).*$!", '$1', $this->FAIdebianMirror);
120 }
122 function execute()
123 {
124 /* Call parent execute */
126 plugin::execute();
128 /* Fill templating stuff */
129 $smarty= get_smarty();
130 $display= "";
132 $this->genMirror();
134 /* Check if we exist already - no need to ask for revisions, etc. then */
135 if ($this->dn != "new"){
136 $this->newDialogShown= true;
137 }
139 if((!$this->is_account)&&(!$this->newDialogShown)){
141 if($this->dialog==NULL){
142 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
143 $this->is_dialog =true;
144 }
146 /* alert possible missconfigurations */
147 if((count($this->releases)==0)||(count($this->servers)==0)||(count($this->sections)==0)){
148 print_red(_("There is no useable package list defined."));
149 }
151 /* Assign posible changes, for mirror combinations */
152 $this->dialog->save_object();
153 $this->dialog->releases = $this->releases;
154 $this->dialog->servers = $this->servers;
155 $this->dialog->sections = $this->sections;
156 $this->dialog->mirrors = $this->mirrors;
158 /* Assign Repository settings*/
159 if(isset($_POST['SaveObjectNew'])){
160 $this->FAIdebianRelease = $this->dialog->FAIdebianRelease;
161 $this->FAIdebianSection = $this->dialog->FAIdebianSection;
162 $this->FAIdebianMirror = $this->dialog->FAIdebianMirror;
163 unset($this->dialog);
164 $this->dialog = false;
165 $this->is_dialog=false;
166 $this->newDialogShown = true;
167 }
169 /* Draw dialog */
170 if($this->dialog){
171 $display=$this->dialog->execute();
172 return($display);
173 }
174 }
176 /* Assign variables */
177 foreach($this->attributes as $attrs){
178 $smarty->assign($attrs,$this->$attrs);
179 }
181 /* Set mirror if changed */
182 if((isset($_POST['FAIdebianMirrorS']))&&(!empty($_POST['FAIdebianMirrorS']))){
183 $this->FAIdebianMirror = $_POST['FAIdebianMirrorS'];
184 }
186 /* check servers matching release and section settings */
187 $availableServer = array();
188 foreach($this->mirrors as $mir){
189 if($mir['release'] == $this->FAIdebianRelease){
190 $pass = true;
191 foreach($this->FAIdebianSection as $sec){
192 if($sec != $mir['section']){
193 $pass = false;
194 }
195 if($pass){
196 $availableServer[$mir['mirror']]=$mir['mirror'];
197 }
198 }
199 }
200 }
202 /* Generate package list */
203 $this->list=$this->genPkgs();
205 /* + was pressed to open the package dialog */
206 if(isset($_POST['Addpkg'])){
207 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
208 $this->is_dialog =true;
209 }
211 /* Delte selected package */
212 if(isset($_POST['Delpkg'])){
213 unset($this->usedPackages[$_POST['usedPackages']]);
214 }
216 /* Abort package selection dialog */
217 if(isset($_POST['CancelSubObject'])){
218 $this->dialog = false;
219 $this->is_dialog=false;
220 }
222 /* attach new packages */
223 if(isset($_POST['SaveSubObject'])) {
224 $this->dialog->save_object();
225 if(count($this->dialog->check())){
226 foreach($this->dialog->check() as $msgs){
227 print_red($msgs);
228 }
229 }else{
230 $use = $this->dialog->save();
231 $this->usedPackages = $use;
232 $this->dialog = false;
233 $this->is_dialog=false;
234 ksort($this->usedPackages);
235 }
236 }
238 /* Configuration dialog open*/
239 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
240 $path = "/etc/gosa/fai/".preg_replace("#^[^/]+//([^/]+).*$#","\\1",$this->FAIdebianMirror)."/".$this->FAIdebianRelease."/".$sec;
241 $pkg_config = array();
242 if(isset($this->ConfiguredPackages[$_POST['usedPackages']])){
243 $pkg_config = $this->ConfiguredPackages[$_POST['usedPackages']];
244 }
246 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$_POST['usedPackages'], $path, $pkg_config);
247 $this->is_dialog =true;
248 }
250 /* Save Configuration */
251 if(isset($_POST['SaveObjectConfig'])){
252 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
253 $this->dialog = false;
254 $this->is_dialog=false;
255 }
257 /* cancel configuration */
258 if(isset($_POST['CancelObjectConfig'])){
259 $this->dialog = false;
260 $this->is_dialog=false;
261 }
263 /* Display dialog */
264 if($this->is_dialog){
265 return $this->dialog->execute();
266 }
268 /* Assign section to smarty */
269 $strsec = "";
270 foreach($this->FAIdebianSection as $sec){
271 $strsec .= $sec." ";
272 }
274 $smarty->assign("OptionsACL","");
275 if(empty($this->FAIdebianMirror)){
276 $smarty->assign("OptionsACL"," disabled ");
277 }
279 $smarty->assign("mirrors" ,$availableServer);
280 $smarty->assign("mirror" ,$this->FAIdebianMirror);
281 $smarty->assign("releases",$this->releases);
282 $smarty->assign("release" ,$this->FAIdebianRelease);
283 $smarty->assign("sections",$this->sections);
284 $smarty->assign("section" ,$strsec);
285 $smarty->assign("usedPackages",$this->printUsedPackages());
286 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
287 return($display);
288 }
290 function genMirror(){
291 $confDir = $this->confDir;
292 if(!is_readable($confDir)){
293 print_red(sprintf(_("Can't read configuration folder '%s'."),$confDir));
294 }else{
296 /* Try to catch all available mirrors
297 Possibly check if each server is reachable
298 */
300 $this->servers= array();
301 $this->releases=array();
302 $this->sections=array();
303 $this->mirrors= array();
304 $fd = opendir($confDir);
305 while($mirror = readdir($fd)){
306 if((is_dir($confDir.$mirror."/"))&&(($mirror != "." )&&($mirror!=".."))){
308 $mirrorDir = $confDir.$mirror."/";
310 $fe = opendir($mirrorDir);
311 while($release = readdir($fe)){
312 if((is_dir($mirrorDir.$release))&&(($release != "." )&&($release!=".."))){
314 $releaseDir = $mirrorDir.$release."/";
316 $ff = opendir($releaseDir);
317 while($section = readdir($ff)){
318 if((is_file($releaseDir.$section))&&(($section != "." )&&($section!="..")&&(!preg_match("/.*\.in$/",$section)))){
320 $this->servers[$mirror]=$mirror;
321 $this->releases[$release]=$release;
322 $this->sections[$section]=$section;
324 $arr=array();
325 $arr['mirror'] = $mirror;
326 $arr['release']= $release;
327 $arr['section']= $section;
329 $this->mirrors[] = $arr ;
330 }
331 }
332 fclose($ff);
333 }
334 }
335 fclose($fe);
336 }
337 }
338 fclose($fd);
339 }
342 }
344 /* Delete me, and all my subtrees
345 */
346 function remove_from_parent()
347 {
348 $ldap = $this->config->get_ldap_link();
349 $ldap->cd ($this->dn);
350 $ldap->rmdir_recursive($this->dn);
351 $this->handle_post_events("remove");
352 }
355 /* Save data to object
356 */
357 function save_object()
358 {
360 plugin::save_object();
361 foreach($this->attributes as $attrs){
362 if(isset($_POST[$attrs])){
363 $this->$attrs = $_POST[$attrs];
364 }
365 }
366 }
369 /* Check supplied data */
370 function check()
371 {
372 $message= array();
374 if(count($this->usedPackages)==0){
375 $message[]=_("Please select a least one Package.");
376 }
378 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))||(empty($this->FAIdebianMirror))){
379 $message[]=_("Please choose a valid combination for your repository setup.");
380 }
382 return ($message);
383 }
385 function printUsedPackages(){
386 $a_ret=array();
387 if(is_array($this->usedPackages)) {
388 foreach($this->usedPackages as $usedName){
389 if(isset($this->list[$usedName][1])){
390 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]";
391 }else{
392 $a_ret[$usedName] = $usedName;
393 }
394 }
395 }
396 return($a_ret);
397 }
399 function genPkgs(){
400 /* Generate a list off available packages for this mirror, section and release
401 */
402 /* Only read this file if it wasn't read before */
403 if($this->buffer==NULL){
404 $this->buffer=array();
405 $a_ret = array();
406 foreach($this->FAIdebianSection as $sec){
407 $strID= "/etc/gosa/fai/".preg_replace("#^[^/]+//([^/]+).*$#","\\1",$this->FAIdebianMirror)."/".$this->FAIdebianRelease."/".$sec;
409 if(!is_file($strID)){
410 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
411 $this->FAIdebianMirror="";
412 unset($this->buffer);
413 return(array());
414 }
415 $fp = fopen($strID,"r");
417 /* Parse every line and create an array */
418 while(!feof($fp)){
419 $str= fgets($fp,512);
420 $stra= split("\|",$str);
421 if(count($stra)==4){
422 $a_ret[$stra[0]] = $stra;
423 }
424 }
425 fclose($fp);
426 /* Save our Data, to avoid reading it again */
427 }
428 $this->buffer = $a_ret;
429 ksort($a_ret);
430 return($a_ret);
431 }else{
432 return $this->buffer;
433 }
434 }
437 /* Save to LDAP */
438 function save()
439 {
440 plugin::save();
442 $ldap = $this->config->get_ldap_link();
444 $this->attrs['FAIpackage'] = array();
445 foreach($this->usedPackages as $pkg => $obj){
446 $this->attrs['FAIpackage'][] = $pkg;
447 }
449 $this->attrs['FAIdebianSection'] = array();
450 foreach($this->FAIdebianSection as $sec){
451 $this->attrs['FAIdebianSection'][] = $sec;
452 }
454 /* Save Package configurations */
455 foreach($this->ConfiguredPackages as $pkgname => $attrs){
456 foreach($attrs as $name => $attr){
458 foreach($attr as $n=>$v){
459 if(empty($v)) $v = array();
460 }
462 /* Set attributes */
463 $pkgattrs['objectClass'] = "FAIdebconfInfo";
464 $pkgattrs['FAIpackage'] = $pkgname;
465 $pkgattrs['FAIvariable'] = $name;
466 $pkgattrs['FAIvariableType'] = $attr['Type'];
467 $pkgattrs['FAIvariableContent'] = $attr['Value'];
468 $pkgdn = "FAIvariable=".$name.",".$this->dn;
470 /* cehck if object already exists */
471 $ldap->cat($pkgdn);
472 if($ldap->count()!=0){
473 $ldap->cd($pkgdn);
474 $ldap->modify($pkgattrs);
475 }else{
476 $ldap->cd($this->config->current['BASE']);
477 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
478 $ldap->cd($pkgdn);
479 $ldap->add($pkgattrs);
480 }
481 show_ldap_error($ldap->get_error());
482 }
483 }
485 $ldap->cat($this->dn);
486 if($ldap->count()!=0){
487 /* Write FAIscript to ldap*/
488 $ldap->cd($this->dn);
489 $ldap->modify($this->attrs);
490 }else{
491 /* Write FAIscript to ldap*/
492 $ldap->cd($this->config->current['BASE']);
493 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
494 $ldap->cd($this->dn);
495 $ldap->add($this->attrs);
496 }
497 show_ldap_error($ldap->get_error());
498 }
499 }
501 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
502 ?>