Code

debe254ddf49d02241bce4327c278e5e2880de6b
[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/","/^menu_action/"));
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       $this->remove_lock();
240       $this->mimetabs= NULL;
241       session::un_set('objectinfo');
242     }
245     /**************** 
246       Edit entry finished 
247      ****************/
249     /* Finish mime edit is triggered by the tabulator dialog, so
250        the user wants to save edited data. Check and save at this point. */
251     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
253       /* Check tabs, will feed message array */
254       $this->mimetabs->save_object();
255       $message= $this->mimetabs->check();
257       /* Save, or display error message? */
258       if (count($message) == 0){
260         /* Save data data to ldap */
261         $this->mimetabs->save();
263         if (!isset($_POST['edit_apply'])){
264           /* Mime type has been saved successfully, remove lock from LDAP. */
265           if ($this->dn != "new"){
266             $this->remove_lock();
267           }
268           unset ($this->mimetabs);
269           $this->mimetabs= NULL;
270           session::un_set('objectinfo');
271         }else{
273           /* Reinitialize tab */
274           if($this->mimetabs instanceof tabs){
275             $this->mimetabs->re_init();
276           }
277         }
278       } else {
279         /* Ok. There seem to be errors regarding to the tab data,
280            show message and continue as usual. */
281         msg_dialog::displayChecks($message);
282       }
283     }
286     /**************** 
287       Edit entry  
288      ****************/
290     /* User wants to edit data? */
291     if (($s_action=="edit") && (!isset($this->mimetabs->config))){
293       /* Get 'dn' from posted 'mimelist', must be unique */
294       $this->dn= $this->mimetypes[$s_entry]['dn'];
296       /* Check locking, save current plugin in 'back_plugin', so
297          the dialog knows where to return. */
298       if (($user= get_lock($this->dn)) != ""){
299         return(gen_locked_message ($user, $this->dn));
300       }
302       /* Lock the current entry, so everyone will get the
303          above dialog */
304       add_lock ($this->dn, $this->ui->dn);
306       /* Register mimetabs to trigger edit dialog */
307       $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
308       $this->mimetabs->parent = &$this;
309       $this->mimetabs->set_acl_base($this->dn);
310       session::set('objectinfo',$this->dn);
311     }
314     /********************
315       Delete MULTIPLE entries requested, display confirm dialog
316      ********************/
318     if ($s_action=="del_multiple"){
319       $ids = $this->list_get_selected_items();
321       $this->dns = array();
322       if(count($ids)){
324         foreach($ids as $id){
325           $this->dns[$id] = $this->mimetypes[$id]['dn'];
326         }
328         /* Check locks */
329         if ($user= get_multiple_locks($this->dns)){
330           return(gen_locked_message($user,$this->dns));
331         }
333         $dns_names = array();
334         foreach($this->dns as $dn){
335           $dns_names[] = @LDAP::fix($dn);
336         }
338         add_lock ($this->dns, $this->ui->dn);
340         /* Lock the current entry, so nobody will edit it during deletion */
341         $smarty->assign("info", msgPool::deleteInfo($dns_names,_("Mime type")));
342         $smarty->assign("multiple", true);
343         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
344       }
345     }
348     /********************
349       Delete MULTIPLE entries confirmed
350      ********************/
352     /* Confirmation for deletion has been passed. Users should be deleted. */
353     if (isset($_POST['delete_multiple_mimetype_confirm'])){
355       $ui = get_userinfo();
357       /* Remove user by user and check acls before removeing them */
358       foreach($this->dns as $key => $dn){
360         $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
361         if(preg_match("/d/",$acl)){
363           /* Delete request is permitted, perform LDAP action */
364           $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
365           $this->mimetabs->parent = &$this;
366           $this->mimetabs->set_acl_base($dn);
367           $this->mimetabs->delete ();
368           unset ($this->mimetabs);
369           $this->mimetabs= NULL;
371         } else {
372           /* Normally this shouldn't be reached, send some extra
373              logs to notify the administrator */
374           msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
375           new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
376         }
377       }
379       /* Remove lock file after successfull deletion */
380       $this->remove_lock();
381       $this->dns = array();
382     }
385     /********************
386       Delete MULTIPLE entries Canceled
387      ********************/
389     /* Remove lock */
390     if(isset($_POST['delete_multiple_mimetype_cancel'])){
391       $this->remove_lock();
392       $this->dns = array();
393     }
396     /**************** 
397       Delete mime type 
398      ****************/
400     /* Remove user was requested */
401     if ($s_action == "del"){
403       /* Get 'dn' from posted 'uid' */
404       $this->dn= $this->mimetypes[$s_entry]['dn'];
406       /* Load permissions for selected 'dn' and check if
407          we're allowed to remove this 'dn' */
408       $ui = get_userinfo();
409       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
410       if (preg_match("/d/",$acl)){
412         /* Check locking, save current plugin in 'back_plugin', so
413            the dialog knows where to return. */
414         if (($user= get_lock($this->dn)) != ""){
415           return (gen_locked_message ($user, $this->dn));
416         }
418         /* Lock the current entry, so nobody will edit it during deletion */
419         add_lock ($this->dn, $this->ui->dn);
420         $smarty= get_smarty();
421         $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("Mime type")));
422         $smarty->assign("multiple", false);
423         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
424       } else {
426         /* Obviously the user isn't allowed to delete. Show message and
427            clean session. */
428         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
429       }
430     }
433     /**************** 
434       Delete mime confirmed 
435      ****************/
437     /* Confirmation for deletion has been passed. Group should be deleted. */
438     if (isset($_POST['delete_mime_confirm'])){
440       /* Some nice guy may send this as POST, so we've to check
441          for the permissions again. */
442       $ui = get_userinfo();
443       $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
444       if(preg_match("/d/",$acl)){
446         /* Delete request is permitted, perform LDAP action */
447         $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
448         $this->mimetabs->parent = &$this;
449         $this->mimetabs->set_acl_base($this->dn);
450         $this->mimetabs->delete ();
451         unset ($this->mimetabs);
452         $this->mimetabs= NULL;
454       } else {
456         /* Normally this shouldn't be reached, send some extra
457            logs to notify the administrator */
458         msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
459         new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
460       }
462       /* Remove lock file after successfull deletion */
463       $this->remove_lock();
464     }
467     /**************** 
468       Delete mime canceled 
469      ****************/
471     /* Delete mime type canceled? */
472     if (isset($_POST['delete_cancel'])){
473       $this->remove_lock();
474       session::un_set('objectinfo');
475     }
477     /* Show tab dialog if object is present */
478     if (($this->mimetabs) && (isset($this->mimetabs->config))){
479       $display= $this->mimetabs->execute();
481       /* Don't show buttons if tab dialog requests this */
482       if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
483         $display.= "<p style=\"text-align:right\">\n";
484         $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
485         $display.= "&nbsp;\n";
486         if ($this->dn != "new"){
487           $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
488           $display.= "&nbsp;\n";
489         }
490         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
491         $display.= "</p>";
492       }
493       return ($display);
494     }
497     /****************
498       Dialog display
499      ****************/
501     /* Check if there is a snapshot dialog open */
502     if($this->IsReleaseManagementActivated()){
503       $base = $this->mime_release;
504     }else{
505       $base = $this->mime_base;
506     }
507     if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
508       return($str);
509     }
511     /* Display dialog with system list */
512     $this->DivListMimeTypes->parent = $this;
513     $this->DivListMimeTypes->execute();
514     if(!$this->IsReleaseManagementActivated()){
515       $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
516     }
517     $this->reload();
518     $this->DivListMimeTypes->setEntries($this->mimetypes);
519     return($this->DivListMimeTypes->Draw());
520   }
522     
523   /* Return departments, that will be included within snapshot detection */
524   function get_used_snapshot_bases()
525   {
526     if($this->IsReleaseManagementActivated()){
527       return(array($this->mime_release));
528     }else{
529       return(array($this->mime_base));
530     }
531   }
534   function reload()
535   {
536     $this->mimetypes= array();
538     /* Set base for all searches */
539     $base       = $this->mime_base;
540     $Regex      = $this->DivListMimeTypes->Regex;
541     $SubSearch  = $this->DivListMimeTypes->SubSearch; 
542     $Flags      =  GL_NONE | GL_SIZELIMIT;
543     $Filter     = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
544     $tmp        = array();
546     if(!$this->IsReleaseManagementActivated()){
547       $use_base = $this->mime_base;
548       if($SubSearch){
549         $use_base = preg_replace("/^".normalizePreg(get_ou("mimeou"))."/","",$use_base);
550       }
551     }else{
552       $use_base = $this->mime_release;
553       $SubSearch= FALSE;
554     }
556     if($SubSearch){
557       $res= get_sub_list($Filter, "mimetypes",get_ou("mimeou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
558     }else{
559       $res= get_list($Filter, "mimetypes",$use_base, array("cn","description","dn","objectClass"), $Flags);
560     }
563     $tmp2 = array();
564     foreach ($res as $val){
565       $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
566       $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
567     }
569     /* sort entries */
570     natcasesort($tmp2);
571     $this->mimetypes=array();
572     foreach($tmp2 as $val){
573       $this->mimetypes[]=$tmp[$val];
574     }
575     reset ($this->mimetypes);
576   }
579   function remove_from_parent()
580   {
581     /* Optionally execute a command after we're done */
582     $this->postremove();
583   }
586   function copyPasteHandling_from_queue($s_action,$s_entry)
587   {
588     /* Check if Copy & Paste is disabled */
589     if(!is_object($this->CopyPasteHandler)){
590       return("");
591     }
593     /* Add a single entry to queue */
594     if($s_action == "cut" || $s_action == "copy"){
596       /* Cleanup object queue */
597       $this->CopyPasteHandler->cleanup_queue();
598       $dn = $this->mimetypes[$s_entry]['dn'];
599       $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
600     }
603     /* Add entries to queue */
604     if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
606       /* Cleanup object queue */
607       $this->CopyPasteHandler->cleanup_queue();
608   
609       /* Add new entries to CP queue */
610       foreach($this->list_get_selected_items() as $id){
611         $dn = $this->mimetypes[$id]['dn'];
613         if($s_action == "copy_multiple"){
614           $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
615         }
616         if($s_action == "cut_multiple"){
617           $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
618         }
619       }
620     }
622     /* Start pasting entries */
623     if($s_action == "editPaste"){
624       $this->start_pasting_copied_objects = TRUE;
625     }
627     /* Return C&P dialog */
628     if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
630       /* Get dialog */
631       $this->CopyPasteHandler->SetVar("base",preg_replace("/^".normalizePreg(get_ou("mimetypeou"))."/","",$this->mime_base));
632       $this->CopyPasteHandler->SetVar("parent",$this);
633       $data = $this->CopyPasteHandler->execute();
635       /* Return dialog data */
636       if(!empty($data)){
637         return($data);
638       }
639     }
641     /* Automatically disable status for pasting */
642     if(!$this->CopyPasteHandler->entries_queued()){
643       $this->start_pasting_copied_objects = FALSE;
644     }
645     return("");
646   }
649   function list_get_selected_items()
650   {
651     $ids = array();
652     foreach($_POST as $name => $value){
653       if(preg_match("/^item_selected_[0-9]*$/",$name)){
654         $id   = preg_replace("/^item_selected_/","",$name);
655         $ids[$id] = $id;
656       }
657     }
658     return($ids);
659   }
662   /* Save to LDAP */
663   function save()
664   {
665     /* Optionally execute a command after we're done */
666     $this->postcreate();
667   }
669   function remove_lock()
670   {
671     if (isset($this->mimetabs->dn)){
672       del_lock ($this->mimetabs->dn);
673     }
674     if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
675       del_lock($this->dn);
676     }
677     if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
678       del_lock($this->dns);
679     }
680   }
682   function save_object() 
683   {
684     $this->DivListMimeTypes->save_object();
685     if(is_object($this->CopyPasteHandler)){
686       $this->CopyPasteHandler->save_object();
687     }
689     if($this->IsReleaseManagementActivated() && isset($_POST['mime_release'])){
690       $sel_rel = get_post('mime_release');
691       $releases = array_flip($this->getReleases());
692       if(isset($releases[$sel_rel])){
693         $this->mime_release = $releases[$sel_rel];
694       }
695       $mime_filter     = session::get("mime_filter");
696       $mime_filter['mime_release'] = $this->mime_release;
697       session::set("mime_filter",$mime_filter);
698     }elseif(!$this->IsReleaseManagementActivated()){
699       $this->mime_base = get_ou("mimetypeou").$this->DivListMimeTypes->selectedBase;
700       $mime_filter     = session::get("mime_filter");
701       $mime_filter['mime_base'] = $this->mime_base;
702       session::set("mime_filter",$mime_filter);
703     }
704   }
707   function check() {}
708   function adapt_from_template($dn, $skip= array()) {}
709   function password_change_needed() {}
711 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
712 ?>