c12bbdf8fc52b9609b88d2eca5262b6a25a21c85
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 $enableReleaseManagement = false;
31 var $mimetabs = NULL;
32 var $snapDialog = NULL;
33 var $CopyPasteHandler = NULL;
34 var $start_pasting_copied_objects = 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 }
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_/","/^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
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);
182 /* handle C&P from layers menu */
183 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
184 $s_action = "copy_multiple";
185 }
186 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
187 $s_action = "cut_multiple";
188 }
189 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
190 $s_action = "editPaste";
191 }
193 /* Create options */
194 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "mime_new"){
195 $s_action = "new";
196 }
198 /* handle remove from layers menu */
199 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
200 $s_action = "del_multiple";
201 }
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 del_lock ($this->mimetabs->dn);
241 unset ($this->mimetabs);
242 $this->mimetabs= NULL;
243 session::un_set('objectinfo');
244 }
247 /****************
248 Edit entry finished
249 ****************/
251 /* Finish mime edit is triggered by the tabulator dialog, so
252 the user wants to save edited data. Check and save at this point. */
253 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
255 /* Check tabs, will feed message array */
256 $this->mimetabs->save_object();
257 $message= $this->mimetabs->check();
259 /* Save, or display error message? */
260 if (count($message) == 0){
262 /* Save data data to ldap */
263 $this->mimetabs->set_release($this->mime_base);
264 $this->mimetabs->save();
266 if (!isset($_POST['edit_apply'])){
267 /* Mime type has been saved successfully, remove lock from LDAP. */
268 if ($this->dn != "new"){
269 del_lock ($this->dn);
270 }
271 unset ($this->mimetabs);
272 $this->mimetabs= NULL;
273 session::un_set('objectinfo');
274 }
275 } else {
276 /* Ok. There seem to be errors regarding to the tab data,
277 show message and continue as usual. */
278 msg_dialog::displayChecks($message);
279 }
280 }
283 /****************
284 Edit entry
285 ****************/
287 /* User wants to edit data? */
288 if (($s_action=="edit") && (!isset($this->mimetabs->config))){
290 /* Get 'dn' from posted 'mimelist', must be unique */
291 $this->dn= $this->mimetypes[$s_entry]['dn'];
293 /* Check locking, save current plugin in 'back_plugin', so
294 the dialog knows where to return. */
295 if (($user= get_lock($this->dn)) != ""){
296 return(gen_locked_message ($user, $this->dn));
297 }
299 /* Lock the current entry, so everyone will get the
300 above dialog */
301 add_lock ($this->dn, $this->ui->dn);
304 /* Register mimetabs to trigger edit dialog */
305 $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
306 $this->mimetabs->parent = &$this;
307 $this->mimetabs->set_acl_base($this->dn);
308 session::set('objectinfo',$this->dn);
309 }
312 /********************
313 Delete MULTIPLE entries requested, display confirm dialog
314 ********************/
316 if ($s_action=="del_multiple"){
317 $ids = $this->list_get_selected_items();
319 $this->dns = array();
320 if(count($ids)){
322 foreach($ids as $id){
323 $dn = $this->mimetypes[$id]['dn'];
324 if (($user= get_lock($dn)) != ""){
325 return(gen_locked_message ($user, $dn));
326 }
327 $this->dns[$id] = $dn;
328 }
330 $dns_names = array();
331 foreach($this->dns as $dn){
332 add_lock ($dn, $this->ui->dn);
333 $dns_names[] = @LDAP::fix($dn);
334 }
336 /* Lock the current entry, so nobody will edit it during deletion */
337 $smarty->assign("info", msgPool::deleteInfo($dns_names,_("Mime type")));
338 $smarty->assign("multiple", true);
339 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
340 }
341 }
346 /********************
347 Delete MULTIPLE entries confirmed
348 ********************/
350 /* Confirmation for deletion has been passed. Users should be deleted. */
351 if (isset($_POST['delete_multiple_mimetype_confirm'])){
353 $ui = get_userinfo();
355 /* Remove user by user and check acls before removeing them */
356 foreach($this->dns as $key => $dn){
358 $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
359 if(preg_match("/d/",$acl)){
361 /* Delete request is permitted, perform LDAP action */
362 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
363 $this->mimetabs->parent = &$this;
364 $this->mimetabs->set_acl_base($dn);
365 $this->mimetabs->delete ();
366 unset ($this->mimetabs);
367 $this->mimetabs= NULL;
369 } else {
370 /* Normally this shouldn't be reached, send some extra
371 logs to notify the administrator */
372 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
373 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
374 }
375 /* Remove lock file after successfull deletion */
376 del_lock ($dn);
377 unset($this->dns[$key]);
378 }
379 }
382 /********************
383 Delete MULTIPLE entries Canceled
384 ********************/
386 /* Remove lock */
387 if(isset($_POST['delete_multiple_mimetype_cancel'])){
388 foreach($this->dns as $key => $dn){
389 del_lock ($dn);
390 unset($this->dns[$key]);
391 }
392 }
395 /****************
396 Delete mime type
397 ****************/
399 /* Remove user was requested */
400 if ($s_action == "del"){
402 /* Get 'dn' from posted 'uid' */
403 $this->dn= $this->mimetypes[$s_entry]['dn'];
405 /* Load permissions for selected 'dn' and check if
406 we're allowed to remove this 'dn' */
407 $ui = get_userinfo();
408 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
409 if (preg_match("/d/",$acl)){
411 /* Check locking, save current plugin in 'back_plugin', so
412 the dialog knows where to return. */
413 if (($user= get_lock($this->dn)) != ""){
414 return (gen_locked_message ($user, $this->dn));
415 }
417 /* Lock the current entry, so nobody will edit it during deletion */
418 add_lock ($this->dn, $this->ui->dn);
419 $smarty= get_smarty();
420 $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("Mime type")));
421 $smarty->assign("multiple", false);
422 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
423 } else {
425 /* Obviously the user isn't allowed to delete. Show message and
426 clean session. */
427 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
428 }
429 }
432 /****************
433 Delete mime confirmed
434 ****************/
436 /* Confirmation for deletion has been passed. Group should be deleted. */
437 if (isset($_POST['delete_mime_confirm'])){
439 /* Some nice guy may send this as POST, so we've to check
440 for the permissions again. */
441 $ui = get_userinfo();
442 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
443 if(preg_match("/d/",$acl)){
445 /* Delete request is permitted, perform LDAP action */
446 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
447 $this->mimetabs->parent = &$this;
448 $this->mimetabs->set_acl_base($this->dn);
449 $this->mimetabs->delete ();
450 unset ($this->mimetabs);
451 $this->mimetabs= NULL;
453 } else {
455 /* Normally this shouldn't be reached, send some extra
456 logs to notify the administrator */
457 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
458 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
459 }
461 /* Remove lock file after successfull deletion */
462 del_lock ($this->dn);
463 }
466 /****************
467 Delete mime canceled
468 ****************/
470 /* Delete mime type canceled? */
471 if (isset($_POST['delete_cancel'])){
472 del_lock ($this->dn);
473 session::un_set('objectinfo');
474 }
476 /* Show tab dialog if object is present */
477 if (($this->mimetabs) && (isset($this->mimetabs->config))){
478 $display= $this->mimetabs->execute();
480 /* Don't show buttons if tab dialog requests this */
481 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
482 $display.= "<p style=\"text-align:right\">\n";
483 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
484 $display.= " \n";
485 if ($this->dn != "new"){
486 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
487 $display.= " \n";
488 }
489 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
490 $display.= "</p>";
491 }
492 return ($display);
493 }
496 /****************
497 Dialog display
498 ****************/
500 /* Check if there is a snapshot dialog open */
501 if($this->IsReleaseManagementActivated()){
502 $base = $this->mime_release;
503 }else{
504 $base = $this->mime_base;
505 }
506 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
507 return($str);
508 }
510 /* Display dialog with system list */
511 $this->DivListMimeTypes->parent = $this;
512 $this->DivListMimeTypes->execute();
513 if(!$this->IsReleaseManagementActivated()){
514 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
515 }
516 $this->reload();
517 $this->DivListMimeTypes->setEntries($this->mimetypes);
518 return($this->DivListMimeTypes->Draw());
519 }
522 /* Return departments, that will be included within snapshot detection */
523 function get_used_snapshot_bases()
524 {
525 if($this->IsReleaseManagementActivated()){
526 return(array($this->mime_release));
527 }else{
528 return(array($this->mime_base));
529 }
530 }
533 function reload()
534 {
535 $this->mimetypes= array();
537 /* Set base for all searches */
538 $base = $this->mime_base;
539 $Regex = $this->DivListMimeTypes->Regex;
540 $SubSearch = $this->DivListMimeTypes->SubSearch;
541 $Flags = GL_NONE | GL_SIZELIMIT;
542 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
543 $tmp = array();
544 $Releases = $this->getReleases();
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 }
578 function remove_from_parent()
579 {
580 /* Optionally execute a command after we're done */
581 $this->postremove();
582 }
585 function copyPasteHandling_from_queue($s_action,$s_entry)
586 {
587 /* Check if Copy & Paste is disabled */
588 if(!is_object($this->CopyPasteHandler)){
589 return("");
590 }
592 /* Add a single entry to queue */
593 if($s_action == "cut" || $s_action == "copy"){
595 /* Cleanup object queue */
596 $this->CopyPasteHandler->cleanup_queue();
597 $this->start_pasting_copied_objects = FALSE;
598 $dn = $this->mimetypes[$s_entry]['dn'];
599 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
600 }
602 /* Add entries to queue */
603 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
605 /* Cleanup object queue */
606 $this->CopyPasteHandler->cleanup_queue();
607 $this->start_pasting_copied_objects = FALSE;
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 $data = $this->CopyPasteHandler->execute();
632 $this->CopyPasteHandler->SetVar("base",$this->DivListMimeTypes->selectedBase);
634 /* Return dialog data */
635 if(!empty($data)){
636 return($data);
637 }
638 }
640 /* Automatically disable status for pasting */
641 #if(!$this->CopyPasteHandler->entries_queued()){
642 # $this->start_pasting_copied_objects = FALSE;
643 #}
644 return("");
645 }
648 function list_get_selected_items()
649 {
650 $ids = array();
651 foreach($_POST as $name => $value){
652 if(preg_match("/^item_selected_[0-9]*$/",$name)){
653 $id = preg_replace("/^item_selected_/","",$name);
654 $ids[$id] = $id;
655 }
656 }
657 return($ids);
658 }
661 /* Save to LDAP */
662 function save()
663 {
664 /* Optionally execute a command after we're done */
665 $this->postcreate();
666 }
668 function remove_lock()
669 {
670 if (isset($this->mimetabs->dn)){
671 del_lock ($this->mimetabs->dn);
672 }
673 }
675 function save_object()
676 {
677 $this->DivListMimeTypes->save_object();
678 if(is_object($this->CopyPasteHandler)){
679 $this->CopyPasteHandler->save_object();
680 }
682 if($this->IsReleaseManagementActivated() && isset($_POST['mime_release'])){
683 $sel_rel = get_post('mime_release');
684 $releases = array_flip($this->getReleases());
685 if(isset($releases[$sel_rel])){
686 $this->mime_release = $releases[$sel_rel];
687 }
688 $mime_filter = session::get("mime_filter");
689 $mime_filter['mime_release'] = $this->mime_release;
690 session::set("mime_filter",$mime_filter);
691 }elseif(!$this->IsReleaseManagementActivated()){
692 $this->mime_base = get_ou("mimetypeou").$this->DivListMimeTypes->selectedBase;
693 $mime_filter = session::get("mime_filter");
694 $mime_filter['mime_base'] = $this->mime_base;
695 session::set("mime_filter",$mime_filter);
696 }
697 }
700 function check() {}
701 function adapt_from_template($dn, $skip= array()) {}
702 function password_change_needed() {}
703 }
704 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
705 ?>