34b2268112f10f7fd7b4f7fc1262dfde41b52a28
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;
37 function mimetypeManagement (&$config, &$ui)
38 {
39 /* Save configuration for internal use */
40 $this->config = &$config;
41 $this->ui = &$ui;
43 /* Check if copy & paste is activated */
44 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
45 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
46 }
48 /* Creat dialog object */
49 $this->DivListMimeTypes = new divListMimeTypes($this->config,$this);
51 if($this->IsReleaseManagementActivated()){
53 /* Check if we should enable the release selection */
54 $this->enableReleaseManagement = true;
56 /* Hide SubSearch checkbox */
57 $this->DivListMimeTypes->DisableCheckBox("SubSearch");
58 }
60 }
63 /* Get all releases */
64 function getReleases($base)
65 {
66 $ldap = $this->config->get_ldap_link();
67 $dn = get_ou('mimetypeou').$base;
68 $ret = array();
69 $ret [get_ou('mimetypeou').$base] = "/";
71 $ldap->cd($dn);
72 $ldap->search("objectClass=organizationalUnit",array("ou"));
74 while($attrs = $ldap->fetch()){
75 $str = str_replace($dn,"",$attrs['dn']);
76 $tmp = array_reverse( split("ou=",$str));
77 $str = "";
78 foreach($tmp as $val){
79 $val = trim(preg_replace("/,/","",$val));
80 if(empty($val)) break;
81 $str .= "/".$val;
82 }
83 if(!empty($str)){
84 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
85 }
86 }
87 asort($ret);
88 return($ret);
89 }
91 function execute()
92 {
93 /* Call parent execute */
94 plugin::execute();
97 /****************
98 Variable init
99 ****************/
101 /* These vars will be stored if you try to open a locked mime,
102 to be able to perform your last requests after showing a warning message */
103 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^mime_edit_/","/^mime_del_/","/^item_selected/","/^remove_multiple_mimetypes/"));
105 $smarty = get_smarty(); // Smarty instance
106 $s_action = ""; // Contains the action to proceed
107 $s_entry = ""; // The value for s_action
108 $base_back = ""; // The Link for Backbutton
110 /* Test Posts */
111 foreach($_POST as $key => $val){
112 // Post for delete
113 if(preg_match("/mime_del.*/",$key)){
114 $s_action = "del";
115 $s_entry = preg_replace("/mime_".$s_action."_/i","",$key);
116 // Post for edit
117 }elseif(preg_match("/mime_edit_.*/",$key)){
118 $s_action="edit";
119 $s_entry = preg_replace("/mime_".$s_action."_/i","",$key);
120 // Post for new
121 }elseif(preg_match("/^copy_.*/",$key)){
122 $s_action="copy";
123 $s_entry = preg_replace("/^copy_/i","",$key);
124 }elseif(preg_match("/^cut_.*/",$key)){
125 $s_action="cut";
126 $s_entry = preg_replace("/^cut_/i","",$key);
127 // Post for new
128 }elseif(preg_match("/^mime_new.*/",$key)){
129 $s_action="new";
130 }elseif(preg_match("/^remove_multiple_mimetypes/",$key)){
131 $s_action="del_multiple";
132 }elseif(preg_match("/^editPaste.*/i",$key)){
133 $s_action="editPaste";
134 }elseif(preg_match("/^multiple_copy_mimetypes/",$key)){
135 $s_action = "copy_multiple";
136 }elseif(preg_match("/^multiple_cut_mimetypes/",$key)){
137 $s_action = "cut_multiple";
138 }
139 }
141 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
142 $s_action ="edit";
143 $s_entry = $_GET['id'];
144 }
146 $s_entry = preg_replace("/_.$/","",$s_entry);
150 /* handle C&P from layers menu */
151 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
152 $s_action = "copy_multiple";
153 }
154 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
155 $s_action = "cut_multiple";
156 }
157 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
158 $s_action = "editPaste";
159 }
161 /* Create options */
162 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "mime_new"){
163 $s_action = "new";
164 }
166 /* handle remove from layers menu */
167 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
168 $s_action = "del_multiple";
169 }
171 /****************
172 Copy & Paste handling
173 ****************/
175 /* Display the copy & paste dialog, if it is currently open */
176 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
177 if($ret){
178 return($ret);
179 }
182 /****************
183 Create a new mime type
184 ****************/
186 /* New mime type? */
187 $ui = get_userinfo();
188 $acl = $ui->get_permissions($this->DivListMimeTypes->selectedBase,"mimetypes/mimetype");
189 if (($s_action=="new") && preg_match("/c/",$acl)){
191 /* By default we set 'dn' to 'new', all relevant plugins will
192 react on this. */
193 $this->dn= "new";
195 /* Create new usertab object */
196 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
197 $this->mimetabs->set_acl_base($this->DivListMimeTypes->selectedBase);
198 }
201 /****************
202 Edit entry canceled
203 ****************/
205 /* Cancel dialogs */
206 if (isset($_POST['edit_cancel'])){
207 del_lock ($this->mimetabs->dn);
208 unset ($this->mimetabs);
209 $this->mimetabs= NULL;
210 session::un_set('objectinfo');
211 }
214 /****************
215 Edit entry finished
216 ****************/
218 /* Finish mime edit is triggered by the tabulator dialog, so
219 the user wants to save edited data. Check and save at this point. */
220 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
222 /* Check tabs, will feed message array */
223 $this->mimetabs->save_object();
224 $message= $this->mimetabs->check();
226 /* Save, or display error message? */
227 if (count($message) == 0){
229 /* Save data data to ldap */
230 $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
231 $this->mimetabs->save();
233 if (!isset($_POST['edit_apply'])){
234 /* Mime type has been saved successfully, remove lock from LDAP. */
235 if ($this->dn != "new"){
236 del_lock ($this->dn);
237 }
238 unset ($this->mimetabs);
239 $this->mimetabs= NULL;
240 session::un_set('objectinfo');
241 }
242 } else {
243 /* Ok. There seem to be errors regarding to the tab data,
244 show message and continue as usual. */
245 msg_dialog::displayChecks($message);
246 }
247 }
250 /****************
251 Edit entry
252 ****************/
254 /* User wants to edit data? */
255 if (($s_action=="edit") && (!isset($this->mimetabs->config))){
257 /* Get 'dn' from posted 'mimelist', must be unique */
258 $this->dn= $this->mimetypes[$s_entry]['dn'];
260 /* Check locking, save current plugin in 'back_plugin', so
261 the dialog knows where to return. */
262 if (($user= get_lock($this->dn)) != ""){
263 return(gen_locked_message ($user, $this->dn));
264 }
266 /* Lock the current entry, so everyone will get the
267 above dialog */
268 add_lock ($this->dn, $this->ui->dn);
271 /* Register mimetabs to trigger edit dialog */
272 $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
273 $this->mimetabs->set_acl_base($this->dn);
274 session::set('objectinfo',$this->dn);
275 }
278 /********************
279 Delete MULTIPLE entries requested, display confirm dialog
280 ********************/
282 if ($s_action=="del_multiple"){
283 $ids = $this->list_get_selected_items();
285 $this->dns = array();
286 if(count($ids)){
288 foreach($ids as $id){
289 $dn = $this->mimetypes[$id]['dn'];
290 if (($user= get_lock($dn)) != ""){
291 return(gen_locked_message ($user, $dn));
292 }
293 $this->dns[$id] = $dn;
294 }
296 $dns_names = array();
297 foreach($this->dns as $dn){
298 add_lock ($dn, $this->ui->dn);
299 $dns_names[] = @LDAP::fix($dn);
300 }
302 /* Lock the current entry, so nobody will edit it during deletion */
303 $smarty->assign("info", msgPool::deleteInfo($dns_names,_("Mime type")));
304 $smarty->assign("multiple", true);
305 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
306 }
307 }
312 /********************
313 Delete MULTIPLE entries confirmed
314 ********************/
316 /* Confirmation for deletion has been passed. Users should be deleted. */
317 if (isset($_POST['delete_multiple_mimetype_confirm'])){
319 $ui = get_userinfo();
321 /* Remove user by user and check acls before removeing them */
322 foreach($this->dns as $key => $dn){
324 $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
325 if(preg_match("/d/",$acl)){
327 /* Delete request is permitted, perform LDAP action */
328 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
329 $this->mimetabs->set_acl_base($dn);
330 $this->mimetabs->delete ();
331 unset ($this->mimetabs);
332 $this->mimetabs= NULL;
334 } else {
335 /* Normally this shouldn't be reached, send some extra
336 logs to notify the administrator */
337 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
338 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
339 }
340 /* Remove lock file after successfull deletion */
341 del_lock ($dn);
342 unset($this->dns[$key]);
343 }
344 }
347 /********************
348 Delete MULTIPLE entries Canceled
349 ********************/
351 /* Remove lock */
352 if(isset($_POST['delete_multiple_mimetype_cancel'])){
353 foreach($this->dns as $key => $dn){
354 del_lock ($dn);
355 unset($this->dns[$key]);
356 }
357 }
360 /****************
361 Delete mime type
362 ****************/
364 /* Remove user was requested */
365 if ($s_action == "del"){
367 /* Get 'dn' from posted 'uid' */
368 $this->dn= $this->mimetypes[$s_entry]['dn'];
370 /* Load permissions for selected 'dn' and check if
371 we're allowed to remove this 'dn' */
372 $ui = get_userinfo();
373 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
374 if (preg_match("/d/",$acl)){
376 /* Check locking, save current plugin in 'back_plugin', so
377 the dialog knows where to return. */
378 if (($user= get_lock($this->dn)) != ""){
379 return (gen_locked_message ($user, $this->dn));
380 }
382 /* Lock the current entry, so nobody will edit it during deletion */
383 add_lock ($this->dn, $this->ui->dn);
384 $smarty= get_smarty();
385 $smarty->assign("info", msgPool::deleteInfo(@LDAP::fix($this->dn),_("Mime type")));
386 $smarty->assign("multiple", false);
387 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
388 } else {
390 /* Obviously the user isn't allowed to delete. Show message and
391 clean session. */
392 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
393 }
394 }
397 /****************
398 Delete mime confirmed
399 ****************/
401 /* Confirmation for deletion has been passed. Group should be deleted. */
402 if (isset($_POST['delete_mime_confirm'])){
404 /* Some nice guy may send this as POST, so we've to check
405 for the permissions again. */
406 $ui = get_userinfo();
407 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
408 if(preg_match("/d/",$acl)){
410 /* Delete request is permitted, perform LDAP action */
411 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
412 $this->mimetabs->set_acl_base($this->dn);
413 $this->mimetabs->delete ();
414 unset ($this->mimetabs);
415 $this->mimetabs= NULL;
417 } else {
419 /* Normally this shouldn't be reached, send some extra
420 logs to notify the administrator */
421 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
422 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
423 }
425 /* Remove lock file after successfull deletion */
426 del_lock ($this->dn);
427 }
430 /****************
431 Delete mime canceled
432 ****************/
434 /* Delete mime type canceled? */
435 if (isset($_POST['delete_cancel'])){
436 del_lock ($this->dn);
437 session::un_set('objectinfo');
438 }
440 /* Show tab dialog if object is present */
441 if (($this->mimetabs) && (isset($this->mimetabs->config))){
442 $display= $this->mimetabs->execute();
444 /* Don't show buttons if tab dialog requests this */
445 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
446 $display.= "<p style=\"text-align:right\">\n";
447 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
448 $display.= " \n";
449 if ($this->dn != "new"){
450 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
451 $display.= " \n";
452 }
453 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
454 $display.= "</p>";
455 }
456 return ($display);
457 }
460 /****************
461 Dialog display
462 ****************/
464 /* Check if there is a snapshot dialog open */
465 $base = $this->DivListMimeTypes->selectedBase;
466 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
467 return($str);
468 }
470 /* Display dialog with system list */
471 $this->DivListMimeTypes->parent = $this;
472 $this->DivListMimeTypes->execute();
473 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
474 $this->reload();
475 $this->DivListMimeTypes->setEntries($this->mimetypes);
476 return($this->DivListMimeTypes->Draw());
477 }
480 /* Return departments, that will be included within snapshot detection */
481 function get_used_snapshot_bases()
482 {
483 return(array($this->DivListMimeTypes->selectedRelease));
484 }
488 function reload()
489 {
490 $this->mimetypes= array();
492 /* Set base for all searches */
493 $base = $this->DivListMimeTypes->selectedBase;
494 $release = $this->DivListMimeTypes->selectedRelease;
495 $Regex = $this->DivListMimeTypes->Regex;
496 $SubSearch = $this->DivListMimeTypes->SubSearch;
497 $Flags = GL_NONE | GL_SIZELIMIT;
498 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
499 $tmp = array();
500 $Releases = $this->getReleases($base);
503 /* If release management is enabled, use release as base. */
504 if(!$this->enableReleaseManagement){
505 $use_base = $base;
506 }else{
507 if(isset($Releases[$release])){
508 $use_base = $release;
509 }else{
510 $use_base = $base;
511 }
512 }
514 /* In case of subsearch, add the subsearch flag */
515 if($SubSearch){
516 $Flags |= GL_SUBSEARCH;
517 }else{
518 if(!$this->enableReleaseManagement){
519 $use_base = get_ou('mimetypeou').$use_base;
520 }
521 }
523 /* Get results and create index */
524 $res= get_sub_list($Filter, "mimetypes",get_ou('mimetypeou'), $use_base, array("cn","description","dn","objectClass"), $Flags);
525 $tmp2 = array();
526 foreach ($res as $val){
527 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
528 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
529 }
531 /* sort entries */
532 natcasesort($tmp2);
533 $this->mimetypes=array();
534 foreach($tmp2 as $val){
535 $this->mimetypes[]=$tmp[$val];
536 }
537 reset ($this->mimetypes);
538 }
540 function remove_from_parent()
541 {
542 /* Optionally execute a command after we're done */
543 $this->postremove();
544 }
547 function copyPasteHandling_from_queue($s_action,$s_entry)
548 {
549 /* Check if Copy & Paste is disabled */
550 if(!is_object($this->CopyPasteHandler)){
551 return("");
552 }
554 /* Add a single entry to queue */
555 if($s_action == "cut" || $s_action == "copy"){
557 /* Cleanup object queue */
558 $this->CopyPasteHandler->cleanup_queue();
559 $this->start_pasting_copied_objects = FALSE;
560 $dn = $this->mimetypes[$s_entry]['dn'];
561 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
562 }
564 /* Add entries to queue */
565 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
567 /* Cleanup object queue */
568 $this->CopyPasteHandler->cleanup_queue();
569 $this->start_pasting_copied_objects = FALSE;
571 /* Add new entries to CP queue */
572 foreach($this->list_get_selected_items() as $id){
573 $dn = $this->mimetypes[$id]['dn'];
575 if($s_action == "copy_multiple"){
576 $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
577 }
578 if($s_action == "cut_multiple"){
579 $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
580 }
581 }
582 }
584 /* Start pasting entries */
585 if($s_action == "editPaste"){
586 $this->start_pasting_copied_objects = TRUE;
587 }
589 /* Return C&P dialog */
590 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
592 /* Get dialog */
593 $data = $this->CopyPasteHandler->execute();
594 $this->CopyPasteHandler->SetVar("base",$this->DivListMimeTypes->selectedBase);
596 /* Return dialog data */
597 if(!empty($data)){
598 return($data);
599 }
600 }
602 /* Automatically disable status for pasting */
603 #if(!$this->CopyPasteHandler->entries_queued()){
604 # $this->start_pasting_copied_objects = FALSE;
605 #}
606 return("");
607 }
610 /* Check if the release management is activated. */
611 function IsReleaseManagementActivated()
612 {
613 /* Check if we should enable the release selection */
614 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
615 if(!empty($tmp)){
616 return(true);
617 }
618 return(false);
619 }
622 function list_get_selected_items()
623 {
624 $ids = array();
625 foreach($_POST as $name => $value){
626 if(preg_match("/^item_selected_[0-9]*$/",$name)){
627 $id = preg_replace("/^item_selected_/","",$name);
628 $ids[$id] = $id;
629 }
630 }
631 return($ids);
632 }
635 /* Save to LDAP */
636 function save()
637 {
638 /* Optionally execute a command after we're done */
639 $this->postcreate();
640 }
642 function remove_lock()
643 {
644 if (isset($this->mimetabs->dn)){
645 del_lock ($this->mimetabs->dn);
646 }
647 }
649 function save_object() {
650 $this->DivListMimeTypes->save_object();
651 }
653 function check() {}
654 function adapt_from_template($dn, $skip= array()) {}
655 function password_change_needed() {}
656 }
657 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
658 ?>