Code

Fixed LDAP change detection
[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         msg_dialog::displayChecks($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           $this->dns[$id] = $dn;
294         }
295         if ($user= get_multiple_locks($this->dns)){
296           return(gen_locked_message($user,$this->dns));
297         }
299         $dns_names[] = array();
300         foreach($this->dns as $dn){
301           add_lock ($dn, $this->ui->dn);
302           $dns_names[] =@LDAP::fix($dn);
303         }
305         /* Lock the current entry, so nobody will edit it during deletion */
306         $smarty->assign("intro",  msgPool::deleteInfo($dns_names,_("application")));
307         $smarty->assign("multiple", true);
308         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
309       }
310     }
313     /********************
314       Delete MULTIPLE entries confirmed
315      ********************/
317     /* Confirmation for deletion has been passed. Users should be deleted. */
318     if (isset($_POST['delete_multiple_application_confirm'])){
320       /* Remove user by user and check acls before removeing them */
321       foreach($this->dns as $key => $dn){
323         $ui = get_userinfo();
324         $acl = $ui->get_permissions($dn ,"application/application");
325         if (preg_match('/d/', $acl)){
327           /* Delete request is permitted, perform LDAP action */
328           $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
329           $this->apptabs->set_acl_base($dn);
330           $this->apptabs->delete ();
331           unset ($this->apptabs);
332           $this->apptabs= NULL;
334         } else {
335           /* Normally this shouldn't be reached, send some extra
336              logs to notify the administrator */
337           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
338           new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
339         }
340         /* Remove lock file after successfull deletion */
341         del_lock ($dn);
342         unset($this->dns[$key]);
343       }
344     }
347     /********************
348       Delete MULTIPLE entries Canceled
349      ********************/
351     /* Remove lock */
352     if(isset($_POST['delete_multiple_application_cancel'])){
353       foreach($this->dns as $key => $dn){
354         del_lock ($dn);
355         unset($this->dns[$key]);
356       }
357     }
359     /**************** 
360       Delete app 
361      ****************/
363     /* Remove user was requested */
364     if ($s_action == "del"){
366       /* Get 'dn' from posted 'uid' */
367       $this->dn= $this->applications[$s_entry]['dn'];
369       /* Load permissions for selected 'dn' and check if
370          we're allowed to remove this 'dn' */
371       $ui = get_userinfo();
372       $acl = $ui->get_permissions($this->dn ,"application/application");
374       if(preg_match("/d/",$acl)){
375         /* Check locking, save current plugin in 'back_plugin', so
376            the dialog knows where to return. */
377         if (($user= get_lock($this->dn)) != ""){
378           return (gen_locked_message ($user, $this->dn));
379         }
381         /* Lock the current entry, so nobody will edit it during deletion */
382         add_lock ($this->dn, $this->ui->dn);
383         $smarty= get_smarty();
384         $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
385         $smarty->assign("multiple", false);
386         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
387       } else {
389         /* Obviously the user isn't allowed to delete. Show message and
390            clean session. */
391         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
392       }
393     }
396     /**************** 
397       Delete app confirmed 
398      ****************/
400     /* Confirmation for deletion has been passed. Group should be deleted. */
401     if (isset($_POST['delete_app_confirm'])){
403       /* Some nice guy may send this as POST, so we've to check
404          for the permissions again. */
405       $ui = get_userinfo();
406       $acl = $ui->get_permissions($this->dn ,"application/application");
408       if(preg_match("/d/",$acl)){
410         /* Delete request is permitted, perform LDAP action */
411         $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
412         $this->apptabs->set_acl_base($this->dn);
413         $this->apptabs->delete ();
414         unset ($this->apptabs);
415         $this->apptabs= NULL;
417       } else {
419         /* Normally this shouldn't be reached, send some extra
420            logs to notify the administrator */
421         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
422         new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
423       }
425       /* Remove lock file after successfull deletion */
426       del_lock ($this->dn);
427     }
430     /**************** 
431       Delete app canceled 
432      ****************/
434     /* Delete application canceled? */
435     if (isset($_POST['delete_cancel'])){
436       del_lock ($this->dn);
437       session::un_set('objectinfo');
438     }
440     /* Show tab dialog if object is present */
441     if (($this->apptabs) && (isset($this->apptabs->config))){
442       $display= $this->apptabs->execute();
444       /* Don't show buttons if tab dialog requests this */
445       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
446         $display.= "<p style=\"text-align:right\">\n";
447         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
448         $display.= "&nbsp;\n";
449         if ($this->dn != "new"){
450           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
451           $display.= "&nbsp;\n";
452         }
453         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
454         $display.= "</p>";
455       }
456       return ($display);
457     }
460     /****************
461       Dialog display
462      ****************/
464     /* Check if there is a snapshot dialog open */
465     $base = $this->DivListApplication->selectedBase;
466     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
467       return($str);
468     }
470     /* Display dialog with system list */
471     $this->DivListApplication->parent = $this;
472     $this->DivListApplication->execute();
473     $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
474     $this->reload();
475     $this->DivListApplication->setEntries($this->applications);
476     return($this->DivListApplication->Draw());
477   }
480   /* Return departments, that will be included within snapshot detection */
481   function get_used_snapshot_bases(){
482     if($this->DivListApplication->selectedRelease == "main"){
483       return(array($this->DivListApplication->selectedBase));
484     }else{
485       return(array($this->DivListApplication->selectedRelease));
486     }
487   }
490   function reload()
491   {
492     $this->applications= array();
494     /* Set base for all searches */
495     $base       = $this->DivListApplication->selectedBase;
496     $release    = $this->DivListApplication->selectedRelease;
497     $Regex      = $this->DivListApplication->Regex;
498     $SubSearch  = $this->DivListApplication->SubSearch; 
499     $Flags      =  GL_NONE | GL_SIZELIMIT;
500     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
501     $tmp        = array();
502     $Releases   = $this->getReleases($base);
504     if(!$this->enableReleaseManagement){
505       $use_base = get_ou('applicationou').$base;
506     }else{
507       if(isset($Releases[$release])){
508         $use_base  = $release;
509       }else{
510         $use_base  = get_ou('applicationou').$base;
511       }
512     }
514     if($SubSearch){
515       $Flags    |= GL_SUBSEARCH;  
516     }
517    
518     $res= get_list($Filter, "application", $use_base, array("cn","description","dn","objectClass"), $Flags);
519     $tmp2 = array();
520     foreach ($res as $val){
521       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
522       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
523     }
525     natcasesort($tmp2);
526     $this->applications=array();
527     foreach($tmp2 as $val){
528       $this->applications[]=$tmp[$val];
529     }
530     reset ($this->applications);
531   }
533   function remove_from_parent()
534   {
535     /* Optionally execute a command after we're done */
536     $this->postremove();
537   }
540   function copyPasteHandling_from_queue($s_action,$s_entry)
541   {
542     /* Check if Copy & Paste is disabled */
543     if(!is_object($this->CopyPasteHandler)){
544       return("");
545     }
547     /* Add a single entry to queue */
548     if($s_action == "cut" || $s_action == "copy"){
550       /* Cleanup object queue */
551       $this->CopyPasteHandler->cleanup_queue();
552       $dn = $this->applications[$s_entry]['dn'];
553       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
554     }
557     /* Add entries to queue */
558     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
560       /* Cleanup object queue */
561       $this->CopyPasteHandler->cleanup_queue();
563       /* Add new entries to CP queue */
564       foreach($this->list_get_selected_items() as $id){
565         $dn = $this->applications[$id]['dn'];
567         if($s_action == "copy_multiple"){
568           $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
569         }
570         if($s_action == "cut_multiple"){
571           $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
572         }
573       }
574     }
576     /* Start pasting entries */
577     if($s_action == "editPaste"){
578       $this->start_pasting_copied_objects = TRUE;
579     }
582     /* Return C&P dialog */
583     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
586       /* Get dialog */
587       $this->CopyPasteHandler->SetVar("base",$this->DivListApplication->selectedBase);
588       $data = $this->CopyPasteHandler->execute();
590       /* Return dialog data */
591       if(!empty($data)){
592         return($data);
593       }
594     }
596     /* Automatically disable status for pasting */
597     if(!$this->CopyPasteHandler->entries_queued()){
598       $this->start_pasting_copied_objects = FALSE;
599     }
600     return("");
601   }
604   function list_get_selected_items()
605   {
606     $ids = array();
607     foreach($_POST as $name => $value){
608       if(preg_match("/^item_selected_[0-9]*$/",$name)){
609         $id   = preg_replace("/^item_selected_/","",$name);
610         $ids[$id] = $id;
611       }
612     }
613     return($ids);
614   }
617   /* Save to LDAP */
618   function save()
619   {
620     /* Optionally execute a command after we're done */
621     $this->postcreate();
622   }
624   function remove_lock()
625   {
626     if (isset($this->apptabs->dn)){
627       del_lock ($this->apptabs->dn);
628     }
629   }
631   function save_object() {
632     $this->DivListApplication->save_object();
633   }
635   function check() {}
636   function adapt_from_template($dn) {}
637   function password_change_needed() {}
639 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
640 ?>