Code

Added admin stuff
[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   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                     = get_ou('applicationou').$base;
73     $ret                    = array();
74     $ret [get_ou('applicationou').$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::set('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     /* handle C&P from layers menu */
155     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
156       $s_action = "copy_multiple";
157     }
158     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
159       $s_action = "cut_multiple";
160     }
161     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
162       $s_action = "editPaste";
163     }
165     /* Create options */
166     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
167       $s_action = "new";
168     }
170     /* handle remove from layers menu */
171     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
172       $s_action = "del_multiple";
173     }
175     /**************** 
176       Copy & Paste handling  
177      ****************/
179     /* Display the copy & paste dialog, if it is currently open */
180     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
181     if($ret){
182       return($ret);
183     }
185     /**************** 
186       Create a new app 
187      ****************/
189     /* New application? */
190     if ($s_action=="new"){
192       /* By default we set 'dn' to 'new', all relevant plugins will
193          react on this. */
194       $this->dn= "new";
196       /* Create new usertab object */
197       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
198       $this->apptabs->set_acl_base($this->DivListApplication->selectedBase);
199     }
202     /**************** 
203       Edit entry canceled 
204      ****************/
206     /* Cancel dialogs */
207     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
208       del_lock ($this->apptabs->dn);
209       unset ($this->apptabs);
210       $this->apptabs= NULL;
211       session::un_set('objectinfo');
212     }
215     /**************** 
216       Edit entry finished 
217      ****************/
219     /* Finish apps edit is triggered by the tabulator dialog, so
220        the user wants to save edited data. Check and save at this
221        point. */
222     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
224       /* Check tabs, will feed message array */
225       $this->apptabs->last= $this->apptabs->current;
226       $this->apptabs->save_object();
227       $message= $this->apptabs->check();
229       /* Save, or display error message? */
230       if (count($message) == 0){
232         /* Save data data to ldap */
233         $this->apptabs->set_release($this->DivListApplication->selectedRelease);
234         $this->apptabs->save();
236         if (!isset($_POST['edit_apply'])){
237           /* Application has been saved successfully, remove lock from
238              LDAP. */
239           if ($this->dn != "new"){
240             del_lock ($this->dn);
241           }
242           unset ($this->apptabs);
243           $this->apptabs= NULL;
244           session::un_set('objectinfo');
245         }
246       } else {
247         /* Ok. There seem to be errors regarding to the tab data,
248            show message and continue as usual. */
249         show_errors($message);
250       }
251     }
254     /**************** 
255       Edit entry  
256      ****************/
258     /* User wants to edit data? */
259     if (($s_action=="edit") && (!isset($this->apptabs->config))){
261       /* Get 'dn' from posted 'applist', must be unique */
262       $this->dn= $this->applications[$s_entry]['dn'];
264       /* Check locking, save current plugin in 'back_plugin', so
265          the dialog knows where to return. */
266       if (($user= get_lock($this->dn)) != ""){
267         return(gen_locked_message ($user, $this->dn));
268       }
270       /* Lock the current entry, so everyone will get the
271          above dialog */
272       add_lock ($this->dn, $this->ui->dn);
274       /* Register apptabs to trigger edit dialog */
275       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
276       $this->apptabs->set_acl_base($this->dn);
277       session::set('objectinfo',$this->dn);
278     }
282     /********************
283       Delete MULTIPLE entries requested, display confirm dialog
284      ********************/
286     if ($s_action=="del_multiple"){
287       $ids = $this->list_get_selected_items();
289       if(count($ids)){
291         foreach($ids as $id){
292           $dn = $this->applications[$id]['dn'];
293           if (($user= get_lock($dn)) != ""){
294             return(gen_locked_message ($user, $dn));
295           }
296           $this->dns[$id] = $dn;
297         }
299         $dns_names = "<br><pre>";
300         foreach($this->dns as $dn){
301           add_lock ($dn, $this->ui->dn);
302           $dns_names .= $dn."\n";
303         }
304         $dns_names .="</pre>";
306         /* Lock the current entry, so nobody will edit it during deletion */
307         if (count($this->dns) == 1){
308           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
309         } else {
310           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
311         }
312         $smarty->assign("multiple", true);
313         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
314       }
315     }
318     /********************
319       Delete MULTIPLE entries confirmed
320      ********************/
322     /* Confirmation for deletion has been passed. Users should be deleted. */
323     if (isset($_POST['delete_multiple_application_confirm'])){
325       /* Remove user by user and check acls before removeing them */
326       foreach($this->dns as $key => $dn){
328         $ui = get_userinfo();
329         $acl = $ui->get_permissions($dn ,"application/application");
330         if (preg_match('/d/', $acl)){
332           /* Delete request is permitted, perform LDAP action */
333           $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
334           $this->apptabs->set_acl_base($dn);
335           $this->apptabs->delete ();
336           unset ($this->apptabs);
337           $this->apptabs= NULL;
339         } else {
340           /* Normally this shouldn't be reached, send some extra
341              logs to notify the administrator */
342           print_red (_("You are not allowed to delete this application!"));
343           new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
344         }
345         /* Remove lock file after successfull deletion */
346         del_lock ($dn);
347         unset($this->dns[$key]);
348       }
349     }
352     /********************
353       Delete MULTIPLE entries Canceled
354      ********************/
356     /* Remove lock */
357     if(isset($_POST['delete_multiple_application_cancel'])){
358       foreach($this->dns as $key => $dn){
359         del_lock ($dn);
360         unset($this->dns[$key]);
361       }
362     }
364     /**************** 
365       Delete app 
366      ****************/
368     /* Remove user was requested */
369     if ($s_action == "del"){
371       /* Get 'dn' from posted 'uid' */
372       $this->dn= $this->applications[$s_entry]['dn'];
374       /* Load permissions for selected 'dn' and check if
375          we're allowed to remove this 'dn' */
376       $ui = get_userinfo();
377       $acl = $ui->get_permissions($this->dn ,"application/application");
379       if(preg_match("/d/",$acl)){
380         /* Check locking, save current plugin in 'back_plugin', so
381            the dialog knows where to return. */
382         if (($user= get_lock($this->dn)) != ""){
383           return (gen_locked_message ($user, $this->dn));
384         }
386         /* Lock the current entry, so nobody will edit it during deletion */
387         add_lock ($this->dn, $this->ui->dn);
388         $smarty= get_smarty();
389         $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
390         $smarty->assign("multiple", false);
391         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
392       } else {
394         /* Obviously the user isn't allowed to delete. Show message and
395            clean session. */
396         print_red (_("You are not allowed to delete this application!"));
397       }
398     }
401     /**************** 
402       Delete app confirmed 
403      ****************/
405     /* Confirmation for deletion has been passed. Group should be deleted. */
406     if (isset($_POST['delete_app_confirm'])){
408       /* Some nice guy may send this as POST, so we've to check
409          for the permissions again. */
410       $ui = get_userinfo();
411       $acl = $ui->get_permissions($this->dn ,"application/application");
413       if(preg_match("/d/",$acl)){
415         /* Delete request is permitted, perform LDAP action */
416         $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
417         $this->apptabs->set_acl_base($this->dn);
418         $this->apptabs->delete ();
419         unset ($this->apptabs);
420         $this->apptabs= NULL;
422       } else {
424         /* Normally this shouldn't be reached, send some extra
425            logs to notify the administrator */
426         print_red (_("You are not allowed to delete this application!"));
427         new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
428       }
430       /* Remove lock file after successfull deletion */
431       del_lock ($this->dn);
432     }
435     /**************** 
436       Delete app canceled 
437      ****************/
439     /* Delete application canceled? */
440     if (isset($_POST['delete_cancel'])){
441       del_lock ($this->dn);
442       session::un_set('objectinfo');
443     }
445     /* Show tab dialog if object is present */
446     if (($this->apptabs) && (isset($this->apptabs->config))){
447       $display= $this->apptabs->execute();
449       /* Don't show buttons if tab dialog requests this */
450       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
451         $display.= "<p style=\"text-align:right\">\n";
452         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
453         $display.= "&nbsp;\n";
454         if ($this->dn != "new"){
455           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
456           $display.= "&nbsp;\n";
457         }
458         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
459         $display.= "</p>";
460       }
461       return ($display);
462     }
465     /****************
466       Dialog display
467      ****************/
469     /* Check if there is a snapshot dialog open */
470     $base = $this->DivListApplication->selectedBase;
471     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
472       return($str);
473     }
475     /* Display dialog with system list */
476     $this->DivListApplication->parent = $this;
477     $this->DivListApplication->execute();
478     $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
479     $this->reload();
480     $this->DivListApplication->setEntries($this->applications);
481     return($this->DivListApplication->Draw());
482   }
485   /* Return departments, that will be included within snapshot detection */
486   function get_used_snapshot_bases(){
487     if($this->DivListApplication->selectedRelease == "main"){
488       return(array($this->DivListApplication->selectedBase));
489     }else{
490       return(array($this->DivListApplication->selectedRelease));
491     }
492   }
495   function reload()
496   {
497     $this->applications= array();
499     /* Set base for all searches */
500     $base       = $this->DivListApplication->selectedBase;
501     $release    = $this->DivListApplication->selectedRelease;
502     $Regex      = $this->DivListApplication->Regex;
503     $SubSearch  = $this->DivListApplication->SubSearch; 
504     $Flags      =  GL_NONE | GL_SIZELIMIT;
505     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
506     $tmp        = array();
507     $Releases   = $this->getReleases($base);
509     if(!$this->enableReleaseManagement){
510       $use_base = get_ou('applicationou').$base;
511     }else{
512       if(isset($Releases[$release])){
513         $use_base  = $release;
514       }else{
515         $use_base  = get_ou('applicationou').$base;
516       }
517     }
519     if($SubSearch){
520       $Flags    |= GL_SUBSEARCH;  
521     }
522    
523     $res= get_sub_list($Filter, "application",get_ou('applicationou'), $use_base, array("cn","description","dn","objectClass"), $Flags);
524     $tmp2 = array();
525     foreach ($res as $val){
526       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
527       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
528     }
530     natcasesort($tmp2);
531     $this->applications=array();
532     foreach($tmp2 as $val){
533       $this->applications[]=$tmp[$val];
534     }
535     reset ($this->applications);
536   }
538   function remove_from_parent()
539   {
540     /* Optionally execute a command after we're done */
541     $this->postremove();
542   }
545   function copyPasteHandling_from_queue($s_action,$s_entry)
546   {
547     /* Check if Copy & Paste is disabled */
548     if(!is_object($this->CopyPasteHandler)){
549       return("");
550     }
552     /* Add a single entry to queue */
553     if($s_action == "cut" || $s_action == "copy"){
555       /* Cleanup object queue */
556       $this->CopyPasteHandler->cleanup_queue();
557       $dn = $this->applications[$s_entry]['dn'];
558       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
559     }
562     /* Add entries to queue */
563     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
565       /* Cleanup object queue */
566       $this->CopyPasteHandler->cleanup_queue();
568       /* Add new entries to CP queue */
569       foreach($this->list_get_selected_items() as $id){
570         $dn = $this->applications[$id]['dn'];
572         if($s_action == "copy_multiple"){
573           $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
574         }
575         if($s_action == "cut_multiple"){
576           $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
577         }
578       }
579     }
581     /* Start pasting entries */
582     if($s_action == "editPaste"){
583       $this->start_pasting_copied_objects = TRUE;
584     }
587     /* Return C&P dialog */
588     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
590       /* Load entry from queue and set base */
591       $this->CopyPasteHandler->load_entry_from_queue();
592       $this->CopyPasteHandler->SetVar("base",$this->DivListApplication->selectedBase);
594       /* Get dialog */
595       $data = $this->CopyPasteHandler->execute();
597       /* Return dialog data */
598       if(!empty($data)){
599         return($data);
600       }
601     }
603     /* Automatically disable status for pasting */
604     if(!$this->CopyPasteHandler->entries_queued()){
605       $this->start_pasting_copied_objects = FALSE;
606     }
607     return("");
608   }
611   function list_get_selected_items()
612   {
613     $ids = array();
614     foreach($_POST as $name => $value){
615       if(preg_match("/^item_selected_[0-9]*$/",$name)){
616         $id   = preg_replace("/^item_selected_/","",$name);
617         $ids[$id] = $id;
618       }
619     }
620     return($ids);
621   }
624   /* Save to LDAP */
625   function save()
626   {
627     /* Optionally execute a command after we're done */
628     $this->postcreate();
629   }
631   function remove_lock()
632   {
633     if (isset($this->apptabs->dn)){
634       del_lock ($this->apptabs->dn);
635     }
636   }
638   function save_object() {
639     $this->DivListApplication->save_object();
640   }
642   function check() {}
643   function adapt_from_template($dn) {}
644   function password_change_needed() {}
646 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
647 ?>