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 = "";
39 var $acl_module = array("mimetypes");
41 function IsReleaseManagementActivated()
42 {
43 /* Check if we should enable the release selection */
44 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
45 if(!empty($tmp)){
46 return(true);
47 }
48 return(false);
49 }
52 function mimetypeManagement (&$config, &$ui)
53 {
54 /* Save configuration for internal use */
55 $this->config = &$config;
56 $this->ui = &$ui;
58 /* Check if copy & paste is activated */
59 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
60 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
61 }
63 /* Creat dialog object */
64 $this->DivListMimeTypes = new divListMimeTypes($this->config,$this);
66 if($this->IsReleaseManagementActivated()){
68 /* Check if we should enable the release selection */
69 $this->enableReleaseManagement = true;
71 /* Hide SubSearch checkbox */
72 $this->DivListMimeTypes->DisableCheckBox("SubSearch");
73 }
75 /* Set default release */
76 if(!$this->IsReleaseManagementActivated()){
77 $this->mime_base = get_ou("mimetypeou").$this->config->current['BASE'];
78 if(!session::is_set("mime_filter")){
79 session::set("mime_filter",array("mime_base" => $this->mime_base));
80 }
81 $mime_filter = session::get("mime_filter");
82 $this->mime_base = $mime_filter['mime_base'];
83 }else{
84 $this->mime_base = get_ou("mimetypeou").$this->config->current['BASE'];
85 if(!session::is_set("mime_filter")){
86 session::set("mime_filter",array("mime_base" => $this->mime_base,"mime_release" => $this->mime_base));
87 }
88 $mime_filter = session::get("mime_filter");
89 $this->mime_base = $mime_filter['mime_base'];
90 $this->mime_release = $mime_filter['mime_release'];
91 }
92 }
95 /* Get all releases */
96 function getReleases()
97 {
98 $ldap = $this->config->get_ldap_link();
99 $ret = array();
100 $ret [$this->mime_base] = "/";
102 $ldap->cd($this->mime_base);
103 $ldap->search("(&(objectClass=FAIbranch)(objectClass=organizationalUnit))",array("ou"));
105 while($attrs = $ldap->fetch()){
106 $str = str_replace($this->mime_base,"",$attrs['dn']);
107 $tmp = array_reverse( split("ou=",$str));
108 $str = "";
109 foreach($tmp as $val){
110 $val = trim(preg_replace("/,/","",$val));
111 if(empty($val)) break;
112 $str .= "/".$val;
113 }
114 if(!empty($str)){
115 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
116 }
117 }
118 asort($ret);
119 return($ret);
120 }
123 function execute()
124 {
125 /* Call parent execute */
126 plugin::execute();
129 /****************
130 Variable init
131 ****************/
133 /* These vars will be stored if you try to open a locked mime,
134 to be able to perform your last requests after showing a warning message */
135 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/",
136 "/^item_selected/","/^remove_multiple_mimetypes/","/^menu_action/"));
138 $smarty = get_smarty(); // Smarty instance
139 $s_action = ""; // Contains the action to proceed
140 $s_entry = ""; // The value for s_action
141 $base_back = ""; // The Link for Backbutton
143 /* Test Posts */
144 foreach($_POST as $key => $val){
145 // Post for delete
146 if(preg_match("/mime_del.*/",$key)){
147 $s_action = "del";
148 $s_entry = preg_replace("/mime_".$s_action."_/i","",$key);
149 // Post for edit
150 }elseif(preg_match("/mime_edit_.*/",$key)){
151 $s_action="edit";
152 $s_entry = preg_replace("/mime_".$s_action."_/i","",$key);
153 // Post for new
154 }elseif(preg_match("/^copy_.*/",$key)){
155 $s_action="copy";
156 $s_entry = preg_replace("/^copy_/i","",$key);
157 }elseif(preg_match("/^cut_.*/",$key)){
158 $s_action="cut";
159 $s_entry = preg_replace("/^cut_/i","",$key);
160 // Post for new
161 }elseif(preg_match("/^mime_new.*/",$key)){
162 $s_action="new";
163 }elseif(preg_match("/^remove_multiple_mimetypes/",$key)){
164 $s_action="del_multiple";
165 }elseif(preg_match("/^editPaste.*/i",$key)){
166 $s_action="editPaste";
167 }elseif(preg_match("/^multiple_copy_mimetypes/",$key)){
168 $s_action = "copy_multiple";
169 }elseif(preg_match("/^multiple_cut_mimetypes/",$key)){
170 $s_action = "cut_multiple";
171 }
172 }
174 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
175 $s_action ="edit";
176 $s_entry = $_GET['id'];
177 }
179 $s_entry = preg_replace("/_.$/","",$s_entry);
181 /* handle C&P from layers menu */
182 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
183 $s_action = "copy_multiple";
184 }
185 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
186 $s_action = "cut_multiple";
187 }
188 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
189 $s_action = "editPaste";
190 }
192 /* Create options */
193 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "mime_new"){
194 $s_action = "new";
195 }
197 /* handle remove from layers menu */
198 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
199 $s_action = "del_multiple";
200 }
203 /****************
204 Copy & Paste handling
205 ****************/
207 /* Display the copy & paste dialog, if it is currently open */
208 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
209 if($ret){
210 return($ret);
211 }
214 /****************
215 Create a new mime type
216 ****************/
218 /* New mime type? */
219 $ui = get_userinfo();
220 $acl = $ui->get_permissions($this->mime_base,"mimetypes/mimetype");
221 if (($s_action=="new") && preg_match("/c/",$acl)){
223 /* By default we set 'dn' to 'new', all relevant plugins will
224 react on this. */
225 $this->dn= "new";
227 /* Create new usertab object */
228 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
229 $this->mimetabs->parent = &$this;
230 $this->mimetabs->set_acl_base($this->mime_base);
231 }
234 /****************
235 Edit entry canceled
236 ****************/
238 /* Cancel dialogs */
239 if (isset($_POST['edit_cancel'])){
240 $this->remove_lock();
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 $this->remove_lock();
268 }
269 unset ($this->mimetabs);
270 $this->mimetabs= NULL;
271 session::un_set('objectinfo');
272 }else{
274 /* Reinitialize tab */
275 if($this->mimetabs instanceof tabs){
276 $this->mimetabs->re_init();
277 }
278 }
279 } else {
280 /* Ok. There seem to be errors regarding to the tab data,
281 show message and continue as usual. */
282 msg_dialog::displayChecks($message);
283 }
284 }
287 /****************
288 Edit entry
289 ****************/
291 /* User wants to edit data? */
292 if (($s_action=="edit") && (!isset($this->mimetabs->config))){
294 /* Get 'dn' from posted 'mimelist', must be unique */
295 $this->dn= $this->mimetypes[$s_entry]['dn'];
297 /* Check locking, save current plugin in 'back_plugin', so
298 the dialog knows where to return. */
299 if (($user= get_lock($this->dn)) != ""){
300 return(gen_locked_message ($user, $this->dn));
301 }
303 /* Lock the current entry, so everyone will get the
304 above dialog */
305 add_lock ($this->dn, $this->ui->dn);
307 /* Register mimetabs to trigger edit dialog */
308 $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
309 $this->mimetabs->parent = &$this;
310 $this->mimetabs->set_acl_base($this->dn);
311 session::set('objectinfo',$this->dn);
312 }
315 /********************
316 Delete MULTIPLE entries requested, display confirm dialog
317 ********************/
319 if ($s_action=="del_multiple"){
320 $ids = $this->list_get_selected_items();
322 $this->dns = array();
323 if(count($ids)){
325 $disallowed = array();
326 foreach($ids as $id){
327 $dn = $this->mimetypes[$id]['dn'];
328 $acl = $this->ui->get_permissions($dn, "mimetypes/mimetype");
329 if(preg_match("/d/",$acl)){
330 $this->dns[$id] = $dn;
331 }else{
332 $disallowed[] = $dn;
333 }
334 }
336 if(count($disallowed)){
337 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
338 }
340 if(count($this->dns)){
342 /* Check locks */
343 if ($user= get_multiple_locks($this->dns)){
344 return(gen_locked_message($user,$this->dns));
345 }
347 $dns_names = array();
348 foreach($this->dns as $dn){
349 $dns_names[] = @LDAP::fix($dn);
350 }
352 add_lock ($this->dns, $this->ui->dn);
354 /* Lock the current entry, so nobody will edit it during deletion */
355 $smarty->assign("info", msgPool::deleteInfo($dns_names,_("Mime type")));
356 $smarty->assign("multiple", true);
357 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
358 }
359 }
360 }
363 /********************
364 Delete MULTIPLE entries confirmed
365 ********************/
367 /* Confirmation for deletion has been passed. Users should be deleted. */
368 if (isset($_POST['delete_multiple_mimetype_confirm'])){
370 $ui = get_userinfo();
372 /* Remove user by user and check acls before removeing them */
373 foreach($this->dns as $key => $dn){
375 $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
376 if(preg_match("/d/",$acl)){
378 /* Delete request is permitted, perform LDAP action */
379 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
380 $this->mimetabs->parent = &$this;
381 $this->mimetabs->set_acl_base($dn);
382 $this->mimetabs->delete ();
383 unset ($this->mimetabs);
384 $this->mimetabs= NULL;
386 } else {
387 /* Normally this shouldn't be reached, send some extra
388 logs to notify the administrator */
389 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
390 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
391 }
392 }
394 /* Remove lock file after successfull deletion */
395 $this->remove_lock();
396 $this->dns = array();
397 }
400 /********************
401 Delete MULTIPLE entries Canceled
402 ********************/
404 /* Remove lock */
405 if(isset($_POST['delete_multiple_mimetype_cancel'])){
406 $this->remove_lock();
407 $this->dns = array();
408 }
411 /****************
412 Delete mime type
413 ****************/
415 /* Remove user was requested */
416 if ($s_action == "del"){
418 /* Get 'dn' from posted 'uid' */
419 $this->dn= $this->mimetypes[$s_entry]['dn'];
421 /* Load permissions for selected 'dn' and check if
422 we're allowed to remove this 'dn' */
423 $ui = get_userinfo();
424 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
425 if (preg_match("/d/",$acl)){
427 /* Check locking, save current plugin in 'back_plugin', so
428 the dialog knows where to return. */
429 if (($user= get_lock($this->dn)) != ""){
430 return (gen_locked_message ($user, $this->dn));
431 }
433 /* Lock the current entry, so nobody will edit it during deletion */
434 add_lock ($this->dn, $this->ui->dn);
435 $smarty= get_smarty();
436 $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("Mime type")));
437 $smarty->assign("multiple", false);
438 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
439 } else {
441 /* Obviously the user isn't allowed to delete. Show message and
442 clean session. */
443 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
444 }
445 }
448 /****************
449 Delete mime confirmed
450 ****************/
452 /* Confirmation for deletion has been passed. Group should be deleted. */
453 if (isset($_POST['delete_mime_confirm'])){
455 /* Some nice guy may send this as POST, so we've to check
456 for the permissions again. */
457 $ui = get_userinfo();
458 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
459 if(preg_match("/d/",$acl)){
461 /* Delete request is permitted, perform LDAP action */
462 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
463 $this->mimetabs->parent = &$this;
464 $this->mimetabs->set_acl_base($this->dn);
465 $this->mimetabs->delete ();
466 unset ($this->mimetabs);
467 $this->mimetabs= NULL;
469 } else {
471 /* Normally this shouldn't be reached, send some extra
472 logs to notify the administrator */
473 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
474 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
475 }
477 /* Remove lock file after successfull deletion */
478 $this->remove_lock();
479 }
482 /****************
483 Delete mime canceled
484 ****************/
486 /* Delete mime type canceled? */
487 if (isset($_POST['delete_cancel'])){
488 $this->remove_lock();
489 session::un_set('objectinfo');
490 }
492 /* Show tab dialog if object is present */
493 if (($this->mimetabs) && (isset($this->mimetabs->config))){
494 $display= $this->mimetabs->execute();
496 /* Don't show buttons if tab dialog requests this */
497 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
498 $display.= "<p style=\"text-align:right\">\n";
499 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
500 $display.= " \n";
501 if ($this->dn != "new"){
502 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
503 $display.= " \n";
504 }
505 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
506 $display.= "</p>";
507 }
508 return ($display);
509 }
512 /****************
513 Dialog display
514 ****************/
516 /* Check if there is a snapshot dialog open */
517 if($this->IsReleaseManagementActivated()){
518 $base = $this->mime_release;
519 }else{
520 $base = $this->mime_base;
521 }
522 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
523 return($str);
524 }
526 /* Display dialog with system list */
527 $this->DivListMimeTypes->parent = $this;
528 $this->DivListMimeTypes->execute();
529 if(!$this->IsReleaseManagementActivated()){
530 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
531 }
532 $this->reload();
533 $this->DivListMimeTypes->setEntries($this->mimetypes);
534 return($this->DivListMimeTypes->Draw());
535 }
538 /* Return departments, that will be included within snapshot detection */
539 function get_used_snapshot_bases()
540 {
541 if($this->IsReleaseManagementActivated()){
542 return(array($this->mime_release));
543 }else{
544 return(array($this->mime_base));
545 }
546 }
549 function reload()
550 {
551 $this->mimetypes= array();
553 /* Set base for all searches */
554 $base = $this->mime_base;
555 $Regex = $this->DivListMimeTypes->Regex;
556 $SubSearch = $this->DivListMimeTypes->SubSearch;
557 $Flags = GL_NONE | GL_SIZELIMIT;
558 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
559 $tmp = array();
561 if(!$this->IsReleaseManagementActivated()){
562 $use_base = $this->mime_base;
563 if($SubSearch){
564 $use_base = preg_replace("/^".normalizePreg(get_ou("mimeou"))."/","",$use_base);
565 }
566 }else{
567 $use_base = $this->mime_release;
568 $SubSearch= FALSE;
569 }
571 if($SubSearch){
572 $res= get_sub_list($Filter, "mimetypes",get_ou("mimeou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
573 }else{
574 $res= get_list($Filter, "mimetypes",$use_base, array("cn","description","dn","objectClass"), $Flags);
575 }
578 $tmp2 = array();
579 foreach ($res as $val){
580 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
581 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
582 }
584 /* sort entries */
585 natcasesort($tmp2);
586 $this->mimetypes=array();
587 foreach($tmp2 as $val){
588 $this->mimetypes[]=$tmp[$val];
589 }
590 reset ($this->mimetypes);
591 }
594 function remove_from_parent()
595 {
596 /* Optionally execute a command after we're done */
597 $this->postremove();
598 }
601 function copyPasteHandling_from_queue($s_action,$s_entry)
602 {
603 /* Check if Copy & Paste is disabled */
604 if(!is_object($this->CopyPasteHandler)){
605 return("");
606 }
608 $ui = get_userinfo();
610 /* Add a single entry to queue */
611 if($s_action == "cut" || $s_action == "copy"){
613 /* Cleanup object queue */
614 $this->CopyPasteHandler->cleanup_queue();
615 $dn = $this->mimetypes[$s_entry]['dn'];
616 if($s_action == "copy" && $ui->is_copyable($dn,"mimetypes","mimetype")){
617 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
618 }
619 if($s_action == "cut" && $ui->is_cutable($dn,"mimetypes","mimetype")){
620 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
621 }
622 }
624 /* Add entries to queue */
625 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
627 /* Cleanup object queue */
628 $this->CopyPasteHandler->cleanup_queue();
630 /* Add new entries to CP queue */
631 foreach($this->list_get_selected_items() as $id){
632 $dn = $this->mimetypes[$id]['dn'];
634 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"mimetypes","mimetype")){
635 $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
636 }
637 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"mimetypes","mimetype")){
638 $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
639 }
640 }
641 }
643 /* Start pasting entries */
644 if($s_action == "editPaste"){
645 $this->start_pasting_copied_objects = TRUE;
646 }
648 /* Return C&P dialog */
649 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
651 /* Get dialog */
652 $this->CopyPasteHandler->SetVar("base",preg_replace("/^".normalizePreg(get_ou("mimetypeou"))."/","",$this->mime_base));
653 $this->CopyPasteHandler->SetVar("parent",$this);
654 $data = $this->CopyPasteHandler->execute();
656 /* Return dialog data */
657 if(!empty($data)){
658 return($data);
659 }
660 }
662 /* Automatically disable status for pasting */
663 if(!$this->CopyPasteHandler->entries_queued()){
664 $this->start_pasting_copied_objects = FALSE;
665 }
666 return("");
667 }
670 function list_get_selected_items()
671 {
672 $ids = array();
673 foreach($_POST as $name => $value){
674 if(preg_match("/^item_selected_[0-9]*$/",$name)){
675 $id = preg_replace("/^item_selected_/","",$name);
676 $ids[$id] = $id;
677 }
678 }
679 return($ids);
680 }
683 /* Save to LDAP */
684 function save()
685 {
686 /* Optionally execute a command after we're done */
687 $this->postcreate();
688 }
690 function remove_lock()
691 {
692 if (isset($this->mimetabs->dn)){
693 del_lock ($this->mimetabs->dn);
694 }
695 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
696 del_lock($this->dn);
697 }
698 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
699 del_lock($this->dns);
700 }
701 }
703 function save_object()
704 {
705 $this->DivListMimeTypes->save_object();
706 if(is_object($this->CopyPasteHandler)){
707 $this->CopyPasteHandler->save_object();
708 }
710 if($this->IsReleaseManagementActivated() && isset($_POST['mime_release'])){
711 $sel_rel = get_post('mime_release');
712 $releases = array_flip($this->getReleases());
713 if(isset($releases[$sel_rel])){
714 $this->mime_release = $releases[$sel_rel];
715 }
716 $mime_filter = session::get("mime_filter");
717 $mime_filter['mime_release'] = $this->mime_release;
718 session::set("mime_filter",$mime_filter);
719 }elseif(!$this->IsReleaseManagementActivated()){
720 $this->mime_base = get_ou("mimetypeou").$this->DivListMimeTypes->selectedBase;
721 $mime_filter = session::get("mime_filter");
722 $mime_filter['mime_base'] = $this->mime_base;
723 session::set("mime_filter",$mime_filter);
724 }
725 }
728 function check() {}
729 function adapt_from_template($dn, $skip= array()) {}
730 function password_change_needed() {}
731 }
732 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
733 ?>