1 <?php
3 class faiPackage extends plugin
4 {
5 /* attribute list for save action */
6 var $ignore_account = TRUE;
8 /* Attributes for this Object */
9 var $attributes = array("cn","description","FAIpackage","FAIdebianRelease","FAIdebianSection", "FAIinstallMethod");
11 /* ObjectClasses for this Object*/
12 var $objectclasses = array("top","FAIclass","FAIpackageList","FAIrepository");
14 /* Class name of the Ldap ObjectClass for the Sub Object */
15 var $subClass = "";
16 var $subClasses = array("top","FAIclass","FAIscriptEntry");
18 /* Class name of the php class which allows us to edit a Sub Object */
19 var $subClassName = "";
21 /* Attributes to initialise for each subObject */
22 var $subAttributes = array("cn","description");
23 var $sub64coded = array();
25 var $ConfiguredPackages = array();
27 /* Specific attributes */
28 var $cn = ""; // The class name for this object
29 var $description = ""; // The description for this set of partitions
30 var $is_dialog = false; // specifies which buttons will be shown to save or abort
31 var $dialog = NULL; // a dialog, e.g. new disk dialog
32 var $SubObjects = array(); // All leafobjects of this object
34 var $FAIdebianRelease = ""; // The selected release
35 var $FAIdebianSection = ""; // selected section
36 var $FAIinstallMethod = "aptitude"; // hard coded
37 var $mirror = ""; // selected mirror
39 var $servers = array(); // All available servers
40 var $releases = array(); // All possible releases
41 var $sections = array(); // All section types
43 var $list = NULL;
45 var $mirrors = array(); // The combination of server/release/section
46 var $confDir = "";
47 var $usedPackages = array();
48 var $buffer = NULL;
49 var $strID ="";
50 var $newDialogShown =false;
52 var $FAIstate = "";
54 var $FAIinstallMethods = array( "install", "ninstall", "remove",
55 "dselect-upgrade", "taskinst", "taskrm",
56 "hold", "clean", "aptitude", "aptitude-r",
57 "pending", "dpkgc" );
59 var $base = "";
60 var $release = "";
61 var $copy_paste_mode = false;
62 var $cut_paste_mode = false;
64 var $CopyPasteVars = array("ConfiguredPackages","FAIdebianRelease","FAIdebianSection","FAIinstallMethod","mirror","servers","releases","sections","list","mirrors","usedPackages");
67 function faiPackage ($config, $dn= NULL)
68 {
69 /* Load Attributes */
70 plugin::plugin ($config, $dn);
72 $this->acl ="#all#";
74 /* If "dn==new" we try to create a new entry
75 * Else we must read all objects from ldap which belong to this entry.
76 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
77 */
78 if($dn != "new"){
79 $this->dn =$dn;
81 /* Set acls
82 */
83 $ui = get_userinfo();
84 $acl = get_permissions ($this->dn, $ui->subtreeACL);
85 $acli = get_module_permission($acl, "FAIclass", $this->dn);
86 $this->acl=$acli;
88 /* Get FAIstate
89 */
90 if(isset($this->attrs['FAIstate'][0])){
91 $this->FAIstate = $this->attrs['FAIstate'][0];
92 }
93 }
95 if(isset($this->attrs['FAIpackage'])){
96 unset($this->attrs['FAIpackage']['count']);
97 foreach($this->attrs['FAIpackage'] as $pkg){
98 $this->usedPackages[$pkg] = $pkg;
99 }
100 ksort($this->usedPackages);
101 }else{
102 $this->usedPackages = array();
103 }
106 if($dn != "new"){
108 /* Create one filter with all package names,
109 instead of calling $ldap->search for every single package
110 */
111 $PackageFilter = "";
112 foreach($this->usedPackages as $name){
113 $PackageFilter .= "(FAIpackage=".$name.")";
114 }
115 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
117 /* Search for configuration objects */
118 $ldap = $this->config->get_ldap_link();
119 $ldap->cd($this->dn);
120 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection"));
122 /* Walk through configurations and append them to our list of ConfiguredPackages */
123 while($attr = $ldap->fetch()){
124 $tmp =array();
125 $tmp['Name'] = $attr['FAIvariable'][0];
126 $tmp['Type'] = $attr['FAIvariableType'][0];
128 if (isset($attr['FAIvariableContent'][0])){
129 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
130 $tmp['Value'] = $attr['FAIvariableContent'][0];
131 }else{
132 $content = array();
133 unset($attr['FAIvariableContent']['count']);
134 foreach($attr['FAIvariableContent'] as $attr){
135 $tmp['Value'][] = $attr;
136 }
137 }
138 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
139 }
140 }
141 }
143 if (isset($this->attrs['FAIdebianSection']['count'])){
144 unset($this->attrs['FAIdebianSection']['count']);
145 }
146 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
147 $this->FAIdebianSection = array();
148 foreach($this->attrs['FAIdebianSection'] as $sec){
149 $this->FAIdebianSection[$sec]=$sec;
150 }
151 }
153 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
154 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
155 }
156 $this->confDir = CONFIG_DIR."/fai/";
157 $this->FAIpackage = array();
160 $methods = array();
161 foreach($this->FAIinstallMethods as $method){
162 $methods[$method] = $method;
163 }
164 $this->FAIinstallMethods = $methods;
165 /* Check if we exist already - no need to ask for revisions, etc. then */
166 if ($this->dn != "new"){
167 $this->newDialogShown= true;
168 }
170 }
172 function execute()
173 {
174 /* Call parent execute */
176 plugin::execute();
178 /* Fill templating stuff */
179 $smarty= get_smarty();
180 $display= "";
182 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
184 if(!$this->is_account){
186 /* Assemble release name */
187 $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['faifilter']['branch']);
188 $tmp= preg_replace('/ou=/', '', $tmp);
189 $rev= array_reverse(split(',', $tmp));
190 $this->FAIdebianRelease= "";
191 foreach ($rev as $part){
192 $this->FAIdebianRelease.= "/$part";
193 }
194 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
196 /* Assemble sections */
197 $repos= $this->getServerInfos();
198 $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
199 $this->FAIdebianSection= array_unique($this->FAIdebianSection);
201 /* Assign Repository settings*/
202 $this->is_account = true;
203 }
205 /* Assign variables */
206 foreach($this->attributes as $attrs){
207 $smarty->assign($attrs,$this->$attrs);
208 }
210 /* Generate package list */
211 $this->list= $this->genPkgs();
213 /* + was pressed to open the package dialog */
214 if(isset($_POST['Addpkg'])){
215 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
216 $this->is_dialog =true;
217 }
219 /* Delte selected package */
220 if(isset($_POST['Delpkg'])){
221 if($this->FAIstate != "freeze"){
222 foreach($_POST['usedPackages'] as $del){
223 if(isset($this->usedPackages[$del])){
224 unset($this->usedPackages[$del]);
225 }
226 }
227 }
228 }
230 /* Abort package selection dialog */
231 if(isset($_POST['CancelSubObject'])){
232 $this->dialog = false;
233 $this->is_dialog=false;
234 }
236 /* attach new packages */
237 if(isset($_POST['SaveSubObject'])) {
238 if($this->FAIstate != "freeze"){
239 $this->dialog->save_object();
240 if(count($this->dialog->check())){
241 foreach($this->dialog->check() as $msgs){
242 print_red($msgs);
243 }
244 }else{
245 $use = $this->dialog->save();
246 $this->usedPackages = $use;
247 $this->dialog = false;
248 $this->is_dialog=false;
249 ksort($this->usedPackages);
250 }
251 }else{
252 $this->dialog = false;
253 $this->is_dialog=false;
254 }
255 }
257 /* Configuration dialog open*/
258 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
259 $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
260 $pkg_config = array();
261 $pkg = $_POST['usedPackages'][0];
263 if(isset($this->ConfiguredPackages[$pkg])){
264 $pkg_config = $this->ConfiguredPackages[$pkg];
265 }
267 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
268 $this->dialog ->acl = $this->acl;
269 $this->is_dialog =true;
270 }
272 /* Configuration dialog open*/
273 if($this->FAIstate != "freeze"){
274 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
275 foreach($_POST['usedPackages'] as $pkg){
276 if (isset($this->usedPackages[$pkg])){
277 unset($this->usedPackages[$pkg]);
278 if (preg_match('/^-/', $pkg)){
279 $pkg= preg_replace('/^-/', '', $pkg);
280 } else {
281 $pkg= preg_replace('/^/', '-', $pkg);
282 }
283 $this->usedPackages[$pkg]= $pkg;
284 }
285 }
286 }
287 }
289 /* Save Configuration */
290 if(isset($_POST['SaveObjectConfig'])){
291 if($this->FAIstate != "freeze"){
292 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
293 }
294 $this->dialog = false;
295 $this->is_dialog=false;
296 }
298 /* cancel configuration */
299 if(isset($_POST['CancelObjectConfig'])){
300 $this->dialog = false;
301 $this->is_dialog=false;
302 }
304 /* Display dialog */
305 if($this->is_dialog){
306 return $this->dialog->execute();
307 }
309 /* Assign section to smarty */
310 $strsec = "";
311 foreach($this->FAIdebianSection as $sec){
312 $strsec .= $sec." ";
313 }
315 foreach($this->attributes as $attr){
316 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
317 }
319 $smarty->assign("OptionsACL","");
321 $smarty->assign("releases",$this->releases);
322 $smarty->assign("release" ,$this->FAIdebianRelease);
323 $smarty->assign("sections",$this->sections);
324 $smarty->assign("section" ,$strsec);
325 $smarty->assign("usedPackages",$this->printUsedPackages());
326 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
327 return($display);
328 }
330 /* Delete me, and all my subtrees
331 */
332 function remove_from_parent()
333 {
334 $ldap = $this->config->get_ldap_link();
335 $ldap->cd ($this->dn);
336 $ldap->rmdir_recursive($this->dn);
337 show_ldap_error($ldap->get_error(), _("Removing FAI package base failed"));
338 $this->handle_post_events("remove");
339 }
342 /* Save data to object
343 */
344 function save_object()
345 {
347 if($this->FAIstate == "freeze") return;
348 plugin::save_object();
351 foreach($this->attributes as $attrs){
352 if(isset($_POST[$attrs])){
353 $this->$attrs = $_POST[$attrs];
354 }
355 }
356 }
359 /* Check supplied data */
360 function check()
361 {
362 /* Call common method to give check the hook */
363 $message= plugin::check();
365 if(count($this->usedPackages)==0){
366 $message[]=_("Please select a least one Package.");
367 }
369 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
370 $message[]=_("Please choose a valid combination for your repository setup.");
371 }
373 /* If this is a new script, check if a script with this name already exists */
374 if(!empty($this->release) && ($this->copy_paste_mode || $this->cut_paste_mode) ){
376 /* Check if current name is already used for fai scripts in selected release */
377 $dn = 'cn='.$this->cn.",ou=packages,".$this->release;
378 $ldap = $this->config->get_ldap_link();
379 $ldap->cat($dn);
380 if($ldap->count()){
382 $r =convert_department_dn($this->release);;
383 $message[] = sprintf(_("Can't insert a fai package list named '%s' in '%s' there is already a package list with the given name."),$this->cn,$r);
384 }
385 }
386 return ($message);
387 }
390 function printUsedPackages(){
391 $a_ret=array();
392 if(is_array($this->usedPackages)) {
393 foreach($this->usedPackages as $usedName){
395 $config = 0;
397 foreach($this->ConfiguredPackages as $name => $value){
398 if($name == $usedName){
399 $config ++;
400 }
401 }
403 $c_str ="";
404 if($config){
405 $c_str = " - "._("package is configured");
406 }
408 /* Adapt used name if we're marked for removal */
409 $dsc= "";
410 if (preg_match('/^-/', $usedName)){
411 $dsc= " - "._("Package marked for removal");
412 // Generally a bad idea here, because the toggel triggers on -, not on !
413 //$usedName= preg_replace('/^-/', '! ', $usedName);
414 }
416 if(isset($this->list[$usedName][1])){
417 $a_ret[$usedName] = $usedName." [".$this->list[$usedName][1]."]".$c_str.$dsc;
418 }else{
419 $a_ret[$usedName] = $usedName.$c_str.$dsc;
420 }
421 }
422 }
423 return($a_ret);
424 }
426 function genPkgs(){
427 /* Generate a list off available packages for this mirror, section and release
428 */
429 /* Only read this file if it wasn't read before */
430 if($this->buffer==NULL){
431 $this->buffer=array();
432 $a_ret = array();
433 foreach($this->FAIdebianSection as $sec){
434 $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
436 if(!is_file($strID)){
437 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
438 unset($this->buffer);
439 return(array());
440 }
441 $fp = fopen($strID,"r");
443 /* Parse every line and create an array */
444 while(!feof($fp)){
445 $str= fgets($fp,512);
446 $stra= split("\|",$str);
447 if(count($stra)==4){
448 $a_ret[$stra[0]] = $stra;
449 }
450 }
451 fclose($fp);
452 /* Save our Data, to avoid reading it again */
453 }
454 $this->buffer = $a_ret;
455 ksort($a_ret);
456 return($a_ret);
457 }else{
458 return $this->buffer;
459 }
460 }
463 /* Save to LDAP */
464 function save()
465 {
466 plugin::save();
468 $ldap = $this->config->get_ldap_link();
470 /* Copy & Paste : Ensure that FAIstate is copied too */
471 if($this->copy_paste_mode && preg_match("/freeze/",$this->FAIstate)){
472 $this->attrs['FAIstate'] = $this->FAIstate;
473 }
475 $this->attrs['FAIpackage'] = array();
476 foreach($this->usedPackages as $pkg => $obj){
477 $this->attrs['FAIpackage'][] = $pkg;
478 }
480 $this->attrs['FAIdebianSection'] = array();
481 foreach($this->FAIdebianSection as $sec){
482 $this->attrs['FAIdebianSection'][] = $sec;
483 }
485 // $this->attrs["FAIinstallMethod"]= "aptitude";
487 $ldap->cat($this->dn, array('dn'));
488 if($ldap->count()!=0){
489 /* Write FAIscript to ldap*/
490 $ldap->cd($this->dn);
491 $this->cleanup();
492 $ldap->modify ($this->attrs);
494 }else{
495 /* Write FAIscript to ldap*/
496 $ldap->cd($this->config->current['BASE']);
497 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
498 $ldap->cd($this->dn);
499 $ldap->add($this->attrs);
500 }
501 show_ldap_error($ldap->get_error(), _("Saving FAI package base failed"));
503 /* Do object tagging */
504 $this->handle_object_tagging();
505 $ldap->cd($this->dn);
507 /* Save Package configurations */
508 foreach($this->ConfiguredPackages as $pkgname => $attrs){
509 foreach($attrs as $name => $attr){
511 $pkgattrs = array();
513 foreach($attr as $n=>$v){
514 if(empty($v)) $v = array();
515 }
517 /* Set attributes */
518 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
520 $pkgattrs['FAIpackage'] = $pkgname;
521 $pkgattrs['FAIvariable'] = $name;
522 $pkgattrs['FAIvariableType'] = $attr['Type'];
523 $pkgattrs['FAIvariableContent'] = $attr['Value'];
524 $pkgdn = "FAIvariable=".$name.",".$this->dn;
526 /* cehck if object already exists */
527 $ldap->cat($pkgdn,array("objectClass"));
529 /* Workaround for missing "gosaAdministrativeUnitTag" */
530 $attrs = $ldap->fetch();
531 if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
532 $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
533 }
535 if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
536 if($ldap->count()!=0){
537 $ldap->cd($pkgdn);
538 $this->cleanup();
539 $ldap->modify ($pkgattrs);
541 }else{
542 $ldap->cd($this->config->current['BASE']);
543 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $pkgdn));
544 $ldap->cd($pkgdn);
545 $ldap->add($pkgattrs);
546 }
547 show_ldap_error($ldap->get_error(), _("Saving FAI package entry failed"));
548 }
550 /* Handle tagging */
551 $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
552 }
553 }
554 }
556 /* return copy & paste dialog
557 */
558 function getCopyDialog()
559 {
560 /* Ask for cn */
561 $smarty = get_smarty();
562 $smarty->assign("cn" ,$this->cn);
563 $str = $smarty->fetch(get_template_path("paste_fai_object.tpl",TRUE));
564 $ret = array();
565 $ret['string'] = $str;
566 $ret['status'] = "";
567 return($ret);
568 }
570 /* Get posted cn */
571 function saveCopyDialog()
572 {
573 if(isset($_POST['cn'])){
574 $this->cn = $_POST['cn'];
575 }
576 }
578 function getServerInfos()
579 {
580 $ret = array();
581 $ldap = $this->config->get_ldap_link();
582 $ldap->cd($this->config->current['BASE']);
583 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
584 while($attrs = $ldap->fetch()){
585 if(isset($attrs['FAIrepository'])){
586 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
587 $obj = $attrs['FAIrepository'][$i];
588 $tmp = split("\|",$obj);
589 if(count($tmp)==4){
590 foreach(split(",",$tmp[3]) as $sec){
591 if(!empty($sec)){
592 $ret[$tmp[2]][] = $sec;
593 }
594 }
595 }
596 }
597 }
598 }
599 return($ret);
600 }
603 }
605 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
606 ?>