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 $enableReleaseManagement = false;
36 function IsReleaseManagementActivated()
37 {
38 /* Check if we should enable the release selection */
39 $tmp = search_config($this->config->data,"faiManagement","CLASS");
40 if(!empty($tmp)){
41 return(true);
42 }
43 return(false);
44 }
46 function applicationManagement ($config, $ui)
47 {
48 /* Save configuration for internal use */
49 $this->config = $config;
50 $this->ui = $ui;
52 /* Check if copy & paste is activated */
53 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
54 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
55 }
57 /* Creat dialog object */
58 $this->DivListApplication = new divListApplication($this->config,$this);
60 if($this->IsReleaseManagementActivated()){
61 /* Check if we should enable the release selection */
62 $this->enableReleaseManagement = true;
64 /* Hide SubSearch checkbox */
65 $this->DivListApplication->DisableCheckBox("SubSearch");
66 }
67 }
69 function getReleases($base)
70 {
71 $ldap = $this->config->get_ldap_link();
72 $dn = "ou=apps,".$base;
73 $ret = array();
74 $ret ["ou=apps,".$base] = "/";
76 $ldap->cd($dn);
77 $ldap->search("objectClass=organizationalUnit",array("ou"));
79 while($attrs = $ldap->fetch()){
80 $str = str_replace($dn,"",$attrs['dn']);
81 $tmp = array_reverse( split("ou=",$str));
82 $str = "";
83 foreach($tmp as $val){
84 $val = trim(preg_replace("/,/","",$val));
85 if(empty($val)) break;
86 $str .= "/".$val;
87 }
88 if(!empty($str)){
89 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
90 }
91 }
92 asort($ret);
93 return($ret);
94 }
96 function execute()
97 {
98 /* Call parent execute */
99 plugin::execute();
102 /****************
103 Variable init
104 ****************/
106 /* These vars will be stored if you try to open a locked app,
107 to be able to perform your last requests after showing a warning message */
108 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/");
110 $smarty = get_smarty(); // Smarty instance
111 $s_action = ""; // Contains the action to proceed
112 $s_entry = ""; // The value for s_action
113 $base_back = ""; // The Link for Backbutton
115 /* Test Posts */
116 foreach($_POST as $key => $val){
117 // Post for delete
118 if(preg_match("/appl_del.*/",$key)){
119 $s_action = "del";
120 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
121 // Post for edit
122 }elseif(preg_match("/appl_edit_.*/",$key)){
123 $s_action="edit";
124 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
125 // Post for new
126 }elseif(preg_match("/^copy_.*/",$key)){
127 $s_action="copy";
128 $s_entry = preg_replace("/^copy_/i","",$key);
129 }elseif(preg_match("/^cut_.*/",$key)){
130 $s_action="cut";
131 $s_entry = preg_replace("/^cut_/i","",$key);
132 // Post for new
133 }elseif(preg_match("/^appl_new.*/",$key)){
134 $s_action="new";
135 }elseif(preg_match("/^remove_multiple_applications/",$key)){
136 $s_action="del_multiple";
137 }elseif(preg_match("/^editPaste.*/i",$key)){
138 $s_action="editPaste";
139 }
140 }
142 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
143 $s_action ="edit";
144 $s_entry = $_GET['id'];
145 }
147 $s_entry = preg_replace("/_.$/","",$s_entry);
150 /****************
151 Copy & Paste handling
152 ****************/
154 /* Only perform copy / paste if it is enabled
155 */
156 if($this->CopyPasteHandler){
157 if($str = $this->copyPasteHandling($s_action,$s_entry)){
158 return($str);
159 };
160 }
163 /****************
164 Create a new app
165 ****************/
167 /* New application? */
168 if ($s_action=="new"){
170 /* By default we set 'dn' to 'new', all relevant plugins will
171 react on this. */
172 $this->dn= "new";
174 /* Create new usertab object */
175 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
176 $this->apptabs->set_acl_base($this->DivListApplication->selectedBase);
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 /* Register apptabs to trigger edit dialog */
254 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
255 $this->apptabs->set_acl_base($this->dn);
256 $_SESSION['objectinfo']= $this->dn;
257 }
261 /********************
262 Delete MULTIPLE entries requested, display confirm dialog
263 ********************/
265 if ($s_action=="del_multiple"){
266 $ids = $this->list_get_selected_items();
268 if(count($ids)){
270 foreach($ids as $id){
271 $dn = $this->applications[$id]['dn'];
272 if (($user= get_lock($dn)) != ""){
273 return(gen_locked_message ($user, $dn));
274 }
275 $this->dns[$id] = $dn;
276 }
278 $dns_names = "<br><pre>";
279 foreach($this->dns as $dn){
280 add_lock ($dn, $this->ui->dn);
281 $dns_names .= $dn."\n";
282 }
283 $dns_names .="</pre>";
285 /* Lock the current entry, so nobody will edit it during deletion */
286 if (count($this->dns) == 1){
287 $smarty->assign("intro", sprintf(_("You're about to delete the following entry %s"), @LDAP::fix($dns_names)));
288 } else {
289 $smarty->assign("intro", sprintf(_("You're about to delete the following entries %s"), @LDAP::fix($dns_names)));
290 }
291 $smarty->assign("multiple", true);
292 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
293 }
294 }
297 /********************
298 Delete MULTIPLE entries confirmed
299 ********************/
301 /* Confirmation for deletion has been passed. Users should be deleted. */
302 if (isset($_POST['delete_multiple_application_confirm'])){
304 /* Remove user by user and check acls before removeing them */
305 foreach($this->dns as $key => $dn){
307 $ui = get_userinfo();
308 $acl = $ui->get_permissions($dn ,"application/application");
309 if (preg_match('/d/', $acl)){
311 /* Delete request is permitted, perform LDAP action */
312 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
313 $this->apptabs->set_acl_base($dn);
314 $this->apptabs->delete ();
315 gosa_log ("Application object'".$dn."' has been removed");
316 unset ($this->apptabs);
317 $this->apptabs= NULL;
319 } else {
320 /* Normally this shouldn't be reached, send some extra
321 logs to notify the administrator */
322 print_red (_("You are not allowed to delete this application!"));
323 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
324 }
325 /* Remove lock file after successfull deletion */
326 del_lock ($dn);
327 unset($this->dns[$key]);
328 }
329 }
332 /********************
333 Delete MULTIPLE entries Canceled
334 ********************/
336 /* Remove lock */
337 if(isset($_POST['delete_multiple_application_cancel'])){
338 foreach($this->dns as $key => $dn){
339 del_lock ($dn);
340 unset($this->dns[$key]);
341 }
342 }
344 /****************
345 Delete app
346 ****************/
348 /* Remove user was requested */
349 if ($s_action == "del"){
351 /* Get 'dn' from posted 'uid' */
352 $this->dn= $this->applications[$s_entry]['dn'];
354 /* Load permissions for selected 'dn' and check if
355 we're allowed to remove this 'dn' */
356 $ui = get_userinfo();
357 $acl = $ui->get_permissions($this->dn ,"application/application");
359 if(preg_match("/d/",$acl)){
360 /* Check locking, save current plugin in 'back_plugin', so
361 the dialog knows where to return. */
362 if (($user= get_lock($this->dn)) != ""){
363 return (gen_locked_message ($user, $this->dn));
364 }
366 /* Lock the current entry, so nobody will edit it during deletion */
367 add_lock ($this->dn, $this->ui->dn);
368 $smarty= get_smarty();
369 $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), @LDAP::fix($this->dn)));
370 $smarty->assign("multiple", false);
371 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
372 } else {
374 /* Obviously the user isn't allowed to delete. Show message and
375 clean session. */
376 print_red (_("You are not allowed to delete this application!"));
377 }
378 }
381 /****************
382 Delete app confirmed
383 ****************/
385 /* Confirmation for deletion has been passed. Group should be deleted. */
386 if (isset($_POST['delete_app_confirm'])){
388 /* Some nice guy may send this as POST, so we've to check
389 for the permissions again. */
390 $ui = get_userinfo();
391 $acl = $ui->get_permissions($this->dn ,"application/application");
393 if(preg_match("/d/",$acl)){
395 /* Delete request is permitted, perform LDAP action */
396 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
397 $this->apptabs->set_acl_base($this->dn);
398 $this->apptabs->delete ();
399 gosa_log ("Application object'".$this->dn."' has been removed");
400 unset ($this->apptabs);
401 $this->apptabs= NULL;
403 } else {
405 /* Normally this shouldn't be reached, send some extra
406 logs to notify the administrator */
407 print_red (_("You are not allowed to delete this application!"));
408 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
409 }
411 /* Remove lock file after successfull deletion */
412 del_lock ($this->dn);
413 }
416 /****************
417 Delete app canceled
418 ****************/
420 /* Delete application canceled? */
421 if (isset($_POST['delete_cancel'])){
422 del_lock ($this->dn);
423 unset($_SESSION['objectinfo']);
424 }
426 /* Show tab dialog if object is present */
427 if (($this->apptabs) && (isset($this->apptabs->config))){
428 $display= $this->apptabs->execute();
430 /* Don't show buttons if tab dialog requests this */
431 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
432 $display.= "<p style=\"text-align:right\">\n";
433 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\""._("Ok")."\">\n";
434 $display.= " \n";
435 if ($this->dn != "new"){
436 $display.= "<input type=submit name=\"edit_apply\" value=\""._("Apply")."\">\n";
437 $display.= " \n";
438 }
439 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
440 $display.= "</p>";
441 }
442 return ($display);
443 }
446 /****************
447 Dialog display
448 ****************/
450 /* Check if there is a snapshot dialog open */
451 $base = $this->DivListApplication->selectedBase;
452 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases($base))){
453 return($str);
454 }
456 /* Display dialog with system list */
457 $this->DivListApplication->parent = $this;
458 $this->DivListApplication->execute();
459 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
460 $this->reload();
461 $this->DivListApplication->setEntries($this->applications);
462 return($this->DivListApplication->Draw());
463 }
466 /* Return departments, that will be included within snapshot detection */
467 function get_used_snapshot_bases(){
468 if($this->DivListApplication->selectedRelease == "main"){
469 return(array($this->DivListApplication->selectedBase));
470 }else{
471 return(array($this->DivListApplication->selectedRelease));
472 }
473 }
476 function reload()
477 {
478 $this->applications= array();
480 /* Set base for all searches */
481 $base = $this->DivListApplication->selectedBase;
482 $release = $this->DivListApplication->selectedRelease;
483 $Regex = $this->DivListApplication->Regex;
484 $SubSearch = $this->DivListApplication->SubSearch;
485 $Flags = GL_NONE | GL_SIZELIMIT;
486 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
487 $tmp = array();
488 $Releases = $this->getReleases($base);
490 if(!$this->enableReleaseManagement){
491 $use_base = "ou=apps,".$base;
492 }else{
493 if(isset($Releases[$release])){
494 $use_base = $release;
495 }else{
496 $use_base = "ou=apps,".$base;
497 }
498 }
500 if($SubSearch){
501 $Flags |= GL_SUBSEARCH;
502 }
504 $res= get_list($Filter, "application", $use_base, array("cn","description","dn","objectClass"), $Flags);
505 foreach ($res as $val){
506 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
507 }
509 ksort($tmp);
510 $this->applications=array();
511 foreach($tmp as $val){
512 $this->applications[]=$val;
513 }
514 reset ($this->applications);
515 }
517 function remove_from_parent()
518 {
519 /* Optionally execute a command after we're done */
520 $this->postremove();
521 }
524 function copyPasteHandling($s_action,$s_entry)
525 {
526 /* Paste copied/cutted object in here
527 */
528 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
529 $this->CopyPasteHandler->save_object();
530 $this->CopyPasteHandler->SetVar("base", $this->DivListApplication->selectedBase);
532 if($str = $this->CopyPasteHandler->execute()) {
533 return($str);
534 }
535 }
537 /* Copy current object to CopyHandler
538 */
539 if($s_action == "copy"){
540 $this->CopyPasteHandler->Clear();
541 $dn = $this->applications[$s_entry]['dn'];
543 /* Check Acls */
544 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
545 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
546 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
547 $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new","application");
548 $this->CopyPasteHandler->Copy($obj,$objNew);
549 }
550 }
552 /* Copy current object to CopyHandler
553 */
554 if($s_action == "cut"){
555 $this->CopyPasteHandler->Clear();
556 $dn = $this->applications[$s_entry]['dn'];
558 /* Check Acls */
559 $acl_all= $this->ui->has_complete_category_acls($dn,"application");
560 if(preg_match("/(c.*w|w.*c)/",$acl_all)){
561 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn, "application");
562 $this->CopyPasteHandler->Cut($obj);
563 }
564 }
565 }
567 function list_get_selected_items()
568 {
569 $ids = array();
570 foreach($_POST as $name => $value){
571 if(preg_match("/^item_selected_[0-9]*$/",$name)){
572 $id = preg_replace("/^item_selected_/","",$name);
573 $ids[$id] = $id;
574 }
575 }
576 return($ids);
577 }
580 /* Save to LDAP */
581 function save()
582 {
583 /* Optionally execute a command after we're done */
584 $this->postcreate();
585 }
587 function remove_lock()
588 {
589 if (isset($this->apptabs->dn)){
590 del_lock ($this->apptabs->dn);
591 }
592 }
594 function save_object() {
595 $this->DivListApplication->save_object();
596 }
598 function check() {}
599 function adapt_from_template($dn) {}
600 function password_change_needed() {}
601 }
602 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
603 ?>