Code

Added snapshot & C&P acls
[gosa.git] / plugins / admin / groups / class_groupApplication.inc
1 <?php
2 class appgroup extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Manage application groups";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* Appgroup attributes */
10   var $gosaMemberApplication= array();
12   /* Helpers */
13   var $departments        = "";       // All departments within $this->curbase;
14   var $apps               = array();  // All available applications
15   var $used_apps          = array();  // Specifies which applications are currently assigned 
17   var $option_name          = array();
18   var $option_value         = array();
19   var $appoption            = array();
21   var $table                  = "";
22   var $curbase                = "";
23   var $curCatDir                  ;
24   var $curCatDepth              =0;         //
26   /* attribute list for save action */
27   var $attributes               = array("gosaMemberApplication","gosaApplicationParameter");
28   var $objectclasses            = array("gosaApplicationGroup");
30   var $FAIrelease               = "/";
31   var $InitialFAIrelease           = "/";
32   var $Releases                 = array();
33   var $enableReleaseManagement  = false;
35   var $AllAppsForRelease        = array();
36   var $AllAppsForReleaseParameter = array();
38   var $CopyPasteVars            = array("Categories");
39   var $gosaApplicationParameter ;
40   var $ui                       = NULL;
41   var $no_release_acls          = false;
43   function appgroup ($config, $dn= NULL, $parent= NULL)
44   {
45    
46     /* Check if we have relase mangement enabled and prepare group application for release management */ 
47     $tmp = search_config($config->data,"faiManagement","CLASS");
48     if(!empty($tmp)){
49       $this->enableReleaseManagement = true;
50       $this->objectclasses  [] = "FAIreleaseTag";
51       $this->attributes     [] = "FAIrelease";
52     }
54     plugin::plugin ($config, $dn, $parent);
56     /* set userinfo object */
57     $this->ui = get_userinfo();
59     /* Compatibility check 
60         In some case of old applikations with old release tag saving, 
61           we must reassign is_account state.
62           (Or if release management wasn't activated before creating this app) */
63     if($this->enableReleaseManagement){
64   
65       /* Release management is activated && this is a valid group app account,
66           but no release class was found, so activate is_account flag  */
67       if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
68          (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
69         $this->is_account =true;
70       }
71     }
72     
73     /* Load member applications */
74     if (isset ($this->attrs["gosaMemberApplication"][0])){
75       $this->gosaMemberApplication = array();
76       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
77         $this->gosaMemberApplication[]=
78           $this->attrs["gosaMemberApplication"][$i];
79       }
80     }
82     /* Load application options */
83     if (isset($this->attrs['gosaApplicationParameter'])){
84       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
85         $option= preg_replace('/^[^:]+:/', '',
86             $this->attrs['gosaApplicationParameter'][$i]);
87         $name= preg_replace('/:.*$/', '',
88             $this->attrs['gosaApplicationParameter'][$i]);
89         $this->appoption[$name]= $option;
90       }
91     }
93     /* Parse MemberApplication*/
94     $tmp    = array();
95     $tmp2   = array();
96     $prios  = array();
97     $cats   = array();
99     foreach($this->gosaMemberApplication as $memberApp){
100       if(preg_match("/\|/i",$memberApp)){
101     
102         $tmp = split("\|",$memberApp);
103  
104         if(!empty($tmp[0])){
105           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
106         }
107         if(!empty($tmp[1])){
108           $n = split("/",$tmp[1]);
109           $c = count($n);
110           $cats [$tmp[1]] = $n[$c-1];
111         }
112         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
113       }else{
114         $tmp2[]['App'] = $memberApp;
115       }
116     }
118     /* Assign collected category and application settings */
119     $this->Categories = $cats;
120     $this->gosaMemberApplication = $tmp2;
122     /* Sort application by category */
123     $cats[""]="";
124     foreach($cats as $cat ){
125       if((isset($prios[$cat]))&&(count($prios[$cat]))){
126         $max = max($prios[$cat]);
127         $min = 1;//min($prios[$cat]);  
128         $last = false;
129         for($i = $min ; $i < $max ; $i++){
130           if(!isset($prios[$cat][$i])){
131             if($last == false){
132               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
133             
134               $last = true;
135             }
136           }else{
137             $last = false;
138           }
139         }
140       }
141     } 
143     /* Sort application by priority */
144     $tmp = array();
145     foreach($this->gosaMemberApplication as $key =>  $entries){
146       ksort ($entries);
147       foreach($entries as $entry){
148         $tmp[$key][]= $entry;
149       }
150     }
151     $this->gosaMemberApplication = $tmp;
152     $this->curbase = $this->config->current['BASE'];
153     
154     /* Get required release informations */
155     if($this->enableReleaseManagement){
156       $this->InitialFAIrelease  = $this->FAIrelease;
157       $this->Releases           = $this->getReleases();
158     }
159   }
162   /* Combine new array */
163   function combineArrays($ar0,$ar1,$ar2)
164   {
165     $ret = array();
166     if(is_array($ar0))
167     foreach($ar0 as $ar => $a){
168         $ret[$ar]=$a;
169     }
170     if(is_array($ar1))
171     foreach($ar1 as $ar => $a){
172         $ret[$ar]=$a;
173     }
174     if(is_array($ar2))
175     foreach($ar2 as $ar => $a){
176         $ret[$ar]=$a;
177     }
178     return($ret);
179   }
181   /* Returns element position within given array */
182   function getpos($atr,$attrs)
183   {
184     $i = 0;
185     foreach($attrs as $attr => $name)    {
186       $i++;
187       if($attr == $atr){
188         return($i);
189       }
190     }
191     return(-1);
192   }
195   /* Check current combination of application && release 
196       Are all application available?, application parameter changed? ... */
197   function diffAppsInReleases()
198   {
199     /* Only diff if it is required  */
200     $ret =array();
202     /* If current release has changed */
203     if($this->FAIrelease != $this->InitialFAIrelease){
205       /* Walk through all apps which are currently displayed */
206       if(isset($this->gosaMemberApplication[$this->curCatDir])){
208         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
210           /* If application is also available in new release, check if parameter differs */
211           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
213             $old = array();   // Old application parameter
214             $new = array();   // New parameters 
216             /* There are possibly no parameters specified */
217             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
218               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
219             }
221             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
222               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
223             }
225             /*  Both (old & new) have no gosaApplicationParameter
226              */
227             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
228               $ret[$entry['App']] = false; 
230               /* Both got gosaApplicationParameter */
231             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
232               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
233                 $ret[$entry['App']]  = true;
234               }else{
235                 $ret[$entry['App']]  = false;
236               }
237             }
238             /* Initially had gosaApplicationParameter bot in new release not */
239           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
240             $ret[$entry['App']]  = true;
242             /* Old release had no gosaApplicationParameter but new got some */
243           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
244             $ret[$entry['App']]  = true;
245           }
246         }
247       }
248     }
249     return($ret);
250   }
252   /* TRansports the geiven Arraykey one position up*/
253   function ArrayUp($atr,$attrs)
254   {
255     $ret = $attrs;
256     $pos = $this->getpos($atr,$attrs) ;
257     $cn = count($attrs);
258     if(!(($pos == -1)||($pos == 1))){
259       $before = array_slice($attrs,0,($pos-2));
260       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
261       $unten  = array_slice($attrs,$pos);
262       $ret = array();
263       $ret = $this->combineArrays($before,$mitte,$unten);
264     }
265     return($ret);
266   }
269   /* TRansports the geiven Arraykey one position up*/
270   function ArrayDown($atr,$attrs)
271   {
272     $ret = $attrs;
273     $pos = $this->getpos($atr,$attrs) ;
274     $cn = count($attrs);
275     if(!(($pos == -1)||($pos == $cn))){
276       $before = array_slice($attrs,0,($pos-1));
277       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
278       $unten  = array_slice($attrs,($pos+1));
279       $ret = array();
280       $ret = $this->combineArrays($before,$mitte,$unten);
281     }
282     return($ret);
283   }
285     
286   /* Sort category on position up  */
287   function catUp($id)
288   {
289     /* Get all cats depinding on current dir */
290     $cats = $this->GetSubdirs($this->curCatDir);
291     $newcats =$this->ArrayUp($id,$cats);
292     foreach($newcats as $cat => $name){
293       unset($this->Categories[$cat]);
294     }
295     foreach($newcats as $cat => $name){
296       $this->Categories[$cat]=$name;
297     }
298   }
301   /* Sort category on position down */
302   function catDown($id)
303   {
304     /* Get all cats depinding on current dir */
305     $cats = $this->GetSubdirs($this->curCatDir);
306     $newcats =$this->ArrayDown($id,$cats);
307     foreach($newcats as $cat => $name){
308       unset($this->Categories[$cat]);
309     }
310     foreach($newcats as $cat => $name){
311       $this->Categories[$cat]=$name;
312     }
313   }
316   /* Increase application priority */
317   function getOneUp($appl)
318   { 
319     $cat  = $this->curCatDir;
320     $apps = $this->gosaMemberApplication[$cat];
321     $appsA = array();
322     foreach ($apps as $appkey => $name){
323       $appsA[$name['App']] =$name['App'];
324     }
325     $result = $this->ArrayUp($appl,$appsA);
326     $ret = array();
327     foreach($result as $app){
328       $ret[]=array("App"=>$app);
329     }
330     $this->gosaMemberApplication[$cat] = $ret;
331   }
334   /* Decrease application priority */
335   function getOneDown($appl)
336   {
337     $cat  = $this->curCatDir;
338     $apps = $this->gosaMemberApplication[$cat];
339     $appsA = array();
340     foreach ($apps as $appkey => $name){
341       $appsA[$name['App']] =$name['App'];
342     }
343     $result = $this->ArrayDown($appl,$appsA);
344     $ret = array();
345     foreach($result as $app){
346       $ret[]=array("App"=>$app);
347     }
348     $this->gosaMemberApplication[$cat] = $ret;
349   } 
352   /* Add seperator to application list */
353   function AddSeperator($id)
354   {
355     $found  = false;
356     $cat    = "";
357     $tmp = array();
358     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
359       $tmp[] = $app;    
360       if(($app['App'] == $id)&&(!$found)){
361         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
362         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
363         $found = true;
364       }
365     }
366     if($found){
367       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
368     }
369   }
372   /* Check if application acls are readable */
373   function check_acls()
374   {
375     return(count($this->ui->get_module_departments("application")));
376   }
379   function execute()
380   {
381     /* Call parent execute */
382     plugin::execute();
384     /* Check if department was selected */ 
385     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
386       $dep = base64_decode($_GET['depid']); 
387       $this->curbase =$dep;
388     }
390     /* Check if category was selected */
391     if((isset($_GET['act']))&&($_GET['act']=="open") && isset($this->Categories[base64_decode($_GET['id'])])){
392       $this->curCatDir = base64_decode($_GET['id']);
393     }
395     /* Do we need to flip is_account state? */
396     if(isset($_POST['modify_state'])){
397       if($this->is_account && $this->acl_is_removeable()){
398         $this->is_account= FALSE;
399       }elseif(!$this->is_account && $this->acl_is_createable()){
400         $this->is_account= TRUE;
401       }
402     }
404     /* Do we represent a valid group? */
405     if (!$this->is_account && $this->parent == NULL){
406       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
407         _("This 'dn' is no appgroup.")."</b>";
408       return ($display);
409     }
411     /* Show tab dialog headers */
412     $display= "";
413     if ($this->parent != NULL){
414       if ($this->is_account){
415         $display= $this->show_disable_header(_("Remove applications"),
416             _("This group has application features enabled. You can disable them by clicking below."));
417       } else {
418         $display.= $this->show_enable_header(_("Create applications"),
419             _("This group has application features disabled. You can enable them by clicking below."));
420         return ($display);
421       }
422     }
424     /* Check acl, applications must be readable */
425     if(!$this->check_acls()){
426       $display .= "<img src='images/button_cancel.png' alt='"._("ACL")."' class='center'>
427                   <b>"._("Insufficient permissions")."</b>
428                   <p>".
429                   _("You do not have permission to query application entries. All your changes will not be saved.").
430                   "</p><p class='seperator'>&nbsp;</p>";
431       $this->no_release_acls = true;
432     }
433     
435     /* Add Categorie */ 
436     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
438       /* Only allow adding a category, if it is allowed */  
439       if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
441         if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
442           print_red(_("Invalid character in category name."));
443         }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
444           if(empty($this->curCatDir)){
445             $this->Categories[$_POST['CatName']]=$_POST['CatName'];
446           }else{
447             $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
448           }
449         }else{
450           print_red(_("The specified category already exists."));
451         }
452       }
453     }
456     $this->reload();
457     $this->diffAppsInReleases();
459     /* Check POST variables for commands 
460         to add/remove some applications */
461     $only_once = false;
462     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
464       /* Walk through posts */
465       foreach($_POST as $name => $value){
467         /* Add a seperator to current category */
468         if((preg_match("/AddSep_/",$name))&&(!$only_once)){
469           $only_once = true;
470           $n = preg_replace("/AddSep_/","",$name);
471           $val= preg_replace("/_.*$/","",$n);
472           $this->AddSeperator($val);
473         }
475         /* Delete application | seperator entry */
476         if((preg_match("/DelApp_/",$name))&&(!$only_once)){
477           $only_once = true;
479           if(preg_match("/DelApp___SEPARATOR__/",$name)) {
480             $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
481             $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
482           }else{
483             $n = preg_replace("/DelApp_/","",$name);
484             $val= preg_replace("/_.*$/","",$n);
485           }
487           foreach($this->gosaMemberApplication as $key =>  $cat){
488             foreach($cat as $key2 => $app){
489               if($app['App'] == $val){
490                 unset($this->gosaMemberApplication[$key][$key2]);
491                 if(isset($this->used_apps[$val])){
492                   foreach($this->getParameters($val) as $para){
493                     if(isset($this->appoption[$para])){
494                       unset($this->appoption[$para]);
495                     }
496                   }
497                   unset($this->used_apps[$val]);
498                 }
499               }
500             }
501           }
502         }
504         /* Remove category entry */
505         if(preg_match("/DelCat_/",$name)){
506           $n = preg_replace("/DelCat_/","",$name);
507           $app = base64_decode( preg_replace("/_.*$/","",$n));
508           foreach($this->Categories as $key =>  $cat){
509             if($cat == $app){
510               foreach($this->Categories as $p => $n){
511                 if(preg_match("/^".$key."\/.*/",$p)){
512                   unset($this->Categories[$p]);    
513                 }
514               }
515               unset($this->Categories[$key]);
516             }
517           }
518         }
520         /* Edit application parameter */
521         if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
523           $only_once = true;
524           $appname = $value;
525           $appname = preg_replace("/EdiApp_/","",$name);  
526           $appname = preg_replace("/_.*$/","",$appname);
528           /* We've got the appname, get parameters from ldap */
529           $ldap= $this->config->get_ldap_link();
531           /* Check if we have release management enabled */
532           $tmp = search_config($this->config->data,"faiManagement","CLASS");
533           if(!empty($tmp)){
534     
535             /* Get application parameter from ldap */
536             $tmp = array_flip($this->Releases);
537             $base = $tmp[$this->FAIrelease];
538             $ldap->cd($base);
539             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
540             $found = "";
541             while($attrs = $ldap->fetch()) {
542               if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
543                 $found = $attrs['dn'];
544               }
545             }
546             $ldap->cat($found, array("gosaApplicationParameter"));
547           }else{
549             /* Get application parameter from ldap */
550             $ldap->cd($this->config->current['BASE']);
551             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
552           }
554           /* Check if this application is unique */
555           if ($ldap->count() != 1){
556             print_red (_("The selected application name is not uniq. Please check your LDAP."));
557           } else {
559             /* Get parameter */
560             $attrs= $ldap->fetch();
561             if(isset($attrs['gosaApplicationParameter'])){
562               $this->dialog= TRUE;
564               /* Fill name and value arrays */
565               for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
566                 $option= preg_replace('/^[^:]+:/', '',
567                     $attrs['gosaApplicationParameter'][$i]);
568                 $name= preg_replace('/:.*$/', '', 
569                     $attrs['gosaApplicationParameter'][$i]);
570                 $this->option_name[$i]= $name;
572                 /* Fill with values from application, default should be
573                    loaded by the external scripts */
574                 if (isset($this->appoption[$name])){
575                   $this->option_value[$i]= $this->appoption[$name];
576                 }
577               }
579               /* Create edit field */
580               $table= "<table summary=\"\">";
581               for ($i= 0; $i < count($this->option_name); $i++){
582                 if (isset($this->option_value[$i])){
583                   $value= $this->option_value[$i];
584                 } else {
585                   $value= "";
586                 }
587                 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
588                   "<input name=\"value$i\" size=60 maxlength=250 ".
589                   "value=\"".$value."\"><br></td></tr>";
590               }
591               $table.= "</table>";
592               $this->table= $table;
593             } else {
594               print_red (_("The selected application has no options."));
595             }
596           }
597         }
598       }
599     }
601     /* Add multiple */
602     if(isset($_POST['AddApps'])){
603       foreach($_POST as $name => $value){
604         if(preg_match("/AddApp_/",$name)){
605           $app = preg_replace("/AddApp_/","",$name);
606           $this->addApp($app);
607         }
608       }
609     }
611     /* Add application with post */
612     if((isset($_GET['act']))&&($_GET['act']=="add")){
613       $this->used_apps[$_GET['id']]= $_GET['id'];
614       asort($this->used_apps);
615       $this->addApp($_GET['id']);
616     }
618     /* Cancel edit options? */
619     if (isset($_POST['edit_options_cancel'])){
620       $this->dialog= FALSE;
621     }
623     /* Finish edit options? */
624     if (isset($_POST['edit_options_finish'])){
625       $this->dialog= FALSE;
627       /* Save informations passed by the user */
628       $this->option_value= array();
629       for ($i= 0; $i<count($this->option_name); $i++){
630         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
631         $this->is_modified= TRUE;
632       }
633     }
635     /* Prepare templating stuff */
636     $smarty= get_smarty();
637     $smarty->assign("used_apps", $this->used_apps);
638     $apps= array();
639     foreach ($this->apps as $key => $value){
640       if (!array_key_exists($key, $this->used_apps)){
641         $apps["$key"]= "$value";
642       }
643     }
646     /* Create application list */
647     $div = new DivSelectBox("appgroup");    
648     $div->SetHeight(300);
649     $departments = array();
650     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
651     foreach($res as $value){
652       $fdn= @LDAP::fix(str_replace($this->curbase,"",$value['dn']));
653       if($value["description"][0]!=".."){
654         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
655       }else{
656         $departments[$value['dn']]=convert_department_dn($fdn);
657       }
658     }
660     /* Create 'open' and 'add' links */
661     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
662       $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
663     }else{
664       $linkadd = "%s";
665     }
666     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
668     /* Create base back entry */
669     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
670     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
671       $div->AddEntry(array(
672             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
673               "attach"=>"style='border:0px;'")
674             ));
675     }
677     /* Append departments for current base */
678     foreach($departments as $key => $app){
679       $div->AddEntry(array(
680             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
681               "attach"=>"style='border:0px;'")
682             ));
683     }
685     /* Add applications found on this base */
686     foreach($apps as $key => $app){
687       $div->AddEntry(array(
688             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
689               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
690               "attach"=>"style='border:0px;'")
691             ));
692     }
694     /* Create list of used/assigned applications */
695     $div2 = new DivSelectBox("appgroup");
696     $div2->SetHeight(300);
698     /* Check acls to create "edit/remove" category links */
699     if(!$this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
700       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
701       $catremove      = "&nbsp;<img src='images/empty.png' alt='&nbsp;'>";
702       $catupdown      = "";
703     }else{
704       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
705       $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
706       $catupdown      = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>".
707         "<img align='top' alt=\"\" src='images/sort_up.png' border=0 title='"._("Move up")."'>".
708         "</a>&nbsp;".
709         "<a href='?plug=".$_GET['plug']."&amp;act=cat_down&amp;id=%s'>".
710         "<img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0>".
711         "</a>";
712     }
714     /* Add back category for current category */
715     if(empty($this->curCatDir)){
716       $cnt =0;
717     }else{
718       $cnt = count(split("/",$this->curCatDir));
719       $tmp = split("/",$this->curCatDir);
720       $bbk = "";
721       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
722         $bbk .= $tmp[$i];
723       }
724       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
725     }
727     /* Add sub categories */ 
728     $this->GetSubdirs($this->curCatDir);
729     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
730       $div2 ->AddEntry(array( 
731             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
732             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
733               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
734     }
736     /* Create priority & seperator links */
737     $separator ="<hr size=1>"; 
738     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
739       $sep      = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
740       $upudown  = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&amp;id=%s'>".
741                   " <img alt='{t}sort{/t}' align='top' src='images/sort_up.png' title='"._("Move up")."' border=0>".
742                   "</a>&nbsp;".
743                   "<a href='?plug=".$_GET['plug']."&amp;act=one_down&amp;id=%s'>".
744                   " <img alt='{t}sort{/t}' src='images/sort_down.png' title='"._("Move down")."' border=0>".
745                   "</a>&nbsp;".
746                   "<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
747     }else{
748       $sep      = "";
749       $upudown  = "";
750     }
751      
752     /* Create edit link */ 
753     if($this->acl_is_writeable("gosaApplicationParameter",$this->no_release_acls)){
754       $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
755     }else{
756       $edit=      "";
757     }
759     /* Get differences to mark those entries that have changed from last edit. */
760     $Differences = $this->diffAppsInReleases();
762     /* Walk through entries an append them to list */
763     if(isset($this->gosaMemberApplication[$this->curCatDir])){
764       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
766         /* Add seperator */ 
767         if(preg_match("/__SEPARATOR__/",$entry['App'])){
768           $div2 ->AddEntry(array(array("string"=>$separator),
769                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
770         }else{
772           /* Add application */
774           $image = "";
775           /* Check if application was available within selected release
776            *  or application list if release management is not active
777            */
778           if(!in_array($entry['App'],$this->AllAppsForRelease)){
780             /*  release managent is active
781              */
782             if(!$this->enableReleaseManagement){
783               $image = "<img class='center' src='images/select_invalid_application.png' 
784                           title='"._("This application is no longer available.")."'>&nbsp;";
785             }else{
786               $image = "<img class='center' src='images/select_invalid_application.png' 
787                           title='".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."'>&nbsp;";
788             }
789       
790           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
791             $entry['App'].=" <i>["._("Check parameter")."]</i>";
792               $image = "<img class='center' src='images/select_invalid_application.png' 
793                           title='"._("This application has changed parameters.")."'>&nbsp;";
794           }else{
795             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
796           }
797           
798           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
799                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
800                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
801         }
802       }
803     }
805     /* Assign created div lists to template */
806     $smarty->assign("UsedApps", $div2->DrawList());
807     $smarty->assign("List", $div->DrawList());
808     $smarty->assign("apps", $apps);
809    
810     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
812     $smarty->assign("ReleaseSelectAble", false);
813     $smarty->assign("Release", $this->FAIrelease);
814  
815     if($this->enableReleaseManagement){
816       $smarty->assign("FAIrelease",  $this->FAIrelease);
817       $smarty->assign("Releases", $this->Releases);
819       if(!$this->no_release_acls && count($this->Releases)){
820         $smarty->assign("ReleaseSelectAble", true);
821       }
822     }
823   
824     /* Set acls to  template */
825     $tmp = $this->plInfo();
826     foreach($tmp['plProvidedAcls'] as $acl => $translation){
827       $smarty->assign($acl."ACL",$this->getacl($acl,$this->no_release_acls));
828     }
830     /* Show main page */
831     if ($this->dialog){
832       $smarty->assign("table", $this->table);
833       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
834     } else {
835       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
836     }
837     
838     return ($display);
839   }
842   function getReleases()
843   {
844     /* Only display those releases that we are able to read */
845     $dn     = $this->config->current['BASE'];
846     $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
847     $res    = get_list($filter,"application", $dn, array("ou"), GL_SUBSEARCH);
849     $ret =array();
850     foreach($res as $attrs){
851       if(preg_match("/ou=apps,/",$attrs['dn'])){
852         $bb     = preg_replace("/ou=apps,.*/","",$attrs['dn']);
853         $parts  = array_reverse(split("ou=",$bb));
855         $str ="";
856         foreach($parts as $part){
857           if(empty($part)) {
858             continue;
859           }
860           $str .= str_replace(",","",$part)."/";
861         }    
862         $name = preg_replace("/\/$/","",$str);
863         if(empty($name)) {
864           $name ="/";
865         }
866         $ret[$attrs['dn']] = $name;
867       }
868     }
869     return($ret);
870   }
872   function save_object()
873   {
875     /* Move category one position up or down */
876     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
877       if($_GET['act']=="cat_up"){
878         $this->catUp(base64_decode($_GET['id']));
879       }
880       if($_GET['act']=="cat_down"){
881         $this->catDown(base64_decode($_GET['id']));
882       }
883     }
885     /* Move application one position up or down */
886     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
887       if(isset($_GET['id'])){
888         $id   = $_GET['id'];
889         $act  = $_GET['act']; 
891         if($act == "one_up"){
892           $this->getOneUp($id);
893         }elseif($act == "one_down")   { 
894           $this->getOneDown($id);
895         }
896       }
897     }
900     plugin::save_object();
901   }
902  
904   function remove_from_parent()
905   {
906     plugin::remove_from_parent();
908     $ldap= $this->config->get_ldap_link();
909     $ldap->cd($this->dn);
910     $this->cleanup();
911     
912     $ldap->modify ($this->attrs); 
914     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/applications with dn '%s' failed."),$this->dn));
916     /* Optionally execute a command after we're done */
917     $this->handle_post_events("remove");
918   }
921   /* Save to LDAP */
922   function save()
923   {
924     /* Skip saving application settings, if we do not have release acls */
925     if($this->no_release_acls){
926       gosa_log("Skip saving group application extension. Caused by insufficient acls");
927       return;
928     }
930     plugin::save();
934     /* Copy members */
935     $this->Categories[""]=""; 
936     $this->attrs["gosaMemberApplication"]= array();
937     $this->attrs["gosaApplicationParameter"]= array();
938     foreach($this->Categories as $name => $cats){
939       $i =0;
940       if(isset($this->gosaMemberApplication[$name])){
941         foreach($this->gosaMemberApplication[$name] as $entry){
942           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
943             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
944           }
945           $i ++;
946         }
947       }
948       if(($i==0)&&(!empty($name))){
949         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$i;
950       }
951     }
953     /* Are there application parameters to be saved */
954     $this->attrs['gosaApplicationParameter']= array();
955     foreach($this->appoption as $name => $value){
956       if ($value != ""){
957         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
958       }
959     }
961     /* Write back to LDAP */
962     $ldap= $this->config->get_ldap_link();
963     $ldap->cd($this->dn);
964     $this->cleanup();
965     $ldap->modify ($this->attrs); 
967     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/applications with dn '%s' failed."),$this->dn));
969     /* Optionally execute a command after we're done */
970     if ($this->initially_was_account == $this->is_account){
971       if ($this->is_modified){
972         $this->handle_post_events("modify");
973       }
974     } else {
975       $this->handle_post_events("add");
976     }
978   }
980   function check()
981   {
982     /* Call common method to give check the hook */
983     $message= plugin::check();
985     return ($message);
986   }
989   function reload()
990   {
991     /* Generate applist */
992     $this->apps= array();
994     /* Special handling for release managed apps */
995     $tmp = search_config($this->config->data,"faiManagement","CLASS");
996     if(!empty($tmp)){
997       $this->enableReleaseManagement = true;
999       /* Check if release is available */
1000       $tmp = array_flip($this->Releases);
1001       if(isset($tmp[$this->FAIrelease])){
1002         $base =  $tmp[$this->FAIrelease];
1003       }else{
1005         /* Release is not available, check if there is an alternative */
1006         $old_r =  $this->FAIrelease;
1007         $k = key($tmp);
1009         if(isset($tmp[$k])){
1010       
1011           /* We have found an alternative release name */
1012           $r = $tmp[$k];
1013           $this->FAIrelease = $k;
1014           $base = $r; 
1016           /* Display msg */
1017           if(!(!$this->initially_was_account && $old_r =="/")){
1018             print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$old_r,$k));   
1019           }
1020         }else{
1021           
1022           /* There are no releases available ... */
1023           print_red(_("There are no releases available. You will not be able to select another release."));
1024           return;
1025         }
1026       }
1028       /* Get applications for this release */
1029       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
1030       $res = get_list("(objectClass=gosaApplication)","application",$base,array("*"));
1031     }else{
1032     
1033       /* Get applications for this base */
1034       $res = get_list("(objectClass=gosaApplication)","application","ou=apps,".$this->curbase,array("*"));
1035     }
1036     
1037     /* Append applications */
1038     foreach($res as $attrs){
1039       if (isset($attrs["description"][0])){    
1040         $this->apps[$attrs["cn"][0]]=
1041           $attrs["cn"][0]." (".
1042           $attrs["description"][0].")";
1043       } else {
1044         $this->apps[$attrs["cn"][0]]=
1045           $attrs["cn"][0];
1046       }
1047     }
1049     /* Get all apps ... */
1050     $res = get_list("objectClass=gosaApplication","application",$this->config->current['BASE'],array("gosaApplicationParameter","cn"),GL_SUBSEARCH); 
1051     $tmp = search_config($this->config->data,"faiManagement","CLASS");
1052     $this->AllAppsForRelease = array();
1053     if(!empty($tmp)){
1054       $tmp = array_flip($this->Releases);
1055       foreach($res as $attrs){
1056         
1057         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
1058         $testdn = preg_replace("/^[^,]+/","",$testdn);
1059         $testdn = preg_replace("/^,/","",$testdn);
1061         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
1062           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1063           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
1064         }
1065       }
1066     }else{
1067       foreach($res as $attrs){
1068         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1069       }
1070     }
1072     natcasesort ($this->apps);
1073     reset ($this->apps);
1075     if(is_array($this->gosaMemberApplication))
1076       foreach ($this->gosaMemberApplication as $cat){   
1077         if(is_array($cat))
1078           foreach($cat as $entry){
1079             $this->used_apps[$entry['App']]= $entry['App'];
1080           }
1081       }
1082   }
1085   function addApp($cn)
1086   {
1087     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
1088       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
1089         if($entry['App'] == $cn) return;
1090       }
1091     }
1092     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
1093     $this->used_apps[$cn]=$cn;
1094     $this->is_modified= TRUE;
1095   }
1098   function removeApp($cn)
1099   {
1100     $temp= array();
1101     foreach ($this->gosaMemberApplication as $value){
1102       if ($value != $cn){
1103         $temp[]= $value;
1104       }
1105     }
1106     $this->gosaMemberApplication= $temp;
1107     $this->is_modified= TRUE;
1108   }
1110   function getParameters($app)
1111   {
1112     $tmp = $this->getReleases();
1113     $ret = array();
1114     if(in_array($this->FAIrelease,$tmp)){
1115       $tmp2 = array_flip($tmp);
1116       $base = $tmp2[$this->FAIrelease];
1117       $ldap = $this->config->get_ldap_link();
1118       $ldap->cd($this->config->current['BASE']);
1119       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1120       if($ldap->count()){
1121         $attrs = $ldap->fetch();
1122         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1123           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1124           $ret[$name] = $name;
1125         } 
1126       }
1127     }
1128     return($ret);
1129   }
1131   function GetSubdirs($dir)
1132   {
1133     $ret = array();
1134     $tmp1 = split("/",$this->curCatDir);
1135  
1136     foreach($this->Categories as $path=>$cat){
1137       $tmp2 = split("/",$path);
1138       
1139       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1140         $abort = false;
1141       }elseif(((count($tmp1))+1) == (count($tmp2))){
1142         $abort = false;
1143         for($i = 0 ; $i < count($tmp1) ; $i++){
1144           if($tmp1[$i] != $tmp2[$i]){
1145             $abort = true;
1146           }
1147         }
1148       }else{
1149         $abort= true;
1150       }
1151       if(!$abort){
1152         $ret[$path]=$cat;
1153       } 
1154     }
1155     return($ret);
1156   }
1159   /* Return plugin informations for acl handling  */ 
1160   function plInfo()
1161   {
1162     return (array(
1163           "plShortName"   => _("Applications"),
1164           "plDescription" => _("Group applications"),
1165           "plSelfModify"  => FALSE,
1166           "plDepends"     => array(),
1167           "plPriority"    => 0,
1168           "plSection"     => array("admin"),
1169           "plCategory"    => array("groups"),
1170           "plProvidedAcls"=> array(
1171             "gosaMemberApplication"     => _("Application"),
1172             "FAIrelease"                => _("Release"),
1173             "gosaApplicationParameter"  => _("Application parameter"))
1174           ));
1175   }
1177 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1178 ?>