Code

12b80fab21da955b609519b1be3058b5c17b5132
[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;         //
25   var $Categories;
27   /* attribute list for save action */
28   var $attributes               = array("gosaMemberApplication","gosaApplicationParameter");
29   var $objectclasses            = array("gosaApplicationGroup");
31   var $FAIrelease               = "/";
32   var $InitialFAIrelease           = "/";
33   var $Releases                 = array();
34   var $enableReleaseManagement  = false;
36   var $AllAppsForRelease        = array();
37   var $AllAppsForReleaseParameter = array();
38   var $view_logged = FALSE;
39   var $CopyPasteVars            = array("Categories");
40   var $gosaApplicationParameter ;
41   var $ui                       = NULL;
42   var $no_release_acls          = false;
44   function appgroup (&$config, $dn= NULL, $parent= NULL)
45   {
46    
47     /* Check if we have relase mangement enabled and prepare group application for release management */ 
48     $tmp = $config->search("faiManagement", "CLASS",array('menu','tabs'));
49     if(!empty($tmp)){
50       $this->enableReleaseManagement = true;
51       $this->objectclasses  [] = "FAIreleaseTag";
52       $this->attributes     [] = "FAIrelease";
53     }
55     plugin::plugin ($config, $dn, $parent);
57     /* set userinfo object */
58     $this->ui = get_userinfo();
60     /* Compatibility check 
61         In some case of old applikations with old release tag saving, 
62           we must reassign is_account state.
63           (Or if release management wasn't activated before creating this app) */
64     if($this->enableReleaseManagement){
65   
66       /* Release management is activated && this is a valid group app account,
67           but no release class was found, so activate is_account flag  */
68       if(isset($this->attrs['objectClass'])){
69         if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
70             (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
71           $this->is_account =true;
72         }
73       }
74     }
75     
76     /* Load member applications */
77     if (isset ($this->attrs["gosaMemberApplication"][0])){
78       $this->gosaMemberApplication = array();
79       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
80         $this->gosaMemberApplication[]=
81           $this->attrs["gosaMemberApplication"][$i];
82       }
83     }
85     /* Load application options */
86     if (isset($this->attrs['gosaApplicationParameter'])){
87       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
88         $option= preg_replace('/^[^:]+:/', '',
89             $this->attrs['gosaApplicationParameter'][$i]);
90         $name= preg_replace('/:.*$/', '',
91             $this->attrs['gosaApplicationParameter'][$i]);
92         $this->appoption[$name]= $option;
93       }
94     }
96     /* Parse MemberApplication*/
97     $tmp    = array();
98     $tmp2   = array();
99     $prios  = array();
100     $cats   = array();
102     foreach($this->gosaMemberApplication as $memberApp){
103       if(preg_match("/\|/i",$memberApp)){
104     
105         $tmp = split("\|",$memberApp);
106  
107         if(!empty($tmp[0])){
108           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
109         }
110         if(!empty($tmp[1])){
111           $n = split("/",$tmp[1]);
112           $c = count($n);
113           $cats [$tmp[1]] = $n[$c-1];
114         }
115         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
116       }else{
117         $tmp2[]['App'] = $memberApp;
118       }
119     }
121     /* Assign collected category and application settings */
122     $this->Categories = $cats;
123     $this->gosaMemberApplication = $tmp2;
125     /* Sort application by category */
126     $cats[""]="";
127     foreach($cats as $cat ){
128       if((isset($prios[$cat]))&&(count($prios[$cat]))){
129         $max = max($prios[$cat]);
130         $min = 1;//min($prios[$cat]);  
131         $last = false;
132         for($i = $min ; $i < $max ; $i++){
133           if(!isset($prios[$cat][$i])){
134             if($last == false){
135               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
136             
137               $last = true;
138             }
139           }else{
140             $last = false;
141           }
142         }
143       }
144     } 
146     /* Sort application by priority */
147     $tmp = array();
148     foreach($this->gosaMemberApplication as $key =>  $entries){
149       ksort ($entries);
150       foreach($entries as $entry){
151         $tmp[$key][]= $entry;
152       }
153     }
154     $this->gosaMemberApplication = $tmp;
155     $this->curbase = $this->config->current['BASE'];
156     
157     /* Get required release informations */
158     if($this->enableReleaseManagement){
159       $this->InitialFAIrelease  = $this->FAIrelease;
160       $this->Releases           = $this->getReleases();
161     }
163   }
166   /* Combine new array */
167   function combineArrays($ar0,$ar1,$ar2)
168   {
169     $ret = array();
170     if(is_array($ar0))
171     foreach($ar0 as $ar => $a){
172         $ret[$ar]=$a;
173     }
174     if(is_array($ar1))
175     foreach($ar1 as $ar => $a){
176         $ret[$ar]=$a;
177     }
178     if(is_array($ar2))
179     foreach($ar2 as $ar => $a){
180         $ret[$ar]=$a;
181     }
182     return($ret);
183   }
185   /* Returns element position within given array */
186   function getpos($atr,$attrs)
187   {
188     $i = 0;
189     foreach($attrs as $attr => $name)    {
190       $i++;
191       if($attr == $atr){
192         return($i);
193       }
194     }
195     return(-1);
196   }
199   /* Check current combination of application && release 
200       Are all application available?, application parameter changed? ... */
201   function diffAppsInReleases()
202   {
203     /* Only diff if it is required  */
204     $ret =array();
206     /* If current release has changed */
207     if($this->FAIrelease != $this->InitialFAIrelease){
209       /* Walk through all apps which are currently displayed */
210       if(isset($this->gosaMemberApplication[$this->curCatDir])){
212         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
214           /* If application is also available in new release, check if parameter differs */
215           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
217             $old = array();   // Old application parameter
218             $new = array();   // New parameters 
220             /* There are possibly no parameters specified */
221             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
222               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
223             }
225             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
226               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
227             }
229             /*  Both (old & new) have no gosaApplicationParameter
230              */
231             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
232               $ret[$entry['App']] = false; 
234               /* Both got gosaApplicationParameter */
235             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
236               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
237                 $ret[$entry['App']]  = true;
238               }else{
239                 $ret[$entry['App']]  = false;
240               }
241             }
242             /* Initially had gosaApplicationParameter bot in new release not */
243           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
244             $ret[$entry['App']]  = true;
246             /* Old release had no gosaApplicationParameter but new got some */
247           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
248             $ret[$entry['App']]  = true;
249           }
250         }
251       }
252     }
253     return($ret);
254   }
256   /* TRansports the geiven Arraykey one position up*/
257   function ArrayUp($atr,$attrs)
258   {
259     $ret = $attrs;
260     $pos = $this->getpos($atr,$attrs) ;
261     $cn = count($attrs);
262     if(!(($pos == -1)||($pos == 1))){
263       $before = array_slice($attrs,0,($pos-2));
264       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
265       $unten  = array_slice($attrs,$pos);
266       $ret = array();
267       $ret = $this->combineArrays($before,$mitte,$unten);
268     }
269     return($ret);
270   }
273   /* TRansports the geiven Arraykey one position up*/
274   function ArrayDown($atr,$attrs)
275   {
276     $ret = $attrs;
277     $pos = $this->getpos($atr,$attrs) ;
278     $cn = count($attrs);
279     if(!(($pos == -1)||($pos == $cn))){
280       $before = array_slice($attrs,0,($pos-1));
281       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
282       $unten  = array_slice($attrs,($pos+1));
283       $ret = array();
284       $ret = $this->combineArrays($before,$mitte,$unten);
285     }
286     return($ret);
287   }
289     
290   /* Sort category on position up  */
291   function catUp($id)
292   {
293     /* Get all cats depinding on current dir */
294     $cats = $this->GetSubdirs($this->curCatDir);
295     $newcats =$this->ArrayUp($id,$cats);
296     foreach($newcats as $cat => $name){
297       unset($this->Categories[$cat]);
298     }
299     foreach($newcats as $cat => $name){
300       $this->Categories[$cat]=$name;
301     }
302   }
305   /* Sort category on position down */
306   function catDown($id)
307   {
308     /* Get all cats depinding on current dir */
309     $cats = $this->GetSubdirs($this->curCatDir);
310     $newcats =$this->ArrayDown($id,$cats);
311     foreach($newcats as $cat => $name){
312       unset($this->Categories[$cat]);
313     }
314     foreach($newcats as $cat => $name){
315       $this->Categories[$cat]=$name;
316     }
317   }
320   /* Increase application priority */
321   function getOneUp($appl)
322   { 
323     $cat  = $this->curCatDir;
324     $apps = $this->gosaMemberApplication[$cat];
325     $appsA = array();
326     foreach ($apps as $appkey => $name){
327       $appsA[$name['App']] =$name['App'];
328     }
329     $result = $this->ArrayUp($appl,$appsA);
330     $ret = array();
331     foreach($result as $app){
332       $ret[]=array("App"=>$app);
333     }
334     $this->gosaMemberApplication[$cat] = $ret;
335   }
338   /* Decrease application priority */
339   function getOneDown($appl)
340   {
341     $cat  = $this->curCatDir;
342     $apps = $this->gosaMemberApplication[$cat];
343     $appsA = array();
344     foreach ($apps as $appkey => $name){
345       $appsA[$name['App']] =$name['App'];
346     }
347     $result = $this->ArrayDown($appl,$appsA);
348     $ret = array();
349     foreach($result as $app){
350       $ret[]=array("App"=>$app);
351     }
352     $this->gosaMemberApplication[$cat] = $ret;
353   } 
356   /* Add seperator to application list */
357   function AddSeperator($id)
358   {
359     $found  = false;
360     $cat    = "";
361     $tmp = array();
362     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
363       $tmp[] = $app;    
364       if(($app['App'] == $id)&&(!$found)){
365         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
366         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
367         $found = true;
368       }
369     }
370     if($found){
371       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
372     }
373   }
376   /* Check if application acls are readable */
377   function check_acls()
378   {
379     return(count($this->ui->get_module_departments("application")));
380   }
383   function execute()
384   {
385     /* Call parent execute */
386     plugin::execute();
388     /* Log view */
389     if($this->is_account && !$this->view_logged){
390       $this->view_logged = TRUE;
391       new log("view","groups/".get_class($this),$this->dn);
392     }
394     /* Check if department was selected */ 
395     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
396       $dep = base64_decode($_GET['depid']); 
397       $this->curbase =$dep;
398     }
400     /* Check if category was selected */
401     if((isset($_GET['act']))&&($_GET['act']=="open") && (empty($_GET['id']) || isset($this->Categories[base64_decode($_GET['id'])]))){
402       $this->curCatDir = base64_decode($_GET['id']);
403     }
405     /* Do we need to flip is_account state? */
406     if(isset($_POST['modify_state'])){
407       if($this->is_account && $this->acl_is_removeable()){
408         $this->is_account= FALSE;
409       }elseif(!$this->is_account && $this->acl_is_createable()){
410         $this->is_account= TRUE;
411       }
412     }
414     /* Do we represent a valid group? */
415     if (!$this->is_account && $this->parent === NULL){
416       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
417         _("This 'dn' is no appgroup.")."</b>";
418       return ($display);
419     }
421     /* Show tab dialog headers */
422     $display= "";
423     if ($this->parent !== NULL){
424       if ($this->is_account){
425         $display= $this->show_disable_header(_("Remove applications"),
426             _("This group has application features enabled. You can disable them by clicking below."));
427       } else {
428         $display.= $this->show_enable_header(_("Create applications"),
429             _("This group has application features disabled. You can enable them by clicking below."));
430         return ($display);
431       }
432     }
434     /* Check acl, applications must be readable */
435     if(!$this->check_acls()){
436       $display .= "<img src='images/button_cancel.png' alt='"._("ACL")."' class='center'>
437                   <b>"._("Insufficient permissions")."</b>
438                   <p>".
439                   _("You do not have permission to query application entries. All your changes will not be saved.").
440                   "</p><p class='seperator'>&nbsp;</p>";
441       $this->no_release_acls = true;
442     }
443     
445     /* Add Categorie */ 
446     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
448       /* Only allow adding a category, if it is allowed */  
449       if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
451         if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
452           print_red(_("Invalid character in category name."));
453         }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
454           if(empty($this->curCatDir)){
455             $this->Categories[$_POST['CatName']]=$_POST['CatName'];
456           }else{
457             $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
458           }
459         }else{
460           print_red(_("The specified category already exists."));
461         }
462       }
463     }
466     $this->reload();
467     $this->diffAppsInReleases();
469     /* Check POST variables for commands 
470         to add/remove some applications */
471     $only_once = false;
472     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
474       /* Walk through posts */
475       foreach($_POST as $name => $value){
477         /* Add a seperator to current category */
478         if((preg_match("/AddSep_/",$name))&&(!$only_once)){
479           $only_once = true;
480           $n = preg_replace("/AddSep_/","",$name);
481           $val= preg_replace("/_.*$/","",$n);
482           $this->AddSeperator($val);
483         }
485         /* Delete application | seperator entry */
486         if((preg_match("/DelApp_/",$name))&&(!$only_once)){
487           $only_once = true;
489           if(preg_match("/DelApp___SEPARATOR__/",$name)) {
490             $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
491             $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
492           }else{
493             $n = preg_replace("/DelApp_/","",$name);
494             $val= preg_replace("/_.*$/","",$n);
495           }
497           foreach($this->gosaMemberApplication as $key =>  $cat){
498             foreach($cat as $key2 => $app){
499               if($app['App'] == $val){
500                 unset($this->gosaMemberApplication[$key][$key2]);
501                 if(isset($this->used_apps[$val])){
502                   foreach($this->getParameters($val) as $para){
503                     if(isset($this->appoption[$para])){
504                       unset($this->appoption[$para]);
505                     }
506                   }
507                   unset($this->used_apps[$val]);
508                 }
509               }
510             }
511           }
512         }
514         if(preg_match("/DelCat_/",$name) && !$only_once){
515           $only_once =TRUE;
516           $cat = preg_replace("/DelCat_/","",$name);
517           $cat = trim(base64_decode( preg_replace("/_.*$/","",$cat)));
519           $free_apps = array();
521           if(isset($this->gosaMemberApplication[$cat]) && is_array($this->gosaMemberApplication[$cat])){
522             foreach($this->gosaMemberApplication[$cat] as $app){
523               $free_apps[] = $app['App'];
524             }
525             unset($this->gosaMemberApplication[$cat]);
526             unset($this->Categories[$cat]);
527           }
528           foreach($this->Categories as $key => $name){
529             if(preg_match("/^".normalizePreg($cat)."\/.*/",$key)){
530               foreach($this->gosaMemberApplication[$key] as $app){
531                 $free_apps[] = $app['App'];
532               }
533               unset($this->gosaMemberApplication[$key]);
534               unset($this->Categories[$key]);
535             }
536           }
537           foreach($free_apps as $app){
538             if(isset($this->used_apps[$app])){
539               unset($this->used_apps[$app]);
540             }
541           }
542         }
544         /* Edit application parameter */
545         if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
547           $only_once = true;
548           $appname = $value;
549           $appname = preg_replace("/EdiApp_/","",$name);  
550           $appname = preg_replace("/_.*$/","",$appname);
552           /* We've got the appname, get parameters from ldap */
553           $ldap= $this->config->get_ldap_link();
555           /* Check if we have release management enabled */
556           $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
557           if(!empty($tmp)){
558     
559             /* Get application parameter from ldap */
560             $tmp = array_flip($this->Releases);
561             $base = $tmp[$this->FAIrelease];
562             $ldap->cd($base);
563             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
564             $found = "";
565             while($attrs = $ldap->fetch()) {
566               if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
567                 $found = $attrs['dn'];
568               }
569             }
570             $ldap->cat($found, array("gosaApplicationParameter"));
571           }else{
573             /* Get application parameter from ldap */
574             $ldap->cd($this->config->current['BASE']);
575             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
576           }
578           /* Check if this application is unique */
579           if ($ldap->count() != 1){
580             print_red (_("The selected application name is not uniq. Please check your LDAP."));
581           } else {
583             /* Get parameter */
584             $attrs= $ldap->fetch();
585             if(isset($attrs['gosaApplicationParameter'])){
586               $this->dialog= TRUE;
588               /* Fill name and value arrays */
589               for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
590                 $option= preg_replace('/^[^:]+:/', '',
591                     $attrs['gosaApplicationParameter'][$i]);
592                 $name= preg_replace('/:.*$/', '', 
593                     $attrs['gosaApplicationParameter'][$i]);
594                 $this->option_name[$i]= $name;
596                 /* Fill with values from application, default should be
597                    loaded by the external scripts */
598                 if (isset($this->appoption[$name])){
599                   $this->option_value[$i]= $this->appoption[$name];
600                 }
601               }
603               /* Create edit field */
604               $table= "<table summary=\"\">";
605               for ($i= 0; $i < count($this->option_name); $i++){
606                 if (isset($this->option_value[$i])){
607                   $value= $this->option_value[$i];
608                 } else {
609                   $value= "";
610                 }
611                 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
612                   "<input name=\"value$i\" size=60 maxlength=250 ".
613                   "value=\"".$value."\"><br></td></tr>";
614               }
615               $table.= "</table>";
616               $this->table= $table;
617             } else {
618               print_red (_("The selected application has no options."));
619             }
620           }
621         }
622       }
623     }
625     /* Add multiple */
626     if(isset($_POST['AddApps'])){
627       foreach($_POST as $name => $value){
628         if(preg_match("/AddApp_/",$name)){
629           $app = preg_replace("/AddApp_/","",$name);
630           $this->addApp($app);
631         }
632       }
633     }
635     /* Add application with post */
636     if((isset($_GET['act']))&&($_GET['act']=="add")){
637       $this->used_apps[$_GET['id']]= $_GET['id'];
638       asort($this->used_apps);
639       $this->addApp($_GET['id']);
640     }
642     /* Cancel edit options? */
643     if (isset($_POST['edit_options_cancel'])){
644       $this->dialog= FALSE;
645     }
647     /* Finish edit options? */
648     if (isset($_POST['edit_options_finish'])){
649       $this->dialog= FALSE;
651       /* Save informations passed by the user */
652       $this->option_value= array();
653       for ($i= 0; $i<count($this->option_name); $i++){
654         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
655         $this->is_modified= TRUE;
656       }
657     }
659     /* Prepare templating stuff */
660     $smarty= get_smarty();
661     $smarty->assign("used_apps", $this->used_apps);
662     $apps= array();
663     foreach ($this->apps as $key => $value){
664       if (!array_key_exists($key, $this->used_apps)){
665         $apps["$key"]= "$value";
666       }
667     }
670     /* Create application list */
671     $div = new divSelectBox("appgroup");    
672     $div->SetHeight(300);
673     $departments = array();
674     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
675     foreach($res as $value){
676       $fdn = $value['dn'];
677       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
678       $fdn= @LDAP::fix($fdn);
679       if($value["description"][0]!=".."){
680         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
681       }else{
682         $departments[$value['dn']]=convert_department_dn($fdn)." ["._("Back")."]";
683       }
684     }
686     /* Create 'open' and 'add' links */
687     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
688       $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
689     }else{
690       $linkadd = "%s";
691     }
692     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
694     /* Create base back entry */
695     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
696     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
697       $div->AddEntry(array(
698             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
699               "attach"=>"style='border:0px;'")
700             ));
701     }
703     /* Append departments for current base */
704     foreach($departments as $key => $app){
705       $div->AddEntry(array(
706             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
707               "attach"=>"style='border:0px;'")
708             ));
709     }
711     /* Add applications found on this base */
712     foreach($apps as $key => $app){
713       $div->AddEntry(array(
714             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
715               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
716               "attach"=>"style='border:0px;'")
717             ));
718     }
720     /* Create list of used/assigned applications */
721     $div2 = new divSelectBox("appgroup");
722     $div2->SetHeight(300);
724     /* Check acls to create "edit/remove" category links */
725     if(!$this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
726       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
727       $catremove      = "&nbsp;<img src='images/empty.png' alt='&nbsp;'>";
728       $catupdown      = "";
729     }else{
730       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
731       $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
732       $catupdown      = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>".
733         "<img align='top' alt=\"\" src='images/sort_up.png' border=0 title='"._("Move up")."'>".
734         "</a>&nbsp;".
735         "<a href='?plug=".$_GET['plug']."&amp;act=cat_down&amp;id=%s'>".
736         "<img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0>".
737         "</a>";
738     }
740     /* Add back category for current category */
741     if(empty($this->curCatDir)){
742       $cnt =0;
743     }else{
744       $cnt = count(split("/",$this->curCatDir));
745       $tmp = split("/",$this->curCatDir);
746       $bbk = "";
747       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
748         $bbk .= $tmp[$i]."/";
749       }
750       $bbk = preg_replace("/\/$/","",$bbk);
751       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),".. ["._("Back")."]")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
752     }
754     /* Add sub categories */ 
755     $this->GetSubdirs($this->curCatDir);
756     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
757       $div2 ->AddEntry(array( 
758             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
759             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
760               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
761     }
763     /* Create priority & seperator links */
764     $separator ="<hr size=1>"; 
765     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
766       $sep      = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
767       $upudown  = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&amp;id=%s'>".
768                   " <img alt='{t}sort{/t}' align='top' src='images/sort_up.png' title='"._("Move up")."' border=0>".
769                   "</a>&nbsp;".
770                   "<a href='?plug=".$_GET['plug']."&amp;act=one_down&amp;id=%s'>".
771                   " <img alt='{t}sort{/t}' src='images/sort_down.png' title='"._("Move down")."' border=0>".
772                   "</a>&nbsp;".
773                   "<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
774     }else{
775       $sep      = "";
776       $upudown  = "";
777     }
778      
779     /* Create edit link */ 
780     if($this->acl_is_writeable("gosaApplicationParameter",$this->no_release_acls)){
781       $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
782     }else{
783       $edit=      "";
784     }
786     /* Get differences to mark those entries that have changed from last edit. */
787     $Differences = $this->diffAppsInReleases();
789     /* Walk through entries an append them to list */
790     if(isset($this->gosaMemberApplication[$this->curCatDir])){
791       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
793         /* Add seperator */ 
794         if(preg_match("/__SEPARATOR__/",$entry['App'])){
795           $div2 ->AddEntry(array(array("string"=>$separator),
796                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
797         }else{
799           /* Add application */
801           $image = "";
802           /* Check if application was available within selected release
803            *  or application list if release management is not active
804            */
805           if(!in_array($entry['App'],$this->AllAppsForRelease)){
807             /*  release managent is active
808              */
809             if(!$this->enableReleaseManagement){
810               $image = "<img class='center' alt='R' src='images/select_invalid_application.png' 
811                           title='"._("This application is no longer available.")."'>&nbsp;";
812             }else{
813               $image = "<img class='center' alt='F' src='images/select_invalid_application.png' 
814                           title=\"".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."\">&nbsp;";
815             }
816       
817           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
818             $entry['App'].=" <i>["._("Check parameter")."]</i>";
819               $image = "<img class='center' src='images/select_invalid_application.png' 
820                           title='"._("This application has changed parameters.")."'>&nbsp;";
821           }else{
822             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
823           }
824           
825           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
826                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
827                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
828         }
829       }
830     }
832     /* Assign created div lists to template */
833     $smarty->assign("UsedApps", $div2->DrawList());
834     $smarty->assign("List", $div->DrawList());
835     $smarty->assign("apps", $apps);
836    
837     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
839     $smarty->assign("ReleaseSelectAble", false);
840     $smarty->assign("Release", $this->FAIrelease);
841  
842     if($this->enableReleaseManagement){
843       $smarty->assign("FAIrelease",  $this->FAIrelease);
844       $smarty->assign("Releases", $this->Releases);
846       if(!$this->no_release_acls && count($this->Releases)){
847         $smarty->assign("ReleaseSelectAble", true);
848       }
849     }
850   
851     /* Set acls to  template */
852     $tmp = $this->plInfo();
853     foreach($tmp['plProvidedAcls'] as $acl => $translation){
854       $smarty->assign($acl."ACL",$this->getacl($acl,$this->no_release_acls));
855     }
857     /* Show main page */
858     if (is_object($this->dialog)){
859       $smarty->assign("table", $this->table);
860       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE,dirname(__FILE__)));
861     } else {
862       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE,dirname(__FILE__)));
863     }
864     
865     return ($display);
866   }
869   function getReleases()
870   {
871     /* Only display those releases that we are able to read */
872     $dn     = $this->config->current['BASE'];
873     $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
874     $res    = get_list($filter,"application", $dn, array("ou"), GL_SUBSEARCH);
876     $ret =array();
877     foreach($res as $attrs){
878       if(preg_match("/ou=apps,/",$attrs['dn'])){
879         $bb     = preg_replace("/ou=apps,.*/","",$attrs['dn']);
880         $parts  = array_reverse(split("ou=",$bb));
882         $str ="";
883         foreach($parts as $part){
884           if(empty($part)) {
885             continue;
886           }
887           $str .= str_replace(",","",$part)."/";
888         }    
889         $name = preg_replace("/\/$/","",$str);
890         if(empty($name)) {
891           $name ="/";
892         }
893         $ret[$attrs['dn']] = $name;
894       }
895     }
896     return($ret);
897   }
899   function save_object()
900   {
902     /* Move category one position up or down */
903     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
904       if($_GET['act']=="cat_up"){
905         $this->catUp(base64_decode($_GET['id']));
906       }
907       if($_GET['act']=="cat_down"){
908         $this->catDown(base64_decode($_GET['id']));
909       }
910     }
912     /* Move application one position up or down */
913     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
914       if(isset($_GET['id'])){
915         $id   = $_GET['id'];
916         $act  = $_GET['act']; 
918         if($act == "one_up"){
919           $this->getOneUp($id);
920         }elseif($act == "one_down")   { 
921           $this->getOneDown($id);
922         }
923       }
924     }
927     plugin::save_object();
928   }
929  
931   function remove_from_parent()
932   {
933     if(!$this->initially_was_account){
934       return;
935     }
937     plugin::remove_from_parent();
939     $ldap= $this->config->get_ldap_link();
940     $ldap->cd($this->dn);
941     $this->cleanup();
942     
943     $ldap->modify ($this->attrs); 
944     new log("remove","group/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
945     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/applications with dn '%s' failed."),$this->dn));
947     /* Optionally execute a command after we're done */
948     $this->handle_post_events("remove");
949   }
952   /* Save to LDAP */
953   function save()
954   {
955     /* Skip saving application settings, if we do not have release acls */
956     if($this->no_release_acls){
957       new log("security","groups/".get_class($this),$this->dn,array(),"Skip saving group application extension. Caused by insufficient acls");
958       
959       return;
960     }
962     plugin::save();
966     /* Copy members */
967     $this->Categories[""]=""; 
968     $this->attrs["gosaMemberApplication"]= array();
969     $this->attrs["gosaApplicationParameter"]= array();
970     $cat_id= 0 ;
971     foreach($this->Categories as $name => $cats){
972       $i =0;
973       if(isset($this->gosaMemberApplication[$name])){
974         foreach($this->gosaMemberApplication[$name] as $entry){
975           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
976             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
977             $i ++;
978           }
979         }
980       }
981       if(($i==0)&&(!empty($name))){
982         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$cat_id;
983       }
984       $cat_id++;
985     }
989     /* Are there application parameters to be saved */
990     $this->attrs['gosaApplicationParameter']= array();
991     foreach($this->appoption as $name => $value){
992       if ($value != ""){
993         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
994       }
995     }
997     /* Write back to LDAP */
998     $ldap= $this->config->get_ldap_link();
999     $ldap->cd($this->dn);
1000     $this->cleanup();
1001     $ldap->modify ($this->attrs); 
1003     if($this->initially_was_account){
1004       new log("modify","group/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
1005     }else{
1006       new log("create","group/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error()); 
1007     }   
1009     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/applications with dn '%s' failed."),$this->dn));
1011     /* Optionally execute a command after we're done */
1012     if ($this->initially_was_account == $this->is_account){
1013       if ($this->is_modified){
1014         $this->handle_post_events("modify");
1015       }
1016     } else {
1017       $this->handle_post_events("add");
1018     }
1020   }
1022   function check()
1023   {
1024     /* Call common method to give check the hook */
1025     $message= plugin::check();
1027     return ($message);
1028   }
1031   function reload()
1032   {
1033     /* Generate applist */
1034     $this->apps= array();
1036     /* Special handling for release managed apps */
1037     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
1038     if(!empty($tmp) && count($this->Releases)){
1039       $this->enableReleaseManagement = true;
1041       /* Check if release is available */
1042       $tmp = array_flip($this->Releases);
1043       if(isset($tmp[$this->FAIrelease])){
1044         $base =  $tmp[$this->FAIrelease];
1045       }else{
1047         /* Release is not available, check if there is an alternative */
1048         $old_r =  $this->FAIrelease;
1049         $k = key($tmp);
1051         if(isset($tmp[$k])){
1052       
1053           /* We have found an alternative release name */
1054           $r = $tmp[$k];
1055           $this->FAIrelease = $k;
1056           $base = $r; 
1058           /* Display msg */
1059           if(!(!$this->initially_was_account && $old_r =="/")){
1060             print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$old_r,$k));   
1061           }
1062         }else{
1063           
1064           /* There are no releases available ... */
1065           print_red(_("There are no releases available. You will not be able to select another release."));
1066           return;
1067         }
1068       }
1070       /* Get applications for this release */
1071       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
1072       $res = get_list("(objectClass=gosaApplication)","application",$base,array("*"));
1073     }else{
1074     
1075       /* Get applications for this base */
1076       $res = get_list("(objectClass=gosaApplication)","application","ou=apps,".$this->curbase,array("*"));
1077     }
1078     
1079     /* Append applications */
1080     foreach($res as $attrs){
1081       if (isset($attrs["description"][0])){    
1082         $this->apps[$attrs["cn"][0]]=
1083           $attrs["cn"][0]." (".
1084           $attrs["description"][0].")";
1085       } else {
1086         $this->apps[$attrs["cn"][0]]=
1087           $attrs["cn"][0];
1088       }
1089     }
1091     /* Get all apps ... */
1092     $res = get_list("objectClass=gosaApplication","application",$this->config->current['BASE'],array("gosaApplicationParameter","cn"),GL_SUBSEARCH); 
1093     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
1094     $this->AllAppsForRelease = array();
1095     if(!empty($tmp)){
1096       $tmp = array_flip($this->Releases);
1097       foreach($res as $attrs){
1098         
1099         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
1100         $testdn = preg_replace("/^[^,]+/","",$testdn);
1101         $testdn = preg_replace("/^,/","",$testdn);
1103         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
1104           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1105           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
1106         }
1107       }
1108     }else{
1109       foreach($res as $attrs){
1110         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1111       }
1112     }
1114     natcasesort ($this->apps);
1115     reset ($this->apps);
1117     if(is_array($this->gosaMemberApplication))
1118       foreach ($this->gosaMemberApplication as $cat){   
1119         if(is_array($cat))
1120           foreach($cat as $entry){
1121             $this->used_apps[$entry['App']]= $entry['App'];
1122           }
1123       }
1124   }
1127   function addApp($cn)
1128   {
1129     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
1130       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
1131         if($entry['App'] == $cn) return;
1132       }
1133     }
1134     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
1135     $this->used_apps[$cn]=$cn;
1136     $this->is_modified= TRUE;
1137   }
1140   function removeApp($cn)
1141   {
1142     $temp= array();
1143     foreach ($this->gosaMemberApplication as $value){
1144       if ($value != $cn){
1145         $temp[]= $value;
1146       }
1147     }
1148     $this->gosaMemberApplication= $temp;
1149     $this->is_modified= TRUE;
1150   }
1152   function getParameters($app)
1153   {
1154     $tmp = $this->getReleases();
1155     $ret = array();
1156     if(in_array($this->FAIrelease,$tmp)){
1157       $tmp2 = array_flip($tmp);
1158       $base = $tmp2[$this->FAIrelease];
1159       $ldap = $this->config->get_ldap_link();
1160       $ldap->cd($this->config->current['BASE']);
1161       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1162       if($ldap->count()){
1163         $attrs = $ldap->fetch();
1164         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1165           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1166           $ret[$name] = $name;
1167         } 
1168       }
1169     }
1170     return($ret);
1171   }
1173   function GetSubdirs($dir)
1174   {
1175     $ret = array();
1176     $tmp1 = split("/",$this->curCatDir);
1177  
1178     foreach($this->Categories as $path=>$cat){
1179       $tmp2 = split("/",$path);
1180       
1181       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1182         $abort = false;
1183       }elseif(((count($tmp1))+1) == (count($tmp2))){
1184         $abort = false;
1185         for($i = 0 ; $i < count($tmp1) ; $i++){
1186           if($tmp1[$i] != $tmp2[$i]){
1187             $abort = true;
1188           }
1189         }
1190       }else{
1191         $abort= true;
1192       }
1193       if(!$abort){
1194         $ret[$path]=$cat;
1195       } 
1196     }
1197     return($ret);
1198   }
1201   function PrepareForCopyPaste($source)
1202   {
1203     $s = new appgroup($this->config,$source['dn']);
1204     $this->FAIrelease = $this->InitialFAIrelease = $s->FAIrelease;
1205     $this->gosaMemberApplication = $s->gosaMemberApplication;
1206     $this->appoption = $s->appoption;
1207     $this->Categories = $s->Categories;
1208   }
1211   /* Return plugin informations for acl handling  */ 
1212   static function plInfo()
1213   {
1214     return (array(
1215           "plShortName"   => _("Applications"),
1216           "plDescription" => _("Group applications"),
1217           "plSelfModify"  => FALSE,
1218           "plDepends"     => array(),
1219           "plPriority"    => 0,
1220           "plSection"     => array("admin"),
1221           "plCategory"    => array("groups"),
1222           "plProvidedAcls"=> array(
1223             "gosaMemberApplication"     => _("Application"),
1224             "FAIrelease"                => _("Release"),
1225             "gosaApplicationParameter"  => _("Application parameter"))
1226           ));
1227   }
1229 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1230 ?>