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 $SubObjects = array(); // All leafobjects of this object
38 var $FAIdebianRelease = ""; // The selected release
39 var $FAIdebianSection = array(); // selected section
40 var $FAIinstallMethod = "aptitude"; // hard coded
41 var $mirror = ""; // selected mirror
43 var $servers = array(); // All available servers
44 var $releases = array(); // All possible releases
45 var $sections = array(); // All section types
47 var $list = NULL;
49 var $mirrors = array(); // The combination of server/release/section
50 var $confDir = "";
51 var $usedPackages = array();
52 var $buffer = NULL;
53 var $strID ="";
54 var $newDialogShown =false;
56 var $FAIstate = "";
57 var $view_logged = FALSE;
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 /* If "dn==new" we try to create a new entry
71 * Else we must read all objects from ldap which belong to this entry.
72 * First read SubObjects from ldap ... and then the partition definitions for the SubObjects.
73 */
74 if($dn != "new"){
75 $this->dn =$dn;
77 /* Get FAIstate
78 */
79 if(isset($this->attrs['FAIstate'][0])){
80 $this->FAIstate = $this->attrs['FAIstate'][0];
81 }
82 }
84 if(isset($this->attrs['FAIpackage'])){
85 unset($this->attrs['FAIpackage']['count']);
86 foreach($this->attrs['FAIpackage'] as $pkg){
87 $this->usedPackages[$pkg] = $pkg;
88 }
89 ksort($this->usedPackages);
90 }else{
91 $this->usedPackages = array();
92 }
94 if($dn != "new"){
96 /* Create one filter with all package names,
97 instead of calling $ldap->search for every single package
98 */
99 $PackageFilter = "";
100 foreach($this->usedPackages as $name){
101 $PackageFilter .= "(FAIpackage=".$name.")";
102 }
103 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
105 /* Search for configuration objects */
106 $ldap = $this->config->get_ldap_link();
107 $ldap->cd($this->dn);
108 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
110 /* Walk through configurations and append them to our list of ConfiguredPackages */
111 while($attr = $ldap->fetch()){
113 /* Skip objects, that are tagged as removed */
114 if(isset($object['FAIstate'][0])){
115 if(preg_match("/removed$/",$attr['FAIstate'][0])){
116 continue;
117 }
118 }
120 $tmp =array();
121 $tmp['Name'] = $attr['FAIvariable'][0];
122 $tmp['Type'] = $attr['FAIvariableType'][0];
124 if (isset($attr['FAIvariableContent'][0])){
125 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
126 $tmp['Value'] = $attr['FAIvariableContent'][0];
127 }else{
128 $content = array();
129 unset($attr['FAIvariableContent']['count']);
130 foreach($attr['FAIvariableContent'] as $attr){
131 $tmp['Value'][] = $attr;
132 }
133 }
134 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
135 }
136 }
137 }
139 if (isset($this->attrs['FAIdebianSection']['count'])){
140 unset($this->attrs['FAIdebianSection']['count']);
141 }
142 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
143 $this->FAIdebianSection = array();
144 foreach($this->attrs['FAIdebianSection'] as $sec){
145 $this->FAIdebianSection[$sec]=$sec;
146 }
147 }
149 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
150 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
151 }
152 $this->confDir = CONFIG_DIR."/fai/";
153 $this->FAIpackage = array();
156 $methods = array();
157 foreach($this->FAIinstallMethods as $method){
158 $methods[$method] = $method;
159 }
160 $this->FAIinstallMethods = $methods;
161 /* Check if we exist already - no need to ask for revisions, etc. then */
162 if ($this->dn != "new"){
163 $this->newDialogShown= true;
164 }
166 }
168 function execute()
169 {
170 /* Call parent execute */
171 plugin::execute();
173 if($this->is_account && !$this->view_logged){
174 $this->view_logged = TRUE;
175 new log("view","fai/".get_class($this),$this->dn);
176 }
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 if(isset($repos[$this->FAIdebianRelease])){
199 $this->FAIdebianSection= $repos[$this->FAIdebianRelease];
200 $this->FAIdebianSection= array_unique($this->FAIdebianSection);
201 }
203 /* Assign Repository settings*/
204 $this->is_account = true;
205 }
207 /* Assign variables */
208 foreach($this->attributes as $attrs){
209 $smarty->assign($attrs,$this->$attrs);
210 }
212 /* Generate package list */
213 $this->list= $this->genPkgs();
215 /* + was pressed to open the package dialog */
216 if(isset($_POST['Addpkg']) && $this->acl_is_writeable("FAIpackage")){
217 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
218 $this->is_dialog =true;
219 }
221 /* Delte selected package */
222 if(isset($_POST['Delpkg']) && $this->acl_is_writeable("FAIpackage")){
223 if($this->FAIstate != "freeze"){
224 foreach($_POST['usedPackages'] as $del){
225 if(isset($this->usedPackages[$del])){
226 unset($this->usedPackages[$del]);
227 }
228 }
229 }
230 }
232 /* Abort package selection dialog */
233 if(isset($_POST['CancelSubObject'])){
234 $this->dialog = false;
235 $this->is_dialog=false;
236 }
238 /* attach new packages */
239 if(isset($_POST['SaveSubObject'])) {
240 if($this->FAIstate != "freeze"){
241 $this->dialog->save_object();
242 if(count($this->dialog->check())){
243 foreach($this->dialog->check() as $msgs){
244 print_red($msgs);
245 }
246 }else{
247 $use = $this->dialog->save();
248 $this->usedPackages = $use;
249 $this->dialog = false;
250 $this->is_dialog=false;
251 ksort($this->usedPackages);
252 }
253 }else{
254 $this->dialog = false;
255 $this->is_dialog=false;
256 }
257 }
259 /* Configuration dialog open*/
260 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages'][0])) && $this->acl_is_writeable("FAIdebconfInfo")){
261 $path = CONFIG_DIR."/fai/".$this->FAIdebianRelease."/debconf.d";
262 $pkg_config = array();
263 $pkg = $_POST['usedPackages'][0];
265 if(isset($this->ConfiguredPackages[$pkg])){
266 $pkg_config = $this->ConfiguredPackages[$pkg];
267 }
269 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
270 $this->is_dialog =true;
271 }
273 /* Configuration dialog open*/
274 if($this->FAIstate != "freeze" && $this->acl_is_writeable("FAIpackage")){
275 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
276 foreach($_POST['usedPackages'] as $pkg){
277 if (isset($this->usedPackages[$pkg])){
278 unset($this->usedPackages[$pkg]);
279 if (preg_match('/^-/', $pkg)){
280 $pkg= preg_replace('/^-/', '', $pkg);
281 } else {
282 $pkg= preg_replace('/^/', '-', $pkg);
283 }
284 $this->usedPackages[$pkg]= $pkg;
285 }
286 }
287 }
288 }
290 /* Save Configuration */
291 if(isset($_POST['SaveObjectConfig'])){
292 if($this->FAIstate != "freeze"){
293 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
294 }
295 $this->dialog = false;
296 $this->is_dialog=false;
297 }
299 /* cancel configuration */
300 if(isset($_POST['CancelObjectConfig'])){
301 $this->dialog = false;
302 $this->is_dialog=false;
303 }
305 /* Display dialog */
306 if($this->is_dialog){
307 return $this->dialog->execute();
308 }
310 /* Assign section to smarty */
311 $strsec = "";
312 foreach($this->FAIdebianSection as $sec){
313 $strsec .= $sec." ";
314 }
316 $tmp = $this->plInfo();
317 foreach($tmp['plProvidedAcls'] as $name => $translated){
318 $smarty->assign($name."ACL",$this->getacl($name,preg_match("/freeze/",$this->FAIstate)));
319 }
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);
337 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
338 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
339 if($_SESSION['faifilter']['branch'] == "main"){
340 $use_dn = $this->dn;
341 }
343 prepare_to_save_FAI_object($use_dn,array(),true);
345 new log("remove","fai/".get_class($this),$use_dn,$this->attributes);
347 foreach($this->ConfiguredPackages as $pkgname => $attrs){
348 foreach($attrs as $name => $attr){
349 $pkgdn = "FAIvariable=".$name.",".$this->dn;
350 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
351 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
352 if($_SESSION['faifilter']['branch'] == "main"){
353 $use_dn = $obj['dn'];
354 }
355 prepare_to_save_FAI_object($use_dn,array(),true);
356 }
357 }
358 $this->handle_post_events("remove");
359 }
362 /* Save data to object
363 */
364 function save_object()
365 {
366 if($this->FAIstate == "freeze") return;
367 plugin::save_object();
368 }
371 /* Check supplied data */
372 function check()
373 {
374 /* Call common method to give check the hook */
375 $message= plugin::check();
377 if(count($this->usedPackages)==0){
378 $message[]=_("Please select a least one Package.");
379 }
381 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
382 $message[]=_("Please choose a valid combination for your repository setup.");
383 }
385 return ($message);
386 }
388 function printUsedPackages(){
389 $a_ret=array();
390 if(is_array($this->usedPackages)) {
391 foreach($this->usedPackages as $usedName){
393 $config = 0;
395 foreach($this->ConfiguredPackages as $name => $value){
396 if($name == $usedName){
397 $config ++;
398 }
399 }
401 $c_str ="";
402 if($config){
403 $c_str = " - "._("package is configured");
404 }
406 /* Adapt used name if we're marked for removal */
407 $dsc= "";
408 if (preg_match('/^-/', $usedName)){
409 $dsc= " - "._("Package marked for removal");
410 // Generally a bad idea here, because the toggel triggers on -, not on !
411 //$usedName= preg_replace('/^-/', '! ', $usedName);
412 }else{
413 $usedName2= $usedName;
414 }
416 if(isset($this->list[$usedName][1])){
417 $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
418 }else{
419 $a_ret[$usedName] = $usedName2.$c_str.$dsc;
420 }
421 }
422 }
423 return($a_ret);
424 }
426 function genPkgs()
427 {
428 /* Generate a list off available packages for this mirror, section and release
429 */
430 /* Only read this file if it wasn't read before */
431 if($this->buffer === NULL){
432 $this->buffer=array();
433 $a_ret = array();
434 foreach($this->FAIdebianSection as $sec){
435 $strID= CONFIG_DIR."/fai/".$this->FAIdebianRelease."/".$sec;
437 if(!is_file($strID)){
438 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
439 unset($this->buffer);
440 return(array());
441 }
442 $fp = fopen($strID,"r");
444 /* Parse every line and create an array */
445 while(!feof($fp)){
446 $str= fgets($fp,512);
447 $stra= split("\|",$str);
448 if(count($stra)==4){
449 $a_ret[$stra[0]] = $stra;
450 }
451 }
452 fclose($fp);
453 /* Save our Data, to avoid reading it again */
454 }
455 $this->buffer = $a_ret;
456 ksort($a_ret);
457 return($a_ret);
458 }else{
459 return $this->buffer;
460 }
461 }
464 /* Save to LDAP */
465 function save()
466 {
467 plugin::save();
469 $ldap = $this->config->get_ldap_link();
471 $this->attrs['FAIpackage'] = array();
472 foreach($this->usedPackages as $pkg => $obj){
473 $this->attrs['FAIpackage'][] = $pkg;
474 }
476 $this->attrs['FAIdebianSection'] = array();
477 foreach($this->FAIdebianSection as $sec){
478 $this->attrs['FAIdebianSection'][] = $sec;
479 }
481 prepare_to_save_FAI_object($this->dn,$this->attrs);
482 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
484 if($this->initially_was_account){
485 new log("modify","fai/".get_class($this),$this->dn,$this->attributes);
486 }else{
487 new log("create","fai/".get_class($this),$this->dn,$this->attributes);
488 }
490 /* Do object tagging */
491 $this->handle_object_tagging();
492 $ldap->cd($this->dn);
494 /* Save Package configurations */
495 foreach($this->ConfiguredPackages as $pkgname => $attrs){
496 foreach($attrs as $name => $attr){
498 $pkgattrs = array();
500 foreach($attr as $n=>$v){
501 if(empty($v)) $v = array();
502 }
504 /* Set attributes */
505 $pkgattrs['objectClass'][] = "FAIdebconfInfo";
507 $pkgattrs['FAIpackage'] = $pkgname;
508 $pkgattrs['FAIvariable'] = $name;
509 $pkgattrs['FAIvariableType'] = $attr['Type'];
510 $pkgattrs['FAIvariableContent'] = $attr['Value'];
511 $pkgdn = "FAIvariable=".$name.",".$this->dn;
513 /* cehck if object already exists */
514 $ldap->cat($pkgdn,array("objectClass"));
516 /* Workaround for missing "gosaAdministrativeUnitTag" */
517 $attrs = $ldap->fetch();
518 if((isset($attrs['objectClass'])) && (in_array_ics("gosaAdministrativeUnitTag",$attrs['objectClass']))){
519 $pkgattrs['objectClass'][] = "gosaAdministrativeUnitTag";
520 }
522 if(in_array($pkgattrs['FAIvariableType'],array("boolean","multiselect","password","select","string","text"))){
523 prepare_to_save_FAI_object($pkgdn,$pkgattrs);
524 }
526 /* Handle tagging */
527 $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
528 }
529 }
530 }
532 /* Return plugin informations for acl handling */
533 function plInfo()
534 {
535 return (array(
536 "plShortName" => _("Package"),
537 "plDescription" => _("FAI Package list"),
538 "plSelfModify" => FALSE,
539 "plDepends" => array(),
540 "plPriority" => 28,
541 "plSection" => array("administration"),
542 "plCategory" => array("fai"),
543 "plProvidedAcls" => array(
544 "cn" => _("Name"),
545 "description" => _("Description"),
546 "FAIpackage" => _("Packages"),
547 "FAIdebianSection" => _("Section")." ("._("Readonly").")",
548 "FAIinstallMethod" => _("Install Method"),
549 "FAIdebconfInfo" => _("Package configuration"),
550 "FAIdebianRelease" => _("Release")." ("._("Readonly").")")
551 ));
552 }
554 function getServerInfos()
555 {
556 $ret = array();
557 $ldap = $this->config->get_ldap_link();
558 $ldap->cd($this->config->current['BASE']);
559 $ldap->search("(objectClass=FAIrepositoryServer)",array("*"));
560 while($attrs = $ldap->fetch()){
561 if(isset($attrs['FAIrepository'])){
562 for($i =0 ; $i < $attrs['FAIrepository']['count']; $i++){
563 $obj = $attrs['FAIrepository'][$i];
564 $tmp = split("\|",$obj);
565 if(count($tmp)==4){
566 foreach(split(",",$tmp[3]) as $sec){
567 if(!empty($sec)){
568 $ret[$tmp[2]][] = $sec;
569 }
570 }
571 }
572 }
573 }
574 }
575 return($ret);
576 }
579 }
581 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
582 ?>