Code

Reset array of dns to delete, before adding new dns
[gosa.git] / gosa-plugins / goto / admin / applications / class_applicationManagement.inc
1 <?php
2 /*
3    This code is part of GOsa (https://gosa.gonicus.de)
4    Copyright (C) 2003  Cajus Pollmeier
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
21 class applicationManagement extends plugin
22 {
23   /* Definitions */
24   var $plHeadline     = "Applications";
25   var $plDescription  = "This does something";
27   /* Dialog attributes */
28   var $apptabs                  = NULL;
29   var $ui                       = NULL;
30   var $CopyPasteHandler         = NULL;
31   var $DivListApplication       = NULL;
32   var $applications             = array();
33   var $enableReleaseManagement  = false;
34   var $start_pasting_copied_objects = FALSE;
36   var $app_base     ="";
37   var $app_release  ="";
39   var $dns = array();
41   function IsReleaseManagementActivated()
42   {
43     /* Check if we should enable the release selection */
44     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
45     if(!empty($tmp)){
46       return(true);
47     }
48     return(false);
49   }
52   function applicationManagement (&$config, &$ui)
53   {
54     /* Save configuration for internal use */
55     $this->config   = &$config;
56     $this->ui       = &$ui;
58     /* Check if copy & paste is activated */
59     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
60       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
61     }
63     /* Creat dialog object */
64     $this->DivListApplication = new divListApplication($this->config,$this);
66     if($this->IsReleaseManagementActivated()){
67     /* Check if we should enable the release selection */
68       $this->enableReleaseManagement = true;
70       /* Hide SubSearch checkbox */
71       $this->DivListApplication->DisableCheckBox("SubSearch");
72     }
74     /* Set default release */
75     if(!$this->IsReleaseManagementActivated()){
76       $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
77       if(!session::is_set("app_filter")){
78         session::set("app_filter",array("app_base" => $this->app_base));
79       }
80       $app_filter     = session::get("app_filter");
81       $this->app_base = $app_filter['app_base'];
82     }else{
83       $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
84       if(!session::is_set("app_filter")){
85         session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
86       }
87       $app_filter         = session::get("app_filter");
88       $this->app_base     = $app_filter['app_base'];
89       $this->app_release  = $app_filter['app_release'];
90     }
91   }
94   function getReleases()
95   {
96     $ldap = $this->config->get_ldap_link();
97     $ret  = array();
98     $base = $this->app_base; 
100     $ret[$this->app_base] = "/";
101     $ldap->cd($base);
102     $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
103     while($attrs = $ldap->fetch()){
104       $str = str_replace($base,"",$attrs['dn']);
105       $tmp = array_reverse( split("ou=",$str));
106       $str = "";
107       foreach($tmp as $val){
108         $val = trim(preg_replace("/,/","",$val));
109         if(empty($val)) break;
110         $str .= "/".$val;
111       } 
112       if(!empty($str)){
113         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
114       }
115     }
116     asort($ret);
117     return($ret);
118   }
120   function execute()
121   {
122     /* Call parent execute */
123     plugin::execute();
126     /**************** 
127       Variable init 
128      ****************/
130     /* These vars will be stored if you try to open a locked app, 
131         to be able to perform your last requests after showing a warning message */
132     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
134     $smarty       = get_smarty();             // Smarty instance
135     $s_action     = "";                       // Contains the action to proceed
136     $s_entry      = "";                       // The value for s_action
137     $base_back    = "";                       // The Link for Backbutton
138     
139     /* Test Posts */
140     foreach($_POST as $key => $val){
141       // Post for delete
142       if(preg_match("/appl_del.*/",$key)){
143         $s_action = "del";
144         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
145         // Post for edit
146       }elseif(preg_match("/appl_edit_.*/",$key)){
147         $s_action="edit";
148         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
149         // Post for new
150       }elseif(preg_match("/^copy_.*/",$key)){
151         $s_action="copy";
152         $s_entry  = preg_replace("/^copy_/i","",$key);
153       }elseif(preg_match("/^cut_.*/",$key)){
154         $s_action="cut";
155         $s_entry  = preg_replace("/^cut_/i","",$key);
156         // Post for new
157       }elseif(preg_match("/^appl_new.*/",$key)){
158         $s_action="new";
159       }elseif(preg_match("/^remove_multiple_applications/",$key)){
160         $s_action="del_multiple";
161       }elseif(preg_match("/^editPaste.*/i",$key)){
162         $s_action="editPaste";
163       }elseif(preg_match("/^multiple_copy_groups/",$key)){
164         $s_action = "copy_multiple";
165       }elseif(preg_match("/^multiple_cut_groups/",$key)){
166         $s_action = "cut_multiple";
167       }
168     }
170     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
171       $s_action ="edit";
172       $s_entry  = $_GET['id'];
173     }
175     $s_entry  = preg_replace("/_.$/","",$s_entry);
178     /* handle C&P from layers menu */
179     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
180       $s_action = "copy_multiple";
181     }
182     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
183       $s_action = "cut_multiple";
184     }
185     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
186       $s_action = "editPaste";
187     }
189     /* Create options */
190     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
191       $s_action = "new";
192     }
194     /* handle remove from layers menu */
195     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
196       $s_action = "del_multiple";
197     }
199     /**************** 
200       Copy & Paste handling  
201      ****************/
203     /* Display the copy & paste dialog, if it is currently open */
204     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
205     if($ret){
206       return($ret);
207     }
209     /**************** 
210       Create a new app 
211      ****************/
213     /* New application? */
214     if ($s_action=="new"){
216       /* By default we set 'dn' to 'new', all relevant plugins will
217          react on this. */
218       $this->dn= "new";
220       /* Create new usertab object */
221       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
222       $this->apptabs->parent = &$this;
223       $this->apptabs->set_acl_base($this->app_base);
224     }
227     /**************** 
228       Edit entry canceled 
229      ****************/
231     /* Cancel dialogs */
232     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
233       $this->remove_lock();
234       $this->apptabs= NULL;
235       session::un_set('objectinfo');
236     }
239     /**************** 
240       Edit entry finished 
241      ****************/
243     /* Finish apps edit is triggered by the tabulator dialog, so
244        the user wants to save edited data. Check and save at this
245        point. */
246     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
248       /* Check tabs, will feed message array */
249       $this->apptabs->last= $this->apptabs->current;
250       $this->apptabs->save_object();
251       $message= $this->apptabs->check();
253       /* Save, or display error message? */
254       if (count($message) == 0){
256         /* Save data data to ldap */
257         $this->apptabs->save();
259         if (!isset($_POST['edit_apply'])){
260           /* Application has been saved successfully, remove lock from
261              LDAP. */
262           if ($this->dn != "new"){
263             $this->remove_lock();
264           }
265           $this->apptabs= NULL;
266           session::un_set('objectinfo');
267         }else{
269           /* Reinitialize tab */
270           if($this->apptabs instanceof tabs){
271             $this->apptabs->re_init();
272           }
273         }
274       } else {
275         /* Ok. There seem to be errors regarding to the tab data,
276            show message and continue as usual. */
277         msg_dialog::displayChecks($message);
278       }
279     }
282     /**************** 
283       Edit entry  
284      ****************/
286     /* User wants to edit data? */
287     if (($s_action=="edit") && (!isset($this->apptabs->config))){
289       /* Get 'dn' from posted 'applist', must be unique */
290       $this->dn= $this->applications[$s_entry]['dn'];
292       /* Check locking, save current plugin in 'back_plugin', so
293          the dialog knows where to return. */
294       if (($user= get_lock($this->dn)) != ""){
295         return(gen_locked_message ($user, $this->dn));
296       }
298       /* Lock the current entry, so everyone will get the
299          above dialog */
300       add_lock ($this->dn, $this->ui->dn);
302       /* Register apptabs to trigger edit dialog */
303       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
304       $this->apptabs->parent = &$this;
305       $this->apptabs->set_acl_base($this->dn);
306       session::set('objectinfo',$this->dn);
307     }
311     /********************
312       Delete MULTIPLE entries requested, display confirm dialog
313      ********************/
315     if ($s_action=="del_multiple"){
316       $ids = $this->list_get_selected_items();
318       if(count($ids)){
319         $this->dns = array();
320         foreach($ids as $id){
321           $this->dns[$id] = $this->applications[$id]['dn'];
322         }
323         if ($user= get_multiple_locks($this->dns)){
324           return(gen_locked_message($user,$this->dns));
325         }
327         $dns_names = array();
328         foreach($this->dns as $dn){
329           add_lock ($dn, $this->ui->dn);
330           $dns_names[] =@LDAP::fix($dn);
331         }
333         /* Lock the current entry, so nobody will edit it during deletion */
334         $smarty->assign("intro",  msgPool::deleteInfo($dns_names,_("application")));
335         $smarty->assign("multiple", true);
336         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
337       }
338     }
341     /********************
342       Delete MULTIPLE entries confirmed
343      ********************/
345     /* Confirmation for deletion has been passed. Users should be deleted. */
346     if (isset($_POST['delete_multiple_application_confirm'])){
348       /* Remove user by user and check acls before removeing them */
349       foreach($this->dns as $key => $dn){
351         $ui = get_userinfo();
352         $acl = $ui->get_permissions($dn ,"application/application");
353         if (preg_match('/d/', $acl)){
355           /* Delete request is permitted, perform LDAP action */
356           $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
357           $this->apptabs->parent = &$this;
358           $this->apptabs->set_acl_base($dn);
359           $this->apptabs->delete ();
360           unset ($this->apptabs);
361           $this->apptabs= NULL;
363         } else {
364           /* Normally this shouldn't be reached, send some extra
365              logs to notify the administrator */
366           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
367           new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
368         }
369       }
371       /* Remove lock file after successfull deletion */
372       $this->remove_lock();
373       $this->dns = array();
374     }
377     /********************
378       Delete MULTIPLE entries Canceled
379      ********************/
381     /* Remove lock */
382     if(isset($_POST['delete_multiple_application_cancel'])){
384       /* Remove lock file after successfull deletion */
385       $this->remove_lock();
386       $this->dns = array();
387     }
389     /**************** 
390       Delete app 
391      ****************/
393     /* Remove user was requested */
394     if ($s_action == "del"){
396       /* Get 'dn' from posted 'uid' */
397       $this->dn= $this->applications[$s_entry]['dn'];
399       /* Load permissions for selected 'dn' and check if
400          we're allowed to remove this 'dn' */
401       $ui = get_userinfo();
402       $acl = $ui->get_permissions($this->dn ,"application/application");
404       if(preg_match("/d/",$acl)){
405         /* Check locking, save current plugin in 'back_plugin', so
406            the dialog knows where to return. */
407         if (($user= get_lock($this->dn)) != ""){
408           return (gen_locked_message ($user, $this->dn));
409         }
411         /* Lock the current entry, so nobody will edit it during deletion */
412         add_lock ($this->dn, $this->ui->dn);
413         $smarty= get_smarty();
414         $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
415         $smarty->assign("multiple", false);
416         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
417       } else {
419         /* Obviously the user isn't allowed to delete. Show message and
420            clean session. */
421         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
422       }
423     }
426     /**************** 
427       Delete app confirmed 
428      ****************/
430     /* Confirmation for deletion has been passed. Group should be deleted. */
431     if (isset($_POST['delete_app_confirm'])){
433       /* Some nice guy may send this as POST, so we've to check
434          for the permissions again. */
435       $ui = get_userinfo();
436       $acl = $ui->get_permissions($this->dn ,"application/application");
438       if(preg_match("/d/",$acl)){
440         /* Delete request is permitted, perform LDAP action */
441         $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
442         $this->apptabs->parent = &$this;
443         $this->apptabs->set_acl_base($this->dn);
444         $this->apptabs->delete ();
445         unset ($this->apptabs);
446         $this->apptabs= NULL;
448       } else {
450         /* Normally this shouldn't be reached, send some extra
451            logs to notify the administrator */
452         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
453         new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
454       }
456       /* Remove lock file after successfull deletion */
457       $this->remove_lock();
458     }
461     /**************** 
462       Delete app canceled 
463      ****************/
465     /* Delete application canceled? */
466     if (isset($_POST['delete_cancel'])){
467       $this->remove_lock();
468       session::un_set('objectinfo');
469     }
471     /* Show tab dialog if object is present */
472     if (($this->apptabs) && (isset($this->apptabs->config))){
473       $display= $this->apptabs->execute();
475       /* Don't show buttons if tab dialog requests this */
476       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
477         $display.= "<p style=\"text-align:right\">\n";
478         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
479         $display.= "&nbsp;\n";
480         if ($this->dn != "new"){
481           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
482           $display.= "&nbsp;\n";
483         }
484         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
485         $display.= "</p>";
486       }
487       return ($display);
488     }
491     /****************
492       Dialog display
493      ****************/
495     /* Check if there is a snapshot dialog open */
496     if($this->IsReleaseManagementActivated()){
497       $base = $this->app_release;  
498     }else{
499       $base = $this->app_base;  
500     }
501     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
502       return($str);
503     }
505     /* Display dialog with system list */
506     $this->DivListApplication->parent = $this;
507     $this->DivListApplication->execute();
508     if(!$this->IsReleaseManagementActivated()){
509       $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
510     } 
511     $this->reload();
512     $this->DivListApplication->setEntries($this->applications);
513     return($this->DivListApplication->Draw());
514   }
517   /* Return departments, that will be included within snapshot detection */
518   function get_used_snapshot_bases()
519   {
520     if($this->IsReleaseManagementActivated()){
521       return(array($this->app_release));  
522     }else{
523       return(array($this->app_base));  
524     }
525   }
528   function reload()
529   {
530     $this->applications= array();
532     /* Set base for all searches */
533     $Regex      = $this->DivListApplication->Regex;
534     $SubSearch  = $this->DivListApplication->SubSearch; 
535     $Flags      =  GL_NONE | GL_SIZELIMIT;
536     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
537     $tmp        = array();
539     if(!$this->IsReleaseManagementActivated()){
540       $use_base = $this->app_base;
541       if($SubSearch){
542         $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
543       }
544     }else{
545       $use_base = $this->app_release;
546       $SubSearch= FALSE;
547     }
548   
549     if($SubSearch){ 
550       $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
551     }else{
552       $res= get_list($Filter, "application",$use_base, array("cn","description","dn","objectClass"), $Flags);
553     }
554     $tmp2 = array();
555     foreach ($res as $val){
556       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
557       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
558     }
560     natcasesort($tmp2);
561     $this->applications=array();
562     foreach($tmp2 as $val){
563       $this->applications[]=$tmp[$val];
564     }
565     reset ($this->applications);
566   }
568   function remove_from_parent()
569   {
570     /* Optionally execute a command after we're done */
571     $this->postremove();
572   }
575   function copyPasteHandling_from_queue($s_action,$s_entry)
576   {
577     /* Check if Copy & Paste is disabled */
578     if(!is_object($this->CopyPasteHandler)){
579       return("");
580     }
582     /* Add a single entry to queue */
583     if($s_action == "cut" || $s_action == "copy"){
585       /* Cleanup object queue */
586       $this->CopyPasteHandler->cleanup_queue();
587       $dn = $this->applications[$s_entry]['dn'];
588       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
589     }
592     /* Add entries to queue */
593     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
595       /* Cleanup object queue */
596       $this->CopyPasteHandler->cleanup_queue();
598       /* Add new entries to CP queue */
599       foreach($this->list_get_selected_items() as $id){
600         $dn = $this->applications[$id]['dn'];
602         if($s_action == "copy_multiple"){
603           $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
604         }
605         if($s_action == "cut_multiple"){
606           $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
607         }
608       }
609     }
611     /* Start pasting entries */
612     if($s_action == "editPaste"){
613       $this->start_pasting_copied_objects = TRUE;
614     }
617     /* Return C&P dialog */
618     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
621       /* Get dialog */
622       $this->CopyPasteHandler->SetVar("base",$this->app_base);
623       $this->CopyPasteHandler->SetVar("parent",$this);
624       $data = $this->CopyPasteHandler->execute();
626       /* Return dialog data */
627       if(!empty($data)){
628         return($data);
629       }
630     }
632     /* Automatically disable status for pasting */
633     if(!$this->CopyPasteHandler->entries_queued()){
634       $this->start_pasting_copied_objects = FALSE;
635     }
636     return("");
637   }
640   function list_get_selected_items()
641   {
642     $ids = array();
643     foreach($_POST as $name => $value){
644       if(preg_match("/^item_selected_[0-9]*$/",$name)){
645         $id   = preg_replace("/^item_selected_/","",$name);
646         $ids[$id] = $id;
647       }
648     }
649     return($ids);
650   }
653   /* Save to LDAP */
654   function save()
655   {
656     /* Optionally execute a command after we're done */
657     $this->postcreate();
658   }
661   function remove_lock()
662   {
663     if (isset($this->apptabs->dn)){
664       del_lock ($this->apptabs->dn);
665     }
666     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
667       del_lock($this->dn);
668     }
669     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
670       del_lock($this->dns);
671     }
672   }
675   function save_object() 
676   {
677     $this->DivListApplication->save_object();
678     if(is_object($this->CopyPasteHandler)){
679       $this->CopyPasteHandler->save_object();
680     }
681     
682     if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
683       $sel_rel = get_post('app_release');
684       $releases = array_flip($this->getReleases());
685       if(isset($releases[$sel_rel])){
686         $this->app_release = $releases[$sel_rel];
687       }
688       $app_filter     = session::get("app_filter");
689       $app_filter['app_release'] = $this->app_release;
690       session::set("app_filter",$app_filter);
691     }elseif(!$this->IsReleaseManagementActivated()){
692       $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
693       $app_filter     = session::get("app_filter");
694       $app_filter['app_base'] = $this->app_base;
695       session::set("app_filter",$app_filter);
696     }
697   }
699   function check() {}
700   function adapt_from_template($dn, $skip= array()) {}
701   function password_change_needed() {}
703 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
704 ?>