Code

Updated server service.
[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     $acl = $ui->get_permissions($this->DivListMimeTypes->selectedBase,"mimetypes/mimetype");
169     if (($s_action=="new") && preg_match("/c/",$acl)){
171       /* By default we set 'dn' to 'new', all relevant plugins will
172          react on this. */
173       $this->dn= "new";
175       /* Create new usertab object */
176       $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
177       $this->mimetabs->set_acl_base($this->DivListMimeTypes->selectedBase);
178     }   
181     /**************** 
182       Edit entry canceled 
183      ****************/
185     /* Cancel dialogs */
186     if (isset($_POST['edit_cancel'])){
187       del_lock ($this->mimetabs->dn);
188       unset ($this->mimetabs);
189       $this->mimetabs= NULL;
190       unset ($_SESSION['objectinfo']);
191     }
194     /**************** 
195       Edit entry finished 
196      ****************/
198     /* Finish mime edit is triggered by the tabulator dialog, so
199        the user wants to save edited data. Check and save at this point. */
200     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
202       /* Check tabs, will feed message array */
203       $this->mimetabs->save_object();
204       $message= $this->mimetabs->check();
206       /* Save, or display error message? */
207       if (count($message) == 0){
209         /* Save data data to ldap */
210         $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
211         $this->mimetabs->save();
213         if (!isset($_POST['edit_apply'])){
214           /* Mime type has been saved successfully, remove lock from LDAP. */
215           if ($this->dn != "new"){
216             del_lock ($this->dn);
217           }
218           unset ($this->mimetabs);
219           $this->mimetabs= NULL;
220           unset ($_SESSION['objectinfo']);
221         }
222       } else {
223         /* Ok. There seem to be errors regarding to the tab data,
224            show message and continue as usual. */
225         show_errors($message);
226       }
227     }
230     /**************** 
231       Edit entry  
232      ****************/
234     /* User wants to edit data? */
235     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
237       /* Get 'dn' from posted 'mimelist', must be unique */
238       $this->dn= $this->mimetypes[$s_entry]['dn'];
240       /* Check locking, save current plugin in 'back_plugin', so
241          the dialog knows where to return. */
242       if (($user= get_lock($this->dn)) != ""){
243         return(gen_locked_message ($user, $this->dn));
244       }
246       /* Lock the current entry, so everyone will get the
247          above dialog */
248       add_lock ($this->dn, $this->ui->dn);
251       /* Register mimetabs to trigger edit dialog */
252       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
253       $this->mimetabs->set_acl_base($this->dn);
254       $_SESSION['objectinfo']= $this->dn;
255     }
258     /********************
259       Delete MULTIPLE entries requested, display confirm dialog
260      ********************/
262     if ($s_action=="del_multiple"){
263       $ids = $this->list_get_selected_items();
265       if(count($ids)){
267         foreach($ids as $id){
268           $dn = $this->mimetypes[$id]['dn'];
269           if (($user= get_lock($dn)) != ""){
270             return(gen_locked_message ($user, $dn));
271           }
272           $this->dns[$id] = $dn;
273         }
275         $dns_names = "<br><pre>";
276         foreach($this->dns as $dn){
277           add_lock ($dn, $this->ui->dn);
278           $dns_names .= $dn."\n";
279         }
280         $dns_names .="</pre>";
282         /* Lock the current entry, so nobody will edit it during deletion */
283         if (count($this->dns) == 1){
284           $smarty->assign("intro",     sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
285         } else {
286           $smarty->assign("intro",     sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
287         }
288         $smarty->assign("multiple", true);
289         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
290       }
291     }
296     /********************
297       Delete MULTIPLE entries confirmed
298      ********************/
300     /* Confirmation for deletion has been passed. Users should be deleted. */
301     if (isset($_POST['delete_multiple_mimetype_confirm'])){
303       $ui = get_userinfo();
305       /* Remove user by user and check acls before removeing them */
306       foreach($this->dns as $key => $dn){
308         $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
309         if(preg_match("/d/",$acl)){
311           /* Delete request is permitted, perform LDAP action */
312           $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
313           $this->mimetabs->set_acl_base($dn);
314           $this->mimetabs->delete ();
315           unset ($this->mimetabs);
316           $this->mimetabs= NULL;
318         } else {
319           /* Normally this shouldn't be reached, send some extra
320              logs to notify the administrator */
321           print_red (_("You are not allowed to delete this mime type!"));
322           new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
323         }
324         /* Remove lock file after successfull deletion */
325         del_lock ($dn);
326         unset($this->dns[$key]);
327       }
328     }
331     /********************
332       Delete MULTIPLE entries Canceled
333      ********************/
335     /* Remove lock */
336     if(isset($_POST['delete_multiple_mimetype_cancel'])){
337       foreach($this->dns as $key => $dn){
338         del_lock ($dn);
339         unset($this->dns[$key]);
340       }
341     }
344     /**************** 
345       Delete mime type 
346      ****************/
348     /* Remove user was requested */
349     if ($s_action == "del"){
351       /* Get 'dn' from posted 'uid' */
352       $this->dn= $this->mimetypes[$s_entry]['dn'];
354       /* Load permissions for selected 'dn' and check if
355          we're allowed to remove this 'dn' */
356       $ui = get_userinfo();
357       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
358       if (preg_match("/d/",$acl)){
360         /* Check locking, save current plugin in 'back_plugin', so
361            the dialog knows where to return. */
362         if (($user= get_lock($this->dn)) != ""){
363           return (gen_locked_message ($user, $this->dn));
364         }
366         /* Lock the current entry, so nobody will edit it during deletion */
367         add_lock ($this->dn, $this->ui->dn);
368         $smarty= get_smarty();
369         $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
370         $smarty->assign("multiple", false);
371         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
372       } else {
374         /* Obviously the user isn't allowed to delete. Show message and
375            clean session. */
376         print_red (_("You are not allowed to delete this mime type!"));
377       }
378     }
381     /**************** 
382       Delete mime confirmed 
383      ****************/
385     /* Confirmation for deletion has been passed. Group should be deleted. */
386     if (isset($_POST['delete_mime_confirm'])){
388       /* Some nice guy may send this as POST, so we've to check
389          for the permissions again. */
390       $ui = get_userinfo();
391       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
392       if(preg_match("/d/",$acl)){
394         /* Delete request is permitted, perform LDAP action */
395         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
396         $this->mimetabs->set_acl_base($this->dn);
397         $this->mimetabs->delete ();
398         unset ($this->mimetabs);
399         $this->mimetabs= NULL;
401       } else {
403         /* Normally this shouldn't be reached, send some extra
404            logs to notify the administrator */
405         print_red (_("You are not allowed to delete this mime type!"));
406         new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
407       }
409       /* Remove lock file after successfull deletion */
410       del_lock ($this->dn);
411     }
414     /**************** 
415       Delete mime canceled 
416      ****************/
418     /* Delete mime type canceled? */
419     if (isset($_POST['delete_cancel'])){
420       del_lock ($this->dn);
421       unset($_SESSION['objectinfo']);
422     }
424     /* Show tab dialog if object is present */
425     if (($this->mimetabs) && (isset($this->mimetabs->config))){
426       $display= $this->mimetabs->execute();
428       /* Don't show buttons if tab dialog requests this */
429       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
430         $display.= "<p style=\"text-align:right\">\n";
431         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
432         $display.= "&nbsp;\n";
433         if ($this->dn != "new"){
434           $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
435           $display.= "&nbsp;\n";
436         }
437         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
438         $display.= "</p>";
439       }
440       return ($display);
441     }
444     /****************
445       Dialog display
446      ****************/
448         /* Check if there is a snapshot dialog open */
449     $base = $this->DivListMimeTypes->selectedBase;
450     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
451       return($str);
452     }
454     /* Display dialog with system list */
455     $this->DivListMimeTypes->parent = $this;
456     $this->DivListMimeTypes->execute();
457     $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
458     $this->reload();
459     $this->DivListMimeTypes->setEntries($this->mimetypes);
460     return($this->DivListMimeTypes->Draw());
461   }
463     
464   /* Return departments, that will be included within snapshot detection */
465   function get_used_snapshot_bases()
466   {
467     return(array($this->DivListMimeTypes->selectedRelease));
468   }
472   function reload()
473   {
474     $this->mimetypes= array();
476     /* Set base for all searches */
477     $base       = $this->DivListMimeTypes->selectedBase;
478     $release    = $this->DivListMimeTypes->selectedRelease;
479     $Regex      = $this->DivListMimeTypes->Regex;
480     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
481     $Flags      =  GL_NONE | GL_SIZELIMIT;
482     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
483     $tmp        = array();
484     $Releases   = $this->getReleases($base);
487     /* If release management is enabled, use release as base. */
488     if(!$this->enableReleaseManagement){
489       $use_base = $base;
490     }else{
491       if(isset($Releases[$release])){
492         $use_base  = $release;
493       }else{
494         $use_base  = $base;
495       }
496     }
498     /* In case of subsearch, add the subsearch flag */
499     if($SubSearch){
500       $Flags    |= GL_SUBSEARCH;  
501     }else{
502       if(!$this->enableReleaseManagement){
503         $use_base ="ou=mime,".$use_base;
504       }
505     }
506   
507     /* Get results and create index */ 
508     $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
509     foreach ($res as $val){
510       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
511     }
513     /* sort entries */
514     ksort($tmp);
515     $this->mimetypes=array();
516     foreach($tmp as $val){
517       $this->mimetypes[]=$val;
518     }
519     reset ($this->mimetypes);
520   }
522   function remove_from_parent()
523   {
524     /* Optionally execute a command after we're done */
525     $this->postremove();
526   }
529   function copyPasteHandling_from_queue($s_action,$s_entry)
530   {
531     /* Check if Copy & Paste is disabled */
532     if(!is_object($this->CopyPasteHandler)){
533       return("");
534     }
536     /* Add a single entry to queue */
537     if($s_action == "cut" || $s_action == "copy"){
539       /* Cleanup object queue */
540       $this->CopyPasteHandler->cleanup_queue();
541       $dn = $this->mimetypes[$s_entry]['dn'];
542       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
543     }
545     /* Add entries to queue */
546     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
548       /* Cleanup object queue */
549       $this->CopyPasteHandler->cleanup_queue();
551       /* Add new entries to CP queue */
552       foreach($this->list_get_selected_items() as $id){
553         $dn = $this->mimetypes[$id]['dn'];
555         if($s_action == "copy_multiple"){
556           $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
557         }
558         if($s_action == "cut_multiple"){
559           $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
560         }
561       }
562     }
564     /* Start pasting entries */
565     if($s_action == "editPaste"){
566       $this->start_pasting_copied_objects = TRUE;
567     }
569     /* Return C&P dialog */
570     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
572       /* Load entry from queue and set base */
573       $this->CopyPasteHandler->load_entry_from_queue();
574       $this->CopyPasteHandler->SetVar("base",$this->DivListMimeTypes->selectedBase);
576       /* Get dialog */
577       $data = $this->CopyPasteHandler->execute();
579       /* Return dialog data */
580       if(!empty($data)){
581         return($data);
582       }
583     }
585     /* Automatically disable status for pasting */
586     #if(!$this->CopyPasteHandler->entries_queued()){
587     #  $this->start_pasting_copied_objects = FALSE;
588     #}
589     return("");
590   }
593   /* Check if the release management is activated. */
594   function IsReleaseManagementActivated()
595   {
596     /* Check if we should enable the release selection */
597     $tmp = search_config($this->config->data,"faiManagement","CLASS");
598     if(!empty($tmp)){
599       return(true);
600     }
601     return(false);
602   }
605   function list_get_selected_items()
606   {
607     $ids = array();
608     foreach($_POST as $name => $value){
609       if(preg_match("/^item_selected_[0-9]*$/",$name)){
610         $id   = preg_replace("/^item_selected_/","",$name);
611         $ids[$id] = $id;
612       }
613     }
614     return($ids);
615   }
618   /* Save to LDAP */
619   function save()
620   {
621     /* Optionally execute a command after we're done */
622     $this->postcreate();
623   }
625   function remove_lock()
626   {
627     if (isset($this->mimetabs->dn)){
628       del_lock ($this->mimetabs->dn);
629     }
630   }
632   function save_object() {
633     $this->DivListMimeTypes->save_object();
634   }
636   function check() {}
637   function adapt_from_template($dn) {}
638   function password_change_needed() {}
640 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
641 ?>