Code

ACL - Hide options
[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 ;
42   function appgroup ($config, $dn= NULL)
43   {
44    
45     /* prepare group app for release management */ 
46     $tmp = search_config($config->data,"faiManagement","CLASS");
47     if(!empty($tmp)){
48       $this->enableReleaseManagement = true;
49       $this->objectclasses [] = "FAIreleaseTag";
50       $this->attributes[] =  "FAIrelease";
51     }
53     plugin::plugin ($config, $dn);
55     /* In some case of old applikations with old release tag saving, we 
56         must reassign is_account state.
57        (Or if release management wasn't activated before creating this app) */
58     if($this->enableReleaseManagement){
59   
60       /* Release management is activated && this is a valid group app account,
61           but no release class was found, so activate is_account flag  */
62       if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
63          (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
64         $this->is_account =true;
65       }
66     }
67     
69     /* Load member applications */
70     if (isset ($this->attrs["gosaMemberApplication"][0])){
71       $this->gosaMemberApplication = array();
72       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
73         $this->gosaMemberApplication[]=
74           $this->attrs["gosaMemberApplication"][$i];
75       }
76     }
78     /* Load application options */
79     if (isset($this->attrs['gosaApplicationParameter'])){
80       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
81         $option= preg_replace('/^[^:]+:/', '',
82             $this->attrs['gosaApplicationParameter'][$i]);
83         $name= preg_replace('/:.*$/', '',
84             $this->attrs['gosaApplicationParameter'][$i]);
85         $this->appoption[$name]= $option;
86       }
87     }
89     /* Parse MemberApplication*/
90     $tmp    = array();
91     $tmp2   = array();
92     $prios  = array();
93     $cats   = array();
95     foreach($this->gosaMemberApplication as $memberApp){
96       if(preg_match("/\|/i",$memberApp)){
97     
98         $tmp = split("\|",$memberApp);
99  
100         if(!empty($tmp[0])){
101           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
102         }
103         if(!empty($tmp[1])){
104           $n = split("/",$tmp[1]);
105           $c = count($n);
106           $cats [$tmp[1]] = $n[$c-1];
107         }
108         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
109       }else{
110         $tmp2[]['App'] = $memberApp;
111       }
112     }
114     $this->Categories = $cats;
116     $this->gosaMemberApplication = $tmp2;
117     $cats[""]="";
118     foreach($cats as $cat ){
119       if((isset($prios[$cat]))&&(count($prios[$cat]))){
120         $max = max($prios[$cat]);
121         $min = 1;//min($prios[$cat]);  
122         $last = false;
123         for($i = $min ; $i < $max ; $i++){
124           if(!isset($prios[$cat][$i])){
125             if($last == false){
126               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
127             
128               $last = true;
129             }
130           }else{
131             $last = false;
132           }
133         }
134       }
135     }
137     $tmp = array();
138     foreach($this->gosaMemberApplication as $key =>  $entries){
139       ksort ($entries);
140       foreach($entries as $entry){
141         $tmp[$key][]= $entry;
142       }
143     }
144     $this->gosaMemberApplication = $tmp;
145     $this->curbase = $this->config->current['BASE'];
146     
147     /* Get required release informations */
148     if($this->enableReleaseManagement){
149       $this->InitialFAIrelease = $this->FAIrelease;
150       $this->Releases       = $this->getReleases();
151     }
152   }
155   /* Combine new array */
156   function combineArrays($ar0,$ar1,$ar2)
157   {
158     $ret = array();
159     if(is_array($ar0))
160     foreach($ar0 as $ar => $a){
161         $ret[$ar]=$a;
162     }
163     if(is_array($ar1))
164     foreach($ar1 as $ar => $a){
165         $ret[$ar]=$a;
166     }
167     if(is_array($ar2))
168     foreach($ar2 as $ar => $a){
169         $ret[$ar]=$a;
170     }
171     return($ret);
172   }
174   function getpos($atr,$attrs)
175   {
176     $i = 0;
177     foreach($attrs as $attr => $name)    {
178       $i++;
179       if($attr == $atr){
180         return($i);
181       }
182     }
183     return(-1);
184   }
186   function diffAppsInReleases()
187   {
188     /* Only diff if it is required 
189      */
190     $ret =array();
192     /* If current release has changed */
193     if($this->FAIrelease != $this->InitialFAIrelease){
195       /* Walk through all apps which are currently displayed */
196       if(isset($this->gosaMemberApplication[$this->curCatDir])){
198         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
200           /* If application is also available in new release, check if parameter differs */
201           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
203             $old = array();   // Old application parameter
204             $new = array();   // New parameters 
206             /* There are possibly no parameters specified */
207             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
208               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
209             }
211             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
212               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
213             }
215             /*  Both (old & new) have no gosaApplicationParameter
216              */
217             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
218               $ret[$entry['App']] = false; 
220               /* Both got gosaApplicationParameter
221                */
222             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
223               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
224                 $ret[$entry['App']]  = true;
225               }else{
226                 $ret[$entry['App']]  = false;
227               }
228             }
229             /* Initialy had gosaApplicationParameter bot in new release not 
230              */
231           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
232             $ret[$entry['App']]  = true;
234             /* Old release had no gosaApplicationParameter but new got some
235              */
236           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
237             $ret[$entry['App']]  = true;
238           }
239         }
240       }
241     }
242     return($ret);
243   }
245   /* TRansports the geiven Arraykey one position up*/
246   function ArrayUp($atr,$attrs)
247   {
248     $ret = $attrs;
249     $pos = $this->getpos($atr,$attrs) ;
250     $cn = count($attrs);
251     if(!(($pos == -1)||($pos == 1))){
252       $before = array_slice($attrs,0,($pos-2));
253       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
254       $unten  = array_slice($attrs,$pos);
255       $ret = array();
256       $ret = $this->combineArrays($before,$mitte,$unten);
257     }
258     return($ret);
259   }
262   /* TRansports the geiven Arraykey one position up*/
263   function ArrayDown($atr,$attrs)
264   {
265     $ret = $attrs;
266     $pos = $this->getpos($atr,$attrs) ;
267     $cn = count($attrs);
268     if(!(($pos == -1)||($pos == $cn))){
269       $before = array_slice($attrs,0,($pos-1));
270       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
271       $unten  = array_slice($attrs,($pos+1));
272       $ret = array();
273       $ret = $this->combineArrays($before,$mitte,$unten);
274     }
275     return($ret);
276   }
279   function catUp($id)
280   {
281     /* Get all cats depinding on current dir */
282     $cats = $this->GetSubdirs($this->curCatDir);
283     $newcats =$this->ArrayUp($id,$cats);
285     foreach($newcats as $cat => $name){
286       unset($this->Categories[$cat]);
287     }
288     foreach($newcats as $cat => $name){
289       $this->Categories[$cat]=$name;
290     }
291   }
294   function catDown($id)
295   {
296     /* Get all cats depinding on current dir */
297     $cats = $this->GetSubdirs($this->curCatDir);
299     $newcats =$this->ArrayDown($id,$cats);
301     foreach($newcats as $cat => $name){
302       unset($this->Categories[$cat]);
303     }
304     foreach($newcats as $cat => $name){
305       $this->Categories[$cat]=$name;
306     }
307   }
310   function getOneUp($appl)
311   { 
312     $cat  = $this->curCatDir;
313     $apps = $this->gosaMemberApplication[$cat];
315     $appsA = array();
316     foreach ($apps as $appkey => $name){
317       $appsA[$name['App']] =$name['App'];
318     }
320     $result = $this->ArrayUp($appl,$appsA);
322     $ret = array();
323     foreach($result as $app){
324       $ret[]=array("App"=>$app);
325     }
326     $this->gosaMemberApplication[$cat] = $ret;
327   }
330   function getOneDown($appl)
331   {
332     $cat  = $this->curCatDir;
333     $apps = $this->gosaMemberApplication[$cat];
335     $appsA = array();
336     foreach ($apps as $appkey => $name){
337       $appsA[$name['App']] =$name['App'];
338     }
340     $result = $this->ArrayDown($appl,$appsA);
342     $ret = array();
343     foreach($result as $app){
344       $ret[]=array("App"=>$app);
345     }
346     $this->gosaMemberApplication[$cat] = $ret;
347   } 
351   function AddSeperator($id)
352   {
353     $found  = false;
354     $cat    = "";
355     $tmp = array();
356     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
357       $tmp[] = $app;    
358       if(($app['App'] == $id)&&(!$found)){
359         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
360         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
361         $found = true;
362       }
363     }
364     if($found){
365       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
366     }
367   }
369   function execute()
370   {
371     /* Call parent execute */
372     plugin::execute();
374     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
375       $dep = base64_decode($_GET['depid']); 
376       $this->curbase =$dep;
377     }
379     if((isset($_GET['act']))&&($_GET['act']=="open")){
380       $this->curCatDir = base64_decode($_GET['id']);
381     }
383     /* Do we need to flip is_account state? */
384     if(isset($_POST['modify_state'])){
385       if($this->is_account && $this->acl_is_removeable()){
386         $this->is_account= FALSE;
387       }elseif(!$this->is_account && $this->acl_is_createable()){
388         $this->is_account= TRUE;
389       }
390     }
392     /* Do we represent a valid group? */
393     if (!$this->is_account && $this->parent == NULL){
394       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
395         _("This 'dn' is no appgroup.")."</b>";
396       return ($display);
397     }
399     /* Show tab dialog headers */
400     $display= "";
401     if ($this->parent != NULL){
402       if ($this->is_account){
403         $display= $this->show_disable_header(_("Remove applications"),
404             _("This group has application features enabled. You can disable them by clicking below."));
405       } else {
406         $display.= $this->show_enable_header(_("Create applications"),
407             _("This group has application features disabled. You can enable them by clicking below."));
408         return ($display);
409       }
410     }
413     /* Add Categorie */ 
416     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
418       if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
419         print_red(_("Invalid character in category name."));
420       }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
421         if(empty($this->curCatDir)){
422           $this->Categories[$_POST['CatName']]=$_POST['CatName'];
423         }else{
424           $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
425         }
426       }else{
427         print_red(_("The specified category already exists."));
428       }
429     }
432     $this->reload();
434     $this->diffAppsInReleases();
435     $only_once = false;
436     foreach($_POST as $name => $value){
437       
438       if((preg_match("/AddSep_/",$name))&&(!$only_once)){
439         $only_once = true;
440         $n = preg_replace("/AddSep_/","",$name);
441         $val= preg_replace("/_.*$/","",$n);
442         $this->AddSeperator($val);
443       }
445       if((preg_match("/DelApp_/",$name))&&(!$only_once)){
446         $only_once = true;
447    
449         if(preg_match("/DelApp___SEPARATOR__/",$name)) {
450           $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
451           $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
452         }else{
453           $n = preg_replace("/DelApp_/","",$name);
454           $val= preg_replace("/_.*$/","",$n);
455         }
457         foreach($this->gosaMemberApplication as $key =>  $cat){
458           foreach($cat as $key2 => $app){
459             
460             if($app['App'] == $val){
461               unset($this->gosaMemberApplication[$key][$key2]);
462               if(isset($this->used_apps[$val])){
463                 foreach($this->getParameters($val) as $para){
464                   if(isset($this->appoption[$para])){
465                     unset($this->appoption[$para]);
466                   }
467                 }
468                 unset($this->used_apps[$val]);
469               }
470             }
471           }
472         }
473       }
474   
475       if(preg_match("/DelCat_/",$name)){
476         $n = preg_replace("/DelCat_/","",$name);
477         $app = base64_decode( preg_replace("/_.*$/","",$n));
478         foreach($this->Categories as $key =>  $cat){
479           if($cat == $app){
480             foreach($this->Categories as $p => $n){
481               if(preg_match("/^".$key."\/.*/",$p)){
482                 unset($this->Categories[$p]);    
483               }
484             }
485             unset($this->Categories[$key]);
486           }
487         }
488       }
489       
490       if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
492         $only_once = true;
493         $appname = $value;
494         $appname = preg_replace("/EdiApp_/","",$name);  
495         $appname = preg_replace("/_.*$/","",$appname);
497         /* We've got the appname, get parameters from ldap 
498          */
499         $ldap= $this->config->get_ldap_link();
501         $tmp = search_config($this->config->data,"faiManagement","CLASS");
502         if(!empty($tmp)){
503           $tmp = array_flip($this->Releases);
504           $base = $tmp[$this->FAIrelease];
506           $ldap->cd($this->config->current['BASE']);
507           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
508           $found = "";
509           while($attrs = $ldap->fetch()) {
510             if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
511               $found = $attrs['dn'];
512             }
513           }
514           $ldap->cat($found, array("gosaApplicationParameter"));
515         }else{
516           $ldap->cd($this->config->current['BASE']);
517           $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
518         }
521         if ($ldap->count() != 1){
522           print_red (_("The selected application name is not uniq. Please check your LDAP."));
523         } else {
524           $attrs= $ldap->fetch();
525           if(isset($attrs['gosaApplicationParameter'])){
526             $this->dialog= TRUE;
528             /* Fill name and value arrays */
529             for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
530               $option= preg_replace('/^[^:]+:/', '',
531                   $attrs['gosaApplicationParameter'][$i]);
532               $name= preg_replace('/:.*$/', '', 
533                   $attrs['gosaApplicationParameter'][$i]);
534               $this->option_name[$i]= $name;
536               /* Fill with values from application, default should be
537                  loaded by the external scripts */
538               if (isset($this->appoption[$name])){
539                 $this->option_value[$i]= $this->appoption[$name];
540               }
541             }
543             /* Create edit field */
544             $table= "<table summary=\"\">";
545             for ($i= 0; $i < count($this->option_name); $i++){
546               if (isset($this->option_value[$i])){
547                 $value= $this->option_value[$i];
548               } else {
549                 $value= "";
550               }
551               $table.="<tr><td>".$this->option_name[$i]."</td><td>".
552                 "<input name=\"value$i\" size=60 maxlength=250 ".
553                 "value=\"".$value."\"><br></td></tr>";
554             }
555             $table.= "</table>";
556             $this->table= $table;
557           } else {
558             print_red (_("The selected application has no options."));
559           }
560         }
561       }
562     }
564     $this->reload();
566     /* Add group with post */
567     if((isset($_GET['act']))&&($_GET['act']=="add")){
568       $this->used_apps[$_GET['id']]= $_GET['id'];
569       asort($this->used_apps);
570       $this->addApp($_GET['id']);
571     }
573     /* Add multiple */
574     if(isset($_POST['AddApps'])){
575       foreach($_POST as $name => $value){
576         if(preg_match("/AddApp_/",$name)){
577           $app = preg_replace("/AddApp_/","",$name);
578           $this->addApp($app);
579         }
580       }
581     }
584     /* Cancel edit options? */
585     if (isset($_POST['edit_options_cancel'])){
586       $this->dialog= FALSE;
587     }
589     /* Finish edit options? */
590     if (isset($_POST['edit_options_finish'])){
591       $this->dialog= FALSE;
593       /* Save informations passed by the user */
594       $this->option_value= array();
595       for ($i= 0; $i<count($this->option_name); $i++){
596         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
597         $this->is_modified= TRUE;
598       }
599     }
601     /* Prepare templating stuff */
602     $smarty= get_smarty();
603     $smarty->assign("used_apps", $this->used_apps);
604     $apps= array();
605     foreach ($this->apps as $key => $value){
606       if (!array_key_exists($key, $this->used_apps)){
607         $apps["$key"]= "$value";
608       }
609     }
611     $div = new DivSelectBox("appgroup");    
613     $div->SetHeight(300);
615     /* get departments */  
616     $ui = get_userinfo();
617     $departments = array();
618     $res = get_list("(objectClass=gosaDepartment)", "department", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
619     foreach($res as $value){
620       $fdn= @LDAP::fix(str_replace($this->curbase,"",$value['dn']));
621       if($value["description"][0]!=".."){
622         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
623       }else{
624         $departments[$value['dn']]=convert_department_dn($fdn);
625       }
626     }
628     if($this->acl_is_writeable("gosaMemberApplication")){
629       $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
630     }else{
631       $linkadd = "%s";
632     }
634     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
636     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
637     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
638       $div->AddEntry(array(
639             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
640               "attach"=>"style='border:0px;'")
641             ));
642     }
643     foreach($departments as $key => $app){
644       $div->AddEntry(array(
645             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
646               "attach"=>"style='border:0px;'")
647             ));
648     }
650     foreach($apps as $key => $app){
651       $div->AddEntry(array(
652             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
653               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
654               "attach"=>"style='border:0px;'")
655             ));
656     }
658     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
659       if($_GET['act']=="cat_up"){
660         $this->catUp(base64_decode($_GET['id']));
661       }
662       if($_GET['act']=="cat_down"){
663         $this->catDown(base64_decode($_GET['id']));
664       }
665     }
667     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
668       if(isset($_GET['id'])){
669         $id   = $_GET['id'];
670         $act  = $_GET['act']; 
672         if($act == "one_up"){
673           $this->getOneUp($id);
674         }elseif($act == "one_down")   { 
675           $this->getOneDown($id);
676         }
677       }
678     }
680     $div2 = new DivSelectBox("appgroup");
681     $div2->SetHeight(300);
683     if(!$this->acl_is_writeable("gosaMemberApplication")){
684       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
685       $catremove      = "&nbsp;<img sry='images/empty.png' alt='&nbsp;'>";
686       $catupdown      = "";
687     }else{
688       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
689       $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
690       $catupdown      = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>".
691         "<img align='top' alt=\"\" src='images/sort_up.png' border=0 title='"._("Move up")."'>".
692         "</a>&nbsp;".
693         "<a href='?plug=".$_GET['plug']."&amp;act=cat_down&amp;id=%s'>".
694         "<img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0>".
695         "</a>";
696     }
698     if(empty($this->curCatDir)){
699       $cnt =0;
700     }else{
701       $cnt = count(split("/",$this->curCatDir));
702       $tmp = split("/",$this->curCatDir);
703       $bbk = "";
704       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
705         $bbk .= $tmp[$i];
706       }
707       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
708     }
710     $this->GetSubdirs($this->curCatDir);
712     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
713       $div2 ->AddEntry(array( 
714             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
715             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
716               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
717     }
719     /* Append entries */
721     $separator ="<hr size=1>"; 
723     if($this->acl_is_writeable("gosaMemberApplication")){
724       $sep      = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
725       $upudown  = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&amp;id=%s'>".
726                   " <img alt='{t}sort{/t}' align='top' src='images/sort_up.png' title='"._("Move up")."' border=0>".
727                   "</a>&nbsp;".
728                   "<a href='?plug=".$_GET['plug']."&amp;act=one_down&amp;id=%s'>".
729                   " <img alt='{t}sort{/t}' src='images/sort_down.png' title='"._("Move down")."' border=0>".
730                   "</a>&nbsp;".
731                   "<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
732     }else{
733       $sep      = "";
734       $upudown  = "";
735     }
736       
737     if($this->acl_is_writeable("gosaApplicationParameter")){
738       $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
739     }else{
740       $edit=      "";
741     }
743     $Differences = $this->diffAppsInReleases();
745     if(isset($this->gosaMemberApplication[$this->curCatDir])){
746       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
748         
749         if(preg_match("/__SEPARATOR__/",$entry['App'])){
750           $div2 ->AddEntry(array(array("string"=>$separator),
751                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
752         }else{
754           $image = "";
755           /* Check if application was available within selected release
756            *  or application list if release management is not active
757            */
758           if(!in_array($entry['App'],$this->AllAppsForRelease)){
760             /*  release managent is active
761              */
762             if(!$this->enableReleaseManagement){
763               $image = "<img class='center' src='images/select_invalid_application.png' 
764                           title='"._("This application is no longer available.")."'>&nbsp;";
765             }else{
766               $image = "<img class='center' src='images/select_invalid_application.png' 
767                           title='".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."'>&nbsp;";
768             }
769       
770           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
771             $entry['App'].=" <i>["._("Check parameter")."]</i>";
772               $image = "<img class='center' src='images/select_invalid_application.png' 
773                           title='"._("This application has changed parameters.")."'>&nbsp;";
774           }else{
775             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
776           }
777           
778           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
779                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
780                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
781         }
782       }
783     }
785     $smarty->assign("UsedApps", $div2->DrawList());
786     $smarty->assign("List", $div->DrawList());
787     $smarty->assign("apps", $apps);
788    
789     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
790  
791     if($this->enableReleaseManagement){
792       $smarty->assign("FAIrelease",  $this->FAIrelease);
793       $smarty->assign("Releases", $this->Releases);
795       if(count($this->used_apps)){
796         $smarty->assign("ReleaseSelectAble", false);
797       }else{
798         $smarty->assign("ReleaseSelectAble", true);
799       }
800     }
801     $smarty->assign("ReleaseSelectAble", true);
803     /* Set acls to  template */
804     $tmp = $this->plInfo();
805     foreach($tmp['plProvidedAcls'] as $acl => $translation){
806       $smarty->assign($acl."ACL",$this->getacl($acl));
807     }
809     /* Show main page */
810     if ($this->dialog){
811       $smarty->assign("table", $this->table);
812       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
813     } else {
814       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
815     }
816     
817     return ($display);
818   }
821   function getReleases()
822   {
823     $dn     = $this->config->current['BASE'];
824     $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
825     $res    = get_list($filter,"groups", $dn, array("ou"), GL_SUBSEARCH);
827     $ret =array();
828     foreach($res as $attrs){
829       if(preg_match("/ou=apps,/",$attrs['dn'])){
830         $bb = preg_replace("/ou=apps,.*/","",$attrs['dn']);
831         $parts = array_reverse(split("ou=",$bb));
833         $str ="";
834         foreach($parts as $part){
835           if(empty($part)) {
836             continue;
837           }
838           $str .= str_replace(",","",$part)."/";
839         }    
840         $name = preg_replace("/\/$/","",$str);
841         if(empty($name)) {
842           $name ="/";
843         }
844         $ret[$attrs['dn']] = $name;
845       }
846     }
847     return($ret);
848   }
850   function save_object()
851   {
852     plugin::save_object();
853   }
854  
856   function remove_from_parent()
857   {
858     plugin::remove_from_parent();
860     $ldap= $this->config->get_ldap_link();
861     $ldap->cd($this->dn);
862     $this->cleanup();
863     
864     $ldap->modify ($this->attrs); 
866     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/applications with dn '%s' failed."),$this->dn));
868     /* Optionally execute a command after we're done */
869     $this->handle_post_events("remove");
870   }
873   /* Save to LDAP */
874   function save()
875   {
876     plugin::save();
878     /* Copy members */
879     $this->Categories[""]=""; 
880     $this->attrs["gosaMemberApplication"]= array();
881     $this->attrs["gosaApplicationParameter"]= array();
882     foreach($this->Categories as $name => $cats){
883       $i =0;
884       if(isset($this->gosaMemberApplication[$name])){
885         foreach($this->gosaMemberApplication[$name] as $entry){
886           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
887             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
888           }
889           $i ++;
890         }
891       }
892       if(($i==0)&&(!empty($name))){
893         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$i;
894       }
895     }
897     /* Are there application parameters to be saved */
898     $this->attrs['gosaApplicationParameter']= array();
899     foreach($this->appoption as $name => $value){
900       if ($value != ""){
901         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
902       }
903     }
905     /* Write back to LDAP */
906     $ldap= $this->config->get_ldap_link();
907     $ldap->cd($this->dn);
908     $this->cleanup();
909     $ldap->modify ($this->attrs); 
911     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/applications with dn '%s' failed."),$this->dn));
913     /* Optionally execute a command after we're done */
914     if ($this->initially_was_account == $this->is_account){
915       if ($this->is_modified){
916         $this->handle_post_events("mofify");
917       }
918     } else {
919       $this->handle_post_events("add");
920     }
922   }
924   function check()
925   {
926     /* Call common method to give check the hook */
927     $message= plugin::check();
929     return ($message);
930   }
933   function reload()
934   {
935     /* Generate applist */
936     $this->apps= array();
938     /* Special handling for release managed apps 
939      */
940     $tmp = search_config($this->config->data,"faiManagement","CLASS");
941     if(!empty($tmp)){
942       $this->enableReleaseManagement = true;
944       $tmp = array_flip($this->Releases);
945       if(isset($tmp[$this->FAIrelease])){
946         $base =  $tmp[$this->FAIrelease];
947       }else{
948         $k = key($tmp);
949         $r = $tmp[$k];
950         $this->FAIrelease = $k;
951         $base = $r; 
952     
953         print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$this->FAIrelease,$k));
954       }
956       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
957     
958       $ldap   = $this->config->get_ldap_link();
959       $ldap->cd ($base);
960       $ldap->ls ("(objectClass=gosaApplication)",$base);
961     }else{
962       $ldap   = $this->config->get_ldap_link();
963       $ldap->cd ("ou=apps,".$this->curbase);
964       $ldap->ls ("(objectClass=gosaApplication)","ou=apps,".$this->curbase);
965     }
966     
967     while ($attrs= $ldap->fetch()){
968       if (isset($attrs["description"][0])){    
969         $this->apps[$attrs["cn"][0]]=
970           $attrs["cn"][0]." (".
971           $attrs["description"][0].")";
972       } else {
973         $this->apps[$attrs["cn"][0]]=
974           $attrs["cn"][0];
975       }
976     }
978     $ldap->cd($this->config->current['BASE']);
979     $ldap->search("objectClass=gosaApplication",array("gosaApplicationParameter","cn"));
980     $tmp = search_config($this->config->data,"faiManagement","CLASS");
981     $this->AllAppsForRelease = array();
982     if(!empty($tmp)){
984       $tmp = array_flip($this->Releases);
986       while($attrs = $ldap->fetch()){
987         
988         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
989         $testdn = preg_replace("/^[^,]+/","",$testdn);
990         $testdn = preg_replace("/^,/","",$testdn);
992         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
993           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
994           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
995         }
996       }
997     }else{
998       while($attrs = $ldap->fetch()){
999         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1000       }
1001     }
1003     natcasesort ($this->apps);
1004     reset ($this->apps);
1006     if(is_array($this->gosaMemberApplication))
1007       foreach ($this->gosaMemberApplication as $cat){   
1008         if(is_array($cat))
1009           foreach($cat as $entry){
1010             $this->used_apps[$entry['App']]= $entry['App'];
1011           }
1012       }
1013   }
1016   function addApp($cn)
1017   {
1018     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
1019       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
1020         if($entry['App'] == $cn) return;
1021       }
1022     }
1023     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
1024     $this->used_apps[$cn]=$cn;
1025     $this->is_modified= TRUE;
1026   }
1029   function removeApp($cn)
1030   {
1031     $temp= array();
1032     foreach ($this->gosaMemberApplication as $value){
1033       if ($value != $cn){
1034         $temp[]= $value;
1035       }
1036     }
1037     $this->gosaMemberApplication= $temp;
1038     $this->is_modified= TRUE;
1039   }
1041   function getParameters($app)
1042   {
1043     $tmp = $this->getReleases();
1044     $ret = array();
1045     if(in_array($this->FAIrelease,$tmp)){
1046       $tmp2 = array_flip($tmp);
1047       $base = $tmp2[$this->FAIrelease];
1048       $ldap = $this->config->get_ldap_link();
1049       $ldap->cd($this->config->current['BASE']);
1050       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1051       if($ldap->count()){
1052         $attrs = $ldap->fetch();
1053         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1054           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1055           $ret[$name] = $name;
1056         } 
1057       }
1058     }
1059     return($ret);
1060   }
1062   function GetSubdirs($dir)
1063   {
1064     $ret = array();
1065     $tmp1 = split("/",$this->curCatDir);
1066  
1067     foreach($this->Categories as $path=>$cat){
1068       $tmp2 = split("/",$path);
1069       
1070       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1071         $abort = false;
1072       }elseif(((count($tmp1))+1) == (count($tmp2))){
1073         $abort = false;
1074         for($i = 0 ; $i < count($tmp1) ; $i++){
1075           if($tmp1[$i] != $tmp2[$i]){
1076             $abort = true;
1077           }
1078         }
1079       }else{
1080         $abort= true;
1081       }
1082       if(!$abort){
1083         $ret[$path]=$cat;
1084       } 
1085     }
1086     return($ret);
1087   }
1090   /* Return plugin informations for acl handling  */ 
1091   function plInfo()
1092   {
1093     return (array(
1094           "plShortName"   => _("Applications"),
1095           "plDescription" => _("Group applications"),
1096           "plSelfModify"  => FALSE,
1097           "plDepends"     => array(),
1098           "plPriority"    => 0,
1099           "plSection"     => array("administration"),
1100           "plCategory"    => array("groups"),
1101           "plProvidedAcls"=> array(
1102             "gosaMemberApplication"     => _("Application"),
1103             "gosaApplicationParameter"  => _("Application parameter"))
1104           ));
1105   }
1107 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1108 ?>