Code

Added seperate list handler for gotomasses.
[gosa.git] / plugins / 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   function IsReleaseManagementActivated()
37   {
38     /* Check if we should enable the release selection */
39     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
40     if(!empty($tmp)){
41       return(true);
42     }
43     return(false);
44   }
46   function applicationManagement (&$config, &$ui)
47   {
48     /* Save configuration for internal use */
49     $this->config   = &$config;
50     $this->ui       = &$ui;
52     /* Check if copy & paste is activated */
53     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
54       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
55     }
57     /* Creat dialog object */
58     $this->DivListApplication = new divListApplication($this->config,$this);
60     if($this->IsReleaseManagementActivated()){
61     /* Check if we should enable the release selection */
62       $this->enableReleaseManagement = true;
64       /* Hide SubSearch checkbox */
65       $this->DivListApplication->DisableCheckBox("SubSearch");
66     }
67   }
69   function getReleases($base)
70   {
71     $ldap                   = $this->config->get_ldap_link();
72     $dn                     = "ou=apps,".$base;
73     $ret                    = array();
74     $ret ["ou=apps,".$base] = "/";
76     $ldap->cd($dn);
77     $ldap->search("objectClass=organizationalUnit",array("ou"));
79     while($attrs = $ldap->fetch()){
80       $str = str_replace($dn,"",$attrs['dn']);
81       $tmp = array_reverse( split("ou=",$str));
82       $str = "";
83       foreach($tmp as $val){
84         $val = trim(preg_replace("/,/","",$val));
85         if(empty($val)) break;
86         $str .= "/".$val;
87       } 
88       if(!empty($str)){
89         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
90       }
91     }
92     asort($ret);
93     return($ret);
94   }
96   function execute()
97   {
98     /* Call parent execute */
99     plugin::execute();
102     /**************** 
103       Variable init 
104      ****************/
106     /* These vars will be stored if you try to open a locked app, 
107         to be able to perform your last requests after showing a warning message */
108     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/");
110     $smarty       = get_smarty();             // Smarty instance
111     $s_action     = "";                       // Contains the action to proceed
112     $s_entry      = "";                       // The value for s_action
113     $base_back    = "";                       // The Link for Backbutton
114     
115     /* Test Posts */
116     foreach($_POST as $key => $val){
117       // Post for delete
118       if(preg_match("/appl_del.*/",$key)){
119         $s_action = "del";
120         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
121         // Post for edit
122       }elseif(preg_match("/appl_edit_.*/",$key)){
123         $s_action="edit";
124         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
125         // Post for new
126       }elseif(preg_match("/^copy_.*/",$key)){
127         $s_action="copy";
128         $s_entry  = preg_replace("/^copy_/i","",$key);
129       }elseif(preg_match("/^cut_.*/",$key)){
130         $s_action="cut";
131         $s_entry  = preg_replace("/^cut_/i","",$key);
132         // Post for new
133       }elseif(preg_match("/^appl_new.*/",$key)){
134         $s_action="new";
135       }elseif(preg_match("/^remove_multiple_applications/",$key)){
136         $s_action="del_multiple";
137       }elseif(preg_match("/^editPaste.*/i",$key)){
138         $s_action="editPaste";
139       }elseif(preg_match("/^multiple_copy_groups/",$key)){
140         $s_action = "copy_multiple";
141       }elseif(preg_match("/^multiple_cut_groups/",$key)){
142         $s_action = "cut_multiple";
143       }
144     }
146     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
147       $s_action ="edit";
148       $s_entry  = $_GET['id'];
149     }
151     $s_entry  = preg_replace("/_.$/","",$s_entry);
154     /**************** 
155       Copy & Paste handling  
156      ****************/
158     /* Display the copy & paste dialog, if it is currently open */
159     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
160     if($ret){
161       return($ret);
162     }
165     /**************** 
166       Create a new app 
167      ****************/
169     /* New application? */
170     if ($s_action=="new"){
172       /* By default we set 'dn' to 'new', all relevant plugins will
173          react on this. */
174       $this->dn= "new";
176       /* Create new usertab object */
177       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
178       $this->apptabs->set_acl_base($this->DivListApplication->selectedBase);
179     }
182     /**************** 
183       Edit entry canceled 
184      ****************/
186     /* Cancel dialogs */
187     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
188       del_lock ($this->apptabs->dn);
189       unset ($this->apptabs);
190       $this->apptabs= NULL;
191       unset ($_SESSION['objectinfo']);
192     }
195     /**************** 
196       Edit entry finished 
197      ****************/
199     /* Finish apps edit is triggered by the tabulator dialog, so
200        the user wants to save edited data. Check and save at this
201        point. */
202     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
204       /* Check tabs, will feed message array */
205       $this->apptabs->last= $this->apptabs->current;
206       $this->apptabs->save_object();
207       $message= $this->apptabs->check();
209       /* Save, or display error message? */
210       if (count($message) == 0){
212         /* Save data data to ldap */
213         $this->apptabs->set_release($this->DivListApplication->selectedRelease);
214         $this->apptabs->save();
216         if (!isset($_POST['edit_apply'])){
217           /* Application has been saved successfully, remove lock from
218              LDAP. */
219           if ($this->dn != "new"){
220             del_lock ($this->dn);
221           }
222           unset ($this->apptabs);
223           $this->apptabs= NULL;
224           unset ($_SESSION['objectinfo']);
225         }
226       } else {
227         /* Ok. There seem to be errors regarding to the tab data,
228            show message and continue as usual. */
229         show_errors($message);
230       }
231     }
234     /**************** 
235       Edit entry  
236      ****************/
238     /* User wants to edit data? */
239     if (($s_action=="edit") && (!isset($this->apptabs->config))){
241       /* Get 'dn' from posted 'applist', must be unique */
242       $this->dn= $this->applications[$s_entry]['dn'];
244       /* Check locking, save current plugin in 'back_plugin', so
245          the dialog knows where to return. */
246       if (($user= get_lock($this->dn)) != ""){
247         return(gen_locked_message ($user, $this->dn));
248       }
250       /* Lock the current entry, so everyone will get the
251          above dialog */
252       add_lock ($this->dn, $this->ui->dn);
254       /* Register apptabs to trigger edit dialog */
255       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
256       $this->apptabs->set_acl_base($this->dn);
257       $_SESSION['objectinfo']= $this->dn;
258     }
262     /********************
263       Delete MULTIPLE entries requested, display confirm dialog
264      ********************/
266     if ($s_action=="del_multiple"){
267       $ids = $this->list_get_selected_items();
269       if(count($ids)){
271         foreach($ids as $id){
272           $dn = $this->applications[$id]['dn'];
273           if (($user= get_lock($dn)) != ""){
274             return(gen_locked_message ($user, $dn));
275           }
276           $this->dns[$id] = $dn;
277         }
279         $dns_names = "<br><pre>";
280         foreach($this->dns as $dn){
281           add_lock ($dn, $this->ui->dn);
282           $dns_names .= $dn."\n";
283         }
284         $dns_names .="</pre>";
286         /* Lock the current entry, so nobody will edit it during deletion */
287         if (count($this->dns) == 1){
288           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
289         } else {
290           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
291         }
292         $smarty->assign("multiple", true);
293         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
294       }
295     }
298     /********************
299       Delete MULTIPLE entries confirmed
300      ********************/
302     /* Confirmation for deletion has been passed. Users should be deleted. */
303     if (isset($_POST['delete_multiple_application_confirm'])){
305       /* Remove user by user and check acls before removeing them */
306       foreach($this->dns as $key => $dn){
308         $ui = get_userinfo();
309         $acl = $ui->get_permissions($dn ,"application/application");
310         if (preg_match('/d/', $acl)){
312           /* Delete request is permitted, perform LDAP action */
313           $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
314           $this->apptabs->set_acl_base($dn);
315           $this->apptabs->delete ();
316           unset ($this->apptabs);
317           $this->apptabs= NULL;
319         } else {
320           /* Normally this shouldn't be reached, send some extra
321              logs to notify the administrator */
322           print_red (_("You are not allowed to delete this application!"));
323           new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
324         }
325         /* Remove lock file after successfull deletion */
326         del_lock ($dn);
327         unset($this->dns[$key]);
328       }
329     }
332     /********************
333       Delete MULTIPLE entries Canceled
334      ********************/
336     /* Remove lock */
337     if(isset($_POST['delete_multiple_application_cancel'])){
338       foreach($this->dns as $key => $dn){
339         del_lock ($dn);
340         unset($this->dns[$key]);
341       }
342     }
344     /**************** 
345       Delete app 
346      ****************/
348     /* Remove user was requested */
349     if ($s_action == "del"){
351       /* Get 'dn' from posted 'uid' */
352       $this->dn= $this->applications[$s_entry]['dn'];
354       /* Load permissions for selected 'dn' and check if
355          we're allowed to remove this 'dn' */
356       $ui = get_userinfo();
357       $acl = $ui->get_permissions($this->dn ,"application/application");
359       if(preg_match("/d/",$acl)){
360         /* Check locking, save current plugin in 'back_plugin', so
361            the dialog knows where to return. */
362         if (($user= get_lock($this->dn)) != ""){
363           return (gen_locked_message ($user, $this->dn));
364         }
366         /* Lock the current entry, so nobody will edit it during deletion */
367         add_lock ($this->dn, $this->ui->dn);
368         $smarty= get_smarty();
369         $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
370         $smarty->assign("multiple", false);
371         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
372       } else {
374         /* Obviously the user isn't allowed to delete. Show message and
375            clean session. */
376         print_red (_("You are not allowed to delete this application!"));
377       }
378     }
381     /**************** 
382       Delete app confirmed 
383      ****************/
385     /* Confirmation for deletion has been passed. Group should be deleted. */
386     if (isset($_POST['delete_app_confirm'])){
388       /* Some nice guy may send this as POST, so we've to check
389          for the permissions again. */
390       $ui = get_userinfo();
391       $acl = $ui->get_permissions($this->dn ,"application/application");
393       if(preg_match("/d/",$acl)){
395         /* Delete request is permitted, perform LDAP action */
396         $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
397         $this->apptabs->set_acl_base($this->dn);
398         $this->apptabs->delete ();
399         unset ($this->apptabs);
400         $this->apptabs= NULL;
402       } else {
404         /* Normally this shouldn't be reached, send some extra
405            logs to notify the administrator */
406         print_red (_("You are not allowed to delete this application!"));
407         new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
408       }
410       /* Remove lock file after successfull deletion */
411       del_lock ($this->dn);
412     }
415     /**************** 
416       Delete app canceled 
417      ****************/
419     /* Delete application canceled? */
420     if (isset($_POST['delete_cancel'])){
421       del_lock ($this->dn);
422       unset($_SESSION['objectinfo']);
423     }
425     /* Show tab dialog if object is present */
426     if (($this->apptabs) && (isset($this->apptabs->config))){
427       $display= $this->apptabs->execute();
429       /* Don't show buttons if tab dialog requests this */
430       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
431         $display.= "<p style=\"text-align:right\">\n";
432         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
433         $display.= "&nbsp;\n";
434         if ($this->dn != "new"){
435           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
436           $display.= "&nbsp;\n";
437         }
438         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
439         $display.= "</p>";
440       }
441       return ($display);
442     }
445     /****************
446       Dialog display
447      ****************/
449     /* Check if there is a snapshot dialog open */
450     $base = $this->DivListApplication->selectedBase;
451     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
452       return($str);
453     }
455     /* Display dialog with system list */
456     $this->DivListApplication->parent = $this;
457     $this->DivListApplication->execute();
458     $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
459     $this->reload();
460     $this->DivListApplication->setEntries($this->applications);
461     return($this->DivListApplication->Draw());
462   }
465   /* Return departments, that will be included within snapshot detection */
466   function get_used_snapshot_bases(){
467     if($this->DivListApplication->selectedRelease == "main"){
468       return(array($this->DivListApplication->selectedBase));
469     }else{
470       return(array($this->DivListApplication->selectedRelease));
471     }
472   }
475   function reload()
476   {
477     $this->applications= array();
479     /* Set base for all searches */
480     $base       = $this->DivListApplication->selectedBase;
481     $release    = $this->DivListApplication->selectedRelease;
482     $Regex      = $this->DivListApplication->Regex;
483     $SubSearch  = $this->DivListApplication->SubSearch; 
484     $Flags      =  GL_NONE | GL_SIZELIMIT;
485     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
486     $tmp        = array();
487     $Releases   = $this->getReleases($base);
489     if(!$this->enableReleaseManagement){
490       $use_base = "ou=apps,".$base;
491     }else{
492       if(isset($Releases[$release])){
493         $use_base  = $release;
494       }else{
495         $use_base  = "ou=apps,".$base;
496       }
497     }
499     if($SubSearch){
500       $Flags    |= GL_SUBSEARCH;  
501     }
502    
503     $res= get_list($Filter, "application", $use_base, array("cn","description","dn","objectClass"), $Flags);
504     foreach ($res as $val){
505       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
506     }
508     ksort($tmp);
509     $this->applications=array();
510     foreach($tmp as $val){
511       $this->applications[]=$val;
512     }
513     reset ($this->applications);
514   }
516   function remove_from_parent()
517   {
518     /* Optionally execute a command after we're done */
519     $this->postremove();
520   }
523   function copyPasteHandling_from_queue($s_action,$s_entry)
524   {
525     /* Check if Copy & Paste is disabled */
526     if(!is_object($this->CopyPasteHandler)){
527       return("");
528     }
530     /* Add a single entry to queue */
531     if($s_action == "cut" || $s_action == "copy"){
533       /* Cleanup object queue */
534       $this->CopyPasteHandler->cleanup_queue();
535       $dn = $this->applications[$s_entry]['dn'];
536       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
537     }
540     /* Add entries to queue */
541     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
543       /* Cleanup object queue */
544       $this->CopyPasteHandler->cleanup_queue();
546       /* Add new entries to CP queue */
547       foreach($this->list_get_selected_items() as $id){
548         $dn = $this->applications[$id]['dn'];
550         if($s_action == "copy_multiple"){
551           $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
552         }
553         if($s_action == "cut_multiple"){
554           $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
555         }
556       }
557     }
559     /* Start pasting entries */
560     if($s_action == "editPaste"){
561       $this->start_pasting_copied_objects = TRUE;
562     }
565     /* Return C&P dialog */
566     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
568       /* Load entry from queue and set base */
569       $this->CopyPasteHandler->load_entry_from_queue();
570       $this->CopyPasteHandler->SetVar("base",$this->DivListApplication->selectedBase);
572       /* Get dialog */
573       $data = $this->CopyPasteHandler->execute();
575       /* Return dialog data */
576       if(!empty($data)){
577         return($data);
578       }
579     }
581     /* Automatically disable status for pasting */
582     if(!$this->CopyPasteHandler->entries_queued()){
583       $this->start_pasting_copied_objects = FALSE;
584     }
585     return("");
586   }
589   function list_get_selected_items()
590   {
591     $ids = array();
592     foreach($_POST as $name => $value){
593       if(preg_match("/^item_selected_[0-9]*$/",$name)){
594         $id   = preg_replace("/^item_selected_/","",$name);
595         $ids[$id] = $id;
596       }
597     }
598     return($ids);
599   }
602   /* Save to LDAP */
603   function save()
604   {
605     /* Optionally execute a command after we're done */
606     $this->postcreate();
607   }
609   function remove_lock()
610   {
611     if (isset($this->apptabs->dn)){
612       del_lock ($this->apptabs->dn);
613     }
614   }
616   function save_object() {
617     $this->DivListApplication->save_object();
618   }
620   function check() {}
621   function adapt_from_template($dn) {}
622   function password_change_needed() {}
624 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
625 ?>