fb845ee416083f500297a89c8b306a45df252301
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", "FAIinstallMethod");
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 $FAIinstallMethod = "aptitude"; // hard coded
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 var $FAIstate = "";
59 var $FAIinstallMethods = array( "install", "ninstall", "remove",
60 "dselect-upgrade", "taskinst", "taskrm",
61 "hold", "clean", "aptitude", "aptitude-r",
62 "pending", "dpkgc" );
65 function faiPackage ($config, $dn= NULL)
66 {
67 /* Load Attributes */
68 plugin::plugin ($config, $dn);
70 $this->acl ="#all#";
72 /* If "dn==new" we try to create a new entry
73 * Else we must read all objects from ldap which belong to this entry.
74 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
75 */
76 if($dn != "new"){
77 $this->dn =$dn;
79 /* Set acls
80 */
81 $ui = get_userinfo();
82 $acl = get_permissions ($this->dn, $ui->subtreeACL);
83 $acli = get_module_permission($acl, "FAIclass", $this->dn);
84 $this->acl=$acli;
86 /* Get FAIstate
87 */
88 if(isset($this->attrs['FAIstate'][0])){
89 $this->FAIstate = $this->attrs['FAIstate'][0];
90 }
91 }
93 if(isset($this->attrs['FAIpackage'])){
94 unset($this->attrs['FAIpackage']['count']);
95 foreach($this->attrs['FAIpackage'] as $pkg){
96 $this->usedPackages[$pkg] = $pkg;
97 }
98 ksort($this->usedPackages);
99 }else{
100 $this->usedPackages = array();
101 }
104 /* Create one filter with all package names,
105 instead of calling $ldap->search for every single package
106 */
107 $PackageFilter = "";
108 foreach($this->usedPackages as $name){
109 $PackageFilter .= "(FAIpackage=".$name.")";
110 }
111 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
113 /* Search for configuration objects */
114 $ldap = $this->config->get_ldap_link();
115 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
117 /* Walk through configurations and append them to our list of ConfiguredPackages */
118 while($attr = $ldap->fetch()){
119 $tmp =array();
120 $tmp['Name'] = $attr['FAIvariable'][0];
121 $tmp['Type'] = $attr['FAIvariableType'][0];
123 if (isset($attr['FAIvariableContent'][0])){
124 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
125 $tmp['Value'] = $attr['FAIvariableContent'][0];
126 }else{
127 $content = array();
128 unset($attr['FAIvariableContent']['count']);
129 foreach($attr['FAIvariableContent'] as $attr){
130 $tmp['Value'][] = $attr;
131 }
132 }
133 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
134 }
135 }
137 if (isset($this->attrs['FAIdebianSection']['count'])){
138 unset($this->attrs['FAIdebianSection']['count']);
139 }
140 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
141 $this->FAIdebianSection = array();
142 foreach($this->attrs['FAIdebianSection'] as $sec){
143 $this->FAIdebianSection[$sec]=$sec;
144 }
145 }
147 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
148 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
149 }
150 $this->confDir = CONFIG_DIR."/fai/";
151 $this->FAIpackage = array();
154 $methods = array();
155 foreach($this->FAIinstallMethods as $method){
156 $methods[$method] = $method;
157 }
158 $this->FAIinstallMethods = $methods;
159 /* Check if we exist already - no need to ask for revisions, etc. then */
160 if ($this->dn != "new"){
161 $this->newDialogShown= true;
162 }
164 }
166 function execute()
167 {
168 /* Call parent execute */
170 plugin::execute();
172 /* Fill templating stuff */
173 $smarty= get_smarty();
174 $display= "";
176 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
178 if((!$this->is_account)&&(!$this->newDialogShown)){
180 if($this->dialog==NULL){
181 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
182 $this->is_dialog =true;
183 }
185 /* Assign posible changes, for mirror combinations */
186 $this->dialog->save_object();
188 /* Assign Repository settings*/
189 if(isset($_POST['SaveObjectNew'])){
190 $obj = $this->dialog->save();
192 $this->FAIdebianSection = $obj['FAIdebianSection'];
193 $this->FAIdebianRelease = $obj['FAIdebianRelease'];
195 unset($this->dialog);
196 $this->dialog = false;
197 $this->is_dialog = false;
198 $this->newDialogShown = true;
199 $this->is_account = true;
200 }
202 /* Draw dialog */
203 if($this->dialog){
204 $display=$this->dialog->execute();
205 return($display);
206 }
207 }
209 /* Assign variables */
210 foreach($this->attributes as $attrs){
211 $smarty->assign($attrs,$this->$attrs);
212 }
214 /* Generate package list */
215 $this->list=$this->genPkgs();
217 /* + was pressed to open the package dialog */
218 if(isset($_POST['Addpkg'])){
219 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
220 $this->is_dialog =true;
221 }
223 /* Delte selected package */
224 if(isset($_POST['Delpkg'])){
225 if($this->FAIstate != "freeze"){
226 foreach($_POST['usedPackages'] as $del){
227 if(isset($this->usedPackages[$del])){
228 unset($this->usedPackages[$del]);
229 }
230 }
231 }
232 }
234 /* Abort package selection dialog */
235 if(isset($_POST['CancelSubObject'])){
236 $this->dialog = false;
237 $this->is_dialog=false;
238 }
240 /* attach new packages */
241 if(isset($_POST['SaveSubObject'])) {
242 if($this->FAIstate != "freeze"){
243 $this->dialog->save_object();
244 if(count($this->dialog->check())){
245 foreach($this->dialog->check() as $msgs){
246 print_red($msgs);
247 }
248 }else{
249 $use = $this->dialog->save();
250 $this->usedPackages = $use;
251 $this->dialog = false;
252 $this->is_dialog=false;
253 ksort($this->usedPackages);
254 }
255 }else{
256 $this->dialog = false;
257 $this->is_dialog=false;
258 }
259 }
261 /* Configuration dialog open*/
262 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
263 $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
264 $pkg_config = array();
265 $pkg = $_POST['usedPackages'][0];
267 if(isset($this->ConfiguredPackages[$pkg])){
268 $pkg_config = $this->ConfiguredPackages[$pkg];
269 }
271 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
272 $this->dialog ->acl = $this->acl;
273 $this->is_dialog =true;
274 }
276 /* Configuration dialog open*/
277 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
278 foreach($_POST['usedPackages'] as $pkg){
279 if (isset($this->usedPackages[$pkg])){
280 unset($this->usedPackages[$pkg]);
281 if (preg_match('/^-/', $pkg)){
282 $pkg= preg_replace('/^-/', '', $pkg);
283 } else {
284 $pkg= preg_replace('/^/', '-', $pkg);
285 }
286 $this->usedPackages[$pkg]= $pkg;
287 }
288 }
289 print_a($this->usedPackages);
290 }
292 /* Save Configuration */
293 if(isset($_POST['SaveObjectConfig'])){
294 if($this->FAIstate != "freeze"){
295 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
296 }
297 $this->dialog = false;
298 $this->is_dialog=false;
299 }
301 /* cancel configuration */
302 if(isset($_POST['CancelObjectConfig'])){
303 $this->dialog = false;
304 $this->is_dialog=false;
305 }
307 /* Display dialog */
308 if($this->is_dialog){
309 return $this->dialog->execute();
310 }
312 /* Assign section to smarty */
313 $strsec = "";
314 foreach($this->FAIdebianSection as $sec){
315 $strsec .= $sec." ";
316 }
318 foreach($this->attributes as $attr){
319 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
320 }
322 $smarty->assign("OptionsACL","");
324 $smarty->assign("releases",$this->releases);
325 $smarty->assign("release" ,$this->FAIdebianRelease);
326 $smarty->assign("sections",$this->sections);
327 $smarty->assign("section" ,$strsec);
328 $smarty->assign("usedPackages",$this->printUsedPackages());
329 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
330 return($display);
331 }
333 /* Delete me, and all my subtrees
334 */
335 function remove_from_parent()
336 {
337 $ldap = $this->config->get_ldap_link();
338 $ldap->cd ($this->dn);
339 $ldap->rmdir_recursive($this->dn);
340 show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
341 $this->handle_post_events("remove");
342 }
345 /* Save data to object
346 */
347 function save_object()
348 {
350 if($this->FAIstate == "freeze") return;
351 plugin::save_object();
354 foreach($this->attributes as $attrs){
355 if(isset($_POST[$attrs])){
356 $this->$attrs = $_POST[$attrs];
357 }
358 }
359 }
362 /* Check supplied data */
363 function check()
364 {
365 /* Call common method to give check the hook */
366 $message= plugin::check();
368 if(count($this->usedPackages)==0){
369 $message[]=_("Please select a least one Package.");
370 }
372 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
373 $message[]=_("Please choose a valid combination for your repository setup.");
374 }
376 return ($message);
377 }
379 function printUsedPackages(){
380 $a_ret=array();
381 if(is_array($this->usedPackages)) {
382 foreach($this->usedPackages as $usedName){
384 $config = 0;
386 foreach($this->ConfiguredPackages as $name => $value){
387 if($name == $usedName){
388 $config ++;
389 }
390 }
392 $c_str ="";
393 if($config){
394 $c_str = " - "._("package is configured");
395 }
397 /* Adapt used name if we're marked for removal */
398 $dsc= "";
399 if (preg_match('/^-/', $usedName)){
400 $dsc= " - "._("Package marked for removal");
401 $usedName= preg_replace('/^-/', '! ', $usedName);
402 }
404 if(isset($this->list[$usedName][1])){
405 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str.$dsc;
406 }else{
407 $a_ret[$usedName] = $usedName.$c_str.$dsc;
408 }
409 }
410 }
411 return($a_ret);
412 }
414 function genPkgs(){
415 /* Generate a list off available packages for this mirror, section and release
416 */
417 /* Only read this file if it wasn't read before */
418 if($this->buffer==NULL){
419 $this->buffer=array();
420 $a_ret = array();
421 foreach($this->FAIdebianSection as $sec){
422 $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
424 if(!is_file($strID)){
425 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
426 unset($this->buffer);
427 return(array());
428 }
429 $fp = fopen($strID,"r");
431 /* Parse every line and create an array */
432 while(!feof($fp)){
433 $str= fgets($fp,512);
434 $stra= split("\|",$str);
435 if(count($stra)==4){
436 $a_ret[$stra[0]] = $stra;
437 }
438 }
439 fclose($fp);
440 /* Save our Data, to avoid reading it again */
441 }
442 $this->buffer = $a_ret;
443 ksort($a_ret);
444 return($a_ret);
445 }else{
446 return $this->buffer;
447 }
448 }
451 /* Save to LDAP */
452 function save()
453 {
454 plugin::save();
456 $ldap = $this->config->get_ldap_link();
458 $this->attrs['FAIpackage'] = array();
459 foreach($this->usedPackages as $pkg => $obj){
460 $this->attrs['FAIpackage'][] = $pkg;
461 }
463 $this->attrs['FAIdebianSection'] = array();
464 foreach($this->FAIdebianSection as $sec){
465 $this->attrs['FAIdebianSection'][] = $sec;
466 }
468 // $this->attrs["FAIinstallMethod"]= "aptitude";
470 $ldap->cat($this->dn, array('dn'));
471 if($ldap->count()!=0){
472 /* Write FAIscript to ldap*/
473 $ldap->cd($this->dn);
474 $this->cleanup();
475 $ldap->modify ($this->attrs);
477 }else{
478 /* Write FAIscript to ldap*/
479 $ldap->cd($this->config->current['BASE']);
480 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
481 $ldap->cd($this->dn);
482 $ldap->add($this->attrs);
483 }
484 show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
486 /* Do object tagging */
487 $this->handle_object_tagging();
488 $ldap->cd($this->dn);
490 /* Save Package configurations */
491 foreach($this->ConfiguredPackages as $pkgname => $attrs){
492 foreach($attrs as $name => $attr){
494 $pkgattrs = array();
496 foreach($attr as $n=>$v){
497 if(empty($v)) $v = array();
498 }
500 /* Set attributes */
501 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
503 $pkgattrs['FAIpackage'] = $pkgname;
504 $pkgattrs['FAIvariable'] = $name;
505 $pkgattrs['FAIvariableType'] = $attr['Type'];
506 $pkgattrs['FAIvariableContent'] = $attr['Value'];
507 $pkgdn = "FAIvariable=".$name.",".$this->dn;
509 /* cehck if object already exists */
510 $ldap->cat($pkgdn,array("objectClass"));
512 /* Workaround for missing "gosaAdministrativeUnitTag" */
513 $attrs = $ldap->fetch();
514 if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
515 $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
516 }
518 if($ldap->count()!=0){
519 $ldap->cd($pkgdn);
520 $this->cleanup();
521 $ldap->modify ($pkgattrs);
523 }else{
524 $ldap->cd($this->config->current['BASE']);
525 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
526 $ldap->cd($pkgdn);
527 $ldap->add($pkgattrs);
528 }
529 show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
531 /* Handle tagging */
532 $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
533 }
534 }
539 }
540 }
542 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
543 ?>