Code

7959e6f6db42b3b12582af949382f2c5d8cbb668
[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  */
20 require "tabs_application.inc";
22 class applicationManagement extends plugin
23 {
24   /* Definitions */
25   var $plHeadline     = "Applications";
26   var $plDescription  = "This does something";
28   /* Dialog attributes */
29   var $apptabs                  = NULL;
30   var $ui                       = NULL;
31   var $CopyPasteHandler         = NULL;
32   var $DivListApplication       = NULL;
33   var $applications             = array();
34   var $acl                      = "";
35   var $enableReleaseManagement  = false;
37   function IsReleaseManagementActivated()
38   {
39     /* Check if we should enable the release selection */
40     $tmp = search_config($this->config->data,"faiManagement","CLASS");
41     if(!empty($tmp)){
42       return(true);
43     }
44     return(false);
45   }
47   function applicationManagement ($config, $ui)
48   {
49     /* Save configuration for internal use */
50     $this->config   = $config;
51     $this->ui       = $ui;
53     /* Check if copy & paste is activated */
54     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
55       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
56     }
58     /* Creat dialog object */
59     $this->DivListApplication = new divListApplication($this->config,$this);
61     if($this->IsReleaseManagementActivated()){
62     /* Check if we should enable the release selection */
63       $this->enableReleaseManagement = true;
65       /* Hide SubSearch checkbox */
66       $this->DivListApplication->DisableCheckBox("SubSearch");
67     }
68   }
70   function getReleases($base)
71   {
72     $ldap                   = $this->config->get_ldap_link();
73     $dn                     = "ou=apps,".$base;
74     $ret                    = array();
75     $ret [$dn] = "/";
77     $ldap->cd($dn);
78     $ldap->search("objectClass=organizationalUnit",array("ou"));
80     while($attrs = $ldap->fetch()){
81       $str = str_replace($dn,"",$attrs['dn']);
82       $tmp = array_reverse( split("ou=",$str));
83       $str = "";
84       foreach($tmp as $val){
85         $val = trim(preg_replace("/,/","",$val));
86         if(empty($val)) break;
87         $str .= "/".$val;
88       } 
89       if(!empty($str)){
90         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
91       }
92     }
93     asort($ret);
94     return($ret);
95   }
97   function execute()
98   {
99     /* Call parent execute */
100     plugin::execute();
103     /**************** 
104       Variable init 
105      ****************/
107     /* These vars will be stored if you try to open a locked app, 
108         to be able to perform your last requests after showing a warning message */
109     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/");
111     $smarty       = get_smarty();             // Smarty instance
112     $s_action     = "";                       // Contains the action to proceed
113     $s_entry      = "";                       // The value for s_action
114     $base_back    = "";                       // The Link for Backbutton
115     
116     /* Test Posts */
117     foreach($_POST as $key => $val){
118       // Post for delete
119       if(preg_match("/appl_del.*/",$key)){
120         $s_action = "del";
121         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
122         // Post for edit
123       }elseif(preg_match("/appl_edit_.*/",$key)){
124         $s_action="edit";
125         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
126         // Post for new
127       }elseif(preg_match("/^copy_.*/",$key)){
128         $s_action="copy";
129         $s_entry  = preg_replace("/^copy_/i","",$key);
130       }elseif(preg_match("/^cut_.*/",$key)){
131         $s_action="cut";
132         $s_entry  = preg_replace("/^cut_/i","",$key);
133         // Post for new
134       }elseif(preg_match("/^appl_new.*/",$key)){
135         $s_action="new";
136       }elseif(preg_match("/^editPaste.*/i",$key)){
137         $s_action="editPaste";
138       }
139     }
141     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
142       $s_action ="edit";
143       $s_entry  = $_GET['id'];
144     }
146     $s_entry  = preg_replace("/_.$/","",$s_entry);
149     /**************** 
150       Copy & Paste handling  
151      ****************/
153     /* Only perform copy / paste if it is enabled 
154      */
155     if($this->CopyPasteHandler){
156       if($str = $this->copyPasteHandling($s_action,$s_entry)){
157        return($str);
158       };
159     }
162     /**************** 
163       Create a new app 
164      ****************/
166     /* New application? */
167     if ($s_action=="new"){
169       /* By default we set 'dn' to 'new', all relevant plugins will
170          react on this. */
171       $this->dn= "new";
173       /* Create new usertab object */
174       $this->apptabs= new apptabs($this->config,
175           $this->config->data['TABS']['APPSTABS'], $this->dn);
176       $this->apptabs->set_acl(array(':all'));
177     }
180     /**************** 
181       Edit entry canceled 
182      ****************/
184     /* Cancel dialogs */
185     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
186       del_lock ($this->apptabs->dn);
187       unset ($this->apptabs);
188       $this->apptabs= NULL;
189       unset ($_SESSION['objectinfo']);
190     }
193     /**************** 
194       Edit entry finished 
195      ****************/
197     /* Finish apps edit is triggered by the tabulator dialog, so
198        the user wants to save edited data. Check and save at this
199        point. */
200     if ((isset($_POST['edit_finish'])) && (isset($this->apptabs->config))){
202       /* Check tabs, will feed message array */
203       $this->apptabs->last= $this->apptabs->current;
204       $this->apptabs->save_object();
205       $message= $this->apptabs->check();
207       /* Save, or display error message? */
208       if (count($message) == 0){
210         /* Save data data to ldap */
211         $this->apptabs->set_release($this->DivListApplication->selectedRelease);
212         $this->apptabs->save();
213         gosa_log ("Application object'".$this->dn."' has been saved");
215         /* Application has been saved successfully, remove lock from
216            LDAP. */
217         if ($this->dn != "new"){
218           del_lock ($this->dn);
219         }
220         unset ($this->apptabs);
221         $this->apptabs= NULL;
222         unset ($_SESSION['objectinfo']);
223       } else {
224         /* Ok. There seem to be errors regarding to the tab data,
225            show message and continue as usual. */
226         show_errors($message);
227       }
228     }
231     /**************** 
232       Edit entry  
233      ****************/
235     /* User wants to edit data? */
236     if (($s_action=="edit") && (!isset($this->apptabs->config))){
238       /* Get 'dn' from posted 'applist', must be unique */
239       $this->dn= $this->applications[$s_entry]['dn'];
241       /* Get faistate to check if this applications is frezzed. 
242          Freezed applications can't be modified anyway */
243       $state = "";
244       if(isset($this->applications[$s_entry]['FAIstate'])){
245         $state = $this->applications[$s_entry]['FAIstate'][0];
246       }
248       /* Check locking, save current plugin in 'back_plugin', so
249          the dialog knows where to return. */
250       if (($user= get_lock($this->dn)) != ""){
251         return(gen_locked_message ($user, $this->dn));
252       }
254       /* Lock the current entry, so everyone will get the
255          above dialog */
256       add_lock ($this->dn, $this->ui->dn);
258       /* Set up the users ACL's for this 'dn' */
259       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
261       /* Register apptabs to trigger edit dialog */
262       $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn);
264       /* If this is a freeze assign acl none  */
265       if(preg_match("/freeze/",$state)){
266         $this->apptabs->set_acl(array());
267       }else{
268         $this->apptabs->set_acl($acl);
269       }
270       $_SESSION['objectinfo']= $this->dn;
271     }
274     /**************** 
275       Delete app 
276      ****************/
278     /* Remove user was requested */
279     if ($s_action == "del"){
281       /* Get 'dn' from posted 'uid' */
282       $this->dn= $this->applications[$s_entry]['dn'];
284       /* Load permissions for selected 'dn' and check if
285          we're allowed to remove this 'dn' */
286       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
287       $this->acl= get_module_permission($acl, "application", $this->dn);
288       if (chkacl($this->acl, "delete") == ""){
290         /* Check locking, save current plugin in 'back_plugin', so
291            the dialog knows where to return. */
292         if (($user= get_lock($this->dn)) != ""){
293           return (gen_locked_message ($user, $this->dn));
294         }
296         /* Lock the current entry, so nobody will edit it during deletion */
297         add_lock ($this->dn, $this->ui->dn);
298         $smarty= get_smarty();
299         $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
300         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
301       } else {
303         /* Obviously the user isn't allowed to delete. Show message and
304            clean session. */
305         print_red (_("You are not allowed to delete this application!"));
306       }
307     }
310     /**************** 
311       Delete app confirmed 
312      ****************/
314     /* Confirmation for deletion has been passed. Group should be deleted. */
315     if (isset($_POST['delete_app_confirm'])){
317       /* Some nice guy may send this as POST, so we've to check
318          for the permissions again. */
319       if (chkacl($this->acl, "delete") == ""){
321         /* Delete request is permitted, perform LDAP action */
322         $this->apptabs= new apptabs($this->config,
323             $this->config->data['TABS']['APPSTABS'], $this->dn);
324         $this->apptabs->set_acl(array($this->acl));
325         $this->apptabs->delete ();
326         gosa_log ("Application object'".$this->dn."' has been removed");
327         unset ($this->apptabs);
328         $this->apptabs= NULL;
330       } else {
332         /* Normally this shouldn't be reached, send some extra
333            logs to notify the administrator */
334         print_red (_("You are not allowed to delete this application!"));
335         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
336       }
338       /* Remove lock file after successfull deletion */
339       del_lock ($this->dn);
340     }
343     /**************** 
344       Delete app canceled 
345      ****************/
347     /* Delete application canceled? */
348     if (isset($_POST['delete_cancel'])){
349       del_lock ($this->dn);
350       unset($_SESSION['objectinfo']);
351     }
353     /* Show tab dialog if object is present */
354     if (($this->apptabs) && (isset($this->apptabs->config))){
355       $display= $this->apptabs->execute();
357       /* Don't show buttons if tab dialog requests this */
358       if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
359         $display.= "<p style=\"text-align:right\">\n";
360         $display.= "<input type=\"submit\" name=\"edit_finish\" value=\""._("Save")."\">\n";
361         $display.= "&nbsp;\n";
362         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
363         $display.= "</p>";
364       }
365       return ($display);
366     }
369     /****************
370       Dialog display
371      ****************/
373     /* Display dialog with system list */
374     $this->DivListApplication->parent = $this;
375     $this->DivListApplication->execute();
376     $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase);
377     $this->reload();
378     $this->DivListApplication->setEntries($this->applications);
379     return($this->DivListApplication->Draw());
380   }
383   function reload()
384   {
385     $this->applications= array();
387     /* Set base for all searches */
388     $base       = $this->DivListApplication->selectedBase;
389     $release    = $this->DivListApplication->selectedRelease;
390     $Regex      = $this->DivListApplication->Regex;
391     $SubSearch  = $this->DivListApplication->SubSearch; 
392     $Flags      =  GL_NONE | GL_SIZELIMIT;
393     $Filter     = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
394     $tmp        = array();
395     $Releases   = $this->getReleases($base);
397     if(!$this->enableReleaseManagement){
398       $use_base = "ou=apps,".$base;
399     }else{
400       if(isset($Releases[$release])){
401         $use_base  = $release;
402       }else{
403         $use_base  = "ou=apps,".$base;
404       }
405     }
407     if($SubSearch){
408       $Flags    |= GL_SUBSEARCH;  
409     }
410    
411     $res= get_list($Filter, $this->ui->subtreeACL,$use_base, array("cn","description","dn","objectClass","FAIstate"), $Flags);
412     foreach ($res as $val){
413       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
414     }
416     ksort($tmp);
417     $this->applications=array();
418     foreach($tmp as $val){
419       $this->applications[]=$val;
420     }
421     reset ($this->applications);
422   }
424   function remove_from_parent()
425   {
426     /* Optionally execute a command after we're done */
427     $this->postremove();
428   }
431   function copyPasteHandling($s_action,$s_entry)
432   {
433     /* Paste copied/cutted object in here
434      */
435     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
436       $this->CopyPasteHandler->save_object();
437       $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
439       if($str = $this->CopyPasteHandler->execute()) {
440         return($str);
441       }
442     }
444     /* Copy current object to CopyHandler
445      */
446     if($s_action == "copy"){
447       $this->CopyPasteHandler->Clear();
448       $dn = $this->applications[$s_entry]['dn'];
449       $obj    = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
450       $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new");
451       $this->CopyPasteHandler->Copy($obj,$objNew);
452     }
454     /* Copy current object to CopyHandler
455      */
456     if($s_action == "cut"){
457       $this->CopyPasteHandler->Clear();
458       $dn = $this->applications[$s_entry]['dn'];
459       $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
460       $this->CopyPasteHandler->Cut($obj);
461     }
462   }
464   /* Save to LDAP */
465   function save()
466   {
467     /* Optionally execute a command after we're done */
468     $this->postcreate();
469   }
471   function remove_lock()
472   {
473     if (isset($this->apptabs->dn)){
474       del_lock ($this->apptabs->dn);
475     }
476   }
478   function save_object() {
479     $this->DivListApplication->save_object();
480   }
482   function check() {}
483   function adapt_from_template($dn) {}
484   function password_change_needed() {}
485   function show_header($button_text, $text, $disabled= FALSE) {}
487 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
488 ?>