Code

Fixed logging for open groupware account
[gosa.git] / plugins / admin / groups / class_groupApplication.inc
1 <?php
2 class appgroup extends plugin
3 {
4   /* CLI vars */
5   var $cli_summary= "Manage application groups";
6   var $cli_description= "Some longer text\nfor help";
7   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
9   /* Appgroup attributes */
10   var $gosaMemberApplication= array();
12   /* Helpers */
13   var $departments        = "";       // All departments within $this->curbase;
14   var $apps               = array();  // All available applications
15   var $used_apps          = array();  // Specifies which applications are currently assigned 
17   var $option_name          = array();
18   var $option_value         = array();
19   var $appoption            = array();
21   var $table                  = "";
22   var $curbase                = "";
23   var $curCatDir                  ;
24   var $curCatDepth              =0;         //
26   /* attribute list for save action */
27   var $attributes               = array("gosaMemberApplication","gosaApplicationParameter");
28   var $objectclasses            = array("gosaApplicationGroup");
30   var $FAIrelease               = "/";
31   var $InitialFAIrelease           = "/";
32   var $Releases                 = array();
33   var $enableReleaseManagement  = false;
35   var $AllAppsForRelease        = array();
36   var $AllAppsForReleaseParameter = array();
38   var $CopyPasteVars            = array("Categories");
39   var $gosaApplicationParameter ;
40   var $ui                       = NULL;
41   var $no_release_acls          = false;
43   function appgroup ($config, $dn= NULL, $parent= NULL)
44   {
45    
46     /* Check if we have relase mangement enabled and prepare group application for release management */ 
47     $tmp = search_config($config->data,"faiManagement","CLASS");
48     if(!empty($tmp)){
49       $this->enableReleaseManagement = true;
50       $this->objectclasses  [] = "FAIreleaseTag";
51       $this->attributes     [] = "FAIrelease";
52     }
54     plugin::plugin ($config, $dn, $parent);
56     /* set userinfo object */
57     $this->ui = get_userinfo();
59     /* Compatibility check 
60         In some case of old applikations with old release tag saving, 
61           we must reassign is_account state.
62           (Or if release management wasn't activated before creating this app) */
63     if($this->enableReleaseManagement){
64   
65       /* Release management is activated && this is a valid group app account,
66           but no release class was found, so activate is_account flag  */
67       if($dn != "new" && ( in_array("gosaApplicationGroup",$this->attrs['objectClass'])) && 
68          (!in_array("FAIreleaseTag",$this->attrs['objectClass']))){
69         $this->is_account =true;
70       }
71     }
72     
73     /* Load member applications */
74     if (isset ($this->attrs["gosaMemberApplication"][0])){
75       $this->gosaMemberApplication = array();
76       for ($i= 0; $i<$this->attrs["gosaMemberApplication"]["count"]; $i++){
77         $this->gosaMemberApplication[]=
78           $this->attrs["gosaMemberApplication"][$i];
79       }
80     }
82     /* Load application options */
83     if (isset($this->attrs['gosaApplicationParameter'])){
84       for ($i= 0; $i<$this->attrs['gosaApplicationParameter']['count']; $i++){
85         $option= preg_replace('/^[^:]+:/', '',
86             $this->attrs['gosaApplicationParameter'][$i]);
87         $name= preg_replace('/:.*$/', '',
88             $this->attrs['gosaApplicationParameter'][$i]);
89         $this->appoption[$name]= $option;
90       }
91     }
93     /* Parse MemberApplication*/
94     $tmp    = array();
95     $tmp2   = array();
96     $prios  = array();
97     $cats   = array();
99     foreach($this->gosaMemberApplication as $memberApp){
100       if(preg_match("/\|/i",$memberApp)){
101     
102         $tmp = split("\|",$memberApp);
103  
104         if(!empty($tmp[0])){
105           $tmp2[$tmp[1]][$tmp[2]] = array("App"=>$tmp[0]);
106         }
107         if(!empty($tmp[1])){
108           $n = split("/",$tmp[1]);
109           $c = count($n);
110           $cats [$tmp[1]] = $n[$c-1];
111         }
112         $prios[$tmp[1]][$tmp[2]] = $tmp[2];
113       }else{
114         $tmp2[]['App'] = $memberApp;
115       }
116     }
118     /* Assign collected category and application settings */
119     $this->Categories = $cats;
120     $this->gosaMemberApplication = $tmp2;
122     /* Sort application by category */
123     $cats[""]="";
124     foreach($cats as $cat ){
125       if((isset($prios[$cat]))&&(count($prios[$cat]))){
126         $max = max($prios[$cat]);
127         $min = 1;//min($prios[$cat]);  
128         $last = false;
129         for($i = $min ; $i < $max ; $i++){
130           if(!isset($prios[$cat][$i])){
131             if($last == false){
132               $this->gosaMemberApplication[$cat][$i]['App'] = "__SEPARATOR__".$i;
133             
134               $last = true;
135             }
136           }else{
137             $last = false;
138           }
139         }
140       }
141     } 
143     /* Sort application by priority */
144     $tmp = array();
145     foreach($this->gosaMemberApplication as $key =>  $entries){
146       ksort ($entries);
147       foreach($entries as $entry){
148         $tmp[$key][]= $entry;
149       }
150     }
151     $this->gosaMemberApplication = $tmp;
152     $this->curbase = $this->config->current['BASE'];
153     
154     /* Get required release informations */
155     if($this->enableReleaseManagement){
156       $this->InitialFAIrelease  = $this->FAIrelease;
157       $this->Releases           = $this->getReleases();
158     }
160     if($this->is_account){
161       @log::log("view","groups/".get_class($this),$this->dn);
162     }
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     /* Check if department was selected */ 
389     if((isset($_GET['act']))&&($_GET['act']=="depopen")){
390       $dep = base64_decode($_GET['depid']); 
391       $this->curbase =$dep;
392     }
394     /* Check if category was selected */
395     if((isset($_GET['act']))&&($_GET['act']=="open") && (empty($_GET['id']) || isset($this->Categories[base64_decode($_GET['id'])]))){
396       $this->curCatDir = base64_decode($_GET['id']);
397     }
399     /* Do we need to flip is_account state? */
400     if(isset($_POST['modify_state'])){
401       if($this->is_account && $this->acl_is_removeable()){
402         $this->is_account= FALSE;
403       }elseif(!$this->is_account && $this->acl_is_createable()){
404         $this->is_account= TRUE;
405       }
406     }
408     /* Do we represent a valid group? */
409     if (!$this->is_account && $this->parent == NULL){
410       $display= "<img alt=\"\" src=\"images/stop.png\" align=\"middle\">&nbsp;<b>".
411         _("This 'dn' is no appgroup.")."</b>";
412       return ($display);
413     }
415     /* Show tab dialog headers */
416     $display= "";
417     if ($this->parent != NULL){
418       if ($this->is_account){
419         $display= $this->show_disable_header(_("Remove applications"),
420             _("This group has application features enabled. You can disable them by clicking below."));
421       } else {
422         $display.= $this->show_enable_header(_("Create applications"),
423             _("This group has application features disabled. You can enable them by clicking below."));
424         return ($display);
425       }
426     }
428     /* Check acl, applications must be readable */
429     if(!$this->check_acls()){
430       $display .= "<img src='images/button_cancel.png' alt='"._("ACL")."' class='center'>
431                   <b>"._("Insufficient permissions")."</b>
432                   <p>".
433                   _("You do not have permission to query application entries. All your changes will not be saved.").
434                   "</p><p class='seperator'>&nbsp;</p>";
435       $this->no_release_acls = true;
436     }
437     
439     /* Add Categorie */ 
440     if((isset($_POST['AddCat']))&&(isset($_POST['CatName']))&&(!empty($_POST['CatName']))){
442       /* Only allow adding a category, if it is allowed */  
443       if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
445         if(preg_match("/[\\\\\/]/i",$_POST['CatName'])){
446           print_red(_("Invalid character in category name."));
447         }elseif(!in_array($_POST['CatName'],$this->Categories)){ 
448           if(empty($this->curCatDir)){
449             $this->Categories[$_POST['CatName']]=$_POST['CatName'];
450           }else{
451             $this->Categories[$this->curCatDir."/".$_POST['CatName']]=$_POST['CatName'];
452           }
453         }else{
454           print_red(_("The specified category already exists."));
455         }
456       }
457     }
460     $this->reload();
461     $this->diffAppsInReleases();
463     /* Check POST variables for commands 
464         to add/remove some applications */
465     $only_once = false;
466     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
468       /* Walk through posts */
469       foreach($_POST as $name => $value){
471         /* Add a seperator to current category */
472         if((preg_match("/AddSep_/",$name))&&(!$only_once)){
473           $only_once = true;
474           $n = preg_replace("/AddSep_/","",$name);
475           $val= preg_replace("/_.*$/","",$n);
476           $this->AddSeperator($val);
477         }
479         /* Delete application | seperator entry */
480         if((preg_match("/DelApp_/",$name))&&(!$only_once)){
481           $only_once = true;
483           if(preg_match("/DelApp___SEPARATOR__/",$name)) {
484             $n=  preg_replace("/DelApp___SEPARATOR__/","",$name);
485             $val= "__SEPARATOR__".preg_replace("/_.*$/","",$n);
486           }else{
487             $n = preg_replace("/DelApp_/","",$name);
488             $val= preg_replace("/_.*$/","",$n);
489           }
491           foreach($this->gosaMemberApplication as $key =>  $cat){
492             foreach($cat as $key2 => $app){
493               if($app['App'] == $val){
494                 unset($this->gosaMemberApplication[$key][$key2]);
495                 if(isset($this->used_apps[$val])){
496                   foreach($this->getParameters($val) as $para){
497                     if(isset($this->appoption[$para])){
498                       unset($this->appoption[$para]);
499                     }
500                   }
501                   unset($this->used_apps[$val]);
502                 }
503               }
504             }
505           }
506         }
508         /* Remove category entry */
509         if(preg_match("/DelCat_/",$name)){
510           $n = preg_replace("/DelCat_/","",$name);
511           $app = base64_decode( preg_replace("/_.*$/","",$n));
512           foreach($this->Categories as $key =>  $cat){
513             if($cat == $app){
514               foreach($this->Categories as $p => $n){
515                 if(preg_match("/^".$key."\/.*/",$p)){
516                   unset($this->Categories[$p]);    
517                 }
518               }
519               unset($this->Categories[$key]);
520             }
521           }
522         }
524         /* Edit application parameter */
525         if((preg_match("/EdiApp_/",$name))&&(!$only_once)){
527           $only_once = true;
528           $appname = $value;
529           $appname = preg_replace("/EdiApp_/","",$name);  
530           $appname = preg_replace("/_.*$/","",$appname);
532           /* We've got the appname, get parameters from ldap */
533           $ldap= $this->config->get_ldap_link();
535           /* Check if we have release management enabled */
536           $tmp = search_config($this->config->data,"faiManagement","CLASS");
537           if(!empty($tmp)){
538     
539             /* Get application parameter from ldap */
540             $tmp = array_flip($this->Releases);
541             $base = $tmp[$this->FAIrelease];
542             $ldap->cd($base);
543             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
544             $found = "";
545             while($attrs = $ldap->fetch()) {
546               if(preg_match("/cn=".$appname.",".$base."/",$attrs['dn'])){
547                 $found = $attrs['dn'];
548               }
549             }
550             $ldap->cat($found, array("gosaApplicationParameter"));
551           }else{
553             /* Get application parameter from ldap */
554             $ldap->cd($this->config->current['BASE']);
555             $ldap->search("(&(objectClass=gosaApplication)(cn=$appname))",array("gosaApplicationParameter"));
556           }
558           /* Check if this application is unique */
559           if ($ldap->count() != 1){
560             print_red (_("The selected application name is not uniq. Please check your LDAP."));
561           } else {
563             /* Get parameter */
564             $attrs= $ldap->fetch();
565             if(isset($attrs['gosaApplicationParameter'])){
566               $this->dialog= TRUE;
568               /* Fill name and value arrays */
569               for ($i= 0; $i<$attrs['gosaApplicationParameter']['count']; $i++){
570                 $option= preg_replace('/^[^:]+:/', '',
571                     $attrs['gosaApplicationParameter'][$i]);
572                 $name= preg_replace('/:.*$/', '', 
573                     $attrs['gosaApplicationParameter'][$i]);
574                 $this->option_name[$i]= $name;
576                 /* Fill with values from application, default should be
577                    loaded by the external scripts */
578                 if (isset($this->appoption[$name])){
579                   $this->option_value[$i]= $this->appoption[$name];
580                 }
581               }
583               /* Create edit field */
584               $table= "<table summary=\"\">";
585               for ($i= 0; $i < count($this->option_name); $i++){
586                 if (isset($this->option_value[$i])){
587                   $value= $this->option_value[$i];
588                 } else {
589                   $value= "";
590                 }
591                 $table.="<tr><td>".$this->option_name[$i]."</td><td>".
592                   "<input name=\"value$i\" size=60 maxlength=250 ".
593                   "value=\"".$value."\"><br></td></tr>";
594               }
595               $table.= "</table>";
596               $this->table= $table;
597             } else {
598               print_red (_("The selected application has no options."));
599             }
600           }
601         }
602       }
603     }
605     /* Add multiple */
606     if(isset($_POST['AddApps'])){
607       foreach($_POST as $name => $value){
608         if(preg_match("/AddApp_/",$name)){
609           $app = preg_replace("/AddApp_/","",$name);
610           $this->addApp($app);
611         }
612       }
613     }
615     /* Add application with post */
616     if((isset($_GET['act']))&&($_GET['act']=="add")){
617       $this->used_apps[$_GET['id']]= $_GET['id'];
618       asort($this->used_apps);
619       $this->addApp($_GET['id']);
620     }
622     /* Cancel edit options? */
623     if (isset($_POST['edit_options_cancel'])){
624       $this->dialog= FALSE;
625     }
627     /* Finish edit options? */
628     if (isset($_POST['edit_options_finish'])){
629       $this->dialog= FALSE;
631       /* Save informations passed by the user */
632       $this->option_value= array();
633       for ($i= 0; $i<count($this->option_name); $i++){
634         $this->appoption[$this->option_name[$i]]= $_POST["value$i"];
635         $this->is_modified= TRUE;
636       }
637     }
639     /* Prepare templating stuff */
640     $smarty= get_smarty();
641     $smarty->assign("used_apps", $this->used_apps);
642     $apps= array();
643     foreach ($this->apps as $key => $value){
644       if (!array_key_exists($key, $this->used_apps)){
645         $apps["$key"]= "$value";
646       }
647     }
650     /* Create application list */
651     $div = new DivSelectBox("appgroup");    
652     $div->SetHeight(300);
653     $departments = array();
654     $res = get_list("(objectClass=gosaDepartment)", "application", $this->curbase,array("description","cn","ou"),GL_SIZELIMIT);
655     foreach($res as $value){
656       $fdn = $value['dn'];
657       $fdn = preg_replace("/".normalizePreg($this->curbase)."/","",$fdn);
658       $fdn= @LDAP::fix($fdn);
659       if($value["description"][0]!=".."){
660         $departments[$value['dn']]= convert_department_dn($fdn)." - [".$value["description"][0]."]";
661       }else{
662         $departments[$value['dn']]=convert_department_dn($fdn);
663       }
664     }
666     /* Create 'open' and 'add' links */
667     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
668       $linkadd  = "<a href='?plug=".$_GET['plug']."&amp;act=add&amp;id=%s'>%s</a>";
669     }else{
670       $linkadd = "%s";
671     }
672     $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=depopen&amp;depid=%s'>%s</a>";
674     /* Create base back entry */
675     $base_back = preg_replace("/^[^,]+,/","",$this->curbase);
676     if((strlen($base_back)>= strlen($this->config->current['BASE']))&&($this->curbase!=$this->config->current['BASE'])){
677       $div->AddEntry(array(
678             array("string"=>sprintf($linkopen,base64_encode($base_back),".. ["._("back")."]"),
679               "attach"=>"style='border:0px;'")
680             ));
681     }
683     /* Append departments for current base */
684     foreach($departments as $key => $app){
685       $div->AddEntry(array(
686             array("string"=>"<img class='center' src='images/folder.png' alt='"._("department")."'>&nbsp;".sprintf($linkopen,base64_encode($key),$app),
687               "attach"=>"style='border:0px;'")
688             ));
689     }
691     /* Add applications found on this base */
692     foreach($apps as $key => $app){
693       $div->AddEntry(array(
694             array("string"=>sprintf("<input class='center' type='checkbox' value='1' name='AddApp_%s'>",$key).
695               "<img class='center' src='images/select_application.png' alt='"._("application")."'>&nbsp;".sprintf($linkadd,$key,$app),
696               "attach"=>"style='border:0px;'")
697             ));
698     }
700     /* Create list of used/assigned applications */
701     $div2 = new DivSelectBox("appgroup");
702     $div2->SetHeight(300);
704     /* Check acls to create "edit/remove" category links */
705     if(!$this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
706       $linkopen       = "<img class='center' src='images/folder.png' alt=\"\">&nbsp;<a href='?plug=".$_GET['plug']."&amp;act=open&amp;id=%s'>%s</a>";
707       $catremove      = "&nbsp;<img src='images/empty.png' alt='&nbsp;'>";
708       $catupdown      = "";
709     }else{
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;<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelCat_%s' value='%s'>";
712       $catupdown      = "<a href='?plug=".$_GET['plug']."&amp;act=cat_up&amp;id=%s'>".
713         "<img align='top' alt=\"\" src='images/sort_up.png' border=0 title='"._("Move up")."'>".
714         "</a>&nbsp;".
715         "<a href='?plug=".$_GET['plug']."&amp;act=cat_down&amp;id=%s'>".
716         "<img alt=\"\" src='images/sort_down.png' title='"._("Move down")."' border=0>".
717         "</a>";
718     }
720     /* Add back category for current category */
721     if(empty($this->curCatDir)){
722       $cnt =0;
723     }else{
724       $cnt = count(split("/",$this->curCatDir));
725       $tmp = split("/",$this->curCatDir);
726       $bbk = "";
727       for($i = 0 ; $i < ($cnt -1 ) ; $i++){
728         $bbk .= $tmp[$i];
729       }
730       $div2 ->AddEntry(array(array("string"=>sprintf($linkopen,base64_encode($bbk),"..")),array("string"=>"&nbsp;","attach"=>"style='border-right:0px;'")));
731     }
733     /* Add sub categories */ 
734     $this->GetSubdirs($this->curCatDir);
735     foreach($this->GetSubdirs($this->curCatDir) as $path => $name){
736       $div2 ->AddEntry(array( 
737             array("string"=>sprintf($linkopen,base64_encode($path),$name)),
738             array("string"=>preg_replace("/%s/",base64_encode($path),$catupdown.$catremove),
739               "attach"=>"align='right' style='width:80px;border-right:0px;'"))); 
740     }
742     /* Create priority & seperator links */
743     $separator ="<hr size=1>"; 
744     if($this->acl_is_writeable("gosaMemberApplication",$this->no_release_acls)){
745       $sep      = "<input type='image' src='images/back.png' title='"._("Insert seperator")."' value='%s' name='AddSep_%s'>";
746       $upudown  = "<a href='?plug=".$_GET['plug']."&amp;act=one_up&amp;id=%s'>".
747                   " <img alt='{t}sort{/t}' align='top' src='images/sort_up.png' title='"._("Move up")."' border=0>".
748                   "</a>&nbsp;".
749                   "<a href='?plug=".$_GET['plug']."&amp;act=one_down&amp;id=%s'>".
750                   " <img alt='{t}sort{/t}' src='images/sort_down.png' title='"._("Move down")."' border=0>".
751                   "</a>&nbsp;".
752                   "<input type='image' src='images/edittrash.png' title='"._("Delete entry")."' name='DelApp_%s' value='%s' alt='{t}delete{/t}' >";
753     }else{
754       $sep      = "";
755       $upudown  = "";
756     }
757      
758     /* Create edit link */ 
759     if($this->acl_is_writeable("gosaApplicationParameter",$this->no_release_acls)){
760       $edit=      "&nbsp;<input type='image' src='images/edit.png' title='"._("Edit entry")."' name='EdiApp_%s' value='%s' alt='{t}edit{/t}' >";
761     }else{
762       $edit=      "";
763     }
765     /* Get differences to mark those entries that have changed from last edit. */
766     $Differences = $this->diffAppsInReleases();
768     /* Walk through entries an append them to list */
769     if(isset($this->gosaMemberApplication[$this->curCatDir])){
770       foreach($this->gosaMemberApplication[$this->curCatDir] as $cat => $entry){
772         /* Add seperator */ 
773         if(preg_match("/__SEPARATOR__/",$entry['App'])){
774           $div2 ->AddEntry(array(array("string"=>$separator),
775                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$upudown),"attach"=>"align='right' style='border-right:0px;'")));
776         }else{
778           /* Add application */
780           $image = "";
781           /* Check if application was available within selected release
782            *  or application list if release management is not active
783            */
784           if(!in_array($entry['App'],$this->AllAppsForRelease)){
786             /*  release managent is active
787              */
788             if(!$this->enableReleaseManagement){
789               $image = "<img class='center' alt='R' src='images/select_invalid_application.png' 
790                           title='"._("This application is no longer available.")."'>&nbsp;";
791             }else{
792               $image = "<img class='center' alt='F' src='images/select_invalid_application.png' 
793                           title=\"".sprintf(_("This application is not available in any release named %s."),$this->FAIrelease)."\">&nbsp;";
794             }
795       
796           }elseif(isset($Differences[$entry['App']]) && ($Differences[$entry['App']] == true)) {
797             $entry['App'].=" <i>["._("Check parameter")."]</i>";
798               $image = "<img class='center' src='images/select_invalid_application.png' 
799                           title='"._("This application has changed parameters.")."'>&nbsp;";
800           }else{
801             $image = "<img class='center' src='images/select_application.png' alt=\"\">&nbsp;"; 
802           }
803           
804           $div2->AddEntry(array(array("string"=>sprintf($image."%s",$entry['App'])),
805                 array("string"=>preg_replace("/\%s/",htmlentities($entry['App']),$sep.$edit.$upudown),
806                       "attach"=>"align='right' style='width:100px;border-right:0px;'")));
807         }
808       }
809     }
811     /* Assign created div lists to template */
812     $smarty->assign("UsedApps", $div2->DrawList());
813     $smarty->assign("List", $div->DrawList());
814     $smarty->assign("apps", $apps);
815    
816     $smarty->assign("enableReleaseManagement",$this->enableReleaseManagement);
818     $smarty->assign("ReleaseSelectAble", false);
819     $smarty->assign("Release", $this->FAIrelease);
820  
821     if($this->enableReleaseManagement){
822       $smarty->assign("FAIrelease",  $this->FAIrelease);
823       $smarty->assign("Releases", $this->Releases);
825       if(!$this->no_release_acls && count($this->Releases)){
826         $smarty->assign("ReleaseSelectAble", true);
827       }
828     }
829   
830     /* Set acls to  template */
831     $tmp = $this->plInfo();
832     foreach($tmp['plProvidedAcls'] as $acl => $translation){
833       $smarty->assign($acl."ACL",$this->getacl($acl,$this->no_release_acls));
834     }
836     /* Show main page */
837     if ($this->dialog){
838       $smarty->assign("table", $this->table);
839       $display.= $smarty->fetch (get_template_path('application_options.tpl', TRUE));
840     } else {
841       $display.= $smarty->fetch (get_template_path('application.tpl', TRUE));
842     }
843     
844     return ($display);
845   }
848   function getReleases()
849   {
850     /* Only display those releases that we are able to read */
851     $dn     = $this->config->current['BASE'];
852     $filter = "(&(objectClass=organizationalUnit)(objectClass=FAIbranch))";
853     $res    = get_list($filter,"application", $dn, array("ou"), GL_SUBSEARCH);
855     $ret =array();
856     foreach($res as $attrs){
857       if(preg_match("/ou=apps,/",$attrs['dn'])){
858         $bb     = preg_replace("/ou=apps,.*/","",$attrs['dn']);
859         $parts  = array_reverse(split("ou=",$bb));
861         $str ="";
862         foreach($parts as $part){
863           if(empty($part)) {
864             continue;
865           }
866           $str .= str_replace(",","",$part)."/";
867         }    
868         $name = preg_replace("/\/$/","",$str);
869         if(empty($name)) {
870           $name ="/";
871         }
872         $ret[$attrs['dn']] = $name;
873       }
874     }
875     return($ret);
876   }
878   function save_object()
879   {
881     /* Move category one position up or down */
882     if((isset($_GET['act']))&&(($_GET['act'] == "cat_up")||($_GET['act']=="cat_down"))){
883       if($_GET['act']=="cat_up"){
884         $this->catUp(base64_decode($_GET['id']));
885       }
886       if($_GET['act']=="cat_down"){
887         $this->catDown(base64_decode($_GET['id']));
888       }
889     }
891     /* Move application one position up or down */
892     if((isset($_GET['act']))&&(($_GET['act'] == "one_up")||($_GET['act']=="one_down"))){
893       if(isset($_GET['id'])){
894         $id   = $_GET['id'];
895         $act  = $_GET['act']; 
897         if($act == "one_up"){
898           $this->getOneUp($id);
899         }elseif($act == "one_down")   { 
900           $this->getOneDown($id);
901         }
902       }
903     }
906     plugin::save_object();
907   }
908  
910   function remove_from_parent()
911   {
912     plugin::remove_from_parent();
914     $ldap= $this->config->get_ldap_link();
915     $ldap->cd($this->dn);
916     $this->cleanup();
917     
918     $ldap->modify ($this->attrs); 
919     @log::log("remove","group/".get_class($this),$use_dn,array_keys($this->attrs),$ldap->get_error());
920     show_ldap_error($ldap->get_error(), sprintf(_("Removing of groups/applications with dn '%s' failed."),$this->dn));
922     /* Optionally execute a command after we're done */
923     $this->handle_post_events("remove");
924   }
927   /* Save to LDAP */
928   function save()
929   {
930     /* Skip saving application settings, if we do not have release acls */
931     if($this->no_release_acls){
932       gosa_log("Skip saving group application extension. Caused by insufficient acls");
933       return;
934     }
936     plugin::save();
940     /* Copy members */
941     $this->Categories[""]=""; 
942     $this->attrs["gosaMemberApplication"]= array();
943     $this->attrs["gosaApplicationParameter"]= array();
944     foreach($this->Categories as $name => $cats){
945       $i =0;
946       if(isset($this->gosaMemberApplication[$name])){
947         foreach($this->gosaMemberApplication[$name] as $entry){
948           if(!preg_match("/__SEPARATOR__/",$entry['App'])){
949             $this->attrs["gosaMemberApplication"][]= $entry['App']."|".$name."|".$i;
950           }
951           $i ++;
952         }
953       }
954       if(($i==0)&&(!empty($name))){
955         $this->attrs["gosaMemberApplication"][]= "|".$name."|".$i;
956       }
957     }
959     /* Are there application parameters to be saved */
960     $this->attrs['gosaApplicationParameter']= array();
961     foreach($this->appoption as $name => $value){
962       if ($value != ""){
963         $this->attrs['gosaApplicationParameter'][]= "$name:$value";
964       }
965     }
967     /* Write back to LDAP */
968     $ldap= $this->config->get_ldap_link();
969     $ldap->cd($this->dn);
970     $this->cleanup();
971     $ldap->modify ($this->attrs); 
973     if($this->initially_was_account){
974       @log::log("modify","group/".get_class($this),$use_dn,array_keys($this->attrs),$ldap->get_error());
975     }else{
976       @log::log("create","group/".get_class($this),$use_dn,array_keys($this->attrs),$ldap->get_error()); 
977     }   
979     show_ldap_error($ldap->get_error(), sprintf(_("Saving of groups/applications with dn '%s' failed."),$this->dn));
981     /* Optionally execute a command after we're done */
982     if ($this->initially_was_account == $this->is_account){
983       if ($this->is_modified){
984         $this->handle_post_events("modify");
985       }
986     } else {
987       $this->handle_post_events("add");
988     }
990   }
992   function check()
993   {
994     /* Call common method to give check the hook */
995     $message= plugin::check();
997     return ($message);
998   }
1001   function reload()
1002   {
1003     /* Generate applist */
1004     $this->apps= array();
1006     /* Special handling for release managed apps */
1007     $tmp = search_config($this->config->data,"faiManagement","CLASS");
1008     if(!empty($tmp) && count($this->Releases)){
1009       $this->enableReleaseManagement = true;
1011       /* Check if release is available */
1012       $tmp = array_flip($this->Releases);
1013       if(isset($tmp[$this->FAIrelease])){
1014         $base =  $tmp[$this->FAIrelease];
1015       }else{
1017         /* Release is not available, check if there is an alternative */
1018         $old_r =  $this->FAIrelease;
1019         $k = key($tmp);
1021         if(isset($tmp[$k])){
1022       
1023           /* We have found an alternative release name */
1024           $r = $tmp[$k];
1025           $this->FAIrelease = $k;
1026           $base = $r; 
1028           /* Display msg */
1029           if(!(!$this->initially_was_account && $old_r =="/")){
1030             print_red(sprintf(_("Can't resolve the release name '%s', setting release name to '%s'. Possibly the objects base has changed."),$old_r,$k));   
1031           }
1032         }else{
1033           
1034           /* There are no releases available ... */
1035           print_red(_("There are no releases available. You will not be able to select another release."));
1036           return;
1037         }
1038       }
1040       /* Get applications for this release */
1041       $base = preg_replace("/ou=apps,.*$/","ou=apps,".$this->curbase,$base);
1042       $res = get_list("(objectClass=gosaApplication)","application",$base,array("*"));
1043     }else{
1044     
1045       /* Get applications for this base */
1046       $res = get_list("(objectClass=gosaApplication)","application","ou=apps,".$this->curbase,array("*"));
1047     }
1048     
1049     /* Append applications */
1050     foreach($res as $attrs){
1051       if (isset($attrs["description"][0])){    
1052         $this->apps[$attrs["cn"][0]]=
1053           $attrs["cn"][0]." (".
1054           $attrs["description"][0].")";
1055       } else {
1056         $this->apps[$attrs["cn"][0]]=
1057           $attrs["cn"][0];
1058       }
1059     }
1061     /* Get all apps ... */
1062     $res = get_list("objectClass=gosaApplication","application",$this->config->current['BASE'],array("gosaApplicationParameter","cn"),GL_SUBSEARCH); 
1063     $tmp = search_config($this->config->data,"faiManagement","CLASS");
1064     $this->AllAppsForRelease = array();
1065     if(!empty($tmp)){
1066       $tmp = array_flip($this->Releases);
1067       foreach($res as $attrs){
1068         
1069         $testdn = preg_replace("/ou=apps,.*$/","ou=apps",$attrs['dn']);
1070         $testdn = preg_replace("/^[^,]+/","",$testdn);
1071         $testdn = preg_replace("/^,/","",$testdn);
1073         if($testdn == preg_replace("/ou=apps,.*$/","ou=apps",$tmp[$this->FAIrelease])){
1074           $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1075           $this->AllAppsForReleaseParameter[$this->FAIrelease][$attrs['cn'][0]] = $attrs;
1076         }
1077       }
1078     }else{
1079       foreach($res as $attrs){
1080         $this->AllAppsForRelease[$attrs['dn']] = $attrs['cn'][0];
1081       }
1082     }
1084     natcasesort ($this->apps);
1085     reset ($this->apps);
1087     if(is_array($this->gosaMemberApplication))
1088       foreach ($this->gosaMemberApplication as $cat){   
1089         if(is_array($cat))
1090           foreach($cat as $entry){
1091             $this->used_apps[$entry['App']]= $entry['App'];
1092           }
1093       }
1094   }
1097   function addApp($cn)
1098   {
1099     if((isset($this->gosaMemberApplication[$this->curCatDir]))&&(is_array($this->gosaMemberApplication[$this->curCatDir]))){
1100       foreach($this->gosaMemberApplication[$this->curCatDir] as $entry){
1101         if($entry['App'] == $cn) return;
1102       }
1103     }
1104     $this->gosaMemberApplication[$this->curCatDir][]= array("App"=>$cn);
1105     $this->used_apps[$cn]=$cn;
1106     $this->is_modified= TRUE;
1107   }
1110   function removeApp($cn)
1111   {
1112     $temp= array();
1113     foreach ($this->gosaMemberApplication as $value){
1114       if ($value != $cn){
1115         $temp[]= $value;
1116       }
1117     }
1118     $this->gosaMemberApplication= $temp;
1119     $this->is_modified= TRUE;
1120   }
1122   function getParameters($app)
1123   {
1124     $tmp = $this->getReleases();
1125     $ret = array();
1126     if(in_array($this->FAIrelease,$tmp)){
1127       $tmp2 = array_flip($tmp);
1128       $base = $tmp2[$this->FAIrelease];
1129       $ldap = $this->config->get_ldap_link();
1130       $ldap->cd($this->config->current['BASE']);
1131       $ldap->search("(&(objectClass=gosaApplication)(cn=".$app.")(gosaApplicationParameter=*))",array("gosaApplicationParameter"));
1132       if($ldap->count()){
1133         $attrs = $ldap->fetch();
1134         for($i = 0 ; $i < $attrs['gosaApplicationParameter']['count'] ; $i ++ ){
1135           $name = preg_replace("/:.*$/","",$attrs['gosaApplicationParameter'][$i]);
1136           $ret[$name] = $name;
1137         } 
1138       }
1139     }
1140     return($ret);
1141   }
1143   function GetSubdirs($dir)
1144   {
1145     $ret = array();
1146     $tmp1 = split("/",$this->curCatDir);
1147  
1148     foreach($this->Categories as $path=>$cat){
1149       $tmp2 = split("/",$path);
1150       
1151       if((empty($this->curCatDir))&&(!preg_match("/\//",$path))){
1152         $abort = false;
1153       }elseif(((count($tmp1))+1) == (count($tmp2))){
1154         $abort = false;
1155         for($i = 0 ; $i < count($tmp1) ; $i++){
1156           if($tmp1[$i] != $tmp2[$i]){
1157             $abort = true;
1158           }
1159         }
1160       }else{
1161         $abort= true;
1162       }
1163       if(!$abort){
1164         $ret[$path]=$cat;
1165       } 
1166     }
1167     return($ret);
1168   }
1171   function PrepareForCopyPaste($source)
1172   {
1173     $this->FAIrelease = $this->InitialFAIrelease = $source->FAIrelease;
1174     $this->gosaMemberApplication = $source->gosaMemberApplication;
1175     $this->appoption = $source->appoption;
1176   }
1179   /* Return plugin informations for acl handling  */ 
1180   function plInfo()
1181   {
1182     return (array(
1183           "plShortName"   => _("Applications"),
1184           "plDescription" => _("Group applications"),
1185           "plSelfModify"  => FALSE,
1186           "plDepends"     => array(),
1187           "plPriority"    => 0,
1188           "plSection"     => array("admin"),
1189           "plCategory"    => array("groups"),
1190           "plProvidedAcls"=> array(
1191             "gosaMemberApplication"     => _("Application"),
1192             "FAIrelease"                => _("Release"),
1193             "gosaApplicationParameter"  => _("Application parameter"))
1194           ));
1195   }
1197 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1198 ?>