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 = "ou=mime,".$base;
68 $ret = array();
69 $ret ["ou=mime,".$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['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 unset ($_SESSION['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 unset ($_SESSION['objectinfo']);
241 }
242 } else {
243 /* Ok. There seem to be errors regarding to the tab data,
244 show message and continue as usual. */
245 show_errors($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['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 = "<br><pre>";
297 foreach($this->dns as $dn){
298 add_lock ($dn, $this->ui->dn);
299 $dns_names .= $dn."\n";
300 }
301 $dns_names .="</pre>";
303 /* Lock the current entry, so nobody will edit it during deletion */
304 if (count($this->dns) == 1){
305 $smarty->assign("intro", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
306 } else {
307 $smarty->assign("intro", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
308 }
309 $smarty->assign("multiple", true);
310 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
311 }
312 }
317 /********************
318 Delete MULTIPLE entries confirmed
319 ********************/
321 /* Confirmation for deletion has been passed. Users should be deleted. */
322 if (isset($_POST['delete_multiple_mimetype_confirm'])){
324 $ui = get_userinfo();
326 /* Remove user by user and check acls before removeing them */
327 foreach($this->dns as $key => $dn){
329 $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
330 if(preg_match("/d/",$acl)){
332 /* Delete request is permitted, perform LDAP action */
333 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
334 $this->mimetabs->set_acl_base($dn);
335 $this->mimetabs->delete ();
336 unset ($this->mimetabs);
337 $this->mimetabs= NULL;
339 } else {
340 /* Normally this shouldn't be reached, send some extra
341 logs to notify the administrator */
342 print_red (_("You are not allowed to delete this mime type!"));
343 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
344 }
345 /* Remove lock file after successfull deletion */
346 del_lock ($dn);
347 unset($this->dns[$key]);
348 }
349 }
352 /********************
353 Delete MULTIPLE entries Canceled
354 ********************/
356 /* Remove lock */
357 if(isset($_POST['delete_multiple_mimetype_cancel'])){
358 foreach($this->dns as $key => $dn){
359 del_lock ($dn);
360 unset($this->dns[$key]);
361 }
362 }
365 /****************
366 Delete mime type
367 ****************/
369 /* Remove user was requested */
370 if ($s_action == "del"){
372 /* Get 'dn' from posted 'uid' */
373 $this->dn= $this->mimetypes[$s_entry]['dn'];
375 /* Load permissions for selected 'dn' and check if
376 we're allowed to remove this 'dn' */
377 $ui = get_userinfo();
378 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
379 if (preg_match("/d/",$acl)){
381 /* Check locking, save current plugin in 'back_plugin', so
382 the dialog knows where to return. */
383 if (($user= get_lock($this->dn)) != ""){
384 return (gen_locked_message ($user, $this->dn));
385 }
387 /* Lock the current entry, so nobody will edit it during deletion */
388 add_lock ($this->dn, $this->ui->dn);
389 $smarty= get_smarty();
390 $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
391 $smarty->assign("multiple", false);
392 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
393 } else {
395 /* Obviously the user isn't allowed to delete. Show message and
396 clean session. */
397 print_red (_("You are not allowed to delete this mime type!"));
398 }
399 }
402 /****************
403 Delete mime confirmed
404 ****************/
406 /* Confirmation for deletion has been passed. Group should be deleted. */
407 if (isset($_POST['delete_mime_confirm'])){
409 /* Some nice guy may send this as POST, so we've to check
410 for the permissions again. */
411 $ui = get_userinfo();
412 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
413 if(preg_match("/d/",$acl)){
415 /* Delete request is permitted, perform LDAP action */
416 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
417 $this->mimetabs->set_acl_base($this->dn);
418 $this->mimetabs->delete ();
419 unset ($this->mimetabs);
420 $this->mimetabs= NULL;
422 } else {
424 /* Normally this shouldn't be reached, send some extra
425 logs to notify the administrator */
426 print_red (_("You are not allowed to delete this mime type!"));
427 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
428 }
430 /* Remove lock file after successfull deletion */
431 del_lock ($this->dn);
432 }
435 /****************
436 Delete mime canceled
437 ****************/
439 /* Delete mime type canceled? */
440 if (isset($_POST['delete_cancel'])){
441 del_lock ($this->dn);
442 unset($_SESSION['objectinfo']);
443 }
445 /* Show tab dialog if object is present */
446 if (($this->mimetabs) && (isset($this->mimetabs->config))){
447 $display= $this->mimetabs->execute();
449 /* Don't show buttons if tab dialog requests this */
450 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
451 $display.= "<p style=\"text-align:right\">\n";
452 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
453 $display.= " \n";
454 if ($this->dn != "new"){
455 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
456 $display.= " \n";
457 }
458 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
459 $display.= "</p>";
460 }
461 return ($display);
462 }
465 /****************
466 Dialog display
467 ****************/
469 /* Check if there is a snapshot dialog open */
470 $base = $this->DivListMimeTypes->selectedBase;
471 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
472 return($str);
473 }
475 /* Display dialog with system list */
476 $this->DivListMimeTypes->parent = $this;
477 $this->DivListMimeTypes->execute();
478 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
479 $this->reload();
480 $this->DivListMimeTypes->setEntries($this->mimetypes);
481 return($this->DivListMimeTypes->Draw());
482 }
485 /* Return departments, that will be included within snapshot detection */
486 function get_used_snapshot_bases()
487 {
488 return(array($this->DivListMimeTypes->selectedRelease));
489 }
493 function reload()
494 {
495 $this->mimetypes= array();
497 /* Set base for all searches */
498 $base = $this->DivListMimeTypes->selectedBase;
499 $release = $this->DivListMimeTypes->selectedRelease;
500 $Regex = $this->DivListMimeTypes->Regex;
501 $SubSearch = $this->DivListMimeTypes->SubSearch;
502 $Flags = GL_NONE | GL_SIZELIMIT;
503 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
504 $tmp = array();
505 $Releases = $this->getReleases($base);
508 /* If release management is enabled, use release as base. */
509 if(!$this->enableReleaseManagement){
510 $use_base = $base;
511 }else{
512 if(isset($Releases[$release])){
513 $use_base = $release;
514 }else{
515 $use_base = $base;
516 }
517 }
519 /* In case of subsearch, add the subsearch flag */
520 if($SubSearch){
521 $Flags |= GL_SUBSEARCH;
522 }else{
523 if(!$this->enableReleaseManagement){
524 $use_base ="ou=mime,".$use_base;
525 }
526 }
528 /* Get results and create index */
529 $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
530 foreach ($res as $val){
531 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
532 }
534 /* sort entries */
535 ksort($tmp);
536 $this->mimetypes=array();
537 foreach($tmp as $val){
538 $this->mimetypes[]=$val;
539 }
540 reset ($this->mimetypes);
541 }
543 function remove_from_parent()
544 {
545 /* Optionally execute a command after we're done */
546 $this->postremove();
547 }
550 function copyPasteHandling_from_queue($s_action,$s_entry)
551 {
552 /* Check if Copy & Paste is disabled */
553 if(!is_object($this->CopyPasteHandler)){
554 return("");
555 }
557 /* Add a single entry to queue */
558 if($s_action == "cut" || $s_action == "copy"){
560 /* Cleanup object queue */
561 $this->CopyPasteHandler->cleanup_queue();
562 $this->start_pasting_copied_objects = FALSE;
563 $dn = $this->mimetypes[$s_entry]['dn'];
564 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
565 }
567 /* Add entries to queue */
568 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
570 /* Cleanup object queue */
571 $this->CopyPasteHandler->cleanup_queue();
572 $this->start_pasting_copied_objects = FALSE;
574 /* Add new entries to CP queue */
575 foreach($this->list_get_selected_items() as $id){
576 $dn = $this->mimetypes[$id]['dn'];
578 if($s_action == "copy_multiple"){
579 $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
580 }
581 if($s_action == "cut_multiple"){
582 $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
583 }
584 }
585 }
587 /* Start pasting entries */
588 if($s_action == "editPaste"){
589 $this->start_pasting_copied_objects = TRUE;
590 }
592 /* Return C&P dialog */
593 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
595 /* Load entry from queue and set base */
596 $this->CopyPasteHandler->load_entry_from_queue();
597 $this->CopyPasteHandler->SetVar("base",$this->DivListMimeTypes->selectedBase);
599 /* Get dialog */
600 $data = $this->CopyPasteHandler->execute();
602 /* Return dialog data */
603 if(!empty($data)){
604 return($data);
605 }
606 }
608 /* Automatically disable status for pasting */
609 #if(!$this->CopyPasteHandler->entries_queued()){
610 # $this->start_pasting_copied_objects = FALSE;
611 #}
612 return("");
613 }
616 /* Check if the release management is activated. */
617 function IsReleaseManagementActivated()
618 {
619 /* Check if we should enable the release selection */
620 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
621 if(!empty($tmp)){
622 return(true);
623 }
624 return(false);
625 }
628 function list_get_selected_items()
629 {
630 $ids = array();
631 foreach($_POST as $name => $value){
632 if(preg_match("/^item_selected_[0-9]*$/",$name)){
633 $id = preg_replace("/^item_selected_/","",$name);
634 $ids[$id] = $id;
635 }
636 }
637 return($ids);
638 }
641 /* Save to LDAP */
642 function save()
643 {
644 /* Optionally execute a command after we're done */
645 $this->postcreate();
646 }
648 function remove_lock()
649 {
650 if (isset($this->mimetabs->dn)){
651 del_lock ($this->mimetabs->dn);
652 }
653 }
655 function save_object() {
656 $this->DivListMimeTypes->save_object();
657 }
659 function check() {}
660 function adapt_from_template($dn) {}
661 function password_change_needed() {}
662 }
663 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
664 ?>