Code

Do not add additional slashes for templates.
[gosa.git] / plugins / admin / mimetypes / class_mimetypeManagement.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 require "tabs_mimetypes.inc";
23 class mimetypeManagement extends plugin
24 {
25   /* Definitions */
26   var $plHeadline     = "Mime types";
27   var $plDescription  = "Manage mime types";
29   /* Dialog attributes */
30   var $ui                             = NULL;
31   var $DivListMimeTypes               = NULL;
32   var $enableReleaseManagement        = false;
33   var $mimetabs                       = NULL;
34   var $snapDialog                     = NULL;
35   var $acl                            = NULL;
36   var $CopyPasteHandler               = NULL;
38   function mimetypeManagement ($config, $ui)
39   {
40     /* Save configuration for internal use */
41     $this->config   = $config;
42     $this->ui       = $ui;
44     /* Check if copy & paste is activated */
45     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
46       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
47     }
49     /* Creat dialog object */
50     $this->DivListMimeTypes = new divListMimeTypes($this->config,$this);
52     if($this->IsReleaseManagementActivated()){
54       /* Check if we should enable the release selection */
55       $this->enableReleaseManagement = true;
57       /* Hide SubSearch checkbox */
58       $this->DivListMimeTypes->DisableCheckBox("SubSearch");
59     }
61     /* Get acls */
62     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
63     $this->acl= get_module_permission($acl, "mimetype", $ui->dn);
64   }
67   /* Get all releases */
68   function getReleases($base)
69   {
70     $ldap                   = $this->config->get_ldap_link();
71     $dn                     = "ou=mime,".$base;
72     $ret                    = array();
73     $ret ["ou=mime,".$base] = "/";    
75     $ldap->cd($dn);
76     $ldap->search("objectClass=organizationalUnit",array("ou"));
78     while($attrs = $ldap->fetch()){
79       $str = str_replace($dn,"",$attrs['dn']);
80       $tmp = array_reverse( split("ou=",$str));
81       $str = "";
82       foreach($tmp as $val){
83         $val = trim(preg_replace("/,/","",$val));
84         if(empty($val)) break;
85         $str .= "/".$val;
86       } 
87       if(!empty($str)){
88         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
89       }
90     }
91     asort($ret);
92     return($ret);
93   }
95   function execute()
96   {
97     /* Call parent execute */
98     plugin::execute();
101     /**************** 
102       Variable init 
103      ****************/
105     /* These vars will be stored if you try to open a locked mime, 
106         to be able to perform your last requests after showing a warning message */
107     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/");
109     $smarty       = get_smarty();             // Smarty instance
110     $s_action     = "";                       // Contains the action to proceed
111     $s_entry      = "";                       // The value for s_action
112     $base_back    = "";                       // The Link for Backbutton
113     
114     /* Test Posts */
115     foreach($_POST as $key => $val){
116       // Post for delete
117       if(preg_match("/mime_del.*/",$key)){
118         $s_action = "del";
119         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
120         // Post for edit
121       }elseif(preg_match("/mime_edit_.*/",$key)){
122         $s_action="edit";
123         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
124         // Post for new
125       }elseif(preg_match("/^copy_.*/",$key)){
126         $s_action="copy";
127         $s_entry  = preg_replace("/^copy_/i","",$key);
128       }elseif(preg_match("/^cut_.*/",$key)){
129         $s_action="cut";
130         $s_entry  = preg_replace("/^cut_/i","",$key);
131         // Post for new
132       }elseif(preg_match("/^mime_new.*/",$key)){
133         $s_action="new";
134       }elseif(preg_match("/^editPaste.*/i",$key)){
135         $s_action="editPaste";
136       }
137     }
139     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
140       $s_action ="edit";
141       $s_entry  = $_GET['id'];
142     }
144     $s_entry  = preg_replace("/_.$/","",$s_entry);
147     /**************** 
148       Copy & Paste handling  
149      ****************/
151     /* Only perform copy / paste if it is enabled 
152      */
153     if($this->CopyPasteHandler){
154       if($str = $this->copyPasteHandling($s_action,$s_entry)){
155        return($str);
156       };
157     }
160     /**************** 
161       Create a new mime type 
162      ****************/
164     /* New mime type? */
165     if ($s_action=="new"){
167       /* By default we set 'dn' to 'new', all relevant plugins will
168          react on this. */
169       $this->dn= "new";
171       /* Create new usertab object */
172       $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn);
174       /* set acl */
175       $acl= get_permissions ($this->DivListMimeTypes->selectedBase, $this->ui->subtreeACL);
176       $this->mimetabs->set_acl($acl);
177     }
180     /**************** 
181       Edit entry canceled 
182      ****************/
184     /* Cancel dialogs */
185     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
186       del_lock ($this->mimetabs->dn);
187       unset ($this->mimetabs);
188       $this->mimetabs= NULL;
189       unset ($_SESSION['objectinfo']);
190     }
193     /**************** 
194       Edit entry finished 
195      ****************/
197     /* Finish mime edit is triggered by the tabulator dialog, so
198        the user wants to save edited data. Check and save at this point. */
199     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
201       /* Check tabs, will feed message array */
202       $this->mimetabs->save_object();
203       $message= $this->mimetabs->check();
205       /* Save, or display error message? */
206       if (count($message) == 0){
208         /* Save data data to ldap */
209         $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
210         $this->mimetabs->save();
211         gosa_log ("Mime object'".$this->dn."' has been saved");
213         if (!isset($_POST['edit_apply'])){
214           /* Mime type has been saved successfully, remove lock from LDAP. */
215           if ($this->dn != "new"){
216             del_lock ($this->dn);
217           }
218           unset ($this->mimetabs);
219           $this->mimetabs= NULL;
220           unset ($_SESSION['objectinfo']);
221         }
222       } else {
223         /* Ok. There seem to be errors regarding to the tab data,
224            show message and continue as usual. */
225         show_errors($message);
226       }
227     }
230     /**************** 
231       Edit entry  
232      ****************/
234     /* User wants to edit data? */
235     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
237       /* Get 'dn' from posted 'mimelist', must be unique */
238       $this->dn= $this->mimetypes[$s_entry]['dn'];
240       /* Check locking, save current plugin in 'back_plugin', so
241          the dialog knows where to return. */
242       if (($user= get_lock($this->dn)) != ""){
243         return(gen_locked_message ($user, $this->dn));
244       }
246       /* Lock the current entry, so everyone will get the
247          above dialog */
248       add_lock ($this->dn, $this->ui->dn);
251       /* Register mimetabs to trigger edit dialog */
252       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn);
254       /* Set up the users ACL's for this 'dn' */
255       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
256       $this->mimetabs->set_acl($acl);
257       $_SESSION['objectinfo']= $this->dn;
258     }
261     /**************** 
262       Delete mime type 
263      ****************/
265     /* Remove user was requested */
266     if ($s_action == "del"){
268       /* Get 'dn' from posted 'uid' */
269       $this->dn= $this->mimetypes[$s_entry]['dn'];
271       /* Load permissions for selected 'dn' and check if
272          we're allowed to remove this 'dn' */
273       if (chkacl($this->acl, "delete") == ""){
275         /* Check locking, save current plugin in 'back_plugin', so
276            the dialog knows where to return. */
277         if (($user= get_lock($this->dn)) != ""){
278           return (gen_locked_message ($user, $this->dn));
279         }
281         /* Lock the current entry, so nobody will edit it during deletion */
282         add_lock ($this->dn, $this->ui->dn);
283         $smarty= get_smarty();
284         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
285         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
286       } else {
288         /* Obviously the user isn't allowed to delete. Show message and
289            clean session. */
290         print_red (_("You are not allowed to delete this mime type!"));
291       }
292     }
295     /**************** 
296       Delete mime confirmed 
297      ****************/
299     /* Confirmation for deletion has been passed. Group should be deleted. */
300     if (isset($_POST['delete_mime_confirm'])){
302       /* Some nice guy may send this as POST, so we've to check
303          for the permissions again. */
304       if (chkacl($this->acl, "delete") == ""){
306         /* Delete request is permitted, perform LDAP action */
307         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn);
308         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
309         $this->mimetabs->set_acl($acl);
310         $this->mimetabs->delete ();
311         gosa_log ("Mime type object'".$this->dn."' has been removed");
312         unset ($this->mimetabs);
313         $this->mimetabs= NULL;
315       } else {
317         /* Normally this shouldn't be reached, send some extra
318            logs to notify the administrator */
319         print_red (_("You are not allowed to delete this mime type!"));
320         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
321       }
323       /* Remove lock file after successfull deletion */
324       del_lock ($this->dn);
325     }
328     /**************** 
329       Delete mime canceled 
330      ****************/
332     /* Delete mime type canceled? */
333     if (isset($_POST['delete_cancel'])){
334       del_lock ($this->dn);
335       unset($_SESSION['objectinfo']);
336     }
338     /* Show tab dialog if object is present */
339     if (($this->mimetabs) && (isset($this->mimetabs->config))){
340       $display= $this->mimetabs->execute();
342       /* Don't show buttons if tab dialog requests this */
343       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
344         $display.= "<p style=\"text-align:right\">\n";
345         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
346         $display.= "&nbsp;\n";
347         if ($this->dn != "new"){
348           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
349           $display.= "&nbsp;\n";
350         }
351         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
352         $display.= "</p>";
353       }
354       return ($display);
355     }
358     /****************
359       Dialog display
360      ****************/
362         /* Check if there is a snapshot dialog open */
363     $base = $this->DivListMimeTypes->selectedBase;
364     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
365       return($str);
366     }
368     /* Display dialog with system list */
369     $this->DivListMimeTypes->parent = $this;
370     $this->DivListMimeTypes->execute();
371     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase);
372     $this->reload();
373     $this->DivListMimeTypes->setEntries($this->mimetypes);
374     return($this->DivListMimeTypes->Draw());
375   }
377     
378   /* Return departments, that will be included within snapshot detection */
379   function get_used_snapshot_bases()
380   {
381     return(array($this->DivListMimeTypes->selectedRelease));
382   }
386   function reload()
387   {
388     $this->mimetypes= array();
390     /* Set base for all searches */
391     $base       = $this->DivListMimeTypes->selectedBase;
392     $release    = $this->DivListMimeTypes->selectedRelease;
393     $Regex      = $this->DivListMimeTypes->Regex;
394     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
395     $Flags      =  GL_NONE | GL_SIZELIMIT;
396     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
397     $tmp        = array();
398     $Releases   = $this->getReleases($base);
401     /* If release management is enabled, use release as base. */
402     if(!$this->enableReleaseManagement){
403       $use_base = $base;
404     }else{
405       if(isset($Releases[$release])){
406         $use_base  = $release;
407       }else{
408         $use_base  = $base;
409       }
410     }
412     /* In case of subsearch, add the subsearch flag */
413     if($SubSearch){
414       $Flags    |= GL_SUBSEARCH;  
415     }
416   
417     /* Get results and create index */ 
418     $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
419     foreach ($res as $val){
420       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
421     }
423     /* sort entries */
424     ksort($tmp);
425     $this->mimetypes=array();
426     foreach($tmp as $val){
427       $this->mimetypes[]=$val;
428     }
429     reset ($this->mimetypes);
430   }
432   function remove_from_parent()
433   {
434     /* Optionally execute a command after we're done */
435     $this->postremove();
436   }
439   function copyPasteHandling($s_action,$s_entry)
440   {
441     /* Paste copied/cutted object in here
442      */
443     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
444       $this->CopyPasteHandler->save_object();
445       $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
447       if($str = $this->CopyPasteHandler->execute()) {
448         return($str);
449       }
450     }
452     /* Copy current object to CopyHandler
453      */
454     if($s_action == "copy"){
455       $this->CopyPasteHandler->Clear();
456       $dn = $this->mimetypes[$s_entry]['dn'];
457       $obj    = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
458       $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new");
459       $this->CopyPasteHandler->Copy($obj,$objNew);
460     }
462     /* Copy current object to CopyHandler
463      */
464     if($s_action == "cut"){
465       $this->CopyPasteHandler->Clear();
466       $dn = $this->mimetypes[$s_entry]['dn'];
467       $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
468       $this->CopyPasteHandler->Cut($obj);
469     }
470   }
472   /* Check if the release management is activated. */
473   function IsReleaseManagementActivated()
474   {
475     /* Check if we should enable the release selection */
476     $tmp = search_config($this->config->data,"faiManagement","CLASS");
477     if(!empty($tmp)){
478       return(true);
479     }
480     return(false);
481   }
483   /* Save to LDAP */
484   function save()
485   {
486     /* Optionally execute a command after we're done */
487     $this->postcreate();
488   }
490   function remove_lock()
491   {
492     if (isset($this->mimetabs->dn)){
493       del_lock ($this->mimetabs->dn);
494     }
495   }
497   function save_object() {
498     $this->DivListMimeTypes->save_object();
499   }
501   function check() {}
502   function adapt_from_template($dn) {}
503   function password_change_needed() {}
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>