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 class applicationManagement extends plugin
22 {
23 /* Definitions */
24 var $plHeadline = "Applications";
25 var $plDescription = "This does something";
26 var $plIcon = "plugins/goto/images/application.png";
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;
35 var $start_pasting_copied_objects = FALSE;
37 var $app_base ="";
38 var $app_release ="";
39 var $acl_module = array("application");
41 var $dns = array();
43 function IsReleaseManagementActivated()
44 {
45 /* Check if we should enable the release selection */
46 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
47 if(!empty($tmp)){
48 return(true);
49 }
50 return(false);
51 }
54 function applicationManagement (&$config, &$ui)
55 {
56 /* Save configuration for internal use */
57 $this->config = &$config;
58 $this->ui = &$ui;
60 /* Check if copy & paste is activated */
61 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
62 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
63 }
65 /* Creat dialog object */
66 $this->DivListApplication = new divListApplication($this->config,$this);
68 if($this->IsReleaseManagementActivated()){
69 /* Check if we should enable the release selection */
70 $this->enableReleaseManagement = true;
72 /* Hide SubSearch checkbox */
73 $this->DivListApplication->DisableCheckBox("SubSearch");
74 }
76 /* Set default release */
77 if(!$this->IsReleaseManagementActivated()){
78 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
79 if(!session::is_set("app_filter")){
80 session::set("app_filter",array("app_base" => $this->app_base));
81 }
82 $app_filter = session::get("app_filter");
83 $this->app_base = $app_filter['app_base'];
84 }else{
85 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
86 if(!session::is_set("app_filter")){
87 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
88 }
89 $app_filter = session::get("app_filter");
90 $this->app_base = $app_filter['app_base'];
91 $this->app_release = $app_filter['app_release'];
92 }
93 }
96 function getReleases()
97 {
98 $ldap = $this->config->get_ldap_link();
99 $ret = array();
100 $base = $this->app_base;
102 $ret[$this->app_base] = "/";
103 $ldap->cd($base);
104 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
105 while($attrs = $ldap->fetch()){
106 $str = str_replace($base,"",$attrs['dn']);
107 $tmp = array_reverse( split("ou=",$str));
108 $str = "";
109 foreach($tmp as $val){
110 $val = trim(preg_replace("/,/","",$val));
111 if(empty($val)) break;
112 $str .= "/".$val;
113 }
114 if(!empty($str)){
115 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
116 }
117 }
118 asort($ret);
119 return($ret);
120 }
122 function execute()
123 {
124 /* Call parent execute */
125 plugin::execute();
128 /****************
129 Variable init
130 ****************/
132 /* These vars will be stored if you try to open a locked app,
133 to be able to perform your last requests after showing a warning message */
134 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/","/^menu_action/"));
136 $smarty = get_smarty(); // Smarty instance
137 $s_action = ""; // Contains the action to proceed
138 $s_entry = ""; // The value for s_action
139 $base_back = ""; // The Link for Backbutton
141 /* Test Posts */
142 foreach($_POST as $key => $val){
143 // Post for delete
144 if(preg_match("/appl_del.*/",$key)){
145 $s_action = "del";
146 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
147 // Post for edit
148 }elseif(preg_match("/appl_edit_.*/",$key)){
149 $s_action="edit";
150 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
151 // Post for new
152 }elseif(preg_match("/^copy_.*/",$key)){
153 $s_action="copy";
154 $s_entry = preg_replace("/^copy_/i","",$key);
155 }elseif(preg_match("/^cut_.*/",$key)){
156 $s_action="cut";
157 $s_entry = preg_replace("/^cut_/i","",$key);
158 // Post for new
159 }elseif(preg_match("/^appl_new.*/",$key)){
160 $s_action="new";
161 }elseif(preg_match("/^remove_multiple_applications/",$key)){
162 $s_action="del_multiple";
163 }elseif(preg_match("/^editPaste.*/i",$key)){
164 $s_action="editPaste";
165 }elseif(preg_match("/^multiple_copy_groups/",$key)){
166 $s_action = "copy_multiple";
167 }elseif(preg_match("/^multiple_cut_groups/",$key)){
168 $s_action = "cut_multiple";
169 }
170 }
172 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
173 $s_action ="edit";
174 $s_entry = $_GET['id'];
175 }
177 $s_entry = preg_replace("/_.$/","",$s_entry);
180 /* handle C&P from layers menu */
181 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
182 $s_action = "copy_multiple";
183 }
184 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
185 $s_action = "cut_multiple";
186 }
187 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
188 $s_action = "editPaste";
189 }
191 /* Create options */
192 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
193 $s_action = "new";
194 }
196 /* handle remove from layers menu */
197 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
198 $s_action = "del_multiple";
199 }
201 /****************
202 Copy & Paste handling
203 ****************/
205 /* Display the copy & paste dialog, if it is currently open */
206 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
207 if($ret){
208 return($ret);
209 }
211 /****************
212 Create a new app
213 ****************/
215 /* New application? */
216 if ($s_action=="new"){
218 /* By default we set 'dn' to 'new', all relevant plugins will
219 react on this. */
220 $this->dn= "new";
222 /* Create new usertab object */
223 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
224 $this->apptabs->parent = &$this;
225 $this->apptabs->set_acl_base($this->app_base);
226 }
229 /****************
230 Edit entry canceled
231 ****************/
233 /* Cancel dialogs */
234 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
235 $this->remove_lock();
236 $this->apptabs= NULL;
237 session::un_set('objectinfo');
238 }
241 /****************
242 Edit entry finished
243 ****************/
245 /* Finish apps edit is triggered by the tabulator dialog, so
246 the user wants to save edited data. Check and save at this
247 point. */
248 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
250 /* Check tabs, will feed message array */
251 $this->apptabs->last= $this->apptabs->current;
252 $this->apptabs->save_object();
253 $message= $this->apptabs->check();
255 /* Save, or display error message? */
256 if (count($message) == 0){
258 /* Save data data to ldap */
259 $this->apptabs->save();
261 if (!isset($_POST['edit_apply'])){
262 /* Application has been saved successfully, remove lock from
263 LDAP. */
264 if ($this->dn != "new"){
265 $this->remove_lock();
266 }
267 $this->apptabs= NULL;
268 session::un_set('objectinfo');
269 }else{
271 /* Reinitialize tab */
272 if($this->apptabs instanceof tabs){
273 $this->apptabs->re_init();
274 }
275 }
276 } else {
277 /* Ok. There seem to be errors regarding to the tab data,
278 show message and continue as usual. */
279 msg_dialog::displayChecks($message);
280 }
281 }
284 /****************
285 Edit entry
286 ****************/
288 /* User wants to edit data? */
289 if (($s_action=="edit") && (!isset($this->apptabs->config))){
291 /* Get 'dn' from posted 'applist', must be unique */
292 $this->dn= $this->applications[$s_entry]['dn'];
294 /* Check locking, save current plugin in 'back_plugin', so
295 the dialog knows where to return. */
296 if (($user= get_lock($this->dn)) != ""){
297 return(gen_locked_message ($user, $this->dn));
298 }
300 /* Lock the current entry, so everyone will get the
301 above dialog */
302 add_lock ($this->dn, $this->ui->dn);
304 /* Register apptabs to trigger edit dialog */
305 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
306 if($this->IsReleaseManagementActivated()){
307 $this->apptabs->set_FAIstate($this->applications[$s_entry]['FAIstate'][0]);
308 }
309 $this->apptabs->parent = &$this;
310 $this->apptabs->set_acl_base($this->dn);
311 session::set('objectinfo',$this->dn);
312 }
316 /********************
317 Delete MULTIPLE entries requested, display confirm dialog
318 ********************/
320 if ($s_action=="del_multiple"){
321 $ids = $this->list_get_selected_items();
323 if(count($ids)){
324 $this->dns = array();
326 $disallowed = array();
327 foreach($ids as $id){
328 $dn = $this->applications[$id]['dn'];
329 $acl = $this->ui->get_permissions($dn, "application/application");
330 if(preg_match("/d/",$acl)){
331 $this->dns[$id] = $dn;
332 }else{
333 $disallowed[] = $dn;
334 }
335 }
337 if(count($disallowed)){
338 msg_dialog::display(_("Permission"),msgPool::permDelete($disallowed),INFO_DIALOG);
339 }
341 if(count($this->dns)){
343 if ($user= get_multiple_locks($this->dns)){
344 return(gen_locked_message($user,$this->dns));
345 }
347 $dns_names = array();
348 foreach($this->dns as $dn){
349 add_lock ($dn, $this->ui->dn);
350 $dns_names[] =@LDAP::fix($dn);
351 }
353 /* Lock the current entry, so nobody will edit it during deletion */
354 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
355 $smarty->assign("multiple", true);
356 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
357 }
358 }
359 }
362 /********************
363 Delete MULTIPLE entries confirmed
364 ********************/
366 /* Confirmation for deletion has been passed. Users should be deleted. */
367 if (isset($_POST['delete_multiple_application_confirm'])){
369 /* Remove user by user and check acls before removeing them */
370 foreach($this->dns as $key => $dn){
372 $ui = get_userinfo();
373 $acl = $ui->get_permissions($dn ,"application/application");
374 if (preg_match('/d/', $acl)){
376 /* Delete request is permitted, perform LDAP action */
377 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
378 $this->apptabs->parent = &$this;
379 $this->apptabs->set_acl_base($dn);
380 $this->apptabs->delete ();
381 unset ($this->apptabs);
382 $this->apptabs= NULL;
384 } else {
385 /* Normally this shouldn't be reached, send some extra
386 logs to notify the administrator */
387 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
388 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
389 }
390 }
392 /* Remove lock file after successfull deletion */
393 $this->remove_lock();
394 $this->dns = array();
395 }
398 /********************
399 Delete MULTIPLE entries Canceled
400 ********************/
402 /* Remove lock */
403 if(isset($_POST['delete_multiple_application_cancel'])){
405 /* Remove lock file after successfull deletion */
406 $this->remove_lock();
407 $this->dns = array();
408 }
410 /****************
411 Delete app
412 ****************/
414 /* Remove user was requested */
415 if ($s_action == "del"){
417 /* Get 'dn' from posted 'uid' */
418 $this->dn= $this->applications[$s_entry]['dn'];
420 /* Load permissions for selected 'dn' and check if
421 we're allowed to remove this 'dn' */
422 $ui = get_userinfo();
423 $acl = $ui->get_permissions($this->dn ,"application/application");
425 if(preg_match("/d/",$acl)){
426 /* Check locking, save current plugin in 'back_plugin', so
427 the dialog knows where to return. */
428 if (($user= get_lock($this->dn)) != ""){
429 return (gen_locked_message ($user, $this->dn));
430 }
432 /* Lock the current entry, so nobody will edit it during deletion */
433 add_lock ($this->dn, $this->ui->dn);
434 $smarty= get_smarty();
435 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
436 $smarty->assign("multiple", false);
437 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
438 } else {
440 /* Obviously the user isn't allowed to delete. Show message and
441 clean session. */
442 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
443 }
444 }
447 /****************
448 Delete app confirmed
449 ****************/
451 /* Confirmation for deletion has been passed. Group should be deleted. */
452 if (isset($_POST['delete_app_confirm'])){
454 /* Some nice guy may send this as POST, so we've to check
455 for the permissions again. */
456 $ui = get_userinfo();
457 $acl = $ui->get_permissions($this->dn ,"application/application");
459 if(preg_match("/d/",$acl)){
461 /* Delete request is permitted, perform LDAP action */
462 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
463 $this->apptabs->parent = &$this;
464 $this->apptabs->set_acl_base($this->dn);
465 $this->apptabs->delete ();
466 unset ($this->apptabs);
467 $this->apptabs= NULL;
469 } else {
471 /* Normally this shouldn't be reached, send some extra
472 logs to notify the administrator */
473 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
474 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
475 }
477 /* Remove lock file after successfull deletion */
478 $this->remove_lock();
479 }
482 /****************
483 Delete app canceled
484 ****************/
486 /* Delete application canceled? */
487 if (isset($_POST['delete_cancel'])){
488 $this->remove_lock();
489 session::un_set('objectinfo');
490 }
492 /* Show tab dialog if object is present */
493 if (($this->apptabs) && (isset($this->apptabs->config))){
494 $display= $this->apptabs->execute();
496 /* Don't show buttons if tab dialog requests this */
497 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
498 $display.= "<p style=\"text-align:right\">\n";
500 if(isset($this->apptabs->FAIstate) && !preg_match("/freeze/i",$this->apptabs->FAIstate)){
501 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
502 $display.= " \n";
503 if ($this->dn != "new"){
504 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
505 $display.= " \n";
506 }
507 }
508 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
509 $display.= "</p>";
510 }
511 return ($display);
512 }
515 /****************
516 Dialog display
517 ****************/
519 /* Check if there is a snapshot dialog open */
520 if($this->IsReleaseManagementActivated()){
521 $base = $this->app_release;
522 }else{
523 $base = $this->app_base;
524 }
525 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases(),$this)){
526 return($str);
527 }
529 /* Display dialog with system list */
530 $this->DivListApplication->parent = $this;
531 $this->DivListApplication->execute();
532 if(!$this->IsReleaseManagementActivated()){
533 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
534 }
535 $this->reload();
536 $this->DivListApplication->setEntries($this->applications);
537 return($this->DivListApplication->Draw());
538 }
541 /* Return departments, that will be included within snapshot detection */
542 function get_used_snapshot_bases()
543 {
544 if($this->IsReleaseManagementActivated()){
545 return(array($this->app_release));
546 }else{
547 return(array($this->app_base));
548 }
549 }
552 function reload()
553 {
554 $this->applications= array();
556 /* Set base for all searches */
557 $Regex = $this->DivListApplication->Regex;
558 $SubSearch = $this->DivListApplication->SubSearch;
559 $Flags = GL_NONE | GL_SIZELIMIT;
560 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
561 $tmp = array();
563 if(!$this->IsReleaseManagementActivated()){
564 $use_base = $this->app_base;
565 if($SubSearch){
566 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
567 }
568 }else{
569 $use_base = $this->app_release;
570 $SubSearch= FALSE;
571 }
573 /* Add FAIstate to the search attributes */
574 $search_attrs = array("cn","description","dn","objectClass");
575 if($this->IsReleaseManagementActivated()) {
576 $search_attrs[] = "FAIstate";
577 }
579 if($SubSearch){
580 $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, $search_attrs, $Flags);
581 }else{
582 $res= get_list($Filter, "application",$use_base, $search_attrs, $Flags);
583 }
584 $tmp2 = array();
585 foreach ($res as $val){
586 if(!isset($val['FAIstate'])){
587 $val['FAIstate'][0] = "";
588 }
589 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
590 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
591 }
593 natcasesort($tmp2);
594 $this->applications=array();
595 foreach($tmp2 as $val){
596 $this->applications[]=$tmp[$val];
597 }
598 reset ($this->applications);
599 }
601 function remove_from_parent()
602 {
603 /* Optionally execute a command after we're done */
604 $this->postremove();
605 }
608 function copyPasteHandling_from_queue($s_action,$s_entry)
609 {
610 /* Check if Copy & Paste is disabled */
611 if(!is_object($this->CopyPasteHandler)){
612 return("");
613 }
615 $ui = get_userinfo();
617 /* Add a single entry to queue */
618 if($s_action == "cut" || $s_action == "copy"){
620 /* Cleanup object queue */
621 $this->CopyPasteHandler->cleanup_queue();
622 $dn = $this->applications[$s_entry]['dn'];
623 if($s_action == "copy" && $ui->is_copyable($dn,"application","application")){
624 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
625 }
626 if($s_action == "cut" && $ui->is_cutable($dn,"application","application")){
627 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
628 }
629 }
632 /* Add entries to queue */
633 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
635 /* Cleanup object queue */
636 $this->CopyPasteHandler->cleanup_queue();
638 /* Add new entries to CP queue */
639 foreach($this->list_get_selected_items() as $id){
640 $dn = $this->applications[$id]['dn'];
642 if($s_action == "copy_multiple" && $ui->is_copyable($dn,"application","application")){
643 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
644 }
645 if($s_action == "cut_multiple" && $ui->is_cutable($dn,"application","application")){
646 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
647 }
648 }
649 }
651 /* Start pasting entries */
652 if($s_action == "editPaste"){
653 $this->start_pasting_copied_objects = TRUE;
654 }
657 /* Return C&P dialog */
658 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
661 /* Get dialog */
662 $this->CopyPasteHandler->SetVar("base",$this->app_base);
663 $this->CopyPasteHandler->SetVar("parent",$this);
664 $data = $this->CopyPasteHandler->execute();
666 /* Return dialog data */
667 if(!empty($data)){
668 return($data);
669 }
670 }
672 /* Automatically disable status for pasting */
673 if(!$this->CopyPasteHandler->entries_queued()){
674 $this->start_pasting_copied_objects = FALSE;
675 }
676 return("");
677 }
680 function list_get_selected_items()
681 {
682 $ids = array();
683 foreach($_POST as $name => $value){
684 if(preg_match("/^item_selected_[0-9]*$/",$name)){
685 $id = preg_replace("/^item_selected_/","",$name);
686 $ids[$id] = $id;
687 }
688 }
689 return($ids);
690 }
693 /* Save to LDAP */
694 function save()
695 {
696 /* Optionally execute a command after we're done */
697 $this->postcreate();
698 }
701 function remove_lock()
702 {
703 if (isset($this->apptabs->dn)){
704 del_lock ($this->apptabs->dn);
705 }
706 if(isset($this->dn) && !empty($this->dn) && $this->dn != "new"){
707 del_lock($this->dn);
708 }
709 if(isset($this->dns) && is_array($this->dns) && count($this->dns)){
710 del_lock($this->dns);
711 }
712 }
715 function save_object()
716 {
717 $this->DivListApplication->save_object();
718 if(is_object($this->CopyPasteHandler)){
719 $this->CopyPasteHandler->save_object();
720 }
722 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
723 $sel_rel = get_post('app_release');
724 $releases = array_flip($this->getReleases());
725 if(isset($releases[$sel_rel])){
726 $this->app_release = $releases[$sel_rel];
727 }
728 $app_filter = session::get("app_filter");
729 $app_filter['app_release'] = $this->app_release;
730 session::set("app_filter",$app_filter);
731 }elseif(!$this->IsReleaseManagementActivated()){
732 $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
733 $app_filter = session::get("app_filter");
734 $app_filter['app_base'] = $this->app_base;
735 session::set("app_filter",$app_filter);
736 }
737 }
739 function check() {}
740 function adapt_from_template($dn, $skip= array()) {}
741 function password_change_needed() {}
742 }
743 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
744 ?>