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";
27 /* Dialog attributes */
28 var $apptabs = NULL;
29 var $ui = NULL;
30 var $CopyPasteHandler = NULL;
31 var $DivListApplication = NULL;
32 var $applications = array();
33 var $enableReleaseManagement = false;
34 var $start_pasting_copied_objects = FALSE;
36 var $app_base ="";
37 var $app_release ="";
39 function IsReleaseManagementActivated()
40 {
41 /* Check if we should enable the release selection */
42 $tmp = $this->config->search("faiManagement", "CLASS",array('menu','tabs'));
43 if(!empty($tmp)){
44 return(true);
45 }
46 return(false);
47 }
50 function applicationManagement (&$config, &$ui)
51 {
52 /* Save configuration for internal use */
53 $this->config = &$config;
54 $this->ui = &$ui;
56 /* Check if copy & paste is activated */
57 if($this->config->boolValueIsTrue("MAIN","ENABLECOPYPASTE")){
58 $this->CopyPasteHandler = new CopyPasteHandler($this->config);
59 }
61 /* Creat dialog object */
62 $this->DivListApplication = new divListApplication($this->config,$this);
64 if($this->IsReleaseManagementActivated()){
65 /* Check if we should enable the release selection */
66 $this->enableReleaseManagement = true;
68 /* Hide SubSearch checkbox */
69 $this->DivListApplication->DisableCheckBox("SubSearch");
70 }
72 /* Set default release */
73 if(!$this->IsReleaseManagementActivated()){
74 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
75 if(!session::is_set("app_filter")){
76 session::set("app_filter",array("app_base" => $this->app_base));
77 }
78 $app_filter = session::get("app_filter");
79 $this->app_base = $app_filter['app_base'];
80 }else{
81 $this->app_base = get_ou("applicationou").$this->config->current['BASE'];
82 if(!session::is_set("app_filter")){
83 session::set("app_filter",array("app_base" => $this->app_base,"app_release" => $this->app_base));
84 }
85 $app_filter = session::get("app_filter");
86 $this->app_base = $app_filter['app_base'];
87 $this->app_release = $app_filter['app_release'];
88 }
89 }
92 function getReleases()
93 {
94 $ldap = $this->config->get_ldap_link();
95 $ret = array();
96 $base = $this->app_base;
98 $ret[$this->app_base] = "/";
99 $ldap->cd($base);
100 $ldap->search("(&(objectClass=organizationalUnit)(objectClass=FAIbranch))",array("ou"));
101 while($attrs = $ldap->fetch()){
102 $str = str_replace($base,"",$attrs['dn']);
103 $tmp = array_reverse( split("ou=",$str));
104 $str = "";
105 foreach($tmp as $val){
106 $val = trim(preg_replace("/,/","",$val));
107 if(empty($val)) break;
108 $str .= "/".$val;
109 }
110 if(!empty($str)){
111 $ret[$attrs['dn']]= preg_replace("/^\//","",$str);
112 }
113 }
114 asort($ret);
115 return($ret);
116 }
118 function execute()
119 {
120 /* Call parent execute */
121 plugin::execute();
124 /****************
125 Variable init
126 ****************/
128 /* These vars will be stored if you try to open a locked app,
129 to be able to perform your last requests after showing a warning message */
130 session::set('LOCK_VARS_TO_USE',array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/","/^item_selected/","/^remove_multiple_applications/"));
132 $smarty = get_smarty(); // Smarty instance
133 $s_action = ""; // Contains the action to proceed
134 $s_entry = ""; // The value for s_action
135 $base_back = ""; // The Link for Backbutton
137 /* Test Posts */
138 foreach($_POST as $key => $val){
139 // Post for delete
140 if(preg_match("/appl_del.*/",$key)){
141 $s_action = "del";
142 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
143 // Post for edit
144 }elseif(preg_match("/appl_edit_.*/",$key)){
145 $s_action="edit";
146 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
147 // Post for new
148 }elseif(preg_match("/^copy_.*/",$key)){
149 $s_action="copy";
150 $s_entry = preg_replace("/^copy_/i","",$key);
151 }elseif(preg_match("/^cut_.*/",$key)){
152 $s_action="cut";
153 $s_entry = preg_replace("/^cut_/i","",$key);
154 // Post for new
155 }elseif(preg_match("/^appl_new.*/",$key)){
156 $s_action="new";
157 }elseif(preg_match("/^remove_multiple_applications/",$key)){
158 $s_action="del_multiple";
159 }elseif(preg_match("/^editPaste.*/i",$key)){
160 $s_action="editPaste";
161 }elseif(preg_match("/^multiple_copy_groups/",$key)){
162 $s_action = "copy_multiple";
163 }elseif(preg_match("/^multiple_cut_groups/",$key)){
164 $s_action = "cut_multiple";
165 }
166 }
168 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
169 $s_action ="edit";
170 $s_entry = $_GET['id'];
171 }
173 $s_entry = preg_replace("/_.$/","",$s_entry);
176 /* handle C&P from layers menu */
177 if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
178 $s_action = "copy_multiple";
179 }
180 if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
181 $s_action = "cut_multiple";
182 }
183 if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
184 $s_action = "editPaste";
185 }
187 /* Create options */
188 if(isset($_POST['menu_action']) && $_POST['menu_action'] == "appl_new"){
189 $s_action = "new";
190 }
192 /* handle remove from layers menu */
193 if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
194 $s_action = "del_multiple";
195 }
197 /****************
198 Copy & Paste handling
199 ****************/
201 /* Display the copy & paste dialog, if it is currently open */
202 $ret = $this->copyPasteHandling_from_queue($s_action,$s_entry);
203 if($ret){
204 return($ret);
205 }
207 /****************
208 Create a new app
209 ****************/
211 /* New application? */
212 if ($s_action=="new"){
214 /* By default we set 'dn' to 'new', all relevant plugins will
215 react on this. */
216 $this->dn= "new";
218 /* Create new usertab object */
219 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
220 $this->apptabs->parent = &$this;
221 $this->apptabs->set_acl_base($this->app_base);
222 }
225 /****************
226 Edit entry canceled
227 ****************/
229 /* Cancel dialogs */
230 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
231 del_lock ($this->apptabs->dn);
232 unset ($this->apptabs);
233 $this->apptabs= NULL;
234 session::un_set('objectinfo');
235 }
238 /****************
239 Edit entry finished
240 ****************/
242 /* Finish apps edit is triggered by the tabulator dialog, so
243 the user wants to save edited data. Check and save at this
244 point. */
245 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply']) ) && (isset($this->apptabs->config))){
247 /* Check tabs, will feed message array */
248 $this->apptabs->last= $this->apptabs->current;
249 $this->apptabs->save_object();
250 $message= $this->apptabs->check();
252 /* Save, or display error message? */
253 if (count($message) == 0){
255 /* Save data data to ldap */
256 $this->apptabs->save();
258 if (!isset($_POST['edit_apply'])){
259 /* Application has been saved successfully, remove lock from
260 LDAP. */
261 if ($this->dn != "new"){
262 del_lock ($this->dn);
263 }
264 unset ($this->apptabs);
265 $this->apptabs= NULL;
266 session::un_set('objectinfo');
267 }
268 } else {
269 /* Ok. There seem to be errors regarding to the tab data,
270 show message and continue as usual. */
271 msg_dialog::displayChecks($message);
272 }
273 }
276 /****************
277 Edit entry
278 ****************/
280 /* User wants to edit data? */
281 if (($s_action=="edit") && (!isset($this->apptabs->config))){
283 /* Get 'dn' from posted 'applist', must be unique */
284 $this->dn= $this->applications[$s_entry]['dn'];
286 /* Check locking, save current plugin in 'back_plugin', so
287 the dialog knows where to return. */
288 if (($user= get_lock($this->dn)) != ""){
289 return(gen_locked_message ($user, $this->dn));
290 }
292 /* Lock the current entry, so everyone will get the
293 above dialog */
294 add_lock ($this->dn, $this->ui->dn);
296 /* Register apptabs to trigger edit dialog */
297 $this->apptabs= new apptabs($this->config,$this->config->data['TABS']['APPSTABS'], $this->dn,"application");
298 $this->apptabs->parent = &$this;
299 $this->apptabs->set_acl_base($this->dn);
300 session::set('objectinfo',$this->dn);
301 }
305 /********************
306 Delete MULTIPLE entries requested, display confirm dialog
307 ********************/
309 if ($s_action=="del_multiple"){
310 $ids = $this->list_get_selected_items();
312 if(count($ids)){
314 foreach($ids as $id){
315 $dn = $this->applications[$id]['dn'];
316 $this->dns[$id] = $dn;
317 }
318 if ($user= get_multiple_locks($this->dns)){
319 return(gen_locked_message($user,$this->dns));
320 }
322 $dns_names = array();
323 foreach($this->dns as $dn){
324 add_lock ($dn, $this->ui->dn);
325 $dns_names[] =@LDAP::fix($dn);
326 }
328 /* Lock the current entry, so nobody will edit it during deletion */
329 $smarty->assign("intro", msgPool::deleteInfo($dns_names,_("application")));
330 $smarty->assign("multiple", true);
331 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
332 }
333 }
336 /********************
337 Delete MULTIPLE entries confirmed
338 ********************/
340 /* Confirmation for deletion has been passed. Users should be deleted. */
341 if (isset($_POST['delete_multiple_application_confirm'])){
343 /* Remove user by user and check acls before removeing them */
344 foreach($this->dns as $key => $dn){
346 $ui = get_userinfo();
347 $acl = $ui->get_permissions($dn ,"application/application");
348 if (preg_match('/d/', $acl)){
350 /* Delete request is permitted, perform LDAP action */
351 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn,"application");
352 $this->apptabs->parent = &$this;
353 $this->apptabs->set_acl_base($dn);
354 $this->apptabs->delete ();
355 unset ($this->apptabs);
356 $this->apptabs= NULL;
358 } else {
359 /* Normally this shouldn't be reached, send some extra
360 logs to notify the administrator */
361 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
362 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
363 }
364 /* Remove lock file after successfull deletion */
365 del_lock ($dn);
366 unset($this->dns[$key]);
367 }
368 }
371 /********************
372 Delete MULTIPLE entries Canceled
373 ********************/
375 /* Remove lock */
376 if(isset($_POST['delete_multiple_application_cancel'])){
377 foreach($this->dns as $key => $dn){
378 del_lock ($dn);
379 unset($this->dns[$key]);
380 }
381 }
383 /****************
384 Delete app
385 ****************/
387 /* Remove user was requested */
388 if ($s_action == "del"){
390 /* Get 'dn' from posted 'uid' */
391 $this->dn= $this->applications[$s_entry]['dn'];
393 /* Load permissions for selected 'dn' and check if
394 we're allowed to remove this 'dn' */
395 $ui = get_userinfo();
396 $acl = $ui->get_permissions($this->dn ,"application/application");
398 if(preg_match("/d/",$acl)){
399 /* Check locking, save current plugin in 'back_plugin', so
400 the dialog knows where to return. */
401 if (($user= get_lock($this->dn)) != ""){
402 return (gen_locked_message ($user, $this->dn));
403 }
405 /* Lock the current entry, so nobody will edit it during deletion */
406 add_lock ($this->dn, $this->ui->dn);
407 $smarty= get_smarty();
408 $smarty->assign("intro",msgPool::deleteInfo(@LDAP::fix($this->dn),_("application")));
409 $smarty->assign("multiple", false);
410 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
411 } else {
413 /* Obviously the user isn't allowed to delete. Show message and
414 clean session. */
415 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
416 }
417 }
420 /****************
421 Delete app confirmed
422 ****************/
424 /* Confirmation for deletion has been passed. Group should be deleted. */
425 if (isset($_POST['delete_app_confirm'])){
427 /* Some nice guy may send this as POST, so we've to check
428 for the permissions again. */
429 $ui = get_userinfo();
430 $acl = $ui->get_permissions($this->dn ,"application/application");
432 if(preg_match("/d/",$acl)){
434 /* Delete request is permitted, perform LDAP action */
435 $this->apptabs= new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $this->dn,"application");
436 $this->apptabs->parent = &$this;
437 $this->apptabs->set_acl_base($this->dn);
438 $this->apptabs->delete ();
439 unset ($this->apptabs);
440 $this->apptabs= NULL;
442 } else {
444 /* Normally this shouldn't be reached, send some extra
445 logs to notify the administrator */
446 msg_dialog::display(_("Permission error"), msgPool::permDelete(), INFO_DIALOG);
447 new log("security","application/".get_class($this),$dn,array(),"Tried to trick deletion.");
448 }
450 /* Remove lock file after successfull deletion */
451 del_lock ($this->dn);
452 }
455 /****************
456 Delete app canceled
457 ****************/
459 /* Delete application canceled? */
460 if (isset($_POST['delete_cancel'])){
461 del_lock ($this->dn);
462 session::un_set('objectinfo');
463 }
465 /* Show tab dialog if object is present */
466 if (($this->apptabs) && (isset($this->apptabs->config))){
467 $display= $this->apptabs->execute();
469 /* Don't show buttons if tab dialog requests this */
470 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
471 $display.= "<p style=\"text-align:right\">\n";
472 $display.= "<input type=\"submit\" name=\"edit_finish\" style=\"width:80px\" value=\"".msgPool::okButton()."\">\n";
473 $display.= " \n";
474 if ($this->dn != "new"){
475 $display.= "<input type=submit name=\"edit_apply\" value=\"".msgPool::applyButton()."\">\n";
476 $display.= " \n";
477 }
478 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\"".msgPool::cancelButton()."\">\n";
479 $display.= "</p>";
480 }
481 return ($display);
482 }
485 /****************
486 Dialog display
487 ****************/
489 /* Check if there is a snapshot dialog open */
490 if($this->IsReleaseManagementActivated()){
491 $base = $this->app_release;
492 }else{
493 $base = $this->app_base;
494 }
495 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
496 return($str);
497 }
499 /* Display dialog with system list */
500 $this->DivListApplication->parent = $this;
501 $this->DivListApplication->execute();
502 if(!$this->IsReleaseManagementActivated()){
503 $this->DivListApplication->AddDepartments($this->DivListApplication->selectedBase,3,1);
504 }
505 $this->reload();
506 $this->DivListApplication->setEntries($this->applications);
507 return($this->DivListApplication->Draw());
508 }
511 /* Return departments, that will be included within snapshot detection */
512 function get_used_snapshot_bases()
513 {
514 if($this->IsReleaseManagementActivated()){
515 return(array($this->app_release));
516 }else{
517 return(array($this->app_base));
518 }
519 }
522 function reload()
523 {
524 $this->applications= array();
526 /* Set base for all searches */
527 $Regex = $this->DivListApplication->Regex;
528 $SubSearch = $this->DivListApplication->SubSearch;
529 $Flags = GL_NONE | GL_SIZELIMIT;
530 $Filter = "(&(cn=".$Regex.")(objectClass=gosaApplication))";
531 $tmp = array();
533 if(!$this->IsReleaseManagementActivated()){
534 $use_base = $this->app_base;
535 if($SubSearch){
536 $use_base = preg_replace("/^".normalizePreg(get_ou("applicationou"))."/","",$use_base);
537 }
538 }else{
539 $use_base = $this->app_release;
540 $SubSearch= FALSE;
541 }
543 if($SubSearch){
544 $res= get_sub_list($Filter, "application",get_ou("applicationou"), $use_base, array("cn","description","dn","objectClass"), $Flags);
545 }else{
546 $res= get_list($Filter, "application",$use_base, array("cn","description","dn","objectClass"), $Flags);
547 }
548 $tmp2 = array();
549 foreach ($res as $val){
550 $tmp[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']]=$val;
551 $tmp2[strtolower($val['cn'][0]).$val['cn'][0].$val['dn']] = strtolower($val['cn'][0]).$val['cn'][0].$val['dn'];
552 }
554 natcasesort($tmp2);
555 $this->applications=array();
556 foreach($tmp2 as $val){
557 $this->applications[]=$tmp[$val];
558 }
559 reset ($this->applications);
560 }
562 function remove_from_parent()
563 {
564 /* Optionally execute a command after we're done */
565 $this->postremove();
566 }
569 function copyPasteHandling_from_queue($s_action,$s_entry)
570 {
571 /* Check if Copy & Paste is disabled */
572 if(!is_object($this->CopyPasteHandler)){
573 return("");
574 }
576 /* Add a single entry to queue */
577 if($s_action == "cut" || $s_action == "copy"){
579 /* Cleanup object queue */
580 $this->CopyPasteHandler->cleanup_queue();
581 $dn = $this->applications[$s_entry]['dn'];
582 $this->CopyPasteHandler->add_to_queue($dn,$s_action,"apptabs","APPSTABS","application");
583 }
586 /* Add entries to queue */
587 if($s_action == "copy_multiple" || $s_action == "cut_multiple"){
589 /* Cleanup object queue */
590 $this->CopyPasteHandler->cleanup_queue();
592 /* Add new entries to CP queue */
593 foreach($this->list_get_selected_items() as $id){
594 $dn = $this->applications[$id]['dn'];
596 if($s_action == "copy_multiple"){
597 $this->CopyPasteHandler->add_to_queue($dn,"copy","apptabs","APPSTABS","application");
598 }
599 if($s_action == "cut_multiple"){
600 $this->CopyPasteHandler->add_to_queue($dn,"cut","apptabs","APPSTABS","application");
601 }
602 }
603 }
605 /* Start pasting entries */
606 if($s_action == "editPaste"){
607 $this->start_pasting_copied_objects = TRUE;
608 }
611 /* Return C&P dialog */
612 if($this->start_pasting_copied_objects && $this->CopyPasteHandler->entries_queued()){
615 /* Get dialog */
616 $this->CopyPasteHandler->SetVar("base",$this->app_base);
617 $this->CopyPasteHandler->SetVar("parent",$this);
618 $data = $this->CopyPasteHandler->execute();
620 /* Return dialog data */
621 if(!empty($data)){
622 return($data);
623 }
624 }
626 /* Automatically disable status for pasting */
627 if(!$this->CopyPasteHandler->entries_queued()){
628 $this->start_pasting_copied_objects = FALSE;
629 }
630 return("");
631 }
634 function list_get_selected_items()
635 {
636 $ids = array();
637 foreach($_POST as $name => $value){
638 if(preg_match("/^item_selected_[0-9]*$/",$name)){
639 $id = preg_replace("/^item_selected_/","",$name);
640 $ids[$id] = $id;
641 }
642 }
643 return($ids);
644 }
647 /* Save to LDAP */
648 function save()
649 {
650 /* Optionally execute a command after we're done */
651 $this->postcreate();
652 }
654 function remove_lock()
655 {
656 if (isset($this->apptabs->dn)){
657 del_lock ($this->apptabs->dn);
658 }
659 }
661 function save_object()
662 {
663 $this->DivListApplication->save_object();
664 if(is_object($this->CopyPasteHandler)){
665 $this->CopyPasteHandler->save_object();
666 }
668 if($this->IsReleaseManagementActivated() && isset($_POST['app_release'])){
669 $sel_rel = get_post('app_release');
670 $releases = array_flip($this->getReleases());
671 if(isset($releases[$sel_rel])){
672 $this->app_release = $releases[$sel_rel];
673 }
674 $app_filter = session::get("app_filter");
675 $app_filter['app_release'] = $this->app_release;
676 session::set("app_filter",$app_filter);
677 }elseif(!$this->IsReleaseManagementActivated()){
678 $this->app_base = get_ou("applicationou").$this->DivListApplication->selectedBase;
679 $app_filter = session::get("app_filter");
680 $app_filter['app_base'] = $this->app_base;
681 session::set("app_filter",$app_filter);
682 }
683 }
685 function check() {}
686 function adapt_from_template($dn, $skip= array()) {}
687 function password_change_needed() {}
688 }
689 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
690 ?>