Code

Updated dhcp handling
[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;
36   var $start_pasting_copied_objects = FALSE;
39   function mimetypeManagement ($config, $ui)
40   {
41     /* Save configuration for internal use */
42     $this->config   = $config;
43     $this->ui       = $ui;
45     /* Check if copy & paste is activated */
46     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
47       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
48     }
50     /* Creat dialog object */
51     $this->DivListMimeTypes = new divListMimeTypes($this->config,$this);
53     if($this->IsReleaseManagementActivated()){
55       /* Check if we should enable the release selection */
56       $this->enableReleaseManagement = true;
58       /* Hide SubSearch checkbox */
59       $this->DivListMimeTypes->DisableCheckBox("SubSearch");
60     }
62   }
65   /* Get all releases */
66   function getReleases($base)
67   {
68     $ldap                   = $this->config->get_ldap_link();
69     $dn                     = "ou=mime,".$base;
70     $ret                    = array();
71     $ret ["ou=mime,".$base] = "/";    
73     $ldap->cd($dn);
74     $ldap->search("objectClass=organizationalUnit",array("ou"));
76     while($attrs = $ldap->fetch()){
77       $str = str_replace($dn,"",$attrs['dn']);
78       $tmp = array_reverse( split("ou=",$str));
79       $str = "";
80       foreach($tmp as $val){
81         $val = trim(preg_replace("/,/","",$val));
82         if(empty($val)) break;
83         $str .= "/".$val;
84       } 
85       if(!empty($str)){
86         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
87       }
88     }
89     asort($ret);
90     return($ret);
91   }
93   function execute()
94   {
95     /* Call parent execute */
96     plugin::execute();
99     /**************** 
100       Variable init 
101      ****************/
103     /* These vars will be stored if you try to open a locked mime, 
104         to be able to perform your last requests after showing a warning message */
105     $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/","/^item_selected/","/^remove_multiple_mimetypes/");
107     $smarty       = get_smarty();             // Smarty instance
108     $s_action     = "";                       // Contains the action to proceed
109     $s_entry      = "";                       // The value for s_action
110     $base_back    = "";                       // The Link for Backbutton
111     
112     /* Test Posts */
113     foreach($_POST as $key => $val){
114       // Post for delete
115       if(preg_match("/mime_del.*/",$key)){
116         $s_action = "del";
117         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
118         // Post for edit
119       }elseif(preg_match("/mime_edit_.*/",$key)){
120         $s_action="edit";
121         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
122         // Post for new
123       }elseif(preg_match("/^copy_.*/",$key)){
124         $s_action="copy";
125         $s_entry  = preg_replace("/^copy_/i","",$key);
126       }elseif(preg_match("/^cut_.*/",$key)){
127         $s_action="cut";
128         $s_entry  = preg_replace("/^cut_/i","",$key);
129         // Post for new
130       }elseif(preg_match("/^mime_new.*/",$key)){
131         $s_action="new";
132       }elseif(preg_match("/^remove_multiple_mimetypes/",$key)){
133         $s_action="del_multiple";
134       }elseif(preg_match("/^editPaste.*/i",$key)){
135         $s_action="editPaste";
136       }elseif(preg_match("/^multiple_copy_mimetypes/",$key)){
137         $s_action = "copy_multiple";
138       }elseif(preg_match("/^multiple_cut_mimetypes/",$key)){
139         $s_action = "cut_multiple";
140       }
141     }
143     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
144       $s_action ="edit";
145       $s_entry  = $_GET['id'];
146     }
148     $s_entry  = preg_replace("/_.$/","",$s_entry);
151     /**************** 
152       Copy & Paste handling  
153      ****************/
155     /* Display the copy & paste dialog, if it is currently open */
156     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
157     if($ret){
158       return($ret);
159     }
162     /**************** 
163       Create a new mime type 
164      ****************/
166     /* New mime type? */
167     $ui = get_userinfo();
168     if (($s_action=="new")){
170       /* By default we set 'dn' to 'new', all relevant plugins will
171          react on this. */
172       $this->dn= "new";
174       /* Create new usertab object */
175       $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn);
177       /* Set up the users ACL's for this 'dn' */
178       $acl= get_permissions ($this->DivListMimeTypes->selectedBase, $this->ui->subtreeACL);
179       $this->mimetabs->set_acl($acl);
180     }   
183     /**************** 
184       Edit entry canceled 
185      ****************/
187     /* Cancel dialogs */
188     if (isset($_POST['edit_cancel'])){
189       del_lock ($this->mimetabs->dn);
190       unset ($this->mimetabs);
191       $this->mimetabs= NULL;
192       unset ($_SESSION['objectinfo']);
193     }
196     /**************** 
197       Edit entry finished 
198      ****************/
200     /* Finish mime edit is triggered by the tabulator dialog, so
201        the user wants to save edited data. Check and save at this point. */
202     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
204       /* Check tabs, will feed message array */
205       $this->mimetabs->save_object();
206       $message= $this->mimetabs->check();
208       /* Save, or display error message? */
209       if (count($message) == 0){
211         /* Save data data to ldap */
212         $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
213         $this->mimetabs->save();
215         if (!isset($_POST['edit_apply'])){
216           /* Mime type has been saved successfully, remove lock from LDAP. */
217           if ($this->dn != "new"){
218             del_lock ($this->dn);
219           }
220           unset ($this->mimetabs);
221           $this->mimetabs= NULL;
222           unset ($_SESSION['objectinfo']);
223         }
224       } else {
225         /* Ok. There seem to be errors regarding to the tab data,
226            show message and continue as usual. */
227         show_errors($message);
228       }
229     }
232     /**************** 
233       Edit entry  
234      ****************/
236     /* User wants to edit data? */
237     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
239       /* Get 'dn' from posted 'mimelist', must be unique */
240       $this->dn= $this->mimetypes[$s_entry]['dn'];
242       /* Check locking, save current plugin in 'back_plugin', so
243          the dialog knows where to return. */
244       if (($user= get_lock($this->dn)) != ""){
245         return(gen_locked_message ($user, $this->dn));
246       }
248       /* Lock the current entry, so everyone will get the
249          above dialog */
250       add_lock ($this->dn, $this->ui->dn);
253       /* Register mimetabs to trigger edit dialog */
254       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn);
256       /* Set up the users ACL's for this 'dn' */
257       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
258       $this->mimetabs->set_acl($acl);
260       $_SESSION['objectinfo']= $this->dn;
261     }
264     /********************
265       Delete MULTIPLE entries requested, display confirm dialog
266      ********************/
268     if ($s_action=="del_multiple"){
269       $ids = $this->list_get_selected_items();
271       if(count($ids)){
273         foreach($ids as $id){
274           $dn = $this->mimetypes[$id]['dn'];
275           if (($user= get_lock($dn)) != ""){
276             return(gen_locked_message ($user, $dn));
277           }
278           $this->dns[$id] = $dn;
279         }
281         $dns_names = "<br><pre>";
282         foreach($this->dns as $dn){
283           add_lock ($dn, $this->ui->dn);
284           $dns_names .= $dn."\n";
285         }
286         $dns_names .="</pre>";
288         /* Lock the current entry, so nobody will edit it during deletion */
289         if (count($this->dns) == 1){
290           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
291         } else {
292           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
293         }
294         $smarty->assign("multiple", true);
295         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
296       }
297     }
300     /**************** 
301       Delete mime type 
302      ****************/
304     /* Remove user was requested */
305     if ($s_action == "del"){
307       /* Get 'dn' from posted 'uid' */
308       $this->dn= $this->mimetypes[$s_entry]['dn'];
310       /* Load permissions for selected 'dn' and check if
311          we're allowed to remove this 'dn' */
312       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
313       $acl= get_module_permission($acl, "mimetype", $this->dn);
315       if (chkacl($acl,"remove") == ""){
317         /* Check locking, save current plugin in 'back_plugin', so
318            the dialog knows where to return. */
319         if (($user= get_lock($this->dn)) != ""){
320           return (gen_locked_message ($user, $this->dn));
321         }
323         /* Lock the current entry, so nobody will edit it during deletion */
324         add_lock ($this->dn, $this->ui->dn);
325         $smarty= get_smarty();
326         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
327         $smarty->assign("multiple", false);
328         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
329       } else {
331         /* Obviously the user isn't allowed to delete. Show message and
332            clean session. */
333         print_red (_("You are not allowed to delete this mime type!"));
334       }
335     }
338     /**************** 
339       Delete mime confirmed 
340      ****************/
342     /* Confirmation for deletion has been passed. Group should be deleted. */
343     if (isset($_POST['delete_mime_confirm'])){
345       /* Load permissions for selected 'dn' and check if
346          we're allowed to remove this 'dn' */
347       $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
348       $acl= get_module_permission($acl, "mimetype", $this->dn);
349   
350       if (chkacl($acl,"remove") == ""){
352         /* Delete request is permitted, perform LDAP action */
353         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetype");
354         $this->mimetabs->set_acl_base($this->dn);
355         $this->mimetabs->delete ();
356         unset ($this->mimetabs);
357         $this->mimetabs= NULL;
359       } else {
361         /* Normally this shouldn't be reached, send some extra
362            logs to notify the administrator */
363         print_red (_("You are not allowed to delete this mime type!"));
364       }
366       /* Remove lock file after successfull deletion */
367       del_lock ($this->dn);
368     }
371     /**************** 
372       Delete mime canceled 
373      ****************/
375     /* Delete mime type canceled? */
376     if (isset($_POST['delete_cancel'])){
377       del_lock ($this->dn);
378       unset($_SESSION['objectinfo']);
379     }
381     /* Show tab dialog if object is present */
382     if (($this->mimetabs) && (isset($this->mimetabs->config))){
383       $display= $this->mimetabs->execute();
385       /* Don't show buttons if tab dialog requests this */
386       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
387         $display.= "<p style=\"text-align:right\">\n";
388         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
389         $display.= "&nbsp;\n";
390         if ($this->dn != "new"){
391           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
392           $display.= "&nbsp;\n";
393         }
394         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
395         $display.= "</p>";
396       }
397       return ($display);
398     }
401     /****************
402       Dialog display
403      ****************/
405     /* Display dialog with system list */
406     $this->DivListMimeTypes->parent = $this;
407     $this->DivListMimeTypes->execute();
408     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
409     $this->reload();
410     $this->DivListMimeTypes->setEntries($this->mimetypes);
411     return($this->DivListMimeTypes->Draw());
412   }
414     
415   /* Return departments, that will be included within snapshot detection */
416   function get_used_snapshot_bases()
417   {
418     return(array($this->DivListMimeTypes->selectedRelease));
419   }
423   function reload()
424   {
425     $this->mimetypes= array();
427     /* Set base for all searches */
428     $base       = $this->DivListMimeTypes->selectedBase;
429     $release    = $this->DivListMimeTypes->selectedRelease;
430     $Regex      = $this->DivListMimeTypes->Regex;
431     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
432     $Flags      =  GL_NONE | GL_SIZELIMIT;
433     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
434     $tmp        = array();
435     $Releases   = $this->getReleases($base);
438     /* If release management is enabled, use release as base. */
439     if(!$this->enableReleaseManagement){
440       $use_base = $base;
441     }else{
442       if(isset($Releases[$release])){
443         $use_base  = $release;
444       }else{
445         $use_base  = $base;
446       }
447     }
449     /* In case of subsearch, add the subsearch flag */
450     if($SubSearch){
451       $Flags    |= GL_SUBSEARCH;  
452     }else{
453       if(!$this->enableReleaseManagement){
454         $use_base ="ou=mime,".$use_base;
455       }
456     }
457   
458     /* Get results and create index */ 
459     $res= get_list($Filter, $this->ui->subtreeACL, $use_base, array("cn","description","dn","objectClass"), $Flags);
460     foreach ($res as $val){
461       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
462     }
464     /* sort entries */
465     ksort($tmp);
466     $this->mimetypes=array();
467     foreach($tmp as $val){
468       $this->mimetypes[]=$val;
469     }
470     reset ($this->mimetypes);
471   }
473   function remove_from_parent()
474   {
475     /* Optionally execute a command after we're done */
476     $this->postremove();
477   }
480   function copyPasteHandling_from_queue($s_action,$s_entry)
481   {
482     /* Check if Copy & Paste is disabled */
483     if(!is_object($this->CopyPasteHandler)){
484       return("");
485     }
487     /* Add a single entry to queue */
488     if($s_action == "cut" || $s_action == "copy"){
490       /* Cleanup object queue */
491       $this->CopyPasteHandler->cleanup_queue();
492       $dn = $this->mimetypes[$s_entry]['dn'];
493       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetype");
494     }
496     /* Add entries to queue */
497     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
499       /* Cleanup object queue */
500       $this->CopyPasteHandler->cleanup_queue();
502       /* Add new entries to CP queue */
503       foreach($this->list_get_selected_items() as $id){
504         $dn = $this->mimetypes[$id]['dn'];
506         if($s_action == "copy_multiple"){
507           $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetype");
508         }
509         if($s_action == "cut_multiple"){
510           $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetype");
511         }
512       }
513     }
515     /* Start pasting entries */
516     if($s_action == "editPaste"){
517       $this->start_pasting_copied_objects = TRUE;
518     }
520     /* Return C&P dialog */
521     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
523       /* Load entry from queue and set base */
524       $this->CopyPasteHandler->load_entry_from_queue();
525       $this->CopyPasteHandler->SetVar("base",$this->DivListMimeTypes->selectedBase);
527       /* Get dialog */
528       $data = $this->CopyPasteHandler->execute();
530       /* Return dialog data */
531       if(!empty($data)){
532         return($data);
533       }
534     }
536     /* Automatically disable status for pasting */
537     if(!$this->CopyPasteHandler->entries_queued()){
538       $this->start_pasting_copied_objects = FALSE;
539     }
540     return("");
541   }
544   /* Check if the release management is activated. */
545   function IsReleaseManagementActivated()
546   {
547     /* Check if we should enable the release selection */
548     $tmp = search_config($this->config->data,"faiManagement","CLASS");
549     if(!empty($tmp)){
550       return(true);
551     }
552     return(false);
553   }
556   function list_get_selected_items()
557   {
558     $ids = array();
559     foreach($_POST as $name => $value){
560       if(preg_match("/^item_selected_[0-9]*$/",$name)){
561         $id   = preg_replace("/^item_selected_/","",$name);
562         $ids[$id] = $id;
563       }
564     }
565     return($ids);
566   }
569   /* Save to LDAP */
570   function save()
571   {
572     /* Optionally execute a command after we're done */
573     $this->postcreate();
574   }
576   function remove_lock()
577   {
578     if (isset($this->mimetabs->dn)){
579       del_lock ($this->mimetabs->dn);
580     }
581   }
583   function save_object() {
584     $this->DivListMimeTypes->save_object();
585   }
587   function check() {}
588   function adapt_from_template($dn) {}
589   function password_change_needed() {}
591 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
592 ?>