Code

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