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/mimetype");
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 if ($this->acl_is_removeable()){
265 /* Check locking, save current plugin in 'back_plugin', so
266 the dialog knows where to return. */
267 if (($user= get_lock($this->dn)) != ""){
268 return (gen_locked_message ($user, $this->dn));
269 }
271 /* Lock the current entry, so nobody will edit it during deletion */
272 add_lock ($this->dn, $this->ui->dn);
273 $smarty= get_smarty();
274 $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
275 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
276 } else {
278 /* Obviously the user isn't allowed to delete. Show message and
279 clean session. */
280 print_red (_("You are not allowed to delete this mime type!"));
281 }
282 }
285 /****************
286 Delete mime confirmed
287 ****************/
289 /* Confirmation for deletion has been passed. Group should be deleted. */
290 if (isset($_POST['delete_mime_confirm'])){
292 /* Some nice guy may send this as POST, so we've to check
293 for the permissions again. */
294 if ($this->acl_is_removeable()){
296 /* Delete request is permitted, perform LDAP action */
297 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes/mimetype");
298 $this->mimetabs->set_acl_base($this->dn);
299 $this->mimetabs->delete ();
300 gosa_log ("Mime type object'".$this->dn."' has been removed");
301 unset ($this->mimetabs);
302 $this->mimetabs= NULL;
304 } else {
306 /* Normally this shouldn't be reached, send some extra
307 logs to notify the administrator */
308 print_red (_("You are not allowed to delete this mime type!"));
309 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
310 }
312 /* Remove lock file after successfull deletion */
313 del_lock ($this->dn);
314 }
317 /****************
318 Delete mime canceled
319 ****************/
321 /* Delete mime type canceled? */
322 if (isset($_POST['delete_cancel'])){
323 del_lock ($this->dn);
324 unset($_SESSION['objectinfo']);
325 }
327 /* Show tab dialog if object is present */
328 if (($this->mimetabs) && (isset($this->mimetabs->config))){
329 $display= $this->mimetabs->execute();
331 /* Don't show buttons if tab dialog requests this */
332 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
333 $display.= "<p style=\"text-align:right\">\n";
334 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
335 $display.= " \n";
336 if ($this->dn != "new"){
337 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
338 $display.= " \n";
339 }
340 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
341 $display.= "</p>";
342 }
343 return ($display);
344 }
347 /****************
348 Dialog display
349 ****************/
351 /* Check if there is a snapshot dialog open */
352 $base = $this->DivListMimeTypes->selectedBase;
353 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
354 return($str);
355 }
357 /* Display dialog with system list */
358 $this->DivListMimeTypes->parent = $this;
359 $this->DivListMimeTypes->execute();
360 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase);
361 $this->reload();
362 $this->DivListMimeTypes->setEntries($this->mimetypes);
363 return($this->DivListMimeTypes->Draw());
364 }
367 /* Return departments, that will be included within snapshot detection */
368 function get_used_snapshot_bases()
369 {
370 return(array($this->DivListMimeTypes->selectedRelease));
371 }
375 function reload()
376 {
377 $this->mimetypes= array();
379 /* Set base for all searches */
380 $base = $this->DivListMimeTypes->selectedBase;
381 $release = $this->DivListMimeTypes->selectedRelease;
382 $Regex = $this->DivListMimeTypes->Regex;
383 $SubSearch = $this->DivListMimeTypes->SubSearch;
384 $Flags = GL_NONE | GL_SIZELIMIT;
385 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
386 $tmp = array();
387 $Releases = $this->getReleases($base);
390 /* If release management is enabled, use release as base. */
391 if(!$this->enableReleaseManagement){
392 $use_base = $base;
393 }else{
394 if(isset($Releases[$release])){
395 $use_base = $release;
396 }else{
397 $use_base = $base;
398 }
399 }
401 /* In case of subsearch, add the subsearch flag */
402 if($SubSearch){
403 $Flags |= GL_SUBSEARCH;
404 }
406 /* Get results and create index */
407 $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
408 foreach ($res as $val){
409 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
410 }
412 /* sort entries */
413 ksort($tmp);
414 $this->mimetypes=array();
415 foreach($tmp as $val){
416 $this->mimetypes[]=$val;
417 }
418 reset ($this->mimetypes);
419 }
421 function remove_from_parent()
422 {
423 /* Optionally execute a command after we're done */
424 $this->postremove();
425 }
428 function copyPasteHandling($s_action,$s_entry)
429 {
430 /* Paste copied/cutted object in here
431 */
432 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
433 $this->CopyPasteHandler->save_object();
434 $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
436 if($str = $this->CopyPasteHandler->execute()) {
437 return($str);
438 }
439 }
441 /* Copy current object to CopyHandler
442 */
443 if($s_action == "copy"){
444 $this->CopyPasteHandler->Clear();
445 $dn = $this->mimetypes[$s_entry]['dn'];
446 $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
447 $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new");
448 $this->CopyPasteHandler->Copy($obj,$objNew);
449 }
451 /* Copy current object to CopyHandler
452 */
453 if($s_action == "cut"){
454 $this->CopyPasteHandler->Clear();
455 $dn = $this->mimetypes[$s_entry]['dn'];
456 $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
457 $this->CopyPasteHandler->Cut($obj);
458 }
459 }
461 /* Check if the release management is activated. */
462 function IsReleaseManagementActivated()
463 {
464 /* Check if we should enable the release selection */
465 $tmp = search_config($this->config->data,"faiManagement","CLASS");
466 if(!empty($tmp)){
467 return(true);
468 }
469 return(false);
470 }
472 /* Save to LDAP */
473 function save()
474 {
475 /* Optionally execute a command after we're done */
476 $this->postcreate();
477 }
479 function remove_lock()
480 {
481 if (isset($this->mimetabs->dn)){
482 del_lock ($this->mimetabs->dn);
483 }
484 }
486 function save_object() {
487 $this->DivListMimeTypes->save_object();
488 }
490 function check() {}
491 function adapt_from_template($dn) {}
492 function password_change_needed() {}
493 }
494 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
495 ?>