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 }
103 if($dn != "new"){
105 /* Create one filter with all package names,
106 instead of calling $ldap->search for every single package
107 */
108 $PackageFilter = "";
109 foreach($this->usedPackages as $name){
110 $PackageFilter .= "(FAIpackage=".$name.")";
111 }
112 $PackageFilter = "(&(objectClass=FAIdebconfInfo)(|".$PackageFilter."))";
114 /* Search for configuration objects */
115 $ldap = $this->config->get_ldap_link();
116 $ldap->cd($this->dn);
117 $ldap->search($PackageFilter,array("FAIvariable","FAIvariableType","FAIvariableContent","FAIpackage","FAIdebianSection","FAIstate"));
119 /* Walk through configurations and append them to our list of ConfiguredPackages */
120 while($attr = $ldap->fetch()){
122 /* Skip objects, that are tagged as removed */
123 if(isset($object['FAIstate'][0])){
124 if(preg_match("/removed$/",$attr['FAIstate'][0])){
125 continue;
126 }
127 }
129 $tmp =array();
130 $tmp['Name'] = $attr['FAIvariable'][0];
131 $tmp['Type'] = $attr['FAIvariableType'][0];
133 if (isset($attr['FAIvariableContent'][0])){
134 if(!in_array($attr['FAIvariableType'],array("multiselect"))){
135 $tmp['Value'] = $attr['FAIvariableContent'][0];
136 }else{
137 $content = array();
138 unset($attr['FAIvariableContent']['count']);
139 foreach($attr['FAIvariableContent'] as $attr){
140 $tmp['Value'][] = $attr;
141 }
142 }
143 $this->ConfiguredPackages[$attr['FAIpackage'][0]][$attr['FAIvariable'][0]]=$tmp;
144 }
145 }
146 }
148 if (isset($this->attrs['FAIdebianSection']['count'])){
149 unset($this->attrs['FAIdebianSection']['count']);
150 }
151 if((isset($this->attrs['FAIdebianSection']))&&(is_array($this->attrs['FAIdebianSection']))){
152 $this->FAIdebianSection = array();
153 foreach($this->attrs['FAIdebianSection'] as $sec){
154 $this->FAIdebianSection[$sec]=$sec;
155 }
156 }
158 if((isset($this->attrs['FAIdebianSection']))&&(is_string($this->attrs['FAIdebianSection']))){
159 $this->FAIdebianSection=array($this->attrs['FAIdebianSection']=>$this->attrs['FAIdebianSection']) ;
160 }
161 $this->confDir = CONFIG_DIR."/fai/";
162 $this->FAIpackage = array();
165 $methods = array();
166 foreach($this->FAIinstallMethods as $method){
167 $methods[$method] = $method;
168 }
169 $this->FAIinstallMethods = $methods;
170 /* Check if we exist already - no need to ask for revisions, etc. then */
171 if ($this->dn != "new"){
172 $this->newDialogShown= true;
173 }
175 }
177 function execute()
178 {
179 /* Call parent execute */
181 plugin::execute();
183 /* Fill templating stuff */
184 $smarty= get_smarty();
185 $display= "";
187 $smarty->assign( "FAIinstallMethods", $this->FAIinstallMethods );
189 if((!$this->is_account)&&(!$this->newDialogShown)){
191 if($this->dialog==NULL){
192 $this->dialog = new faiPackageNew($this->config, $this->dn,$this->mirrors,$this->servers,$this->sections,$this->releases);
193 $this->is_dialog =true;
194 }
196 /* Assign posible changes, for mirror combinations */
197 $this->dialog->save_object();
199 /* Assign Repository settings*/
200 if(isset($_POST['SaveObjectNew'])){
201 $obj = $this->dialog->save();
203 $this->FAIdebianSection = $obj['FAIdebianSection'];
204 $this->FAIdebianRelease = $obj['FAIdebianRelease'];
206 unset($this->dialog);
207 $this->dialog = false;
208 $this->is_dialog = false;
209 $this->newDialogShown = true;
210 $this->is_account = true;
211 }
213 /* Draw dialog */
214 if($this->dialog){
215 $display=$this->dialog->execute();
216 return($display);
217 }
218 }
220 /* Assign variables */
221 foreach($this->attributes as $attrs){
222 $smarty->assign($attrs,$this->$attrs);
223 }
225 /* Generate package list */
226 $this->list=$this->genPkgs();
228 /* + was pressed to open the package dialog */
229 if(isset($_POST['Addpkg'])){
230 $this->dialog = new faiPackageEntry($this->config, $this->dn,$this->list,$this->usedPackages);
231 $this->is_dialog =true;
232 }
234 /* Delte selected package */
235 if(isset($_POST['Delpkg'])){
236 if($this->FAIstate != "freeze"){
237 foreach($_POST['usedPackages'] as $del){
238 if(isset($this->usedPackages[$del])){
239 unset($this->usedPackages[$del]);
240 }
241 }
242 }
243 }
245 /* Abort package selection dialog */
246 if(isset($_POST['CancelSubObject'])){
247 $this->dialog = false;
248 $this->is_dialog=false;
249 }
251 /* attach new packages */
252 if(isset($_POST['SaveSubObject'])) {
253 if($this->FAIstate != "freeze"){
254 $this->dialog->save_object();
255 if(count($this->dialog->check())){
256 foreach($this->dialog->check() as $msgs){
257 print_red($msgs);
258 }
259 }else{
260 $use = $this->dialog->save();
261 $this->usedPackages = $use;
262 $this->dialog = false;
263 $this->is_dialog=false;
264 ksort($this->usedPackages);
265 }
266 }else{
267 $this->dialog = false;
268 $this->is_dialog=false;
269 }
270 }
272 /* Configuration dialog open*/
273 if((isset($_POST['Conpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
274 $path = "/etc/gosa/fai/".$this->FAIdebianRelease."/debconf.d";
275 $pkg_config = array();
276 $pkg = $_POST['usedPackages'][0];
278 if(isset($this->ConfiguredPackages[$pkg])){
279 $pkg_config = $this->ConfiguredPackages[$pkg];
280 }
282 $this->dialog = new faiPackageConfiguration($this->config, $this->dn,$pkg, $path, $pkg_config);
283 $this->dialog ->acl = $this->acl;
284 $this->is_dialog =true;
285 }
287 /* Configuration dialog open*/
288 if($this->FAIstate != "freeze"){
289 if((isset($_POST['Markpkg']))&&(isset($_POST['usedPackages']))&&(!empty($_POST['usedPackages']))){
290 foreach($_POST['usedPackages'] as $pkg){
291 if (isset($this->usedPackages[$pkg])){
292 unset($this->usedPackages[$pkg]);
293 if (preg_match('/^-/', $pkg)){
294 $pkg= preg_replace('/^-/', '', $pkg);
295 } else {
296 $pkg= preg_replace('/^/', '-', $pkg);
297 }
298 $this->usedPackages[$pkg]= $pkg;
299 }
300 }
301 }
302 }
304 /* Save Configuration */
305 if(isset($_POST['SaveObjectConfig'])){
306 if($this->FAIstate != "freeze"){
307 $this->ConfiguredPackages= array_merge($this->ConfiguredPackages,$this->dialog->save());
308 }
309 $this->dialog = false;
310 $this->is_dialog=false;
311 }
313 /* cancel configuration */
314 if(isset($_POST['CancelObjectConfig'])){
315 $this->dialog = false;
316 $this->is_dialog=false;
317 }
319 /* Display dialog */
320 if($this->is_dialog){
321 return $this->dialog->execute();
322 }
324 /* Assign section to smarty */
325 $strsec = "";
326 foreach($this->FAIdebianSection as $sec){
327 $strsec .= $sec." ";
328 }
330 foreach($this->attributes as $attr){
331 $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
332 }
334 $smarty->assign("OptionsACL","");
336 $smarty->assign("releases",$this->releases);
337 $smarty->assign("release" ,$this->FAIdebianRelease);
338 $smarty->assign("sections",$this->sections);
339 $smarty->assign("section" ,$strsec);
340 $smarty->assign("usedPackages",$this->printUsedPackages());
341 $display.= $smarty->fetch(get_template_path('faiPackage.tpl', TRUE));
342 return($display);
343 }
345 /* Delete me, and all my subtrees
346 */
347 function remove_from_parent()
348 {
349 $ldap = $this->config->get_ldap_link();
350 $ldap->cd ($this->dn);
352 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $this->dn);
353 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $this->dn);
354 if($_SESSION['faifilter']['branch'] == "main"){
355 $use_dn = $this->dn;
356 }
358 prepare_to_save_FAI_object($use_dn,array(),true);
360 foreach($this->ConfiguredPackages as $pkgname => $attrs){
361 foreach($attrs as $name => $attr){
362 $pkgdn = "FAIvariable=".$name.",".$this->dn;
363 # $use_dn = str_ireplace( get_release_dn($this->dn), $_SESSION['faifilter']['branch'], $pkgdn);
364 $use_dn = preg_replace("/".normalizePreg(get_release_dn($this->dn))."/i", $_SESSION['faifilter']['branch'], $pkgdn);
365 if($_SESSION['faifilter']['branch'] == "main"){
366 $use_dn = $obj['dn'];
367 }
368 prepare_to_save_FAI_object($use_dn,array(),true);
369 }
370 }
371 $this->handle_post_events("remove");
372 }
375 /* Save data to object
376 */
377 function save_object()
378 {
380 if($this->FAIstate == "freeze") return;
381 plugin::save_object();
384 foreach($this->attributes as $attrs){
385 if(isset($_POST[$attrs])){
386 $this->$attrs = $_POST[$attrs];
387 }
388 }
389 }
392 /* Check supplied data */
393 function check()
394 {
395 /* Call common method to give check the hook */
396 $message= plugin::check();
398 if(count($this->usedPackages)==0){
399 $message[]=_("Please select a least one Package.");
400 }
402 if((empty($this->FAIdebianRelease))||(empty($this->FAIdebianSection))){
403 $message[]=_("Please choose a valid combination for your repository setup.");
404 }
406 return ($message);
407 }
409 function printUsedPackages(){
410 $a_ret=array();
411 if(is_array($this->usedPackages)) {
412 foreach($this->usedPackages as $usedName){
414 $config = 0;
416 foreach($this->ConfiguredPackages as $name => $value){
417 if($name == $usedName){
418 $config ++;
419 }
420 }
422 $c_str ="";
423 if($config){
424 $c_str = " - "._("package is configured");
425 }
427 /* Adapt used name if we're marked for removal */
428 $dsc= "";
429 if (preg_match('/^-/', $usedName)){
430 $dsc= " - "._("Package marked for removal");
431 $usedName2= preg_replace('/^-/', '! ', $usedName);
432 }else{
433 $usedName2= $usedName;
434 }
436 if(isset($this->list[$usedName][1])){
437 $a_ret[$usedName] = $usedName2." [".$this->list[$usedName][1]."]".$c_str.$dsc;
438 }else{
439 $a_ret[$usedName] = $usedName2.$c_str.$dsc;
440 }
441 }
442 }
443 return($a_ret);
444 }
446 function genPkgs(){
447 /* Generate a list off available packages for this mirror, section and release
448 */
449 /* Only read this file if it wasn't read before */
450 if($this->buffer==NULL){
451 $this->buffer=array();
452 $a_ret = array();
453 foreach($this->FAIdebianSection as $sec){
454 $strID= "/etc/gosa/fai/".$this->FAIdebianRelease."/".$sec;
456 if(!is_file($strID)){
457 print_red(sprintf(_("Package file '%s' does not exist."),$strID));
458 unset($this->buffer);
459 return(array());
460 }
461 $fp = fopen($strID,"r");
463 /* Parse every line and create an array */
464 while(!feof($fp)){
465 $str= fgets($fp,512);
466 $stra= split("\|",$str);
467 if(count($stra)==4){
468 $a_ret[$stra[0]] = $stra;
469 }
470 }
471 fclose($fp);
472 /* Save our Data, to avoid reading it again */
473 }
474 $this->buffer = $a_ret;
475 ksort($a_ret);
476 return($a_ret);
477 }else{
478 return $this->buffer;
479 }
480 }
483 /* Save to LDAP */
484 function save()
485 {
486 plugin::save();
488 $ldap = $this->config->get_ldap_link();
490 $this->attrs['FAIpackage'] = array();
491 foreach($this->usedPackages as $pkg => $obj){
492 $this->attrs['FAIpackage'][] = $pkg;
493 }
495 $this->attrs['FAIdebianSection'] = array();
496 foreach($this->FAIdebianSection as $sec){
497 $this->attrs['FAIdebianSection'][] = $sec;
498 }
500 prepare_to_save_FAI_object($this->dn,$this->attrs);
501 show_ldap_error($ldap->get_error(), sprintf(_("Saving of FAI/package list with dn '%s' failed."),$this->dn));
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 prepare_to_save_FAI_object($pkgdn,$pkgattrs);
537 /* Handle tagging */
538 $this->handle_object_tagging($pkgdn, $this->gosaUnitTag);
539 }
540 }
541 }
543 /* Return plugin informations for acl handling */
544 function plInfo()
545 {
546 return (array(
547 "plShortName" => _("Package"),
548 "plDescription" => _("FAI Package list"),
549 "plSelfModify" => FALSE,
550 "plDepends" => array(),
551 "plPriority" => 30,
552 "plSection" => array("administration"),
553 "plCategory" => array("fai"),
554 "plProvidedAcls" => array(
555 "cn" => _("Name"),
556 "description" => _("Description"),
557 "FAIpackage" => _("Packages"),
558 "FAIdebianSection" => _("Section"),
559 "FAIinstallMethod" => _("Install Method"),
560 "FAIdebianRelease" => _("Release"))
561 ));
562 }
563 }
565 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
566 ?>