Code

Added mimetype classes
[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;
37   function mimetypeManagement ($config, $ui)
38   {
39     /* Save configuration for internal use */
40     $this->config   = $config;
41     $this->ui       = $ui;
43     /* Check if copy & paste is activated */
44     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
45       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
46     }
48     /* Creat dialog object */
49     $this->DivListMimeTypes = new divListMimeTypes($this->config,$this);
51     if($this->IsReleaseManagementActivated()){
53       /* Check if we should enable the release selection */
54       $this->enableReleaseManagement = true;
56       /* Hide SubSearch checkbox */
57       $this->DivListMimeTypes->DisableCheckBox("SubSearch");
58     }
60     /* Get acls */
61     $acl= get_permissions ($ui->dn, $ui->subtreeACL);
62     $this->acl= get_module_permission($acl, "mimetype", $ui->dn);
63   }
66   /* Get all releases */
67   function getReleases($base)
68   {
69     $ldap                   = $this->config->get_ldap_link();
70     $dn                     = "ou=mime,".$base;
71     $ret                    = array();
72     $ret ["ou=mime,".$base] = "/";    
74     $ldap->cd($dn);
75     $ldap->search("objectClass=organizationalUnit",array("ou"));
77     while($attrs = $ldap->fetch()){
78       $str = str_replace($dn,"",$attrs['dn']);
79       $tmp = array_reverse( split("ou=",$str));
80       $str = "";
81       foreach($tmp as $val){
82         $val = trim(preg_replace("/,/","",$val));
83         if(empty($val)) break;
84         $str .= "/".$val;
85       } 
86       if(!empty($str)){
87         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
88       }
89     }
90     asort($ret);
91     return($ret);
92   }
94   function execute()
95   {
96     /* Call parent execute */
97     plugin::execute();
100     /**************** 
101       Variable init 
102      ****************/
104     /* These vars will be stored if you try to open a locked mime, 
105         to be able to perform your last requests after showing a warning message */
106     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/");
108     $smarty       = get_smarty();             // Smarty instance
109     $s_action     = "";                       // Contains the action to proceed
110     $s_entry      = "";                       // The value for s_action
111     $base_back    = "";                       // The Link for Backbutton
112     
113     /* Test Posts */
114     foreach($_POST as $key => $val){
115       // Post for delete
116       if(preg_match("/mime_del.*/",$key)){
117         $s_action = "del";
118         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
119         // Post for edit
120       }elseif(preg_match("/mime_edit_.*/",$key)){
121         $s_action="edit";
122         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
123         // Post for new
124       }elseif(preg_match("/^copy_.*/",$key)){
125         $s_action="copy";
126         $s_entry  = preg_replace("/^copy_/i","",$key);
127       }elseif(preg_match("/^cut_.*/",$key)){
128         $s_action="cut";
129         $s_entry  = preg_replace("/^cut_/i","",$key);
130         // Post for new
131       }elseif(preg_match("/^mime_new.*/",$key)){
132         $s_action="new";
133       }elseif(preg_match("/^editPaste.*/i",$key)){
134         $s_action="editPaste";
135       }
136     }
138     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
139       $s_action ="edit";
140       $s_entry  = $_GET['id'];
141     }
143     $s_entry  = preg_replace("/_.$/","",$s_entry);
146     /**************** 
147       Copy & Paste handling  
148      ****************/
150     /* Only perform copy / paste if it is enabled 
151      */
152     if($this->CopyPasteHandler){
153       if($str = $this->copyPasteHandling($s_action,$s_entry)){
154        return($str);
155       };
156     }
159     /**************** 
160       Create a new mime type 
161      ****************/
163     /* New mime type? */
164     if ($s_action=="new"){
166       /* By default we set 'dn' to 'new', all relevant plugins will
167          react on this. */
168       $this->dn= "new";
170       /* Create new usertab object */
171       $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn);
173       /* set acl */
174       $acl= get_permissions ($this->DivListMimeTypes->selectedBase, $this->ui->subtreeACL);
175       $this->mimetabs->set_acl($acl);
176     }
179     /**************** 
180       Edit entry canceled 
181      ****************/
183     /* Cancel dialogs */
184     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
185       del_lock ($this->mimetabs->dn);
186       unset ($this->mimetabs);
187       $this->mimetabs= NULL;
188       unset ($_SESSION['objectinfo']);
189     }
192     /**************** 
193       Edit entry finished 
194      ****************/
196     /* Finish mime edit is triggered by the tabulator dialog, so
197        the user wants to save edited data. Check and save at this point. */
198     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
200       /* Check tabs, will feed message array */
201       $this->mimetabs->save_object();
202       $message= $this->mimetabs->check();
204       /* Save, or display error message? */
205       if (count($message) == 0){
207         /* Save data data to ldap */
208         $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
209         $this->mimetabs->save();
210         gosa_log ("Mime object'".$this->dn."' has been saved");
212         if (!isset($_POST['edit_apply'])){
213           /* Mime type has been saved successfully, remove lock from LDAP. */
214           if ($this->dn != "new"){
215             del_lock ($this->dn);
216           }
217           unset ($this->mimetabs);
218           $this->mimetabs= NULL;
219           unset ($_SESSION['objectinfo']);
220         }
221       } else {
222         /* Ok. There seem to be errors regarding to the tab data,
223            show message and continue as usual. */
224         show_errors($message);
225       }
226     }
229     /**************** 
230       Edit entry  
231      ****************/
233     /* User wants to edit data? */
234     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
236       /* Get 'dn' from posted 'mimelist', must be unique */
237       $this->dn= $this->mimetypes[$s_entry]['dn'];
239       /* Check locking, save current plugin in 'back_plugin', so
240          the dialog knows where to return. */
241       if (($user= get_lock($this->dn)) != ""){
242         return(gen_locked_message ($user, $this->dn));
243       }
245       /* Lock the current entry, so everyone will get the
246          above dialog */
247       add_lock ($this->dn, $this->ui->dn);
250       /* Register mimetabs to trigger edit dialog */
251       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn);
253       /* Set up the users ACL's for this 'dn' */
254       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
255       $this->mimetabs->set_acl($acl);
256       $_SESSION['objectinfo']= $this->dn;
257     }
260     /**************** 
261       Delete mime type 
262      ****************/
264     /* Remove user was requested */
265     if ($s_action == "del"){
267       /* Get 'dn' from posted 'uid' */
268       $this->dn= $this->mimetypes[$s_entry]['dn'];
270       /* Load permissions for selected 'dn' and check if
271          we're allowed to remove this 'dn' */
272       if (chkacl($this->acl, "delete") == ""){
274         /* Check locking, save current plugin in 'back_plugin', so
275            the dialog knows where to return. */
276         if (($user= get_lock($this->dn)) != ""){
277           return (gen_locked_message ($user, $this->dn));
278         }
280         /* Lock the current entry, so nobody will edit it during deletion */
281         add_lock ($this->dn, $this->ui->dn);
282         $smarty= get_smarty();
283         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
284         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
285       } else {
287         /* Obviously the user isn't allowed to delete. Show message and
288            clean session. */
289         print_red (_("You are not allowed to delete this mime type!"));
290       }
291     }
294     /**************** 
295       Delete mime confirmed 
296      ****************/
298     /* Confirmation for deletion has been passed. Group should be deleted. */
299     if (isset($_POST['delete_mime_confirm'])){
301       /* Some nice guy may send this as POST, so we've to check
302          for the permissions again. */
303       if (chkacl($this->acl, "delete") == ""){
305         /* Delete request is permitted, perform LDAP action */
306         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn);
307         $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
308         $this->mimetabs->set_acl($acl);
309         $this->mimetabs->delete ();
310         gosa_log ("Mime type object'".$this->dn."' has been removed");
311         unset ($this->mimetabs);
312         $this->mimetabs= NULL;
314       } else {
316         /* Normally this shouldn't be reached, send some extra
317            logs to notify the administrator */
318         print_red (_("You are not allowed to delete this mime type!"));
319         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
320       }
322       /* Remove lock file after successfull deletion */
323       del_lock ($this->dn);
324     }
327     /**************** 
328       Delete mime canceled 
329      ****************/
331     /* Delete mime type canceled? */
332     if (isset($_POST['delete_cancel'])){
333       del_lock ($this->dn);
334       unset($_SESSION['objectinfo']);
335     }
337     /* Show tab dialog if object is present */
338     if (($this->mimetabs) && (isset($this->mimetabs->config))){
339       $display= $this->mimetabs->execute();
341       /* Don't show buttons if tab dialog requests this */
342       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
343         $display.= "<p style=\"text-align:right\">\n";
344         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
345         $display.= "&nbsp;\n";
346         if ($this->dn != "new"){
347           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
348           $display.= "&nbsp;\n";
349         }
350         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
351         $display.= "</p>";
352       }
353       return ($display);
354     }
357     /****************
358       Dialog display
359      ****************/
361         /* Check if there is a snapshot dialog open */
362     $base = $this->DivListMimeTypes->selectedBase;
363     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
364       return($str);
365     }
367     /* Display dialog with system list */
368     $this->DivListMimeTypes->parent = $this;
369     $this->DivListMimeTypes->execute();
370     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase);
371     $this->reload();
372     $this->DivListMimeTypes->setEntries($this->mimetypes);
373     return($this->DivListMimeTypes->Draw());
374   }
376     
377   /* Return departments, that will be included within snapshot detection */
378   function get_used_snapshot_bases()
379   {
380     return(array($this->DivListMimeTypes->selectedRelease));
381   }
385   function reload()
386   {
387     $this->mimetypes= array();
389     /* Set base for all searches */
390     $base       = $this->DivListMimeTypes->selectedBase;
391     $release    = $this->DivListMimeTypes->selectedRelease;
392     $Regex      = $this->DivListMimeTypes->Regex;
393     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
394     $Flags      =  GL_NONE | GL_SIZELIMIT;
395     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
396     $tmp        = array();
397     $Releases   = $this->getReleases($base);
400     /* If release management is enabled, use release as base. */
401     if(!$this->enableReleaseManagement){
402       $use_base = $base;
403     }else{
404       if(isset($Releases[$release])){
405         $use_base  = $release;
406       }else{
407         $use_base  = $base;
408       }
409     }
411     /* In case of subsearch, add the subsearch flag */
412     if($SubSearch){
413       $Flags    |= GL_SUBSEARCH;  
414     }
415   
416     /* Get results and create index */ 
417     $res= get_list($Filter, $this->ui->subtreeACL,$use_base, array("cn","description","dn","objectClass"), $Flags);
418     foreach ($res as $val){
419       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
420     }
422     /* sort entries */
423     ksort($tmp);
424     $this->mimetypes=array();
425     foreach($tmp as $val){
426       $this->mimetypes[]=$val;
427     }
428     reset ($this->mimetypes);
429   }
431   function remove_from_parent()
432   {
433     /* Optionally execute a command after we're done */
434     $this->postremove();
435   }
438   function copyPasteHandling($s_action,$s_entry)
439   {
440     /* Paste copied/cutted object in here
441      */
442     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
443       $this->CopyPasteHandler->save_object();
444       $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
446       if($str = $this->CopyPasteHandler->execute()) {
447         return($str);
448       }
449     }
451     /* Copy current object to CopyHandler
452      */
453     if($s_action == "copy"){
454       $this->CopyPasteHandler->Clear();
455       $dn = $this->mimetypes[$s_entry]['dn'];
456       $obj    = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
457       $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new");
458       $this->CopyPasteHandler->Copy($obj,$objNew);
459     }
461     /* Copy current object to CopyHandler
462      */
463     if($s_action == "cut"){
464       $this->CopyPasteHandler->Clear();
465       $dn = $this->mimetypes[$s_entry]['dn'];
466       $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
467       $this->CopyPasteHandler->Cut($obj);
468     }
469   }
471   /* Check if the release management is activated. */
472   function IsReleaseManagementActivated()
473   {
474     /* Check if we should enable the release selection */
475     $tmp = search_config($this->config->data,"faiManagement","CLASS");
476     if(!empty($tmp)){
477       return(true);
478     }
479     return(false);
480   }
482   /* Save to LDAP */
483   function save()
484   {
485     /* Optionally execute a command after we're done */
486     $this->postcreate();
487   }
489   function remove_lock()
490   {
491     if (isset($this->mimetabs->dn)){
492       del_lock ($this->mimetabs->dn);
493     }
494   }
496   function save_object() {
497     $this->DivListMimeTypes->save_object();
498   }
500   function check() {}
501   function adapt_from_template($dn) {}
502   function password_change_needed() {}
503   function show_header($button_text, $text, $disabled= FALSE) {}
505 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
506 ?>