Code

10e3294e622b751016e85fe0ad611eb69e79be3d
[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();
37   var $view_logged = FALSE;
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     }
160   }
163   /* Combine new array */
164   function combineArrays($ar0,$ar1,$ar2)
165   {
166     $ret = array();
167     if(is_array($ar0))
168     foreach($ar0 as $ar => $a){
169         $ret[$ar]=$a;
170     }
171     if(is_array($ar1))
172     foreach($ar1 as $ar => $a){
173         $ret[$ar]=$a;
174     }
175     if(is_array($ar2))
176     foreach($ar2 as $ar => $a){
177         $ret[$ar]=$a;
178     }
179     return($ret);
180   }
182   /* Returns element position within given array */
183   function getpos($atr,$attrs)
184   {
185     $i = 0;
186     foreach($attrs as $attr => $name)    {
187       $i++;
188       if($attr == $atr){
189         return($i);
190       }
191     }
192     return(-1);
193   }
196   /* Check current combination of application && release 
197       Are all application available?, application parameter changed? ... */
198   function diffAppsInReleases()
199   {
200     /* Only diff if it is required  */
201     $ret =array();
203     /* If current release has changed */
204     if($this->FAIrelease != $this->InitialFAIrelease){
206       /* Walk through all apps which are currently displayed */
207       if(isset($this->gosaMemberApplication[$this->curCatDir])){
209         foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
211           /* If application is also available in new release, check if parameter differs */
212           if(in_array($entry['App'],$this->AllAppsForRelease)){ 
214             $old = array();   // Old application parameter
215             $new = array();   // New parameters 
217             /* There are possibly no parameters specified */
218             if(isset($this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']])){
219               $old = $this->AllAppsForReleaseParameter[$this->InitialFAIrelease][$entry['App']];
220             }
222             if(isset($this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']])){
223               $new = $this->AllAppsForReleaseParameter[$this->FAIrelease][$entry['App']];
224             }
226             /*  Both (old & new) have no gosaApplicationParameter
227              */
228             if((!isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
229               $ret[$entry['App']] = false; 
231               /* Both got gosaApplicationParameter */
232             }elseif((isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
233               if(array_differs($old['gosaApplicationParameter'],$new['gosaApplicationParameter'])){
234                 $ret[$entry['App']]  = true;
235               }else{
236                 $ret[$entry['App']]  = false;
237               }
238             }
239             /* Initially had gosaApplicationParameter bot in new release not */
240           }elseif((isset($old['gosaApplicationParameter'])) && (!isset($new['gosaApplicationParameter']))){
241             $ret[$entry['App']]  = true;
243             /* Old release had no gosaApplicationParameter but new got some */
244           }elseif((!isset($old['gosaApplicationParameter'])) && (isset($new['gosaApplicationParameter']))){
245             $ret[$entry['App']]  = true;
246           }
247         }
248       }
249     }
250     return($ret);
251   }
253   /* TRansports the geiven Arraykey one position up*/
254   function ArrayUp($atr,$attrs)
255   {
256     $ret = $attrs;
257     $pos = $this->getpos($atr,$attrs) ;
258     $cn = count($attrs);
259     if(!(($pos == -1)||($pos == 1))){
260       $before = array_slice($attrs,0,($pos-2));
261       $mitte  = array_reverse(array_slice($attrs,($pos-2),2));
262       $unten  = array_slice($attrs,$pos);
263       $ret = array();
264       $ret = $this->combineArrays($before,$mitte,$unten);
265     }
266     return($ret);
267   }
270   /* TRansports the geiven Arraykey one position up*/
271   function ArrayDown($atr,$attrs)
272   {
273     $ret = $attrs;
274     $pos = $this->getpos($atr,$attrs) ;
275     $cn = count($attrs);
276     if(!(($pos == -1)||($pos == $cn))){
277       $before = array_slice($attrs,0,($pos-1));
278       $mitte  = array_reverse(array_slice($attrs,($pos-1),2));
279       $unten  = array_slice($attrs,($pos+1));
280       $ret = array();
281       $ret = $this->combineArrays($before,$mitte,$unten);
282     }
283     return($ret);
284   }
286     
287   /* Sort category on position up  */
288   function catUp($id)
289   {
290     /* Get all cats depinding on current dir */
291     $cats = $this->GetSubdirs($this->curCatDir);
292     $newcats =$this->ArrayUp($id,$cats);
293     foreach($newcats as $cat => $name){
294       unset($this->Categories[$cat]);
295     }
296     foreach($newcats as $cat => $name){
297       $this->Categories[$cat]=$name;
298     }
299   }
302   /* Sort category on position down */
303   function catDown($id)
304   {
305     /* Get all cats depinding on current dir */
306     $cats = $this->GetSubdirs($this->curCatDir);
307     $newcats =$this->ArrayDown($id,$cats);
308     foreach($newcats as $cat => $name){
309       unset($this->Categories[$cat]);
310     }
311     foreach($newcats as $cat => $name){
312       $this->Categories[$cat]=$name;
313     }
314   }
317   /* Increase application priority */
318   function getOneUp($appl)
319   { 
320     $cat  = $this->curCatDir;
321     $apps = $this->gosaMemberApplication[$cat];
322     $appsA = array();
323     foreach ($apps as $appkey => $name){
324       $appsA[$name['App']] =$name['App'];
325     }
326     $result = $this->ArrayUp($appl,$appsA);
327     $ret = array();
328     foreach($result as $app){
329       $ret[]=array("App"=>$app);
330     }
331     $this->gosaMemberApplication[$cat] = $ret;
332   }
335   /* Decrease application priority */
336   function getOneDown($appl)
337   {
338     $cat  = $this->curCatDir;
339     $apps = $this->gosaMemberApplication[$cat];
340     $appsA = array();
341     foreach ($apps as $appkey => $name){
342       $appsA[$name['App']] =$name['App'];
343     }
344     $result = $this->ArrayDown($appl,$appsA);
345     $ret = array();
346     foreach($result as $app){
347       $ret[]=array("App"=>$app);
348     }
349     $this->gosaMemberApplication[$cat] = $ret;
350   } 
353   /* Add seperator to application list */
354   function AddSeperator($id)
355   {
356     $found  = false;
357     $cat    = "";
358     $tmp = array();
359     foreach($this->gosaMemberApplication[$this->curCatDir] as $appID => $app){  
360       $tmp[] = $app;    
361       if(($app['App'] == $id)&&(!$found)){
362         $cnt = count($this->gosaMemberApplication[$this->curCatDir]);
363         $tmp[] = array("App" => "__SEPARATOR__".($cnt+1));
364         $found = true;
365       }
366     }
367     if($found){
368       $this->gosaMemberApplication[$this->curCatDir]=$tmp;
369     }
370   }
373   /* Check if application acls are readable */
374   function check_acls()
375   {
376     return(count($this->ui->get_module_departments("application")));
377   }
380   function execute()
381   {
382     /* Call parent execute */
383     plugin::execute();
385     /* Log view */
386     if($this->is_account && !$this->view_logged){
387       $this->view_logged = TRUE;
388       new log("view","groups/".get_class($this),$this->dn);
389     }
391     /* Check if department was selected */ 
392     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
393       $dep = base64_decode($_GET['depid']); 
394       $this->curbase =$dep;
395     }
397     /* Check if category was selected */
398     if((isset($_GET['act']))&&($_GET['act']=="open") && (empty($_GET['id']) || isset($this->Categories[base64_decode($_GET['id'])]))){
399       $this->curCatDir = base64_decode($_GET['id']);
400     }
402     /* Do we need to flip is_account state? */
403     if(isset($_POST['modify_state'])){
404       if($this->is_account && $this->acl_is_removeable()){
405         $this->is_account= FALSE;
406       }elseif(!$this->is_account && $this->acl_is_createable()){
407         $this->is_account= TRUE;
408       }
409     }
411     /* Do we represent a valid group? */
412     if (!$this->is_account && $this->parent == NULL){
413       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
414         _("This 'dn' is no appgroup.")."</b>";
415       return ($display);
416     }
418     /* Show tab dialog headers */
419     $display= "";
420     if ($this->parent != NULL){
421       if ($this->is_account){
422         $display= $this->show_disable_header(_("Remove applications"),
423             _("This group has application features enabled. You can disable them by clicking below."));
424       } else {
425         $display.= $this->show_enable_header(_("Create applications"),
426             _("This group has application features disabled. You can enable them by clicking below."));
427         return ($display);
428       }
429     }
431     /* Check acl, applications must be readable */
432     if(!$this->check_acls()){
433       $display .= "<img src='images/button_cancel.png' alt='"._("ACL")."' class='center'>
434                   <b>"._("Insufficient permissions")."</b>
435                   <p>".
436                   _("You do not have permission to query application entries. All your changes will not be saved.").
437                   "</p><p class='seperator'>&nbsp;</p>";
438       $this->no_release_acls = true;
439     }
440     
442     /* Add Categorie */ 
443     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
445       /* Only allow adding a category, if it is allowed */  
446       if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
448         if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
449           print_red(_("Invalid character in category name."));
450         }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
451           if(empty($this->curCatDir)){
452             $this->Categories[$_POST['CatName']]=$_POST['CatName'];
453           }else{
454             $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
455           }
456         }else{
457           print_red(_("The specified category already exists."));
458         }
459       }
460     }
463     $this->reload();
464     $this->diffAppsInReleases();
466     /* Check POST variables for commands 
467         to add/remove some applications */
468     $only_once = false;
469     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
471       /* Walk through posts */
472       foreach($_POST as $name => $value){
474         /* Add a seperator to current category */
475         if((preg_match("/AddSep_/",$name))&&(!$only_once)){
476           $only_once = true;
477           $n = preg_replace("/AddSep_/","",$name);
478           $val= preg_replace("/_.*$/","",$n);
479           $this->AddSeperator($val);
480         }
482         /* Delete application | seperator entry */
483         if((preg_match("/DelApp_/",$name))&&(!$only_once)){
484           $only_once = true;
486           if(preg_match("/DelApp___SEPARATOR__/",$name)) {
487             $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
488             $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
489           }else{
490             $n = preg_replace("/DelApp_/","",$name);
491             $val= preg_replace("/_.*$/","",$n);
492           }
494           foreach($this->gosaMemberApplication as $key =>  $cat){
495             foreach($cat as $key2 => $app){
496               if($app['App'] == $val){
497                 unset($this->gosaMemberApplication[$key][$key2]);
498                 if(isset($this->used_apps[$val])){
499                   foreach($this->getParameters($val) as $para){
500                     if(isset($this->appoption[$para])){
501                       unset($this->appoption[$para]);
502                     }
503                   }
504                   unset($this->used_apps[$val]);
505                 }
506               }
507             }
508           }
509         }
511         /* Remove category entry */
512         if(preg_match("/DelCat_/",$name)){
513           $n = preg_replace("/DelCat_/","",$name);
514           $app = base64_decode( preg_replace("/_.*$/","",$n));
515           foreach($this->Categories as $key =>  $cat){
516             if($cat == $app){
517               foreach($this->Categories as $p => $n){
518                 if(preg_match("/^".$key."\/.*/",$p)){
519                   unset($this->Categories[$p]);    
520                 }
521               }
522               unset($this->Categories[$key]);
523             }
524           }
525         }
527         /* Edit application parameter */
528         if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
530           $only_once = true;
531           $appname = $value;
532           $appname = preg_replace("/EdiApp_/","",$name);  
533           $appname = preg_replace("/_.*$/","",$appname);
535           /* We've got the appname, get parameters from ldap */
536           $ldap= $this->config->get_ldap_link();
538           /* Check if we have release management enabled */
539           $tmp = search_config($this->config->data,"faiManagement","CLASS");
540           if(!empty($tmp)){
541     
542             /* Get application parameter from ldap */
543             $tmp = array_flip($this->Releases);
544             $base = $tmp[$this->FAIrelease];
545             $ldap->cd($base);
546             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
547             $found = "";
548             while($attrs = $ldap->fetch()) {
549               if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
550                 $found = $attrs['dn'];
551               }
552             }
553             $ldap->cat($found, array("gosaApplicationParameter"));
554           }else{
556             /* Get application parameter from ldap */
557             $ldap->cd($this->config->current['BASE']);
558             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
559           }
561           /* Check if this application is unique */
562           if ($ldap->count() != 1){
563             print_red (_("The selected application name is not uniq. Please check your LDAP."));
564           } else {
566             /* Get parameter */
567             $attrs= $ldap->fetch();
568             if(isset($attrs['gosaApplicationParameter'])){
569               $this->dialog= TRUE;
571               /* Fill name and value arrays */
572               for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
573                 $option= preg_replace('/^[^:]+:/', '',
574                     $attrs['gosaApplicationParameter'][$i]);
575                 $name= preg_replace('/:.*$/', '', 
576                     $attrs['gosaApplicationParameter'][$i]);
577                 $this->option_name[$i]= $name;
579                 /* Fill with values from application, default should be
580                    loaded by the external scripts */
581                 if (isset($this->appoption[$name])){
582                   $this->option_value[$i]= $this->appoption[$name];
583                 }
584               }
586               /* Create edit field */
587               $table= "<table summary=\"\">";
588               for ($i= 0; $i < count($this->option_name); $i++){
589                 if (isset($this->option_value[$i])){
590                   $value= $this->option_value[$i];
591                 } else {
592                   $value= "";
593                 }
594                 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
595                   "<input name=\"value$i\" size=60 maxlength=250 ".
596                   "value=\"".$value."\"><br></td></tr>";
597               }
598               $table.= "</table>";
599               $this->table= $table;
600             } else {
601               print_red (_("The selected application has no options."));
602             }
603           }
604         }
605       }
606     }
608     /* Add multiple */
609     if(isset($_POST['AddApps'])){
610       foreach($_POST as $name => $value){
611         if(preg_match("/AddApp_/",$name)){
612           $app = preg_replace("/AddApp_/","",$name);
613           $this->addApp($app);
614         }
615       }
616     }
618     /* Add application with post */
619     if((isset($_GET['act']))&&($_GET['act']=="add")){
620       $this->used_apps[$_GET['id']]= $_GET['id'];
621       asort($this->used_apps);
622       $this->addApp($_GET['id']);
623     }
625     /* Cancel edit options? */
626     if (isset($_POST['edit_options_cancel'])){
627       $this->dialog= FALSE;
628     }
630     /* Finish edit options? */
631     if (isset($_POST['edit_options_finish'])){
632       $this->dialog= FALSE;
634       /* Save informations passed by the user */
635       $this->option_value= array();
636       for ($i= 0; $i<count($this->option_name); $i++){
637         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
638         $this->is_modified= TRUE;
639       }
640     }
642     /* Prepare templating stuff */
643     $smarty= get_smarty();
644     $smarty->assign("used_apps", $this->used_apps);
645     $apps= array();
646     foreach ($this->apps as $key => $value){
647       if (!array_key_exists($key, $this->used_apps)){
648         $apps["$key"]= "$value";
649       }
650     }
653     /* Create application list */
654     $div = new DivSelectBox("appgroup");    
655     $div->SetHeight(300);
656     $departments = array();
657     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
658     foreach($res as $value){
659       $fdn = $value['dn'];
660       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
661       $fdn= @LDAP::fix($fdn);
662       if($value["description"][0]!=".."){
663         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
664       }else{
665         $departments[$value['dn']]=convert_department_dn($fdn);
666       }
667     }
669     /* Create 'open' and 'add' links */
670     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
671       $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
672     }else{
673       $linkadd = "%s";
674     }
675     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
677     /* Create base back entry */
678     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
679     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
680       $div->AddEntry(array(
681             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
682               "attach"=>"style='border:0px;'")
683             ));
684     }
686     /* Append departments for current base */
687     foreach($departments as $key => $app){
688       $div->AddEntry(array(
689             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
690               "attach"=>"style='border:0px;'")
691             ));
692     }
694     /* Add applications found on this base */
695     foreach($apps as $key => $app){
696       $div->AddEntry(array(
697             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
698               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
699               "attach"=>"style='border:0px;'")
700             ));
701     }
703     /* Create list of used/assigned applications */
704     $div2 = new DivSelectBox("appgroup");
705     $div2->SetHeight(300);
707     /* Check acls to create "edit/remove" category links */
708     if(!$this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
709       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
710       $catremove      = "&nbsp;<img src='images/empty.png' alt='&nbsp;'>";
711       $catupdown      = "";
712     }else{
713       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
714       $catremove      = "&nbsp;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
715       $catupdown      = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>".
716         "<img align='top' alt=\"\" src='images/sort_up.png' border=0 title='"._("Move up")."'>".
717         "</a>&nbsp;".
718         "<a href='?plug=".$_GET['plug']."&amp;act=cat_down&amp;id=%s'>".
719         "<img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0>".
720         "</a>";
721     }
723     /* Add back category for current category */
724     if(empty($this->curCatDir)){
725       $cnt =0;
726     }else{
727       $cnt = count(split("/",$this->curCatDir));
728       $tmp = split("/",$this->curCatDir);
729       $bbk = "";
730       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
731         $bbk .= $tmp[$i];
732       }
733       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
734     }
736     /* Add sub categories */ 
737     $this->GetSubdirs($this->curCatDir);
738     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
739       $div2 ->AddEntry(array( 
740             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
741             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
742               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
743     }
745     /* Create priority & seperator links */
746     $separator ="<hr size=1>"; 
747     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
748       $sep      = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
749       $upudown  = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&amp;id=%s'>".
750                   " <img alt='{t}sort{/t}' align='top' src='images/sort_up.png' title='"._("Move up")."' border=0>".
751                   "</a>&nbsp;".
752                   "<a href='?plug=".$_GET['plug']."&amp;act=one_down&amp;id=%s'>".
753                   " <img alt='{t}sort{/t}' src='images/sort_down.png' title='"._("Move down")."' border=0>".
754                   "</a>&nbsp;".
755                   "<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
756     }else{
757       $sep      = "";
758       $upudown  = "";
759     }
760      
761     /* Create edit link */ 
762     if($this->acl_is_writeable("gosaApplicationParameter",$this->no_release_acls)){
763       $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
764     }else{
765       $edit=      "";
766     }
768     /* Get differences to mark those entries that have changed from last edit. */
769     $Differences = $this->diffAppsInReleases();
771     /* Walk through entries an append them to list */
772     if(isset($this->gosaMemberApplication[$this->curCatDir])){
773       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
775         /* Add seperator */ 
776         if(preg_match("/__SEPARATOR__/",$entry['App'])){
777           $div2 ->AddEntry(array(array("string"=>$separator),
778                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
779         }else{
781           /* Add application */
783           $image = "";
784           /* Check if application was available within selected release
785            *  or application list if release management is not active
786            */
787           if(!in_array($entry['App'],$this->AllAppsForRelease)){
789             /*  release managent is active
790              */
791             if(!$this->enableReleaseManagement){
792               $image = "<img class='center' alt='R' src='images/select_invalid_application.png' 
793                           title='"._("This application is no longer available.")."'>&nbsp;";
794             }else{
795               $image = "<img class='center' alt='F' src='images/select_invalid_application.png' 
796                           title=\"".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."\">&nbsp;";
797             }
798       
799           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
800             $entry['App'].=" <i>["._("Check parameter")."]</i>";
801               $image = "<img class='center' src='images/select_invalid_application.png' 
802                           title='"._("This application has changed parameters.")."'>&nbsp;";
803           }else{
804             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
805           }
806           
807           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
808                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
809                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
810         }
811       }
812     }
814     /* Assign created div lists to template */
815     $smarty->assign("UsedApps", $div2->DrawList());
816     $smarty->assign("List", $div->DrawList());
817     $smarty->assign("apps", $apps);
818    
819     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
821     $smarty->assign("ReleaseSelectAble", false);
822     $smarty->assign("Release", $this->FAIrelease);
823  
824     if($this->enableReleaseManagement){
825       $smarty->assign("FAIrelease",  $this->FAIrelease);
826       $smarty->assign("Releases", $this->Releases);
828       if(!$this->no_release_acls && count($this->Releases)){
829         $smarty->assign("ReleaseSelectAble", true);
830       }
831     }
832   
833     /* Set acls to  template */
834     $tmp = $this->plInfo();
835     foreach($tmp['plProvidedAcls'] as $acl => $translation){
836       $smarty->assign($acl."ACL",$this->getacl($acl,$this->no_release_acls));
837     }
839     /* Show main page */
840     if ($this->dialog){
841       $smarty->assign("table", $this->table);
842       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
843     } else {
844       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
845     }
846     
847     return ($display);
848   }
851   function getReleases()
852   {
853     /* Only display those releases that we are able to read */
854     $dn     = $this->config->current['BASE'];
855     $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
856     $res    = get_list($filter,"application", $dn, array("ou"), GL_SUBSEARCH);
858     $ret =array();
859     foreach($res as $attrs){
860       if(preg_match("/ou=apps,/",$attrs['dn'])){
861         $bb     = preg_replace("/ou=apps,.*/","",$attrs['dn']);
862         $parts  = array_reverse(split("ou=",$bb));
864         $str ="";
865         foreach($parts as $part){
866           if(empty($part)) {
867             continue;
868           }
869           $str .= str_replace(",","",$part)."/";
870         }    
871         $name = preg_replace("/\/$/","",$str);
872         if(empty($name)) {
873           $name ="/";
874         }
875         $ret[$attrs['dn']] = $name;
876       }
877     }
878     return($ret);
879   }
881   function save_object()
882   {
884     /* Move category one position up or down */
885     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
886       if($_GET['act']=="cat_up"){
887         $this->catUp(base64_decode($_GET['id']));
888       }
889       if($_GET['act']=="cat_down"){
890         $this->catDown(base64_decode($_GET['id']));
891       }
892     }
894     /* Move application one position up or down */
895     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
896       if(isset($_GET['id'])){
897         $id   = $_GET['id'];
898         $act  = $_GET['act']; 
900         if($act == "one_up"){
901           $this->getOneUp($id);
902         }elseif($act == "one_down")   { 
903           $this->getOneDown($id);
904         }
905       }
906     }
909     plugin::save_object();
910   }
911  
913   function remove_from_parent()
914   {
915     if(!$this->initially_was_account){
916       return;
917     }
919     plugin::remove_from_parent();
921     $ldap= $this->config->get_ldap_link();
922     $ldap->cd($this->dn);
923     $this->cleanup();
924     
925     $ldap->modify ($this->attrs); 
926     new log("remove","group/".get_class($this),$use_dn,array_keys($this->attrs),$ldap->get_error());
927     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/applications with dn '%s' failed."),$this->dn));
929     /* Optionally execute a command after we're done */
930     $this->handle_post_events("remove");
931   }
934   /* Save to LDAP */
935   function save()
936   {
937     /* Skip saving application settings, if we do not have release acls */
938     if($this->no_release_acls){
939       new log("security","groups/".get_class($this),$this->dn,array(),"Skip saving group application extension. Caused by insufficient acls");
940       
941       return;
942     }
944     plugin::save();
948     /* Copy members */
949     $this->Categories[""]=""; 
950     $this->attrs["gosaMemberApplication"]= array();
951     $this->attrs["gosaApplicationParameter"]= array();
952     foreach($this->Categories as $name => $cats){
953       $i =0;
954       if(isset($this->gosaMemberApplication[$name])){
955         foreach($this->gosaMemberApplication[$name] as $entry){
956           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
957             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
958           }
959           $i ++;
960         }
961       }
962       if(($i==0)&&(!empty($name))){
963         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$i;
964       }
965     }
967     /* Are there application parameters to be saved */
968     $this->attrs['gosaApplicationParameter']= array();
969     foreach($this->appoption as $name => $value){
970       if ($value != ""){
971         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
972       }
973     }
975     /* Write back to LDAP */
976     $ldap= $this->config->get_ldap_link();
977     $ldap->cd($this->dn);
978     $this->cleanup();
979     $ldap->modify ($this->attrs); 
981     if($this->initially_was_account){
982       new log("modify","group/".get_class($this),$use_dn,array_keys($this->attrs),$ldap->get_error());
983     }else{
984       new log("create","group/".get_class($this),$use_dn,array_keys($this->attrs),$ldap->get_error()); 
985     }   
987     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/applications with dn '%s' failed."),$this->dn));
989     /* Optionally execute a command after we're done */
990     if ($this->initially_was_account == $this->is_account){
991       if ($this->is_modified){
992         $this->handle_post_events("modify");
993       }
994     } else {
995       $this->handle_post_events("add");
996     }
998   }
1000   function check()
1001   {
1002     /* Call common method to give check the hook */
1003     $message= plugin::check();
1005     return ($message);
1006   }
1009   function reload()
1010   {
1011     /* Generate applist */
1012     $this->apps= array();
1014     /* Special handling for release managed apps */
1015     $tmp = search_config($this->config->data,"faiManagement","CLASS");
1016     if(!empty($tmp) && count($this->Releases)){
1017       $this->enableReleaseManagement = true;
1019       /* Check if release is available */
1020       $tmp = array_flip($this->Releases);
1021       if(isset($tmp[$this->FAIrelease])){
1022         $base =  $tmp[$this->FAIrelease];
1023       }else{
1025         /* Release is not available, check if there is an alternative */
1026         $old_r =  $this->FAIrelease;
1027         $k = key($tmp);
1029         if(isset($tmp[$k])){
1030       
1031           /* We have found an alternative release name */
1032           $r = $tmp[$k];
1033           $this->FAIrelease = $k;
1034           $base = $r; 
1036           /* Display msg */
1037           if(!(!$this->initially_was_account && $old_r =="/")){
1038             print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$old_r,$k));   
1039           }
1040         }else{
1041           
1042           /* There are no releases available ... */
1043           print_red(_("There are no releases available. You will not be able to select another release."));
1044           return;
1045         }
1046       }
1048       /* Get applications for this release */
1049       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
1050       $res = get_list("(objectClass=gosaApplication)","application",$base,array("*"));
1051     }else{
1052     
1053       /* Get applications for this base */
1054       $res = get_list("(objectClass=gosaApplication)","application","ou=apps,".$this->curbase,array("*"));
1055     }
1056     
1057     /* Append applications */
1058     foreach($res as $attrs){
1059       if (isset($attrs["description"][0])){    
1060         $this->apps[$attrs["cn"][0]]=
1061           $attrs["cn"][0]." (".
1062           $attrs["description"][0].")";
1063       } else {
1064         $this->apps[$attrs["cn"][0]]=
1065           $attrs["cn"][0];
1066       }
1067     }
1069     /* Get all apps ... */
1070     $res = get_list("objectClass=gosaApplication","application",$this->config->current['BASE'],array("gosaApplicationParameter","cn"),GL_SUBSEARCH); 
1071     $tmp = search_config($this->config->data,"faiManagement","CLASS");
1072     $this->AllAppsForRelease = array();
1073     if(!empty($tmp)){
1074       $tmp = array_flip($this->Releases);
1075       foreach($res as $attrs){
1076         
1077         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
1078         $testdn = preg_replace("/^[^,]+/","",$testdn);
1079         $testdn = preg_replace("/^,/","",$testdn);
1081         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
1082           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1083           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
1084         }
1085       }
1086     }else{
1087       foreach($res as $attrs){
1088         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1089       }
1090     }
1092     natcasesort ($this->apps);
1093     reset ($this->apps);
1095     if(is_array($this->gosaMemberApplication))
1096       foreach ($this->gosaMemberApplication as $cat){   
1097         if(is_array($cat))
1098           foreach($cat as $entry){
1099             $this->used_apps[$entry['App']]= $entry['App'];
1100           }
1101       }
1102   }
1105   function addApp($cn)
1106   {
1107     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
1108       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
1109         if($entry['App'] == $cn) return;
1110       }
1111     }
1112     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
1113     $this->used_apps[$cn]=$cn;
1114     $this->is_modified= TRUE;
1115   }
1118   function removeApp($cn)
1119   {
1120     $temp= array();
1121     foreach ($this->gosaMemberApplication as $value){
1122       if ($value != $cn){
1123         $temp[]= $value;
1124       }
1125     }
1126     $this->gosaMemberApplication= $temp;
1127     $this->is_modified= TRUE;
1128   }
1130   function getParameters($app)
1131   {
1132     $tmp = $this->getReleases();
1133     $ret = array();
1134     if(in_array($this->FAIrelease,$tmp)){
1135       $tmp2 = array_flip($tmp);
1136       $base = $tmp2[$this->FAIrelease];
1137       $ldap = $this->config->get_ldap_link();
1138       $ldap->cd($this->config->current['BASE']);
1139       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1140       if($ldap->count()){
1141         $attrs = $ldap->fetch();
1142         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1143           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1144           $ret[$name] = $name;
1145         } 
1146       }
1147     }
1148     return($ret);
1149   }
1151   function GetSubdirs($dir)
1152   {
1153     $ret = array();
1154     $tmp1 = split("/",$this->curCatDir);
1155  
1156     foreach($this->Categories as $path=>$cat){
1157       $tmp2 = split("/",$path);
1158       
1159       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1160         $abort = false;
1161       }elseif(((count($tmp1))+1) == (count($tmp2))){
1162         $abort = false;
1163         for($i = 0 ; $i < count($tmp1) ; $i++){
1164           if($tmp1[$i] != $tmp2[$i]){
1165             $abort = true;
1166           }
1167         }
1168       }else{
1169         $abort= true;
1170       }
1171       if(!$abort){
1172         $ret[$path]=$cat;
1173       } 
1174     }
1175     return($ret);
1176   }
1179   function PrepareForCopyPaste($source)
1180   {
1181     $this->FAIrelease = $this->InitialFAIrelease = $source->FAIrelease;
1182     $this->gosaMemberApplication = $source->gosaMemberApplication;
1183     $this->appoption = $source->appoption;
1184   }
1187   /* Return plugin informations for acl handling  */ 
1188   function plInfo()
1189   {
1190     return (array(
1191           "plShortName"   => _("Applications"),
1192           "plDescription" => _("Group applications"),
1193           "plSelfModify"  => FALSE,
1194           "plDepends"     => array(),
1195           "plPriority"    => 0,
1196           "plSection"     => array("admin"),
1197           "plCategory"    => array("groups"),
1198           "plProvidedAcls"=> array(
1199             "gosaMemberApplication"     => _("Application"),
1200             "FAIrelease"                => _("Release"),
1201             "gosaApplicationParameter"  => _("Application parameter"))
1202           ));
1203   }
1205 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1206 ?>