Code

Remved Backup output
[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 $CopyPasteHandler               = 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   }
63   /* Get all releases */
64   function getReleases($base)
65   {
66     $ldap                   = $this->config->get_ldap_link();
67     $dn                     = "ou=mime,".$base;
68     $ret                    = array();
69     $ret ["ou=mime,".$base] = "/";    
71     $ldap->cd($dn);
72     $ldap->search("objectClass=organizationalUnit",array("ou"));
74     while($attrs = $ldap->fetch()){
75       $str = str_replace($dn,"",$attrs['dn']);
76       $tmp = array_reverse( split("ou=",$str));
77       $str = "";
78       foreach($tmp as $val){
79         $val = trim(preg_replace("/,/","",$val));
80         if(empty($val)) break;
81         $str .= "/".$val;
82       } 
83       if(!empty($str)){
84         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
85       }
86     }
87     asort($ret);
88     return($ret);
89   }
91   function execute()
92   {
93     /* Call parent execute */
94     plugin::execute();
97     /**************** 
98       Variable init 
99      ****************/
101     /* These vars will be stored if you try to open a locked mime, 
102         to be able to perform your last requests after showing a warning message */
103     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/","/^item_selected/","/^remove_multiple_mimetypes/");
105     $smarty       = get_smarty();             // Smarty instance
106     $s_action     = "";                       // Contains the action to proceed
107     $s_entry      = "";                       // The value for s_action
108     $base_back    = "";                       // The Link for Backbutton
109     
110     /* Test Posts */
111     foreach($_POST as $key => $val){
112       // Post for delete
113       if(preg_match("/mime_del.*/",$key)){
114         $s_action = "del";
115         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
116         // Post for edit
117       }elseif(preg_match("/mime_edit_.*/",$key)){
118         $s_action="edit";
119         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
120         // Post for new
121       }elseif(preg_match("/^copy_.*/",$key)){
122         $s_action="copy";
123         $s_entry  = preg_replace("/^copy_/i","",$key);
124       }elseif(preg_match("/^cut_.*/",$key)){
125         $s_action="cut";
126         $s_entry  = preg_replace("/^cut_/i","",$key);
127         // Post for new
128       }elseif(preg_match("/^mime_new.*/",$key)){
129         $s_action="new";
130       }elseif(preg_match("/^remove_multiple_mimetypes/",$key)){
131         $s_action="del_multiple";
132       }elseif(preg_match("/^editPaste.*/i",$key)){
133         $s_action="editPaste";
134       }
135     }
137     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
138       $s_action ="edit";
139       $s_entry  = $_GET['id'];
140     }
142     $s_entry  = preg_replace("/_.$/","",$s_entry);
145     /**************** 
146       Copy & Paste handling  
147      ****************/
149     /* Only perform copy / paste if it is enabled 
150      */
151     if($this->CopyPasteHandler){
152       if($str = $this->copyPasteHandling($s_action,$s_entry)){
153        return($str);
154       };
155     }
158     /**************** 
159       Create a new mime type 
160      ****************/
162     /* New mime type? */
163     $ui = get_userinfo();
164     $acl = $ui->get_permissions($this->DivListMimeTypes->selectedBase,"mimetypes/mimetype");
165     if (($s_action=="new") && preg_match("/c/",$acl)){
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,"mimetypes");
173       $this->mimetabs->set_acl_base($this->DivListMimeTypes->selectedBase);
174     }   
177     /**************** 
178       Edit entry canceled 
179      ****************/
181     /* Cancel dialogs */
182     if (isset($_POST['edit_cancel'])){
183       del_lock ($this->mimetabs->dn);
184       unset ($this->mimetabs);
185       $this->mimetabs= NULL;
186       unset ($_SESSION['objectinfo']);
187     }
190     /**************** 
191       Edit entry finished 
192      ****************/
194     /* Finish mime edit is triggered by the tabulator dialog, so
195        the user wants to save edited data. Check and save at this point. */
196     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
198       /* Check tabs, will feed message array */
199       $this->mimetabs->save_object();
200       $message= $this->mimetabs->check();
202       /* Save, or display error message? */
203       if (count($message) == 0){
205         /* Save data data to ldap */
206         $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
207         $this->mimetabs->save();
209         if (!isset($_POST['edit_apply'])){
210           /* Mime type has been saved successfully, remove lock from LDAP. */
211           if ($this->dn != "new"){
212             del_lock ($this->dn);
213           }
214           unset ($this->mimetabs);
215           $this->mimetabs= NULL;
216           unset ($_SESSION['objectinfo']);
217         }
218       } else {
219         /* Ok. There seem to be errors regarding to the tab data,
220            show message and continue as usual. */
221         show_errors($message);
222       }
223     }
226     /**************** 
227       Edit entry  
228      ****************/
230     /* User wants to edit data? */
231     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
233       /* Get 'dn' from posted 'mimelist', must be unique */
234       $this->dn= $this->mimetypes[$s_entry]['dn'];
236       /* Check locking, save current plugin in 'back_plugin', so
237          the dialog knows where to return. */
238       if (($user= get_lock($this->dn)) != ""){
239         return(gen_locked_message ($user, $this->dn));
240       }
242       /* Lock the current entry, so everyone will get the
243          above dialog */
244       add_lock ($this->dn, $this->ui->dn);
247       /* Register mimetabs to trigger edit dialog */
248       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
249       $this->mimetabs->set_acl_base($this->dn);
250       $_SESSION['objectinfo']= $this->dn;
251     }
254     /********************
255       Delete MULTIPLE entries requested, display confirm dialog
256      ********************/
258     if ($s_action=="del_multiple"){
259       $ids = $this->list_get_selected_items();
261       if(count($ids)){
263         foreach($ids as $id){
264           $dn = $this->mimetypes[$id]['dn'];
265           if (($user= get_lock($dn)) != ""){
266             return(gen_locked_message ($user, $dn));
267           }
268           $this->dns[$id] = $dn;
269         }
271         $dns_names = "<br><pre>";
272         foreach($this->dns as $dn){
273           add_lock ($dn, $this->ui->dn);
274           $dns_names .= $dn."\n";
275         }
276         $dns_names .="</pre>";
278         /* Lock the current entry, so nobody will edit it during deletion */
279         if (count($this->dns) == 1){
280           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
281         } else {
282           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
283         }
284         $smarty->assign("multiple", true);
285         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
286       }
287     }
292     /********************
293       Delete MULTIPLE entries confirmed
294      ********************/
296     /* Confirmation for deletion has been passed. Users should be deleted. */
297     if (isset($_POST['delete_multiple_mimetype_confirm'])){
299       $ui = get_userinfo();
301       /* Remove user by user and check acls before removeing them */
302       foreach($this->dns as $key => $dn){
304         $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
305         if(preg_match("/d/",$acl)){
307           /* Delete request is permitted, perform LDAP action */
308           $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
309           $this->mimetabs->set_acl_base($dn);
310           $this->mimetabs->delete ();
311           unset ($this->mimetabs);
312           $this->mimetabs= NULL;
314         } else {
315           /* Normally this shouldn't be reached, send some extra
316              logs to notify the administrator */
317           print_red (_("You are not allowed to delete this mime type!"));
318           new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
319         }
320         /* Remove lock file after successfull deletion */
321         del_lock ($dn);
322         unset($this->dns[$key]);
323       }
324     }
327     /********************
328       Delete MULTIPLE entries Canceled
329      ********************/
331     /* Remove lock */
332     if(isset($_POST['delete_multiple_mimetype_cancel'])){
333       foreach($this->dns as $key => $dn){
334         del_lock ($dn);
335         unset($this->dns[$key]);
336       }
337     }
340     /**************** 
341       Delete mime type 
342      ****************/
344     /* Remove user was requested */
345     if ($s_action == "del"){
347       /* Get 'dn' from posted 'uid' */
348       $this->dn= $this->mimetypes[$s_entry]['dn'];
350       /* Load permissions for selected 'dn' and check if
351          we're allowed to remove this 'dn' */
352       $ui = get_userinfo();
353       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
354       if (preg_match("/d/",$acl)){
356         /* Check locking, save current plugin in 'back_plugin', so
357            the dialog knows where to return. */
358         if (($user= get_lock($this->dn)) != ""){
359           return (gen_locked_message ($user, $this->dn));
360         }
362         /* Lock the current entry, so nobody will edit it during deletion */
363         add_lock ($this->dn, $this->ui->dn);
364         $smarty= get_smarty();
365         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
366         $smarty->assign("multiple", false);
367         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
368       } else {
370         /* Obviously the user isn't allowed to delete. Show message and
371            clean session. */
372         print_red (_("You are not allowed to delete this mime type!"));
373       }
374     }
377     /**************** 
378       Delete mime confirmed 
379      ****************/
381     /* Confirmation for deletion has been passed. Group should be deleted. */
382     if (isset($_POST['delete_mime_confirm'])){
384       /* Some nice guy may send this as POST, so we've to check
385          for the permissions again. */
386       $ui = get_userinfo();
387       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
388       if(preg_match("/d/",$acl)){
390         /* Delete request is permitted, perform LDAP action */
391         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
392         $this->mimetabs->set_acl_base($this->dn);
393         $this->mimetabs->delete ();
394         unset ($this->mimetabs);
395         $this->mimetabs= NULL;
397       } else {
399         /* Normally this shouldn't be reached, send some extra
400            logs to notify the administrator */
401         print_red (_("You are not allowed to delete this mime type!"));
402         new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
403       }
405       /* Remove lock file after successfull deletion */
406       del_lock ($this->dn);
407     }
410     /**************** 
411       Delete mime canceled 
412      ****************/
414     /* Delete mime type canceled? */
415     if (isset($_POST['delete_cancel'])){
416       del_lock ($this->dn);
417       unset($_SESSION['objectinfo']);
418     }
420     /* Show tab dialog if object is present */
421     if (($this->mimetabs) && (isset($this->mimetabs->config))){
422       $display= $this->mimetabs->execute();
424       /* Don't show buttons if tab dialog requests this */
425       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
426         $display.= "<p style=\"text-align:right\">\n";
427         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
428         $display.= "&nbsp;\n";
429         if ($this->dn != "new"){
430           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
431           $display.= "&nbsp;\n";
432         }
433         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
434         $display.= "</p>";
435       }
436       return ($display);
437     }
440     /****************
441       Dialog display
442      ****************/
444         /* Check if there is a snapshot dialog open */
445     $base = $this->DivListMimeTypes->selectedBase;
446     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
447       return($str);
448     }
450     /* Display dialog with system list */
451     $this->DivListMimeTypes->parent = $this;
452     $this->DivListMimeTypes->execute();
453     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
454     $this->reload();
455     $this->DivListMimeTypes->setEntries($this->mimetypes);
456     return($this->DivListMimeTypes->Draw());
457   }
459     
460   /* Return departments, that will be included within snapshot detection */
461   function get_used_snapshot_bases()
462   {
463     return(array($this->DivListMimeTypes->selectedRelease));
464   }
468   function reload()
469   {
470     $this->mimetypes= array();
472     /* Set base for all searches */
473     $base       = $this->DivListMimeTypes->selectedBase;
474     $release    = $this->DivListMimeTypes->selectedRelease;
475     $Regex      = $this->DivListMimeTypes->Regex;
476     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
477     $Flags      =  GL_NONE | GL_SIZELIMIT;
478     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
479     $tmp        = array();
480     $Releases   = $this->getReleases($base);
483     /* If release management is enabled, use release as base. */
484     if(!$this->enableReleaseManagement){
485       $use_base = $base;
486     }else{
487       if(isset($Releases[$release])){
488         $use_base  = $release;
489       }else{
490         $use_base  = $base;
491       }
492     }
494     /* In case of subsearch, add the subsearch flag */
495     if($SubSearch){
496       $Flags    |= GL_SUBSEARCH;  
497     }
498   
499     /* Get results and create index */ 
500     $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
501     foreach ($res as $val){
502       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
503     }
505     /* sort entries */
506     ksort($tmp);
507     $this->mimetypes=array();
508     foreach($tmp as $val){
509       $this->mimetypes[]=$val;
510     }
511     reset ($this->mimetypes);
512   }
514   function remove_from_parent()
515   {
516     /* Optionally execute a command after we're done */
517     $this->postremove();
518   }
521   function copyPasteHandling($s_action,$s_entry)
522   {
523     /* Paste copied/cutted object in here
524      */
525     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
526       $this->CopyPasteHandler->save_object();
527       $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
529       if($str = $this->CopyPasteHandler->execute()) {
530         return($str);
531       }
532     }
534     /* Copy current object to CopyHandler
535      */
536     if($s_action == "copy"){
537       $this->CopyPasteHandler->Clear();
538       $dn = $this->mimetypes[$s_entry]['dn'];
540       /* Check acls */
541       $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
542       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
543         $obj    = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
544         $obj->set_acl_base($this->dn);
545         $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new","mimetypes");
546         $objNew->set_acl_base($this->dn);
547         $this->CopyPasteHandler->Copy($obj,$objNew);
548       }
549     }
551     /* Copy current object to CopyHandler
552      */
553     if($s_action == "cut"){
554       $this->CopyPasteHandler->Clear();
555       $dn = $this->mimetypes[$s_entry]['dn'];
557       /* Check acls */
558       $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
559       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
560         $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
561         $this->CopyPasteHandler->Cut($obj);
562       }
563     }
564   }
566   /* Check if the release management is activated. */
567   function IsReleaseManagementActivated()
568   {
569     /* Check if we should enable the release selection */
570     $tmp = search_config($this->config->data,"faiManagement","CLASS");
571     if(!empty($tmp)){
572       return(true);
573     }
574     return(false);
575   }
578   function list_get_selected_items()
579   {
580     $ids = array();
581     foreach($_POST as $name => $value){
582       if(preg_match("/^item_selected_[0-9]*$/",$name)){
583         $id   = preg_replace("/^item_selected_/","",$name);
584         $ids[$id] = $id;
585       }
586     }
587     return($ids);
588   }
591   /* Save to LDAP */
592   function save()
593   {
594     /* Optionally execute a command after we're done */
595     $this->postcreate();
596   }
598   function remove_lock()
599   {
600     if (isset($this->mimetabs->dn)){
601       del_lock ($this->mimetabs->dn);
602     }
603   }
605   function save_object() {
606     $this->DivListMimeTypes->save_object();
607   }
609   function check() {}
610   function adapt_from_template($dn) {}
611   function password_change_needed() {}
613 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
614 ?>