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);
149 /****************
150 Copy & Paste handling
151 ****************/
153 /* Display the copy & paste dialog, if it is currently open */
154 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
155 if($ret){
156 return($ret);
157 }
160 /****************
161 Create a new mime type
162 ****************/
164 /* New mime type? */
165 $ui = get_userinfo();
166 $acl = $ui->get_permissions($this->DivListMimeTypes->selectedBase,"mimetypes/mimetype");
167 if (($s_action=="new") && preg_match("/c/",$acl)){
169 /* By default we set 'dn' to 'new', all relevant plugins will
170 react on this. */
171 $this->dn= "new";
173 /* Create new usertab object */
174 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
175 $this->mimetabs->set_acl_base($this->DivListMimeTypes->selectedBase);
176 }
179 /****************
180 Edit entry canceled
181 ****************/
183 /* Cancel dialogs */
184 if (isset($_POST['edit_cancel'])){
185 del_lock ($this->mimetabs->dn);
186 unset ($this->mimetabs);
187 $this->mimetabs= NULL;
188 unset ($_SESSION['objectinfo']);
189 }
192 /****************
193 Edit entry finished
194 ****************/
196 /* Finish mime edit is triggered by the tabulator dialog, so
197 the user wants to save edited data. Check and save at this point. */
198 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
200 /* Check tabs, will feed message array */
201 $this->mimetabs->save_object();
202 $message= $this->mimetabs->check();
204 /* Save, or display error message? */
205 if (count($message) == 0){
207 /* Save data data to ldap */
208 $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
209 $this->mimetabs->save();
211 if (!isset($_POST['edit_apply'])){
212 /* Mime type has been saved successfully, remove lock from LDAP. */
213 if ($this->dn != "new"){
214 del_lock ($this->dn);
215 }
216 unset ($this->mimetabs);
217 $this->mimetabs= NULL;
218 unset ($_SESSION['objectinfo']);
219 }
220 } else {
221 /* Ok. There seem to be errors regarding to the tab data,
222 show message and continue as usual. */
223 show_errors($message);
224 }
225 }
228 /****************
229 Edit entry
230 ****************/
232 /* User wants to edit data? */
233 if (($s_action=="edit") && (!isset($this->mimetabs->config))){
235 /* Get 'dn' from posted 'mimelist', must be unique */
236 $this->dn= $this->mimetypes[$s_entry]['dn'];
238 /* Check locking, save current plugin in 'back_plugin', so
239 the dialog knows where to return. */
240 if (($user= get_lock($this->dn)) != ""){
241 return(gen_locked_message ($user, $this->dn));
242 }
244 /* Lock the current entry, so everyone will get the
245 above dialog */
246 add_lock ($this->dn, $this->ui->dn);
249 /* Register mimetabs to trigger edit dialog */
250 $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
251 $this->mimetabs->set_acl_base($this->dn);
252 $_SESSION['objectinfo']= $this->dn;
253 }
256 /********************
257 Delete MULTIPLE entries requested, display confirm dialog
258 ********************/
260 if ($s_action=="del_multiple"){
261 $ids = $this->list_get_selected_items();
263 if(count($ids)){
265 foreach($ids as $id){
266 $dn = $this->mimetypes[$id]['dn'];
267 if (($user= get_lock($dn)) != ""){
268 return(gen_locked_message ($user, $dn));
269 }
270 $this->dns[$id] = $dn;
271 }
273 $dns_names = "<br><pre>";
274 foreach($this->dns as $dn){
275 add_lock ($dn, $this->ui->dn);
276 $dns_names .= $dn."\n";
277 }
278 $dns_names .="</pre>";
280 /* Lock the current entry, so nobody will edit it during deletion */
281 if (count($this->dns) == 1){
282 $smarty->assign("intro", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
283 } else {
284 $smarty->assign("intro", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
285 }
286 $smarty->assign("multiple", true);
287 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
288 }
289 }
294 /********************
295 Delete MULTIPLE entries confirmed
296 ********************/
298 /* Confirmation for deletion has been passed. Users should be deleted. */
299 if (isset($_POST['delete_multiple_mimetype_confirm'])){
301 $ui = get_userinfo();
303 /* Remove user by user and check acls before removeing them */
304 foreach($this->dns as $key => $dn){
306 $acl = $ui->get_permissions($dn,"mimetypes/mimetype");
307 if(preg_match("/d/",$acl)){
309 /* Delete request is permitted, perform LDAP action */
310 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
311 $this->mimetabs->set_acl_base($dn);
312 $this->mimetabs->delete ();
313 unset ($this->mimetabs);
314 $this->mimetabs= NULL;
316 } else {
317 /* Normally this shouldn't be reached, send some extra
318 logs to notify the administrator */
319 print_red (_("You are not allowed to delete this mime type!"));
320 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
321 }
322 /* Remove lock file after successfull deletion */
323 del_lock ($dn);
324 unset($this->dns[$key]);
325 }
326 }
329 /********************
330 Delete MULTIPLE entries Canceled
331 ********************/
333 /* Remove lock */
334 if(isset($_POST['delete_multiple_mimetype_cancel'])){
335 foreach($this->dns as $key => $dn){
336 del_lock ($dn);
337 unset($this->dns[$key]);
338 }
339 }
342 /****************
343 Delete mime type
344 ****************/
346 /* Remove user was requested */
347 if ($s_action == "del"){
349 /* Get 'dn' from posted 'uid' */
350 $this->dn= $this->mimetypes[$s_entry]['dn'];
352 /* Load permissions for selected 'dn' and check if
353 we're allowed to remove this 'dn' */
354 $ui = get_userinfo();
355 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
356 if (preg_match("/d/",$acl)){
358 /* Check locking, save current plugin in 'back_plugin', so
359 the dialog knows where to return. */
360 if (($user= get_lock($this->dn)) != ""){
361 return (gen_locked_message ($user, $this->dn));
362 }
364 /* Lock the current entry, so nobody will edit it during deletion */
365 add_lock ($this->dn, $this->ui->dn);
366 $smarty= get_smarty();
367 $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
368 $smarty->assign("multiple", false);
369 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
370 } else {
372 /* Obviously the user isn't allowed to delete. Show message and
373 clean session. */
374 print_red (_("You are not allowed to delete this mime type!"));
375 }
376 }
379 /****************
380 Delete mime confirmed
381 ****************/
383 /* Confirmation for deletion has been passed. Group should be deleted. */
384 if (isset($_POST['delete_mime_confirm'])){
386 /* Some nice guy may send this as POST, so we've to check
387 for the permissions again. */
388 $ui = get_userinfo();
389 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
390 if(preg_match("/d/",$acl)){
392 /* Delete request is permitted, perform LDAP action */
393 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
394 $this->mimetabs->set_acl_base($this->dn);
395 $this->mimetabs->delete ();
396 unset ($this->mimetabs);
397 $this->mimetabs= NULL;
399 } else {
401 /* Normally this shouldn't be reached, send some extra
402 logs to notify the administrator */
403 print_red (_("You are not allowed to delete this mime type!"));
404 new log("security","mimetypes/".get_class($this),$dn,array(),"Tried to trick deletion.");
405 }
407 /* Remove lock file after successfull deletion */
408 del_lock ($this->dn);
409 }
412 /****************
413 Delete mime canceled
414 ****************/
416 /* Delete mime type canceled? */
417 if (isset($_POST['delete_cancel'])){
418 del_lock ($this->dn);
419 unset($_SESSION['objectinfo']);
420 }
422 /* Show tab dialog if object is present */
423 if (($this->mimetabs) && (isset($this->mimetabs->config))){
424 $display= $this->mimetabs->execute();
426 /* Don't show buttons if tab dialog requests this */
427 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
428 $display.= "<p style=\"text-align:right\">\n";
429 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
430 $display.= " \n";
431 if ($this->dn != "new"){
432 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
433 $display.= " \n";
434 }
435 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
436 $display.= "</p>";
437 }
438 return ($display);
439 }
442 /****************
443 Dialog display
444 ****************/
446 /* Check if there is a snapshot dialog open */
447 $base = $this->DivListMimeTypes->selectedBase;
448 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
449 return($str);
450 }
452 /* Display dialog with system list */
453 $this->DivListMimeTypes->parent = $this;
454 $this->DivListMimeTypes->execute();
455 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase,3,1);
456 $this->reload();
457 $this->DivListMimeTypes->setEntries($this->mimetypes);
458 return($this->DivListMimeTypes->Draw());
459 }
462 /* Return departments, that will be included within snapshot detection */
463 function get_used_snapshot_bases()
464 {
465 return(array($this->DivListMimeTypes->selectedRelease));
466 }
470 function reload()
471 {
472 $this->mimetypes= array();
474 /* Set base for all searches */
475 $base = $this->DivListMimeTypes->selectedBase;
476 $release = $this->DivListMimeTypes->selectedRelease;
477 $Regex = $this->DivListMimeTypes->Regex;
478 $SubSearch = $this->DivListMimeTypes->SubSearch;
479 $Flags = GL_NONE | GL_SIZELIMIT;
480 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
481 $tmp = array();
482 $Releases = $this->getReleases($base);
485 /* If release management is enabled, use release as base. */
486 if(!$this->enableReleaseManagement){
487 $use_base = $base;
488 }else{
489 if(isset($Releases[$release])){
490 $use_base = $release;
491 }else{
492 $use_base = $base;
493 }
494 }
496 /* In case of subsearch, add the subsearch flag */
497 if($SubSearch){
498 $Flags |= GL_SUBSEARCH;
499 }else{
500 if(!$this->enableReleaseManagement){
501 $use_base ="ou=mime,".$use_base;
502 }
503 }
505 /* Get results and create index */
506 $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
507 foreach ($res as $val){
508 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
509 }
511 /* sort entries */
512 ksort($tmp);
513 $this->mimetypes=array();
514 foreach($tmp as $val){
515 $this->mimetypes[]=$val;
516 }
517 reset ($this->mimetypes);
518 }
520 function remove_from_parent()
521 {
522 /* Optionally execute a command after we're done */
523 $this->postremove();
524 }
527 function copyPasteHandling_from_queue($s_action,$s_entry)
528 {
529 /* Check if Copy & Paste is disabled */
530 if(!is_object($this->CopyPasteHandler)){
531 return("");
532 }
534 /* Add a single entry to queue */
535 if($s_action == "cut" || $s_action == "copy"){
537 /* Cleanup object queue */
538 $this->CopyPasteHandler->cleanup_queue();
539 $dn = $this->mimetypes[$s_entry]['dn'];
540 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"mimetabs","MIMETABS","mimetypes");
541 }
543 /* Add entries to queue */
544 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
546 /* Cleanup object queue */
547 $this->CopyPasteHandler->cleanup_queue();
549 /* Add new entries to CP queue */
550 foreach($this->list_get_selected_items() as $id){
551 $dn = $this->mimetypes[$id]['dn'];
553 if($s_action == "copy_multiple"){
554 $this->CopyPasteHandler->add_to_queue($dn,"copy","mimetabs","MIMETABS","mimetypes");
555 }
556 if($s_action == "cut_multiple"){
557 $this->CopyPasteHandler->add_to_queue($dn,"cut","mimetabs","MIMETABS","mimetypes");
558 }
559 }
560 }
562 /* Start pasting entries */
563 if($s_action == "editPaste"){
564 $this->start_pasting_copied_objects = TRUE;
565 }
567 /* Return C&P dialog */
568 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
570 /* Load entry from queue and set base */
571 $this->CopyPasteHandler->load_entry_from_queue();
572 $this->CopyPasteHandler->SetVar("base",$this->DivListMimeTypes->selectedBase);
574 /* Get dialog */
575 $data = $this->CopyPasteHandler->execute();
577 /* Return dialog data */
578 if(!empty($data)){
579 return($data);
580 }
581 }
583 /* Automatically disable status for pasting */
584 #if(!$this->CopyPasteHandler->entries_queued()){
585 # $this->start_pasting_copied_objects = FALSE;
586 #}
587 return("");
588 }
591 /* Check if the release management is activated. */
592 function IsReleaseManagementActivated()
593 {
594 /* Check if we should enable the release selection */
595 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
596 if(!empty($tmp)){
597 return(true);
598 }
599 return(false);
600 }
603 function list_get_selected_items()
604 {
605 $ids = array();
606 foreach($_POST as $name => $value){
607 if(preg_match("/^item_selected_[0-9]*$/",$name)){
608 $id = preg_replace("/^item_selected_/","",$name);
609 $ids[$id] = $id;
610 }
611 }
612 return($ids);
613 }
616 /* Save to LDAP */
617 function save()
618 {
619 /* Optionally execute a command after we're done */
620 $this->postcreate();
621 }
623 function remove_lock()
624 {
625 if (isset($this->mimetabs->dn)){
626 del_lock ($this->mimetabs->dn);
627 }
628 }
630 function save_object() {
631 $this->DivListMimeTypes->save_object();
632 }
634 function check() {}
635 function adapt_from_template($dn) {}
636 function password_change_needed() {}
637 }
638 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
639 ?>