Code

* Fixed undefined index for "All"
[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();
208         gosa_log ("Mime object'".$this->dn."' has been saved");
210         if (!isset($_POST['edit_apply'])){
211           /* Mime type has been saved successfully, remove lock from LDAP. */
212           if ($this->dn != "new"){
213             del_lock ($this->dn);
214           }
215           unset ($this->mimetabs);
216           $this->mimetabs= NULL;
217           unset ($_SESSION['objectinfo']);
218         }
219       } else {
220         /* Ok. There seem to be errors regarding to the tab data,
221            show message and continue as usual. */
222         show_errors($message);
223       }
224     }
227     /**************** 
228       Edit entry  
229      ****************/
231     /* User wants to edit data? */
232     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
234       /* Get 'dn' from posted 'mimelist', must be unique */
235       $this->dn= $this->mimetypes[$s_entry]['dn'];
237       /* Check locking, save current plugin in 'back_plugin', so
238          the dialog knows where to return. */
239       if (($user= get_lock($this->dn)) != ""){
240         return(gen_locked_message ($user, $this->dn));
241       }
243       /* Lock the current entry, so everyone will get the
244          above dialog */
245       add_lock ($this->dn, $this->ui->dn);
248       /* Register mimetabs to trigger edit dialog */
249       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
250       $this->mimetabs->set_acl_base($this->dn);
251       $_SESSION['objectinfo']= $this->dn;
252     }
255     /********************
256       Delete MULTIPLE entries requested, display confirm dialog
257      ********************/
259     if ($s_action=="del_multiple"){
260       $ids = $this->list_get_selected_items();
262       if(count($ids)){
264         foreach($ids as $id){
265           $dn = $this->mimetypes[$id]['dn'];
266           if (($user= get_lock($dn)) != ""){
267             return(gen_locked_message ($user, $dn));
268           }
269           $this->dns[$id] = $dn;
270         }
272         $dns_names = "<br><pre>";
273         foreach($this->dns as $dn){
274           add_lock ($dn, $this->ui->dn);
275           $dns_names .= $dn."\n";
276         }
277         $dns_names .="</pre>";
279         /* Lock the current entry, so nobody will edit it during deletion */
280         if (count($this->dns) == 1){
281           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
282         } else {
283           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
284         }
285         $smarty->assign("multiple", true);
286         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
287       }
288     }
293     /********************
294       Delete MULTIPLE entries confirmed
295      ********************/
297     /* Confirmation for deletion has been passed. Users should be deleted. */
298     if (isset($_POST['delete_multiple_mimetype_confirm'])){
300       $ui = get_userinfo();
302       /* Remove user by user and check acls before removeing them */
303       foreach($this->dns as $key => $dn){
305         $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
306         if(preg_match("/d/",$acl)){
308           /* Delete request is permitted, perform LDAP action */
309           $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
310           $this->mimetabs->set_acl_base($dn);
311           $this->mimetabs->delete ();
312           gosa_log ("Mime type object'".$dn."' has been removed");
313           unset ($this->mimetabs);
314           $this->mimetabs= NULL;
316         } 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         }
322         /* Remove lock file after successfull deletion */
323         del_lock ($dn);
324         unset($this->dns[$key]);
325       }
326     }
329     /********************
330       Delete MULTIPLE entries Canceled
331      ********************/
333     /* Remove lock */
334     if(isset($_POST['delete_multiple_mimetype_cancel'])){
335       foreach($this->dns as $key => $dn){
336         del_lock ($dn);
337         unset($this->dns[$key]);
338       }
339     }
342     /**************** 
343       Delete mime type 
344      ****************/
346     /* Remove user was requested */
347     if ($s_action == "del"){
349       /* Get 'dn' from posted 'uid' */
350       $this->dn= $this->mimetypes[$s_entry]['dn'];
352       /* Load permissions for selected 'dn' and check if
353          we're allowed to remove this 'dn' */
354       $ui = get_userinfo();
355       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
356       if (preg_match("/d/",$acl)){
358         /* Check locking, save current plugin in 'back_plugin', so
359            the dialog knows where to return. */
360         if (($user= get_lock($this->dn)) != ""){
361           return (gen_locked_message ($user, $this->dn));
362         }
364         /* Lock the current entry, so nobody will edit it during deletion */
365         add_lock ($this->dn, $this->ui->dn);
366         $smarty= get_smarty();
367         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
368         $smarty->assign("multiple", false);
369         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
370       } else {
372         /* Obviously the user isn't allowed to delete. Show message and
373            clean session. */
374         print_red (_("You are not allowed to delete this mime type!"));
375       }
376     }
379     /**************** 
380       Delete mime confirmed 
381      ****************/
383     /* Confirmation for deletion has been passed. Group should be deleted. */
384     if (isset($_POST['delete_mime_confirm'])){
386       /* Some nice guy may send this as POST, so we've to check
387          for the permissions again. */
388       $ui = get_userinfo();
389       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
390       if(preg_match("/d/",$acl)){
392         /* Delete request is permitted, perform LDAP action */
393         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
394         $this->mimetabs->set_acl_base($this->dn);
395         $this->mimetabs->delete ();
396         gosa_log ("Mime type object'".$this->dn."' has been removed");
397         unset ($this->mimetabs);
398         $this->mimetabs= NULL;
400       } else {
402         /* Normally this shouldn't be reached, send some extra
403            logs to notify the administrator */
404         print_red (_("You are not allowed to delete this mime type!"));
405         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
406       }
408       /* Remove lock file after successfull deletion */
409       del_lock ($this->dn);
410     }
413     /**************** 
414       Delete mime canceled 
415      ****************/
417     /* Delete mime type canceled? */
418     if (isset($_POST['delete_cancel'])){
419       del_lock ($this->dn);
420       unset($_SESSION['objectinfo']);
421     }
423     /* Show tab dialog if object is present */
424     if (($this->mimetabs) && (isset($this->mimetabs->config))){
425       $display= $this->mimetabs->execute();
427       /* Don't show buttons if tab dialog requests this */
428       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
429         $display.= "<p style=\"text-align:right\">\n";
430         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
431         $display.= "&nbsp;\n";
432         if ($this->dn != "new"){
433           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
434           $display.= "&nbsp;\n";
435         }
436         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
437         $display.= "</p>";
438       }
439       return ($display);
440     }
443     /****************
444       Dialog display
445      ****************/
447         /* Check if there is a snapshot dialog open */
448     $base = $this->DivListMimeTypes->selectedBase;
449     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
450       return($str);
451     }
453     /* Display dialog with system list */
454     $this->DivListMimeTypes->parent = $this;
455     $this->DivListMimeTypes->execute();
456     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
457     $this->reload();
458     $this->DivListMimeTypes->setEntries($this->mimetypes);
459     return($this->DivListMimeTypes->Draw());
460   }
462     
463   /* Return departments, that will be included within snapshot detection */
464   function get_used_snapshot_bases()
465   {
466     return(array($this->DivListMimeTypes->selectedRelease));
467   }
471   function reload()
472   {
473     $this->mimetypes= array();
475     /* Set base for all searches */
476     $base       = $this->DivListMimeTypes->selectedBase;
477     $release    = $this->DivListMimeTypes->selectedRelease;
478     $Regex      = $this->DivListMimeTypes->Regex;
479     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
480     $Flags      =  GL_NONE | GL_SIZELIMIT;
481     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
482     $tmp        = array();
483     $Releases   = $this->getReleases($base);
486     /* If release management is enabled, use release as base. */
487     if(!$this->enableReleaseManagement){
488       $use_base = $base;
489     }else{
490       if(isset($Releases[$release])){
491         $use_base  = $release;
492       }else{
493         $use_base  = $base;
494       }
495     }
497     /* In case of subsearch, add the subsearch flag */
498     if($SubSearch){
499       $Flags    |= GL_SUBSEARCH;  
500     }
501   
502     /* Get results and create index */ 
503     $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
504     foreach ($res as $val){
505       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
506     }
508     /* sort entries */
509     ksort($tmp);
510     $this->mimetypes=array();
511     foreach($tmp as $val){
512       $this->mimetypes[]=$val;
513     }
514     reset ($this->mimetypes);
515   }
517   function remove_from_parent()
518   {
519     /* Optionally execute a command after we're done */
520     $this->postremove();
521   }
524   function copyPasteHandling($s_action,$s_entry)
525   {
526     /* Paste copied/cutted object in here
527      */
528     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
529       $this->CopyPasteHandler->save_object();
530       $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
532       if($str = $this->CopyPasteHandler->execute()) {
533         return($str);
534       }
535     }
537     /* Copy current object to CopyHandler
538      */
539     if($s_action == "copy"){
540       $this->CopyPasteHandler->Clear();
541       $dn = $this->mimetypes[$s_entry]['dn'];
543       /* Check acls */
544       $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
545       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
546         $obj    = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
547         $obj->set_acl_base($this->dn);
548         $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new","mimetypes");
549         $objNew->set_acl_base($this->dn);
550         $this->CopyPasteHandler->Copy($obj,$objNew);
551       }
552     }
554     /* Copy current object to CopyHandler
555      */
556     if($s_action == "cut"){
557       $this->CopyPasteHandler->Clear();
558       $dn = $this->mimetypes[$s_entry]['dn'];
560       /* Check acls */
561       $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
562       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
563         $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
564         $this->CopyPasteHandler->Cut($obj);
565       }
566     }
567   }
569   /* Check if the release management is activated. */
570   function IsReleaseManagementActivated()
571   {
572     /* Check if we should enable the release selection */
573     $tmp = search_config($this->config->data,"faiManagement","CLASS");
574     if(!empty($tmp)){
575       return(true);
576     }
577     return(false);
578   }
581   function list_get_selected_items()
582   {
583     $ids = array();
584     foreach($_POST as $name => $value){
585       if(preg_match("/^item_selected_[0-9]*$/",$name)){
586         $id   = preg_replace("/^item_selected_/","",$name);
587         $ids[$id] = $id;
588       }
589     }
590     return($ids);
591   }
594   /* Save to LDAP */
595   function save()
596   {
597     /* Optionally execute a command after we're done */
598     $this->postcreate();
599   }
601   function remove_lock()
602   {
603     if (isset($this->mimetabs->dn)){
604       del_lock ($this->mimetabs->dn);
605     }
606   }
608   function save_object() {
609     $this->DivListMimeTypes->save_object();
610   }
612   function check() {}
613   function adapt_from_template($dn) {}
614   function password_change_needed() {}
616 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
617 ?>