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 = array(); // 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 = "";
58 var $view_logged = FALSE;
60 var $FAIinstallMethods = array( "install", "ninstall", "remove",
61 "dselect-upgrade", "taskinst", "taskrm",
62 "hold", "clean", "aptitude", "aptitude-r",
63 "pending", "dpkgc" );
66 function faiPackage ($config, $dn= NULL)
67 {
68 /* Load Attributes */
69 plugin::plugin ($config, $dn);
71 /* If "dn==new" we try to create a new entry
72 * Else we must read all objects from ldap which belong to this entry.
73 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
74 */
75 if($dn != "new"){
76 $this->dn =$dn;
78 /* Get FAIstate
79 */
80 if(isset($this->attrs['FAIstate'][0])){
81 $this->FAIstate = $this->attrs['FAIstate'][0];
82 }
83 }
85 if(isset($this->attrs['FAIpackage'])){
86 unset($this->attrs['FAIpackage']['count']);
87 foreach($this->attrs['FAIpackage'] as $pkg){
88 $this->usedPackages[$pkg] = $pkg;
89 }
90 ksort($this->usedPackages);
91 }else{
92 $this->usedPackages = array();
93 }
95 if($dn != "new"){
97 /* Create one filter with all package names,
98 instead of calling $ldap->search for every single package
99 */
100 $PackageFilter = "";
101 foreach($this->usedPackages as $name){
102 $PackageFilter .= "(FAIpackage=".$name.")";
103 }
104 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
106 /* Search for configuration objects */
107 $ldap = $this->config->get_ldap_link();
108 $ldap->cd($this->dn);
109 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
111 /* Walk through configurations and append them to our list of ConfiguredPackages */
112 while($attr = $ldap->fetch()){
114 /* Skip objects, that are tagged as removed */
115 if(isset($object['FAIstate'][0])){
116 if(preg_match("/removed$/",$attr['FAIstate'][0])){
117 continue;
118 }
119 }
121 $tmp =array();
122 $tmp['Name'] = $attr['FAIvariable'][0];
123 $tmp['Type'] = $attr['FAIvariableType'][0];
125 if (isset($attr['FAIvariableContent'][0])){
126 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
127 $tmp['Value'] = $attr['FAIvariableContent'][0];
128 }else{
129 $content = array();
130 unset($attr['FAIvariableContent']['count']);
131 foreach($attr['FAIvariableContent'] as $attr){
132 $tmp['Value'][] = $attr;
133 }
134 }
135 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
136 }
137 }
138 }
140 if (isset($this->attrs['FAIdebianSection']['count'])){
141 unset($this->attrs['FAIdebianSection']['count']);
142 }
143 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
144 $this->FAIdebianSection = array();
145 foreach($this->attrs['FAIdebianSection'] as $sec){
146 $this->FAIdebianSection[$sec]=$sec;
147 }
148 }
150 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
151 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
152 }
153 $this->confDir = CONFIG_DIR."/fai/";
154 $this->FAIpackage = array();
157 $methods = array();
158 foreach($this->FAIinstallMethods as $method){
159 $methods[$method] = $method;
160 }
161 $this->FAIinstallMethods = $methods;
162 /* Check if we exist already - no need to ask for revisions, etc. then */
163 if ($this->dn != "new"){
164 $this->newDialogShown= true;
165 }
167 }
169 function execute()
170 {
171 /* Call parent execute */
172 plugin::execute();
174 if($this->is_account && !$this->view_logged){
175 $this->view_logged = TRUE;
176 new log("view","fai/".get_class($this),$this->dn);
177 }
179 /* Fill templating stuff */
180 $smarty= get_smarty();
181 $display= "";
183 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
185 if(!$this->is_account){
187 /* Assemble release name */
188 $tmp= preg_replace('/,ou=fai,ou=configs,ou=systems,.*$/', '', $_SESSION['faifilter']['branch']);
189 $tmp= preg_replace('/ou=/', '', $tmp);
190 $rev= array_reverse(split(',', $tmp));
191 $this->FAIdebianRelease= "";
192 foreach ($rev as $part){
193 $this->FAIdebianRelease.= "/$part";
194 }
195 $this->FAIdebianRelease= preg_replace('#^/#', '', $this->FAIdebianRelease);
197 /* Assemble sections */
198 $repos= $this->getServerInfos();
199 if(isset($repos[$this->FAIdebianRelease])){
200 $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
201 $this->FAIdebianSection= array_unique($this->FAIdebianSection);
202 }
204 /* Assign Repository settings*/
205 $this->is_account = true;
206 }
208 /* Assign variables */
209 foreach($this->attributes as $attrs){
210 $smarty->assign($attrs,$this->$attrs);
211 }
213 /* Generate package list */
214 $this->list= $this->genPkgs();
216 /* + was pressed to open the package dialog */
217 if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
218 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
219 $this->is_dialog =true;
220 }
222 /* Delte selected package */
223 if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
224 if($this->FAIstate != "freeze"){
225 foreach($_POST['usedPackages'] as $del){
226 if(isset($this->usedPackages[$del])){
227 unset($this->usedPackages[$del]);
228 }
229 }
230 }
231 }
233 /* Abort package selection dialog */
234 if(isset($_POST['CancelSubObject'])){
235 $this->dialog = false;
236 $this->is_dialog=false;
237 }
239 /* attach new packages */
240 if(isset($_POST['SaveSubObject'])) {
241 if($this->FAIstate != "freeze"){
242 $this->dialog->save_object();
243 if(count($this->dialog->check())){
244 foreach($this->dialog->check() as $msgs){
245 print_red($msgs);
246 }
247 }else{
248 $use = $this->dialog->save();
249 $this->usedPackages = $use;
250 $this->dialog = false;
251 $this->is_dialog=false;
252 ksort($this->usedPackages);
253 }
254 }else{
255 $this->dialog = false;
256 $this->is_dialog=false;
257 }
258 }
260 /* Configuration dialog open*/
261 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'])) && $this->acl_is_writeable("FAIdebconfInfo")){
262 $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
263 $pkg_config = array();
264 $pkg = $_POST['usedPackages'][0];
266 if(isset($this->ConfiguredPackages[$pkg])){
267 $pkg_config = $this->ConfiguredPackages[$pkg];
268 }
270 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
271 $this->is_dialog =true;
272 }
274 /* Configuration dialog open*/
275 if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
276 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
277 foreach($_POST['usedPackages'] as $pkg){
278 if (isset($this->usedPackages[$pkg])){
279 unset($this->usedPackages[$pkg]);
280 if (preg_match('/^-/', $pkg)){
281 $pkg= preg_replace('/^-/', '', $pkg);
282 } else {
283 $pkg= preg_replace('/^/', '-', $pkg);
284 }
285 $this->usedPackages[$pkg]= $pkg;
286 }
287 }
288 }
289 }
291 /* Save Configuration */
292 if(isset($_POST['SaveObjectConfig'])){
293 if($this->FAIstate != "freeze"){
294 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
295 }
296 $this->dialog = false;
297 $this->is_dialog=false;
298 }
300 /* cancel configuration */
301 if(isset($_POST['CancelObjectConfig'])){
302 $this->dialog = false;
303 $this->is_dialog=false;
304 }
306 /* Display dialog */
307 if($this->is_dialog){
308 return $this->dialog->execute();
309 }
311 /* Assign section to smarty */
312 $strsec = "";
313 foreach($this->FAIdebianSection as $sec){
314 $strsec .= $sec." ";
315 }
317 $tmp = $this->plInfo();
318 foreach($tmp['plProvidedAcls'] as $name => $translated){
319 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
320 }
322 $smarty->assign("releases",$this->releases);
323 $smarty->assign("release" ,$this->FAIdebianRelease);
324 $smarty->assign("sections",$this->sections);
325 $smarty->assign("section" ,$strsec);
326 $smarty->assign("usedPackages",$this->printUsedPackages());
327 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
328 return($display);
329 }
331 /* Delete me, and all my subtrees
332 */
333 function remove_from_parent()
334 {
335 $ldap = $this->config->get_ldap_link();
336 $ldap->cd ($this->dn);
338 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
339 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
340 if($_SESSION['faifilter']['branch'] == "main"){
341 $use_dn = $this->dn;
342 }
344 prepare_to_save_FAI_object($use_dn,array(),true);
346 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
348 foreach($this->ConfiguredPackages as $pkgname => $attrs){
349 foreach($attrs as $name => $attr){
350 $pkgdn = "FAIvariable=".$name.",".$this->dn;
351 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
352 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
353 if($_SESSION['faifilter']['branch'] == "main"){
354 $use_dn = $obj['dn'];
355 }
356 prepare_to_save_FAI_object($use_dn,array(),true);
357 }
358 }
359 $this->handle_post_events("remove");
360 }
363 /* Save data to object
364 */
365 function save_object()
366 {
367 if($this->FAIstate == "freeze") return;
368 plugin::save_object();
369 }
372 /* Check supplied data */
373 function check()
374 {
375 /* Call common method to give check the hook */
376 $message= plugin::check();
378 if(count($this->usedPackages)==0){
379 $message[]=_("Please select a least one Package.");
380 }
382 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
383 $message[]=_("Please choose a valid combination for your repository setup.");
384 }
386 return ($message);
387 }
389 function printUsedPackages(){
390 $a_ret=array();
391 if(is_array($this->usedPackages)) {
392 foreach($this->usedPackages as $usedName){
394 $config = 0;
396 foreach($this->ConfiguredPackages as $name => $value){
397 if($name == $usedName){
398 $config ++;
399 }
400 }
402 $c_str ="";
403 if($config){
404 $c_str = " - "._("package is configured");
405 }
407 /* Adapt used name if we're marked for removal */
408 $dsc= "";
409 if (preg_match('/^-/', $usedName)){
410 $dsc= " - "._("Package marked for removal");
411 // Generally a bad idea here, because the toggel triggers on -, not on !
412 //$usedName= preg_replace('/^-/', '! ', $usedName);
413 }else{
414 $usedName2= $usedName;
415 }
417 if(isset($this->list[$usedName][1])){
418 $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
419 }else{
420 $a_ret[$usedName] = $usedName2.$c_str.$dsc;
421 }
422 }
423 }
424 return($a_ret);
425 }
427 function genPkgs()
428 {
429 /* Generate a list off available packages for this mirror, section and release
430 */
431 /* Only read this file if it wasn't read before */
432 if($this->buffer==NULL){
433 $this->buffer=array();
434 $a_ret = array();
435 foreach($this->FAIdebianSection as $sec){
436 $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
438 if(!is_file($strID)){
439 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
440 unset($this->buffer);
441 return(array());
442 }
443 $fp = fopen($strID,"r");
445 /* Parse every line and create an array */
446 while(!feof($fp)){
447 $str= fgets($fp,512);
448 $stra= split("\|",$str);
449 if(count($stra)==4){
450 $a_ret[$stra[0]] = $stra;
451 }
452 }
453 fclose($fp);
454 /* Save our Data, to avoid reading it again */
455 }
456 $this->buffer = $a_ret;
457 ksort($a_ret);
458 return($a_ret);
459 }else{
460 return $this->buffer;
461 }
462 }
465 /* Save to LDAP */
466 function save()
467 {
468 plugin::save();
470 $ldap = $this->config->get_ldap_link();
472 $this->attrs['FAIpackage'] = array();
473 foreach($this->usedPackages as $pkg => $obj){
474 $this->attrs['FAIpackage'][] = $pkg;
475 }
477 $this->attrs['FAIdebianSection'] = array();
478 foreach($this->FAIdebianSection as $sec){
479 $this->attrs['FAIdebianSection'][] = $sec;
480 }
482 prepare_to_save_FAI_object($this->dn,$this->attrs);
483 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
485 if($this->initially_was_account){
486 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
487 }else{
488 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
489 }
491 /* Do object tagging */
492 $this->handle_object_tagging();
493 $ldap->cd($this->dn);
495 /* Save Package configurations */
496 foreach($this->ConfiguredPackages as $pkgname => $attrs){
497 foreach($attrs as $name => $attr){
499 $pkgattrs = array();
501 foreach($attr as $n=>$v){
502 if(empty($v)) $v = array();
503 }
505 /* Set attributes */
506 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
508 $pkgattrs['FAIpackage'] = $pkgname;
509 $pkgattrs['FAIvariable'] = $name;
510 $pkgattrs['FAIvariableType'] = $attr['Type'];
511 $pkgattrs['FAIvariableContent'] = $attr['Value'];
512 $pkgdn = "FAIvariable=".$name.",".$this->dn;
514 /* cehck if object already exists */
515 $ldap->cat($pkgdn,array("objectClass"));
517 /* Workaround for missing "gosaAdministrativeUnitTag" */
518 $attrs = $ldap->fetch();
519 if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
520 $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
521 }
523 if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
524 prepare_to_save_FAI_object($pkgdn,$pkgattrs);
525 }
527 /* Handle tagging */
528 $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
529 }
530 }
531 }
533 /* Return plugin informations for acl handling */
534 function plInfo()
535 {
536 return (array(
537 "plShortName" => _("Package"),
538 "plDescription" => _("FAI Package list"),
539 "plSelfModify" => FALSE,
540 "plDepends" => array(),
541 "plPriority" => 28,
542 "plSection" => array("administration"),
543 "plCategory" => array("fai"),
544 "plProvidedAcls" => array(
545 "cn" => _("Name"),
546 "description" => _("Description"),
547 "FAIpackage" => _("Packages"),
548 "FAIdebianSection" => _("Section")." ("._("Readonly").")",
549 "FAIinstallMethod" => _("Install Method"),
550 "FAIdebconfInfo" => _("Package configuration"),
551 "FAIdebianRelease" => _("Release")." ("._("Readonly").")")
552 ));
553 }
555 function getServerInfos()
556 {
557 $ret = array();
558 $ldap = $this->config->get_ldap_link();
559 $ldap->cd($this->config->current['BASE']);
560 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
561 while($attrs = $ldap->fetch()){
562 if(isset($attrs['FAIrepository'])){
563 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
564 $obj = $attrs['FAIrepository'][$i];
565 $tmp = split("\|",$obj);
566 if(count($tmp)==4){
567 foreach(split(",",$tmp[3]) as $sec){
568 if(!empty($sec)){
569 $ret[$tmp[2]][] = $sec;
570 }
571 }
572 }
573 }
574 }
575 }
576 return($ret);
577 }
580 }
582 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
583 ?>