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 */
20 require "tabs_application.inc";
22 class applicationManagement extends plugin
23 {
24 /* Definitions */
25 var $plHeadline = "Applications";
26 var $plDescription = "This does something";
28 /* Dialog attributes */
29 var $apptabs = NULL;
30 var $ui = NULL;
31 var $CopyPasteHandler = NULL;
32 var $DivListApplication = NULL;
33 var $applications = array();
34 var $acl = "";
35 var $enableReleaseManagement = false;
37 function IsReleaseManagementActivated()
38 {
39 /* Check if we should enable the release selection */
40 $tmp = search_config($this->config->data,"faiManagement","CLASS");
41 if(!empty($tmp)){
42 return(true);
43 }
44 return(false);
45 }
47 function applicationManagement ($config, $ui)
48 {
49 /* Save configuration for internal use */
50 $this->config = $config;
51 $this->ui = $ui;
53 /* Check if copy & paste is activated */
54 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
55 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
56 }
58 /* Creat dialog object */
59 $this->DivListApplication = new divListApplication($this->config,$this);
61 if($this->IsReleaseManagementActivated()){
62 /* Check if we should enable the release selection */
63 $this->enableReleaseManagement = true;
65 /* Hide SubSearch checkbox */
66 $this->DivListApplication->DisableCheckBox("SubSearch");
67 }
68 }
70 function getReleases($base)
71 {
72 $ldap = $this->config->get_ldap_link();
73 $dn = "ou=apps,".$base;
74 $ret = array();
75 $ret ["ou=apps,".$base] = "/";
77 $ldap->cd($dn);
78 $ldap->search("objectClass=organizationalUnit",array("ou"));
80 while($attrs = $ldap->fetch()){
81 $str = str_replace($dn,"",$attrs['dn']);
82 $tmp = array_reverse( split("ou=",$str));
83 $str = "";
84 foreach($tmp as $val){
85 $val = trim(preg_replace("/,/","",$val));
86 if(empty($val)) break;
87 $str .= "/".$val;
88 }
89 if(!empty($str)){
90 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
91 }
92 }
93 asort($ret);
94 return($ret);
95 }
97 function execute()
98 {
99 /* Call parent execute */
100 plugin::execute();
103 /****************
104 Variable init
105 ****************/
107 /* These vars will be stored if you try to open a locked app,
108 to be able to perform your last requests after showing a warning message */
109 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/");
111 $smarty = get_smarty(); // Smarty instance
112 $s_action = ""; // Contains the action to proceed
113 $s_entry = ""; // The value for s_action
114 $base_back = ""; // The Link for Backbutton
116 /* Test Posts */
117 foreach($_POST as $key => $val){
118 // Post for delete
119 if(preg_match("/appl_del.*/",$key)){
120 $s_action = "del";
121 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
122 // Post for edit
123 }elseif(preg_match("/appl_edit_.*/",$key)){
124 $s_action="edit";
125 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
126 // Post for new
127 }elseif(preg_match("/^copy_.*/",$key)){
128 $s_action="copy";
129 $s_entry = preg_replace("/^copy_/i","",$key);
130 }elseif(preg_match("/^cut_.*/",$key)){
131 $s_action="cut";
132 $s_entry = preg_replace("/^cut_/i","",$key);
133 // Post for new
134 }elseif(preg_match("/^appl_new.*/",$key)){
135 $s_action="new";
136 }elseif(preg_match("/^editPaste.*/i",$key)){
137 $s_action="editPaste";
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 /* Only perform copy / paste if it is enabled
154 */
155 if($this->CopyPasteHandler){
156 if($str = $this->copyPasteHandling($s_action,$s_entry)){
157 return($str);
158 };
159 }
162 /****************
163 Create a new app
164 ****************/
166 /* New application? */
167 if ($s_action=="new"){
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->apptabs= new apptabs($this->config,
175 $this->config->data['TABS']['APPSTABS'], $this->dn);
176 $this->apptabs->set_acl(array(':all'));
177 }
180 /****************
181 Edit entry canceled
182 ****************/
184 /* Cancel dialogs */
185 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
186 del_lock ($this->apptabs->dn);
187 unset ($this->apptabs);
188 $this->apptabs= NULL;
189 unset ($_SESSION['objectinfo']);
190 }
193 /****************
194 Edit entry finished
195 ****************/
197 /* Finish apps edit is triggered by the tabulator dialog, so
198 the user wants to save edited data. Check and save at this
199 point. */
200 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
202 /* Check tabs, will feed message array */
203 $this->apptabs->last= $this->apptabs->current;
204 $this->apptabs->save_object();
205 $message= $this->apptabs->check();
207 /* Save, or display error message? */
208 if (count($message) == 0){
210 /* Save data data to ldap */
211 $this->apptabs->set_release($this->DivListApplication->selectedRelease);
212 $this->apptabs->save();
213 gosa_log ("Application object'".$this->dn."' has been saved");
215 if (!isset($_POST['edit_apply'])){
216 /* Application has been saved successfully, remove lock from
217 LDAP. */
218 if ($this->dn != "new"){
219 del_lock ($this->dn);
220 }
221 unset ($this->apptabs);
222 $this->apptabs= NULL;
223 unset ($_SESSION['objectinfo']);
224 }
225 } else {
226 /* Ok. There seem to be errors regarding to the tab data,
227 show message and continue as usual. */
228 show_errors($message);
229 }
230 }
233 /****************
234 Edit entry
235 ****************/
237 /* User wants to edit data? */
238 if (($s_action=="edit") && (!isset($this->apptabs->config))){
240 /* Get 'dn' from posted 'applist', must be unique */
241 $this->dn= $this->applications[$s_entry]['dn'];
243 /* Check locking, save current plugin in 'back_plugin', so
244 the dialog knows where to return. */
245 if (($user= get_lock($this->dn)) != ""){
246 return(gen_locked_message ($user, $this->dn));
247 }
249 /* Lock the current entry, so everyone will get the
250 above dialog */
251 add_lock ($this->dn, $this->ui->dn);
253 /* Set up the users ACL's for this 'dn' */
254 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
256 /* Register apptabs to trigger edit dialog */
257 $this->apptabs= new apptabs($this->config,
258 $this->config->data['TABS']['APPSTABS'], $this->dn);
259 $this->apptabs->set_acl($acl);
260 $_SESSION['objectinfo']= $this->dn;
261 }
264 /****************
265 Delete app
266 ****************/
268 /* Remove user was requested */
269 if ($s_action == "del"){
271 /* Get 'dn' from posted 'uid' */
272 $this->dn= $this->applications[$s_entry]['dn'];
274 /* Load permissions for selected 'dn' and check if
275 we're allowed to remove this 'dn' */
276 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
277 $this->acl= get_module_permission($acl, "application", $this->dn);
278 if (chkacl($this->acl, "delete") == ""){
280 /* Check locking, save current plugin in 'back_plugin', so
281 the dialog knows where to return. */
282 if (($user= get_lock($this->dn)) != ""){
283 return (gen_locked_message ($user, $this->dn));
284 }
286 /* Lock the current entry, so nobody will edit it during deletion */
287 add_lock ($this->dn, $this->ui->dn);
288 $smarty= get_smarty();
289 $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
290 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
291 } else {
293 /* Obviously the user isn't allowed to delete. Show message and
294 clean session. */
295 print_red (_("You are not allowed to delete this application!"));
296 }
297 }
300 /****************
301 Delete app confirmed
302 ****************/
304 /* Confirmation for deletion has been passed. Group should be deleted. */
305 if (isset($_POST['delete_app_confirm'])){
307 /* Some nice guy may send this as POST, so we've to check
308 for the permissions again. */
309 if (chkacl($this->acl, "delete") == ""){
311 /* Delete request is permitted, perform LDAP action */
312 $this->apptabs= new apptabs($this->config,
313 $this->config->data['TABS']['APPSTABS'], $this->dn);
314 $this->apptabs->set_acl(array($this->acl));
315 $this->apptabs->delete ();
316 gosa_log ("Application object'".$this->dn."' has been removed");
317 unset ($this->apptabs);
318 $this->apptabs= NULL;
320 } else {
322 /* Normally this shouldn't be reached, send some extra
323 logs to notify the administrator */
324 print_red (_("You are not allowed to delete this application!"));
325 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
326 }
328 /* Remove lock file after successfull deletion */
329 del_lock ($this->dn);
330 }
333 /****************
334 Delete app canceled
335 ****************/
337 /* Delete application canceled? */
338 if (isset($_POST['delete_cancel'])){
339 del_lock ($this->dn);
340 unset($_SESSION['objectinfo']);
341 }
343 /* Show tab dialog if object is present */
344 if (($this->apptabs) && (isset($this->apptabs->config))){
345 $display= $this->apptabs->execute();
347 /* Don't show buttons if tab dialog requests this */
348 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
349 $display.= "<p style=\"text-align:right\">\n";
350 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
351 $display.= " \n";
352 if ($this->dn != "new"){
353 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
354 $display.= " \n";
355 }
356 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
357 $display.= "</p>";
358 }
359 return ($display);
360 }
363 /****************
364 Dialog display
365 ****************/
367 /* Check if there is a snapshot dialog open */
368 $base = $this->DivListApplication->selectedBase;
369 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
370 return($str);
371 }
373 /* Display dialog with system list */
374 $this->DivListApplication->parent = $this;
375 $this->DivListApplication->execute();
376 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase);
377 $this->reload();
378 $this->DivListApplication->setEntries($this->applications);
379 return($this->DivListApplication->Draw());
380 }
383 /* Return departments, that will be included within snapshot detection */
384 function get_used_snapshot_bases(){
385 if($this->DivListApplication->selectedRelease == "main"){
386 return(array($this->DivListApplication->selectedBase));
387 }else{
388 return(array($this->DivListApplication->selectedRelease));
389 }
390 }
393 function reload()
394 {
395 $this->applications= array();
397 /* Set base for all searches */
398 $base = $this->DivListApplication->selectedBase;
399 $release = $this->DivListApplication->selectedRelease;
400 $Regex = $this->DivListApplication->Regex;
401 $SubSearch = $this->DivListApplication->SubSearch;
402 $Flags = GL_NONE | GL_SIZELIMIT;
403 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
404 $tmp = array();
405 $Releases = $this->getReleases($base);
407 if(!$this->enableReleaseManagement){
408 $use_base = $base;
409 }else{
410 if(isset($Releases[$release])){
411 $use_base = $release;
412 }else{
413 $use_base = $base;
414 }
415 }
417 if($SubSearch){
418 $Flags |= GL_SUBSEARCH;
419 }
421 $res= get_list($Filter, $this->ui->subtreeACL,$use_base, array("cn","description","dn","objectClass"), $Flags);
422 foreach ($res as $val){
423 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
424 }
426 ksort($tmp);
427 $this->applications=array();
428 foreach($tmp as $val){
429 $this->applications[]=$val;
430 }
431 reset ($this->applications);
432 }
434 function remove_from_parent()
435 {
436 /* Optionally execute a command after we're done */
437 $this->postremove();
438 }
441 function copyPasteHandling($s_action,$s_entry)
442 {
443 /* Paste copied/cutted object in here
444 */
445 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
446 $this->CopyPasteHandler->save_object();
447 $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
449 if($str = $this->CopyPasteHandler->execute()) {
450 return($str);
451 }
452 }
454 /* Copy current object to CopyHandler
455 */
456 if($s_action == "copy"){
457 $this->CopyPasteHandler->Clear();
458 $dn = $this->applications[$s_entry]['dn'];
459 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
460 $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new");
461 $this->CopyPasteHandler->Copy($obj,$objNew);
462 }
464 /* Copy current object to CopyHandler
465 */
466 if($s_action == "cut"){
467 $this->CopyPasteHandler->Clear();
468 $dn = $this->applications[$s_entry]['dn'];
469 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
470 $this->CopyPasteHandler->Cut($obj);
471 }
472 }
474 /* Save to LDAP */
475 function save()
476 {
477 /* Optionally execute a command after we're done */
478 $this->postcreate();
479 }
481 function remove_lock()
482 {
483 if (isset($this->apptabs->dn)){
484 del_lock ($this->apptabs->dn);
485 }
486 }
488 function save_object() {
489 $this->DivListApplication->save_object();
490 }
492 function check() {}
493 function adapt_from_template($dn) {}
494 function password_change_needed() {}
495 function show_header($button_text, $text, $disabled= FALSE) {}
496 }
497 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
498 ?>