Code

Removed pass-by-reference
[gosa.git] / gosa-plugins / goto / 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 class mimetypeManagement extends plugin
22 {
23   /* Definitions */
24   var $plHeadline     = "Mime types";
25   var $plDescription  = "Manage mime types";
27   /* Dialog attributes */
28   var $ui                             = NULL;
29   var $DivListMimeTypes               = NULL;
30   var $mimetabs                       = NULL;
31   var $snapDialog                     = NULL;
32   var $CopyPasteHandler               = NULL;
33   var $start_pasting_copied_objects   = FALSE;
34   var $enableReleaseManagement        = false;
36   var $mime_base    = "";
37   var $mime_release = "";
40   function IsReleaseManagementActivated()
41   {
42     /* Check if we should enable the release selection */
43     $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
44     if(!empty($tmp)){
45       return(true);
46     }
47     return(false);
48   }
51   function mimetypeManagement (&$config, &$ui)
52   {
53     /* Save configuration for internal use */
54     $this->config   = &$config;
55     $this->ui       = &$ui;
57     /* Check if copy & paste is activated */
58     if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
59       $this->CopyPasteHandler = new CopyPasteHandler($this->config);
60     }
62     /* Creat dialog object */
63     $this->DivListMimeTypes = new divListMimeTypes($this->config,$this);
65     if($this->IsReleaseManagementActivated()){
67       /* Check if we should enable the release selection */
68       $this->enableReleaseManagement = true;
70       /* Hide SubSearch checkbox */
71       $this->DivListMimeTypes->DisableCheckBox("SubSearch");
72     }
74     /* Set default release */
75     if(!$this->IsReleaseManagementActivated()){
76       $this->mime_base = get_ou("mimetypeou").$this->config->current['BASE'];
77       if(!session::is_set("mime_filter")){
78         session::set("mime_filter",array("mime_base" => $this->mime_base));
79       }
80       $mime_filter     = session::get("mime_filter");
81       $this->mime_base = $mime_filter['mime_base'];
82     }else{
83       $this->mime_base = get_ou("mimetypeou").$this->config->current['BASE'];
84       if(!session::is_set("mime_filter")){
85         session::set("mime_filter",array("mime_base" => $this->mime_base,"mime_release" => $this->mime_base));
86       }
87       $mime_filter         = session::get("mime_filter");
88       $this->mime_base     = $mime_filter['mime_base'];
89       $this->mime_release  = $mime_filter['mime_release'];
90     }
91   }
94   /* Get all releases */
95   function getReleases()
96   {
97     $ldap                   = $this->config->get_ldap_link();
98     $ret                    = array();
99     $ret [$this->mime_base] = "/";    
101     $ldap->cd($this->mime_base);
102     $ldap->search("(&(objectClass=FAIbranch)(objectClass=organizationalUnit))",array("ou"));
104     while($attrs = $ldap->fetch()){
105       $str = str_replace($this->mime_base,"",$attrs['dn']);
106       $tmp = array_reverse( split("ou=",$str));
107       $str = "";
108       foreach($tmp as $val){
109         $val = trim(preg_replace("/,/","",$val));
110         if(empty($val)) break;
111         $str .= "/".$val;
112       } 
113       if(!empty($str)){
114         $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
115       }
116     }
117     asort($ret);
118     return($ret);
119   }
122   function execute()
123   {
124     /* Call parent execute */
125     plugin::execute();
128     /**************** 
129       Variable init 
130      ****************/
132     /* These vars will be stored if you try to open a locked mime, 
133         to be able to perform your last requests after showing a warning message */
134     session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/",
135           "/^item_selected/","/^remove_multiple_mimetypes/"));
137     $smarty       = get_smarty();             // Smarty instance
138     $s_action     = "";                       // Contains the action to proceed
139     $s_entry      = "";                       // The value for s_action
140     $base_back    = "";                       // The Link for Backbutton
141     
142     /* Test Posts */
143     foreach($_POST as $key => $val){
144       // Post for delete
145       if(preg_match("/mime_del.*/",$key)){
146         $s_action = "del";
147         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
148         // Post for edit
149       }elseif(preg_match("/mime_edit_.*/",$key)){
150         $s_action="edit";
151         $s_entry  = preg_replace("/mime_".$s_action."_/i","",$key);
152         // Post for new
153       }elseif(preg_match("/^copy_.*/",$key)){
154         $s_action="copy";
155         $s_entry  = preg_replace("/^copy_/i","",$key);
156       }elseif(preg_match("/^cut_.*/",$key)){
157         $s_action="cut";
158         $s_entry  = preg_replace("/^cut_/i","",$key);
159         // Post for new
160       }elseif(preg_match("/^mime_new.*/",$key)){
161         $s_action="new";
162       }elseif(preg_match("/^remove_multiple_mimetypes/",$key)){
163         $s_action="del_multiple";
164       }elseif(preg_match("/^editPaste.*/i",$key)){
165         $s_action="editPaste";
166       }elseif(preg_match("/^multiple_copy_mimetypes/",$key)){
167         $s_action = "copy_multiple";
168       }elseif(preg_match("/^multiple_cut_mimetypes/",$key)){
169         $s_action = "cut_multiple";
170       }
171     }
173     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
174       $s_action ="edit";
175       $s_entry  = $_GET['id'];
176     }
178     $s_entry  = preg_replace("/_.$/","",$s_entry);
180     /* handle C&P from layers menu */
181     if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
182       $s_action = "copy_multiple";
183     }
184     if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
185       $s_action = "cut_multiple";
186     }
187     if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
188       $s_action = "editPaste";
189     }
191     /* Create options */
192     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "mime_new"){
193       $s_action = "new";
194     }
196     /* handle remove from layers menu */
197     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
198       $s_action = "del_multiple";
199     }
202     /**************** 
203       Copy & Paste handling  
204      ****************/
206     /* Display the copy & paste dialog, if it is currently open */
207     $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
208     if($ret){
209       return($ret);
210     }
213     /**************** 
214       Create a new mime type 
215      ****************/
217     /* New mime type? */
218     $ui = get_userinfo();
219     $acl = $ui->get_permissions($this->mime_base,"mimetypes/mimetype");
220     if (($s_action=="new") && preg_match("/c/",$acl)){
222       /* By default we set 'dn' to 'new', all relevant plugins will
223          react on this. */
224       $this->dn= "new";
226       /* Create new usertab object */
227       $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
228       $this->mimetabs->parent = &$this;
229       $this->mimetabs->set_acl_base($this->mime_base);
230     }   
233     /**************** 
234       Edit entry canceled 
235      ****************/
237     /* Cancel dialogs */
238     if (isset($_POST['edit_cancel'])){
239       del_lock ($this->mimetabs->dn);
240       unset ($this->mimetabs);
241       $this->mimetabs= NULL;
242       session::un_set('objectinfo');
243     }
246     /**************** 
247       Edit entry finished 
248      ****************/
250     /* Finish mime edit is triggered by the tabulator dialog, so
251        the user wants to save edited data. Check and save at this point. */
252     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
254       /* Check tabs, will feed message array */
255       $this->mimetabs->save_object();
256       $message= $this->mimetabs->check();
258       /* Save, or display error message? */
259       if (count($message) == 0){
261         /* Save data data to ldap */
262         $this->mimetabs->save();
264         if (!isset($_POST['edit_apply'])){
265           /* Mime type has been saved successfully, remove lock from LDAP. */
266           if ($this->dn != "new"){
267             del_lock ($this->dn);
268           }
269           unset ($this->mimetabs);
270           $this->mimetabs= NULL;
271           session::un_set('objectinfo');
272         }
273       } else {
274         /* Ok. There seem to be errors regarding to the tab data,
275            show message and continue as usual. */
276         msg_dialog::displayChecks($message);
277       }
278     }
281     /**************** 
282       Edit entry  
283      ****************/
285     /* User wants to edit data? */
286     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
288       /* Get 'dn' from posted 'mimelist', must be unique */
289       $this->dn= $this->mimetypes[$s_entry]['dn'];
291       /* Check locking, save current plugin in 'back_plugin', so
292          the dialog knows where to return. */
293       if (($user= get_lock($this->dn)) != ""){
294         return(gen_locked_message ($user, $this->dn));
295       }
297       /* Lock the current entry, so everyone will get the
298          above dialog */
299       add_lock ($this->dn, $this->ui->dn);
301       /* Register mimetabs to trigger edit dialog */
302       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
303       $this->mimetabs->parent = &$this;
304       $this->mimetabs->set_acl_base($this->dn);
305       session::set('objectinfo',$this->dn);
306     }
309     /********************
310       Delete MULTIPLE entries requested, display confirm dialog
311      ********************/
313     if ($s_action=="del_multiple"){
314       $ids = $this->list_get_selected_items();
316       $this->dns = array();
317       if(count($ids)){
319         foreach($ids as $id){
320           $dn = $this->mimetypes[$id]['dn'];
321           if (($user= get_lock($dn)) != ""){
322             return(gen_locked_message ($user, $dn));
323           }
324           $this->dns[$id] = $dn;
325         }
327         $dns_names = array();
328         foreach($this->dns as $dn){
329           add_lock ($dn, $this->ui->dn);
330           $dns_names[] = @LDAP::fix($dn);
331         }
333         /* Lock the current entry, so nobody will edit it during deletion */
334         $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("Mime type")));
335         $smarty->assign("multiple", true);
336         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
337       }
338     }
341     /********************
342       Delete MULTIPLE entries confirmed
343      ********************/
345     /* Confirmation for deletion has been passed. Users should be deleted. */
346     if (isset($_POST['delete_multiple_mimetype_confirm'])){
348       $ui = get_userinfo();
350       /* Remove user by user and check acls before removeing them */
351       foreach($this->dns as $key => $dn){
353         $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
354         if(preg_match("/d/",$acl)){
356           /* Delete request is permitted, perform LDAP action */
357           $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
358           $this->mimetabs->parent = &$this;
359           $this->mimetabs->set_acl_base($dn);
360           $this->mimetabs->delete ();
361           unset ($this->mimetabs);
362           $this->mimetabs= NULL;
364         } else {
365           /* Normally this shouldn't be reached, send some extra
366              logs to notify the administrator */
367           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
368           new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
369         }
370         /* Remove lock file after successfull deletion */
371         del_lock ($dn);
372         unset($this->dns[$key]);
373       }
374     }
377     /********************
378       Delete MULTIPLE entries Canceled
379      ********************/
381     /* Remove lock */
382     if(isset($_POST['delete_multiple_mimetype_cancel'])){
383       foreach($this->dns as $key => $dn){
384         del_lock ($dn);
385         unset($this->dns[$key]);
386       }
387     }
390     /**************** 
391       Delete mime type 
392      ****************/
394     /* Remove user was requested */
395     if ($s_action == "del"){
397       /* Get 'dn' from posted 'uid' */
398       $this->dn= $this->mimetypes[$s_entry]['dn'];
400       /* Load permissions for selected 'dn' and check if
401          we're allowed to remove this 'dn' */
402       $ui = get_userinfo();
403       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
404       if (preg_match("/d/",$acl)){
406         /* Check locking, save current plugin in 'back_plugin', so
407            the dialog knows where to return. */
408         if (($user= get_lock($this->dn)) != ""){
409           return (gen_locked_message ($user, $this->dn));
410         }
412         /* Lock the current entry, so nobody will edit it during deletion */
413         add_lock ($this->dn, $this->ui->dn);
414         $smarty= get_smarty();
415         $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("Mime type")));
416         $smarty->assign("multiple", false);
417         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
418       } else {
420         /* Obviously the user isn't allowed to delete. Show message and
421            clean session. */
422         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
423       }
424     }
427     /**************** 
428       Delete mime confirmed 
429      ****************/
431     /* Confirmation for deletion has been passed. Group should be deleted. */
432     if (isset($_POST['delete_mime_confirm'])){
434       /* Some nice guy may send this as POST, so we've to check
435          for the permissions again. */
436       $ui = get_userinfo();
437       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
438       if(preg_match("/d/",$acl)){
440         /* Delete request is permitted, perform LDAP action */
441         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
442         $this->mimetabs->parent = &$this;
443         $this->mimetabs->set_acl_base($this->dn);
444         $this->mimetabs->delete ();
445         unset ($this->mimetabs);
446         $this->mimetabs= NULL;
448       } else {
450         /* Normally this shouldn't be reached, send some extra
451            logs to notify the administrator */
452         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
453         new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
454       }
456       /* Remove lock file after successfull deletion */
457       del_lock ($this->dn);
458     }
461     /**************** 
462       Delete mime canceled 
463      ****************/
465     /* Delete mime type canceled? */
466     if (isset($_POST['delete_cancel'])){
467       del_lock ($this->dn);
468       session::un_set('objectinfo');
469     }
471     /* Show tab dialog if object is present */
472     if (($this->mimetabs) && (isset($this->mimetabs->config))){
473       $display= $this->mimetabs->execute();
475       /* Don't show buttons if tab dialog requests this */
476       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
477         $display.= "<p style=\"text-align:right\">\n";
478         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
479         $display.= "&nbsp;\n";
480         if ($this->dn != "new"){
481           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
482           $display.= "&nbsp;\n";
483         }
484         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
485         $display.= "</p>";
486       }
487       return ($display);
488     }
491     /****************
492       Dialog display
493      ****************/
495     /* Check if there is a snapshot dialog open */
496     if($this->IsReleaseManagementActivated()){
497       $base = $this->mime_release;
498     }else{
499       $base = $this->mime_base;
500     }
501     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
502       return($str);
503     }
505     /* Display dialog with system list */
506     $this->DivListMimeTypes->parent = $this;
507     $this->DivListMimeTypes->execute();
508     if(!$this->IsReleaseManagementActivated()){
509       $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
510     }
511     $this->reload();
512     $this->DivListMimeTypes->setEntries($this->mimetypes);
513     return($this->DivListMimeTypes->Draw());
514   }
516     
517   /* Return departments, that will be included within snapshot detection */
518   function get_used_snapshot_bases()
519   {
520     if($this->IsReleaseManagementActivated()){
521       return(array($this->mime_release));
522     }else{
523       return(array($this->mime_base));
524     }
525   }
528   function reload()
529   {
530     $this->mimetypes= array();
532     /* Set base for all searches */
533     $base       = $this->mime_base;
534     $Regex      = $this->DivListMimeTypes->Regex;
535     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
536     $Flags      =  GL_NONE | GL_SIZELIMIT;
537     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
538     $tmp        = array();
540     if(!$this->IsReleaseManagementActivated()){
541       $use_base = $this->mime_base;
542       if($SubSearch){
543         $use_base = preg_replace("/^".normalizePreg(get_ou("mimeou"))."/","",$use_base);
544       }
545     }else{
546       $use_base = $this->mime_release;
547       $SubSearch= FALSE;
548     }
550     if($SubSearch){
551       $res= get_sub_list($Filter, "mimetypes",get_ou("mimeou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
552     }else{
553       $res= get_list($Filter, "mimetypes",$use_base, array("cn","description","dn","objectClass"), $Flags);
554     }
557     $tmp2 = array();
558     foreach ($res as $val){
559       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
560       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
561     }
563     /* sort entries */
564     natcasesort($tmp2);
565     $this->mimetypes=array();
566     foreach($tmp2 as $val){
567       $this->mimetypes[]=$tmp[$val];
568     }
569     reset ($this->mimetypes);
570   }
573   function remove_from_parent()
574   {
575     /* Optionally execute a command after we're done */
576     $this->postremove();
577   }
580   function copyPasteHandling_from_queue($s_action,$s_entry)
581   {
582     /* Check if Copy & Paste is disabled */
583     if(!is_object($this->CopyPasteHandler)){
584       return("");
585     }
587     /* Add a single entry to queue */
588     if($s_action == "cut" || $s_action == "copy"){
590       /* Cleanup object queue */
591       $this->CopyPasteHandler->cleanup_queue();
592       $dn = $this->mimetypes[$s_entry]['dn'];
593       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
594     }
597     /* Add entries to queue */
598     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
600       /* Cleanup object queue */
601       $this->CopyPasteHandler->cleanup_queue();
602   
603       /* Add new entries to CP queue */
604       foreach($this->list_get_selected_items() as $id){
605         $dn = $this->mimetypes[$id]['dn'];
607         if($s_action == "copy_multiple"){
608           $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
609         }
610         if($s_action == "cut_multiple"){
611           $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
612         }
613       }
614     }
616     /* Start pasting entries */
617     if($s_action == "editPaste"){
618       $this->start_pasting_copied_objects = TRUE;
619     }
621     /* Return C&P dialog */
622     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
624       /* Get dialog */
625       $this->CopyPasteHandler->SetVar("base",preg_replace("/^".normalizePreg(get_ou("mimetypeou"))."/","",$this->mime_base));
626       $this->CopyPasteHandler->SetVar("parent",$this);
627       $data = $this->CopyPasteHandler->execute();
629       /* Return dialog data */
630       if(!empty($data)){
631         return($data);
632       }
633     }
635     /* Automatically disable status for pasting */
636     if(!$this->CopyPasteHandler->entries_queued()){
637       $this->start_pasting_copied_objects = FALSE;
638     }
639     return("");
640   }
643   function list_get_selected_items()
644   {
645     $ids = array();
646     foreach($_POST as $name => $value){
647       if(preg_match("/^item_selected_[0-9]*$/",$name)){
648         $id   = preg_replace("/^item_selected_/","",$name);
649         $ids[$id] = $id;
650       }
651     }
652     return($ids);
653   }
656   /* Save to LDAP */
657   function save()
658   {
659     /* Optionally execute a command after we're done */
660     $this->postcreate();
661   }
663   function remove_lock()
664   {
665     if (isset($this->mimetabs->dn)){
666       del_lock ($this->mimetabs->dn);
667     }
668   }
670   function save_object() 
671   {
672     $this->DivListMimeTypes->save_object();
673     if(is_object($this->CopyPasteHandler)){
674       $this->CopyPasteHandler->save_object();
675     }
677     if($this->IsReleaseManagementActivated() && isset($_POST['mime_release'])){
678       $sel_rel = get_post('mime_release');
679       $releases = array_flip($this->getReleases());
680       if(isset($releases[$sel_rel])){
681         $this->mime_release = $releases[$sel_rel];
682       }
683       $mime_filter     = session::get("mime_filter");
684       $mime_filter['mime_release'] = $this->mime_release;
685       session::set("mime_filter",$mime_filter);
686     }elseif(!$this->IsReleaseManagementActivated()){
687       $this->mime_base = get_ou("mimetypeou").$this->DivListMimeTypes->selectedBase;
688       $mime_filter     = session::get("mime_filter");
689       $mime_filter['mime_base'] = $this->mime_base;
690       session::set("mime_filter",$mime_filter);
691     }
692   }
695   function check() {}
696   function adapt_from_template($dn, $skip= array()) {}
697   function password_change_needed() {}
699 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
700 ?>