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 require "tabs_mimetypes.inc";
23 class mimetypeManagement extends plugin
24 {
25 /* Definitions */
26 var $plHeadline = "Mime types";
27 var $plDescription = "Manage mime types";
29 /* Dialog attributes */
30 var $ui = NULL;
31 var $DivListMimeTypes = NULL;
32 var $enableReleaseManagement = false;
33 var $mimetabs = NULL;
34 var $snapDialog = NULL;
35 var $CopyPasteHandler = NULL;
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_/");
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("/^editPaste.*/i",$key)){
131 $s_action="editPaste";
132 }
133 }
135 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
136 $s_action ="edit";
137 $s_entry = $_GET['id'];
138 }
140 $s_entry = preg_replace("/_.$/","",$s_entry);
143 /****************
144 Copy & Paste handling
145 ****************/
147 /* Only perform copy / paste if it is enabled
148 */
149 if($this->CopyPasteHandler){
150 if($str = $this->copyPasteHandling($s_action,$s_entry)){
151 return($str);
152 };
153 }
156 /****************
157 Create a new mime type
158 ****************/
160 /* New mime type? */
161 if ($s_action=="new"){
163 /* By default we set 'dn' to 'new', all relevant plugins will
164 react on this. */
165 $this->dn= "new";
167 /* Create new usertab object */
168 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
169 $this->mimetabs->set_acl_base($this->DivListMimeTypes->selectedBase);
170 }
173 /****************
174 Edit entry canceled
175 ****************/
177 /* Cancel dialogs */
178 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
179 del_lock ($this->mimetabs->dn);
180 unset ($this->mimetabs);
181 $this->mimetabs= NULL;
182 unset ($_SESSION['objectinfo']);
183 }
186 /****************
187 Edit entry finished
188 ****************/
190 /* Finish mime edit is triggered by the tabulator dialog, so
191 the user wants to save edited data. Check and save at this point. */
192 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
194 /* Check tabs, will feed message array */
195 $this->mimetabs->save_object();
196 $message= $this->mimetabs->check();
198 /* Save, or display error message? */
199 if (count($message) == 0){
201 /* Save data data to ldap */
202 $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
203 $this->mimetabs->save();
204 gosa_log ("Mime object'".$this->dn."' has been saved");
206 if (!isset($_POST['edit_apply'])){
207 /* Mime type has been saved successfully, remove lock from LDAP. */
208 if ($this->dn != "new"){
209 del_lock ($this->dn);
210 }
211 unset ($this->mimetabs);
212 $this->mimetabs= NULL;
213 unset ($_SESSION['objectinfo']);
214 }
215 } else {
216 /* Ok. There seem to be errors regarding to the tab data,
217 show message and continue as usual. */
218 show_errors($message);
219 }
220 }
223 /****************
224 Edit entry
225 ****************/
227 /* User wants to edit data? */
228 if (($s_action=="edit") && (!isset($this->mimetabs->config))){
230 /* Get 'dn' from posted 'mimelist', must be unique */
231 $this->dn= $this->mimetypes[$s_entry]['dn'];
233 /* Check locking, save current plugin in 'back_plugin', so
234 the dialog knows where to return. */
235 if (($user= get_lock($this->dn)) != ""){
236 return(gen_locked_message ($user, $this->dn));
237 }
239 /* Lock the current entry, so everyone will get the
240 above dialog */
241 add_lock ($this->dn, $this->ui->dn);
244 /* Register mimetabs to trigger edit dialog */
245 $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
246 $this->mimetabs->set_acl_base($this->dn);
247 $_SESSION['objectinfo']= $this->dn;
248 }
251 /****************
252 Delete mime type
253 ****************/
255 /* Remove user was requested */
256 if ($s_action == "del"){
258 /* Get 'dn' from posted 'uid' */
259 $this->dn= $this->mimetypes[$s_entry]['dn'];
261 /* Load permissions for selected 'dn' and check if
262 we're allowed to remove this 'dn' */
263 $ui = get_userinfo();
264 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
265 if (preg_match("/d/",$acl)){
267 /* Check locking, save current plugin in 'back_plugin', so
268 the dialog knows where to return. */
269 if (($user= get_lock($this->dn)) != ""){
270 return (gen_locked_message ($user, $this->dn));
271 }
273 /* Lock the current entry, so nobody will edit it during deletion */
274 add_lock ($this->dn, $this->ui->dn);
275 $smarty= get_smarty();
276 $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
277 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
278 } else {
280 /* Obviously the user isn't allowed to delete. Show message and
281 clean session. */
282 print_red (_("You are not allowed to delete this mime type!"));
283 }
284 }
287 /****************
288 Delete mime confirmed
289 ****************/
291 /* Confirmation for deletion has been passed. Group should be deleted. */
292 if (isset($_POST['delete_mime_confirm'])){
294 /* Some nice guy may send this as POST, so we've to check
295 for the permissions again. */
296 $ui = get_userinfo();
297 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
298 if(preg_match("/d/",$acl)){
299 /* Delete request is permitted, perform LDAP action */
300 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
301 $this->mimetabs->set_acl_base($this->dn);
302 $this->mimetabs->delete ();
303 gosa_log ("Mime type object'".$this->dn."' has been removed");
304 unset ($this->mimetabs);
305 $this->mimetabs= NULL;
307 } else {
309 /* Normally this shouldn't be reached, send some extra
310 logs to notify the administrator */
311 print_red (_("You are not allowed to delete this mime type!"));
312 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
313 }
315 /* Remove lock file after successfull deletion */
316 del_lock ($this->dn);
317 }
320 /****************
321 Delete mime canceled
322 ****************/
324 /* Delete mime type canceled? */
325 if (isset($_POST['delete_cancel'])){
326 del_lock ($this->dn);
327 unset($_SESSION['objectinfo']);
328 }
330 /* Show tab dialog if object is present */
331 if (($this->mimetabs) && (isset($this->mimetabs->config))){
332 $display= $this->mimetabs->execute();
334 /* Don't show buttons if tab dialog requests this */
335 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
336 $display.= "<p style=\"text-align:right\">\n";
337 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
338 $display.= " \n";
339 if ($this->dn != "new"){
340 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
341 $display.= " \n";
342 }
343 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
344 $display.= "</p>";
345 }
346 return ($display);
347 }
350 /****************
351 Dialog display
352 ****************/
354 /* Check if there is a snapshot dialog open */
355 $base = $this->DivListMimeTypes->selectedBase;
356 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
357 return($str);
358 }
360 /* Display dialog with system list */
361 $this->DivListMimeTypes->parent = $this;
362 $this->DivListMimeTypes->execute();
363 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase);
364 $this->reload();
365 $this->DivListMimeTypes->setEntries($this->mimetypes);
366 return($this->DivListMimeTypes->Draw());
367 }
370 /* Return departments, that will be included within snapshot detection */
371 function get_used_snapshot_bases()
372 {
373 return(array($this->DivListMimeTypes->selectedRelease));
374 }
378 function reload()
379 {
380 $this->mimetypes= array();
382 /* Set base for all searches */
383 $base = $this->DivListMimeTypes->selectedBase;
384 $release = $this->DivListMimeTypes->selectedRelease;
385 $Regex = $this->DivListMimeTypes->Regex;
386 $SubSearch = $this->DivListMimeTypes->SubSearch;
387 $Flags = GL_NONE | GL_SIZELIMIT;
388 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
389 $tmp = array();
390 $Releases = $this->getReleases($base);
393 /* If release management is enabled, use release as base. */
394 if(!$this->enableReleaseManagement){
395 $use_base = $base;
396 }else{
397 if(isset($Releases[$release])){
398 $use_base = $release;
399 }else{
400 $use_base = $base;
401 }
402 }
404 /* In case of subsearch, add the subsearch flag */
405 if($SubSearch){
406 $Flags |= GL_SUBSEARCH;
407 }
409 /* Get results and create index */
410 $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
411 foreach ($res as $val){
412 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
413 }
415 /* sort entries */
416 ksort($tmp);
417 $this->mimetypes=array();
418 foreach($tmp as $val){
419 $this->mimetypes[]=$val;
420 }
421 reset ($this->mimetypes);
422 }
424 function remove_from_parent()
425 {
426 /* Optionally execute a command after we're done */
427 $this->postremove();
428 }
431 function copyPasteHandling($s_action,$s_entry)
432 {
433 /* Paste copied/cutted object in here
434 */
435 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
436 $this->CopyPasteHandler->save_object();
437 $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
439 if($str = $this->CopyPasteHandler->execute()) {
440 return($str);
441 }
442 }
444 /* Copy current object to CopyHandler
445 */
446 if($s_action == "copy"){
447 $this->CopyPasteHandler->Clear();
448 $dn = $this->mimetypes[$s_entry]['dn'];
449 $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
450 $obj->set_acl_base($this->dn);
451 $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new","mimetypes");
452 $objNew->set_acl_base($this->dn);
453 $this->CopyPasteHandler->Copy($obj,$objNew);
454 }
456 /* Copy current object to CopyHandler
457 */
458 if($s_action == "cut"){
459 $this->CopyPasteHandler->Clear();
460 $dn = $this->mimetypes[$s_entry]['dn'];
461 $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
462 $this->CopyPasteHandler->Cut($obj);
463 }
464 }
466 /* Check if the release management is activated. */
467 function IsReleaseManagementActivated()
468 {
469 /* Check if we should enable the release selection */
470 $tmp = search_config($this->config->data,"faiManagement","CLASS");
471 if(!empty($tmp)){
472 return(true);
473 }
474 return(false);
475 }
477 /* Save to LDAP */
478 function save()
479 {
480 /* Optionally execute a command after we're done */
481 $this->postcreate();
482 }
484 function remove_lock()
485 {
486 if (isset($this->mimetabs->dn)){
487 del_lock ($this->mimetabs->dn);
488 }
489 }
491 function save_object() {
492 $this->DivListMimeTypes->save_object();
493 }
495 function check() {}
496 function adapt_from_template($dn) {}
497 function password_change_needed() {}
498 }
499 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
500 ?>