Code

git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@6088 594d385d-05f5-0310...
[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         $smarty->assign("intro",     sprintf(_("You're about to delete the following user(s) %s"), @LDAP::fix($dns_names)));
281         $smarty->assign("multiple", true);
282         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
283       }
284     }
289     /********************
290       Delete MULTIPLE entries confirmed
291      ********************/
293     /* Confirmation for deletion has been passed. Users should be deleted. */
294     if (isset($_POST['delete_multiple_mimetype_confirm'])){
296       $ui = get_userinfo();
298       /* Remove user by user and check acls before removeing them */
299       foreach($this->dns as $key => $dn){
301         $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
302         if(preg_match("/d/",$acl)){
304           /* Delete request is permitted, perform LDAP action */
305           $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
306           $this->mimetabs->set_acl_base($dn);
307           $this->mimetabs->delete ();
308           gosa_log ("Mime type object'".$dn."' has been removed");
309           unset ($this->mimetabs);
310           $this->mimetabs= NULL;
312         } else {
313           /* Normally this shouldn't be reached, send some extra
314              logs to notify the administrator */
315           print_red (_("You are not allowed to delete this mime type!"));
316           gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
317         }
318         /* Remove lock file after successfull deletion */
319         del_lock ($dn);
320         unset($this->dns[$key]);
321       }
322     }
325     /********************
326       Delete MULTIPLE entries Canceled
327      ********************/
329     /* Remove lock */
330     if(isset($_POST['delete_multiple_mimetype_cancel'])){
331       foreach($this->dns as $key => $dn){
332         del_lock ($dn);
333         unset($this->dns[$key]);
334       }
335     }
338     /**************** 
339       Delete mime type 
340      ****************/
342     /* Remove user was requested */
343     if ($s_action == "del"){
345       /* Get 'dn' from posted 'uid' */
346       $this->dn= $this->mimetypes[$s_entry]['dn'];
348       /* Load permissions for selected 'dn' and check if
349          we're allowed to remove this 'dn' */
350       $ui = get_userinfo();
351       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
352       if (preg_match("/d/",$acl)){
354         /* Check locking, save current plugin in 'back_plugin', so
355            the dialog knows where to return. */
356         if (($user= get_lock($this->dn)) != ""){
357           return (gen_locked_message ($user, $this->dn));
358         }
360         /* Lock the current entry, so nobody will edit it during deletion */
361         add_lock ($this->dn, $this->ui->dn);
362         $smarty= get_smarty();
363         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
364         $smarty->assign("multiple", false);
365         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
366       } else {
368         /* Obviously the user isn't allowed to delete. Show message and
369            clean session. */
370         print_red (_("You are not allowed to delete this mime type!"));
371       }
372     }
375     /**************** 
376       Delete mime confirmed 
377      ****************/
379     /* Confirmation for deletion has been passed. Group should be deleted. */
380     if (isset($_POST['delete_mime_confirm'])){
382       /* Some nice guy may send this as POST, so we've to check
383          for the permissions again. */
384       $ui = get_userinfo();
385       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
386       if(preg_match("/d/",$acl)){
388         /* Delete request is permitted, perform LDAP action */
389         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
390         $this->mimetabs->set_acl_base($this->dn);
391         $this->mimetabs->delete ();
392         gosa_log ("Mime type object'".$this->dn."' has been removed");
393         unset ($this->mimetabs);
394         $this->mimetabs= NULL;
396       } else {
398         /* Normally this shouldn't be reached, send some extra
399            logs to notify the administrator */
400         print_red (_("You are not allowed to delete this mime type!"));
401         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
402       }
404       /* Remove lock file after successfull deletion */
405       del_lock ($this->dn);
406     }
409     /**************** 
410       Delete mime canceled 
411      ****************/
413     /* Delete mime type canceled? */
414     if (isset($_POST['delete_cancel'])){
415       del_lock ($this->dn);
416       unset($_SESSION['objectinfo']);
417     }
419     /* Show tab dialog if object is present */
420     if (($this->mimetabs) && (isset($this->mimetabs->config))){
421       $display= $this->mimetabs->execute();
423       /* Don't show buttons if tab dialog requests this */
424       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
425         $display.= "<p style=\"text-align:right\">\n";
426         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
427         $display.= "&nbsp;\n";
428         if ($this->dn != "new"){
429           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
430           $display.= "&nbsp;\n";
431         }
432         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
433         $display.= "</p>";
434       }
435       return ($display);
436     }
439     /****************
440       Dialog display
441      ****************/
443         /* Check if there is a snapshot dialog open */
444     $base = $this->DivListMimeTypes->selectedBase;
445     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
446       return($str);
447     }
449     /* Display dialog with system list */
450     $this->DivListMimeTypes->parent = $this;
451     $this->DivListMimeTypes->execute();
452     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
453     $this->reload();
454     $this->DivListMimeTypes->setEntries($this->mimetypes);
455     return($this->DivListMimeTypes->Draw());
456   }
458     
459   /* Return departments, that will be included within snapshot detection */
460   function get_used_snapshot_bases()
461   {
462     return(array($this->DivListMimeTypes->selectedRelease));
463   }
467   function reload()
468   {
469     $this->mimetypes= array();
471     /* Set base for all searches */
472     $base       = $this->DivListMimeTypes->selectedBase;
473     $release    = $this->DivListMimeTypes->selectedRelease;
474     $Regex      = $this->DivListMimeTypes->Regex;
475     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
476     $Flags      =  GL_NONE | GL_SIZELIMIT;
477     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
478     $tmp        = array();
479     $Releases   = $this->getReleases($base);
482     /* If release management is enabled, use release as base. */
483     if(!$this->enableReleaseManagement){
484       $use_base = $base;
485     }else{
486       if(isset($Releases[$release])){
487         $use_base  = $release;
488       }else{
489         $use_base  = $base;
490       }
491     }
493     /* In case of subsearch, add the subsearch flag */
494     if($SubSearch){
495       $Flags    |= GL_SUBSEARCH;  
496     }
497   
498     /* Get results and create index */ 
499     $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
500     foreach ($res as $val){
501       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
502     }
504     /* sort entries */
505     ksort($tmp);
506     $this->mimetypes=array();
507     foreach($tmp as $val){
508       $this->mimetypes[]=$val;
509     }
510     reset ($this->mimetypes);
511   }
513   function remove_from_parent()
514   {
515     /* Optionally execute a command after we're done */
516     $this->postremove();
517   }
520   function copyPasteHandling($s_action,$s_entry)
521   {
522     /* Paste copied/cutted object in here
523      */
524     if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
525       $this->CopyPasteHandler->save_object();
526       $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
528       if($str = $this->CopyPasteHandler->execute()) {
529         return($str);
530       }
531     }
533     /* Copy current object to CopyHandler
534      */
535     if($s_action == "copy"){
536       $this->CopyPasteHandler->Clear();
537       $dn = $this->mimetypes[$s_entry]['dn'];
539       /* Check acls */
540       $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
541       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
542         $obj    = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
543         $obj->set_acl_base($this->dn);
544         $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new","mimetypes");
545         $objNew->set_acl_base($this->dn);
546         $this->CopyPasteHandler->Copy($obj,$objNew);
547       }
548     }
550     /* Copy current object to CopyHandler
551      */
552     if($s_action == "cut"){
553       $this->CopyPasteHandler->Clear();
554       $dn = $this->mimetypes[$s_entry]['dn'];
556       /* Check acls */
557       $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
558       if(preg_match("/(c.*w|w.*c)/",$acl_all)){
559         $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
560         $this->CopyPasteHandler->Cut($obj);
561       }
562     }
563   }
565   /* Check if the release management is activated. */
566   function IsReleaseManagementActivated()
567   {
568     /* Check if we should enable the release selection */
569     $tmp = search_config($this->config->data,"faiManagement","CLASS");
570     if(!empty($tmp)){
571       return(true);
572     }
573     return(false);
574   }
577   function list_get_selected_items()
578   {
579     $ids = array();
580     foreach($_POST as $name => $value){
581       if(preg_match("/^item_selected_[0-9]*$/",$name)){
582         $id   = preg_replace("/^item_selected_/","",$name);
583         $ids[$id] = $id;
584       }
585     }
586     return($ids);
587   }
590   /* Save to LDAP */
591   function save()
592   {
593     /* Optionally execute a command after we're done */
594     $this->postcreate();
595   }
597   function remove_lock()
598   {
599     if (isset($this->mimetabs->dn)){
600       del_lock ($this->mimetabs->dn);
601     }
602   }
604   function save_object() {
605     $this->DivListMimeTypes->save_object();
606   }
608   function check() {}
609   function adapt_from_template($dn) {}
610   function password_change_needed() {}
612 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
613 ?>