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 $ui = get_userinfo();
162 $acl = $ui->get_permissions($this->DivListMimeTypes->selectedBase,"mimetypes/mimetype");
163 if (($s_action=="new") && preg_match("/c/",$acl)){
165 /* By default we set 'dn' to 'new', all relevant plugins will
166 react on this. */
167 $this->dn= "new";
169 /* Create new usertab object */
170 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
171 $this->mimetabs->set_acl_base($this->DivListMimeTypes->selectedBase);
172 }
175 /****************
176 Edit entry canceled
177 ****************/
179 /* Cancel dialogs */
180 if (isset($_POST['edit_cancel'])){
181 del_lock ($this->mimetabs->dn);
182 unset ($this->mimetabs);
183 $this->mimetabs= NULL;
184 unset ($_SESSION['objectinfo']);
185 }
188 /****************
189 Edit entry finished
190 ****************/
192 /* Finish mime edit is triggered by the tabulator dialog, so
193 the user wants to save edited data. Check and save at this point. */
194 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->mimetabs->config))){
196 /* Check tabs, will feed message array */
197 $this->mimetabs->save_object();
198 $message= $this->mimetabs->check();
200 /* Save, or display error message? */
201 if (count($message) == 0){
203 /* Save data data to ldap */
204 $this->mimetabs->set_release($this->DivListMimeTypes->selectedRelease);
205 $this->mimetabs->save();
206 gosa_log ("Mime object'".$this->dn."' has been saved");
208 if (!isset($_POST['edit_apply'])){
209 /* Mime type has been saved successfully, remove lock from LDAP. */
210 if ($this->dn != "new"){
211 del_lock ($this->dn);
212 }
213 unset ($this->mimetabs);
214 $this->mimetabs= NULL;
215 unset ($_SESSION['objectinfo']);
216 }
217 } else {
218 /* Ok. There seem to be errors regarding to the tab data,
219 show message and continue as usual. */
220 show_errors($message);
221 }
222 }
225 /****************
226 Edit entry
227 ****************/
229 /* User wants to edit data? */
230 if (($s_action=="edit") && (!isset($this->mimetabs->config))){
232 /* Get 'dn' from posted 'mimelist', must be unique */
233 $this->dn= $this->mimetypes[$s_entry]['dn'];
235 /* Check locking, save current plugin in 'back_plugin', so
236 the dialog knows where to return. */
237 if (($user= get_lock($this->dn)) != ""){
238 return(gen_locked_message ($user, $this->dn));
239 }
241 /* Lock the current entry, so everyone will get the
242 above dialog */
243 add_lock ($this->dn, $this->ui->dn);
246 /* Register mimetabs to trigger edit dialog */
247 $this->mimetabs= new mimetabs($this->config,$this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
248 $this->mimetabs->set_acl_base($this->dn);
249 $_SESSION['objectinfo']= $this->dn;
250 }
253 /****************
254 Delete mime type
255 ****************/
257 /* Remove user was requested */
258 if ($s_action == "del"){
260 /* Get 'dn' from posted 'uid' */
261 $this->dn= $this->mimetypes[$s_entry]['dn'];
263 /* Load permissions for selected 'dn' and check if
264 we're allowed to remove this 'dn' */
265 $ui = get_userinfo();
266 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
267 if (preg_match("/d/",$acl)){
269 /* Check locking, save current plugin in 'back_plugin', so
270 the dialog knows where to return. */
271 if (($user= get_lock($this->dn)) != ""){
272 return (gen_locked_message ($user, $this->dn));
273 }
275 /* Lock the current entry, so nobody will edit it during deletion */
276 add_lock ($this->dn, $this->ui->dn);
277 $smarty= get_smarty();
278 $smarty->assign("intro", sprintf(_("You're about to delete the mime type '%s'."), @LDAP::fix($this->dn)));
279 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
280 } else {
282 /* Obviously the user isn't allowed to delete. Show message and
283 clean session. */
284 print_red (_("You are not allowed to delete this mime type!"));
285 }
286 }
289 /****************
290 Delete mime confirmed
291 ****************/
293 /* Confirmation for deletion has been passed. Group should be deleted. */
294 if (isset($_POST['delete_mime_confirm'])){
296 /* Some nice guy may send this as POST, so we've to check
297 for the permissions again. */
298 $ui = get_userinfo();
299 $acl = $ui->get_permissions($this->dn,"mimetypes/mimetype");
300 if(preg_match("/d/",$acl)){
301 /* Delete request is permitted, perform LDAP action */
302 $this->mimetabs= new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $this->dn,"mimetypes");
303 $this->mimetabs->set_acl_base($this->dn);
304 $this->mimetabs->delete ();
305 gosa_log ("Mime type object'".$this->dn."' has been removed");
306 unset ($this->mimetabs);
307 $this->mimetabs= NULL;
309 } else {
311 /* Normally this shouldn't be reached, send some extra
312 logs to notify the administrator */
313 print_red (_("You are not allowed to delete this mime type!"));
314 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
315 }
317 /* Remove lock file after successfull deletion */
318 del_lock ($this->dn);
319 }
322 /****************
323 Delete mime canceled
324 ****************/
326 /* Delete mime type canceled? */
327 if (isset($_POST['delete_cancel'])){
328 del_lock ($this->dn);
329 unset($_SESSION['objectinfo']);
330 }
332 /* Show tab dialog if object is present */
333 if (($this->mimetabs) && (isset($this->mimetabs->config))){
334 $display= $this->mimetabs->execute();
336 /* Don't show buttons if tab dialog requests this */
337 if (!$this->mimetabs->by_object[$this->mimetabs->current]->dialog){
338 $display.= "<p style=\"text-align:right\">\n";
339 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
340 $display.= " \n";
341 if ($this->dn != "new"){
342 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
343 $display.= " \n";
344 }
345 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
346 $display.= "</p>";
347 }
348 return ($display);
349 }
352 /****************
353 Dialog display
354 ****************/
356 /* Check if there is a snapshot dialog open */
357 $base = $this->DivListMimeTypes->selectedBase;
358 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
359 return($str);
360 }
362 /* Display dialog with system list */
363 $this->DivListMimeTypes->parent = $this;
364 $this->DivListMimeTypes->execute();
365 $this->DivListMimeTypes->AddDepartments($this->DivListMimeTypes->selectedBase);
366 $this->reload();
367 $this->DivListMimeTypes->setEntries($this->mimetypes);
368 return($this->DivListMimeTypes->Draw());
369 }
372 /* Return departments, that will be included within snapshot detection */
373 function get_used_snapshot_bases()
374 {
375 return(array($this->DivListMimeTypes->selectedRelease));
376 }
380 function reload()
381 {
382 $this->mimetypes= array();
384 /* Set base for all searches */
385 $base = $this->DivListMimeTypes->selectedBase;
386 $release = $this->DivListMimeTypes->selectedRelease;
387 $Regex = $this->DivListMimeTypes->Regex;
388 $SubSearch = $this->DivListMimeTypes->SubSearch;
389 $Flags = GL_NONE | GL_SIZELIMIT;
390 $Filter = "(&(|(cn=".$Regex.")(description=".$Regex."))(objectClass=gotoMimeType))";
391 $tmp = array();
392 $Releases = $this->getReleases($base);
395 /* If release management is enabled, use release as base. */
396 if(!$this->enableReleaseManagement){
397 $use_base = $base;
398 }else{
399 if(isset($Releases[$release])){
400 $use_base = $release;
401 }else{
402 $use_base = $base;
403 }
404 }
406 /* In case of subsearch, add the subsearch flag */
407 if($SubSearch){
408 $Flags |= GL_SUBSEARCH;
409 }
411 /* Get results and create index */
412 $res= get_list($Filter, "mimetypes", $use_base, array("cn","description","dn","objectClass"), $Flags);
413 foreach ($res as $val){
414 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
415 }
417 /* sort entries */
418 ksort($tmp);
419 $this->mimetypes=array();
420 foreach($tmp as $val){
421 $this->mimetypes[]=$val;
422 }
423 reset ($this->mimetypes);
424 }
426 function remove_from_parent()
427 {
428 /* Optionally execute a command after we're done */
429 $this->postremove();
430 }
433 function copyPasteHandling($s_action,$s_entry)
434 {
435 /* Paste copied/cutted object in here
436 */
437 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
438 $this->CopyPasteHandler->save_object();
439 $this->CopyPasteHandler->SetVar("base", $this->DivListMimeTypes->selectedBase);
441 if($str = $this->CopyPasteHandler->execute()) {
442 return($str);
443 }
444 }
446 /* Copy current object to CopyHandler
447 */
448 if($s_action == "copy"){
449 $this->CopyPasteHandler->Clear();
450 $dn = $this->mimetypes[$s_entry]['dn'];
452 /* Check acls */
453 $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
454 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
455 $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn,"mimetypes");
456 $obj->set_acl_base($this->dn);
457 $objNew = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], "new","mimetypes");
458 $objNew->set_acl_base($this->dn);
459 $this->CopyPasteHandler->Copy($obj,$objNew);
460 }
461 }
463 /* Copy current object to CopyHandler
464 */
465 if($s_action == "cut"){
466 $this->CopyPasteHandler->Clear();
467 $dn = $this->mimetypes[$s_entry]['dn'];
469 /* Check acls */
470 $acl_all= $this->ui->has_complete_category_acls($dn,"mimetypes");
471 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
472 $obj = new mimetabs($this->config, $this->config->data['TABS']['MIMETABS'], $dn);
473 $this->CopyPasteHandler->Cut($obj);
474 }
475 }
476 }
478 /* Check if the release management is activated. */
479 function IsReleaseManagementActivated()
480 {
481 /* Check if we should enable the release selection */
482 $tmp = search_config($this->config->data,"faiManagement","CLASS");
483 if(!empty($tmp)){
484 return(true);
485 }
486 return(false);
487 }
489 /* Save to LDAP */
490 function save()
491 {
492 /* Optionally execute a command after we're done */
493 $this->postcreate();
494 }
496 function remove_lock()
497 {
498 if (isset($this->mimetabs->dn)){
499 del_lock ($this->mimetabs->dn);
500 }
501 }
503 function save_object() {
504 $this->DivListMimeTypes->save_object();
505 }
507 function check() {}
508 function adapt_from_template($dn) {}
509 function password_change_needed() {}
510 }
511 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
512 ?>