Code

60a83685fa76f31fc1f9ef8c20d367785e7f8e43
[gosa.git] / plugins / admin / applications / class_applicationManagement.inc
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 $applications= array();
31   var $ui= NULL;
32   var $acl= "";
34   var $CopyPasteHandler ;
36   var $enableCopyPaste = false;
38   function applicationManagement ($config, $ui)
39   {
40         /* Save configuration for internal use */
41         $this->config= $config;
42         $this->ui= $ui;
45   if( (isset($this->config->data['MAIN']['ENABLECOPYPASTE'])) 
46      && 
47       (preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
48     $this->enableCopyPaste = true;
49   }
53   $this->CopyPasteHandler = new CopyPasteHandler($this->config);
55         /* Get global filter config */
56         if (!is_global("appfilter")){
57                 $base= get_base_from_people($ui->dn);
58                 $appfilter= array("depselect" => $base,
59                                   "regex" => "*");
60                 register_global("appfilter", $appfilter);
61         }
62   }
64   function execute()
65   {
66         /* Call parent execute */
67         plugin::execute();
69 //   print_a($_POST);
70 //   print_a($_GET);
71   $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/");
73           /* Save data */
74           $appfilter    = get_global("appfilter");
75           $smarty       = get_smarty();             // Smarty instance
76           $s_action     = "";                       // Contains the action to proceed
77           $s_entry      = "";                       // The value for s_action
78     $base_back    = "";                       // The Link for Backbutton
80     /* Start for New List Managment */
81     if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
82             $s_action="open";
83             $s_entry = base64_decode($_GET['dep_id']);
84             $appfilter['depselect']= "".$this->config->departments[trim($s_entry)];
85     }
87     /* Test Posts */
88     foreach($_POST as $key => $val){
89       // Post for delete
90       if(preg_match("/appl_del.*/",$key)){
91         $s_action = "del";
92         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
93         // Post for edit
94       }elseif(preg_match("/appl_edit_.*/",$key)){
95         $s_action="edit";
96         $s_entry  = preg_replace("/appl_".$s_action."_/i","",$key);
97         // Post for new
98       }elseif(preg_match("/^copy_.*/",$key)){
99         $s_action="copy";
100         $s_entry  = preg_replace("/^copy_/i","",$key);
101       }elseif(preg_match("/^cut_.*/",$key)){
102         $s_action="cut";
103         $s_entry  = preg_replace("/^cut_/i","",$key);
104         // Post for new
105       }elseif(preg_match("/^dep_back.*/i",$key)){
106         $s_action="back";
107       }elseif(preg_match("/^appl_new.*/",$key)){
108         $s_action="new";
109       }elseif(preg_match("/^dep_home.*/i",$key)){
110         $s_action="home";
111       }elseif(preg_match("/^dep_root.*/i",$key)){
112         $s_action="root";
113       }elseif(preg_match("/^editPaste.*/i",$key)){
114         $s_action="editPaste";
115       }
116     }
118     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
119       $s_action ="edit";
120       $s_entry  = $_GET['id'];
121     }
123     $s_entry  = preg_replace("/_.$/","",$s_entry);
125     /* Department changed? */
126     if(isset($_POST['depselect']) && $_POST['depselect']){
127       $appfilter['depselect']= $_POST['depselect'];
128     }
130     /* Homebutton is posted */
131     if($s_action=="home"){
132       $appfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
133       $appfilter['depselect']=(preg_replace("/^[^,]+,/","",$appfilter['depselect']));
134     }
136     if($s_action=="root"){
137       $appfilter['depselect']=($this->config->current['BASE']);
138     }
140     /* If Backbutton is Posted */
141     if($s_action=="back"){
142       $base_back          = preg_replace("/^[^,]+,/","",$appfilter['depselect']);
143       $base_back          = convert_department_dn($base_back);
145       if(isset($this->config->departments[trim($base_back)])){
146         $appfilter['depselect']= $this->config->departments[trim($base_back)];
147       }else{
148         $appfilter['depselect']= $this->config->departments["/"];
149       }
150     }
152     if (isset($_POST['regex'])){
153       $appfilter['regex']= $_POST['regex'];
154     }
155     if (isset($_GET['search'])){
156       $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
157       if ($s == "**"){
158         $s= "*";
159       }
160       $appfilter['regex']= $s;
161     }
162     register_global("appfilter", $appfilter);
164     /* Check sorting variable */
165     $this->reload();
166     $smarty= get_smarty();
168     /* Check for exeeded sizelimit */
169     if (($message= check_sizelimit()) != ""){
170       return($message);
171     }
174     /* Only perform copy / paste if it is enabled 
175      */
176     if($this->enableCopyPaste){
177     
178        /* Paste copied/cutted object in here
179        */
180       if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
181         $this->CopyPasteHandler->save_object();
182         $this->CopyPasteHandler->SetVar("base",$appfilter['depselect']);
183         return($this->CopyPasteHandler->execute());
184       }
187       /* Copy current object to CopyHandler
188        */
189       if($s_action == "copy"){
190         $this->CopyPasteHandler->Clear();
191         $dn = $this->applications[$s_entry]['dn'];
192         $obj    = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
193         $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new");
194         $this->CopyPasteHandler->Copy($obj,$objNew);
195       }
198       /* Copy current object to CopyHandler
199        */
200       if($s_action == "cut"){
201         $this->CopyPasteHandler->Clear();
202         $dn = $this->applications[$s_entry]['dn'];
203         $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
204         $this->CopyPasteHandler->Cut($obj);
205       }
206     }
208     /* New application? */
209     if ($s_action=="new"){
211       /* By default we set 'dn' to 'new', all relevant plugins will
212          react on this. */
213       $this->dn= "new";
215       /* Create new usertab object */
216       $this->apptabs= new apptabs($this->config,
217           $this->config->data['TABS']['APPSTABS'], $this->dn);
218       $this->apptabs->set_acl(array(':all'));
219     }
221     /* Cancel dialogs */
222     if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
223       del_lock ($this->apptabs->dn);
224       unset ($this->apptabs);
225       $this->apptabs= NULL;
226       unset ($_SESSION['objectinfo']);
227     }
229     /* Finish apps edit is triggered by the tabulator dialog, so
230        the user wants to save edited data. Check and save at this
231        point. */
232     if ((isset($_POST['edit_finish'])) && (isset($this->apptabs->config))){
234       /* Check tabs, will feed message array */
235       $this->apptabs->last= $this->apptabs->current;
236       $this->apptabs->save_object();
237       $message= $this->apptabs->check();
239       /* Save, or display error message? */
240       if (count($message) == 0){
242         /* Save data data to ldap */
243         $this->apptabs->save();
244                         gosa_log ("Application object'".$this->dn."' has been saved");
246                         /* Application has been saved successfully, remove lock from
247                            LDAP. */
248                         if ($this->dn != "new"){
249                                 del_lock ($this->dn);
250                         }
252                         /* There's no page reload so we have to read new apps at
253                            this point. */
254                         $this->reload ();
255                         unset ($this->apptabs);
256                         $this->apptabs= NULL;
257                         unset ($_SESSION['objectinfo']);
258                 } else {
259                         /* Ok. There seem to be errors regarding to the tab data,
260                            show message and continue as usual. */
261                         show_errors($message);
262                 }
263         }
265         /* User wants to edit data? */
266         if (($s_action=="edit") && (!isset($this->apptabs->config))){
268                 /* Get 'dn' from posted 'applist', must be unique */
269                 $this->dn= $this->applications[$s_entry]['dn'];
271                 /* Check locking, save current plugin in 'back_plugin', so
272                    the dialog knows where to return. */
273                 if (($user= get_lock($this->dn)) != ""){
274                         return(gen_locked_message ($user, $this->dn));
275                 }
277                 /* Lock the current entry, so everyone will get the
278                    above dialog */
279                 add_lock ($this->dn, $this->ui->dn);
281                 /* Set up the users ACL's for this 'dn' */
282                 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
284                 /* Register apptabs to trigger edit dialog */
285                 $this->apptabs= new apptabs($this->config,
286                                 $this->config->data['TABS']['APPSTABS'], $this->dn);
287                 $this->apptabs->set_acl($acl);
288                 $_SESSION['objectinfo']= $this->dn;
289         }
291         /* Remove user was requested */
292         if ($s_action == "del"){
294                 /* Get 'dn' from posted 'uid' */
295                 $this->dn= $this->applications[$s_entry]['dn'];
297                 /* Load permissions for selected 'dn' and check if
298                    we're allowed to remove this 'dn' */
299                 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
300                 $this->acl= get_module_permission($acl, "application", $this->dn);
301                 if (chkacl($this->acl, "delete") == ""){
303                         /* Check locking, save current plugin in 'back_plugin', so
304                            the dialog knows where to return. */
305                         if (($user= get_lock($this->dn)) != ""){
306                                 return (gen_locked_message ($user, $this->dn));
307                         }
309                         /* Lock the current entry, so nobody will edit it during deletion */
310                         add_lock ($this->dn, $this->ui->dn);
311                         $smarty= get_smarty();
312                         $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), LDAP::fix($this->dn)));
313                         return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
314                 } else {
316                         /* Obviously the user isn't allowed to delete. Show message and
317                            clean session. */
318                         print_red (_("You are not allowed to delete this application!"));
319                 }
320         }
322         /* Confirmation for deletion has been passed. Group should be deleted. */
323         if (isset($_POST['delete_app_confirm'])){
325                 /* Some nice guy may send this as POST, so we've to check
326                    for the permissions again. */
327                 if (chkacl($this->acl, "delete") == ""){
329                         /* Delete request is permitted, perform LDAP action */
330                         $this->apptabs= new apptabs($this->config,
331                                         $this->config->data['TABS']['APPSTABS'], $this->dn);
332                         $this->apptabs->set_acl(array($this->acl));
333                         $this->apptabs->delete ();
334                         gosa_log ("Application object'".$this->dn."' has been removed");
335                         unset ($this->apptabs);
336                         $this->apptabs= NULL;
338                         /* Group list has changed, reload it. */
339                         $this->reload ();
340                 } else {
342                         /* Normally this shouldn't be reached, send some extra
343                            logs to notify the administrator */
344                         print_red (_("You are not allowed to delete this application!"));
345                         gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
346                 }
348                 /* Remove lock file after successfull deletion */
349                 del_lock ($this->dn);
350         }
353         /* Delete application canceled? */
354         if (isset($_POST['delete_cancel'])){
355                 del_lock ($this->dn);
356                 unset($_SESSION['objectinfo']);
357         }
359         /* Show tab dialog if object is present */
360         if (($this->apptabs) && (isset($this->apptabs->config))){
361                 $display= $this->apptabs->execute();
363                 /* Don't show buttons if tab dialog requests this */
364                 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
365                         $display.= "<p style=\"text-align:right\">\n";
366                         $display.= "<input type=\"submit\" name=\"edit_finish\" value=\""._("Finish")."\">\n";
367                         $display.= "&nbsp;\n";
368                         $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
369                         $display.= "</p>";
370                 }
371                 return ($display);
372         }
374         /* Show main page */
376         /* Prepare departments */
377         $options= "";
378         foreach ($this->config->idepartments as $key => $value){
379           if ($appfilter['depselect'] == $key){
380             $options.= "<option selected='selected' value='$key'>$value</option>";
381           } else {
382             $options.= "<option value='$key'>$value</option>";
383           }
384         }
386         if($this->enableCopyPaste){
387           $Copy_Paste = " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;";
388           if($this->CopyPasteHandler->isCurrentObjectPastAble()){
389             $Copy_Paste .= "<input type='image' name='editPaste' class='center' src='images/editpaste.png' alt='"._("Paste")."'>&nbsp;";
390           }else{
391             $Copy_Paste .= "<img class='center' src='images/cant_editpaste.png' alt='"._("Can't paste")."'>&nbsp;";
392           }
393         }else{
394           $Copy_Paste ="";
395         }
396         
397         // Managment
398         $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
399           " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'>&nbsp;".
400           " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'>&nbsp;".
401           " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'>&nbsp;".
402           " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
403           " <input class='center' type='image' align='middle' src='images/list_new_app.png' alt='"._("new")."' title='"._("Create new application")."' name='appl_new'>&nbsp;".
404           $Copy_Paste.
405           " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'>&nbsp;".
406           _("Base")."&nbsp;<select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
407           " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'>&nbsp;".
408           "</div>";
412         if($this->enableCopyPaste){
413           $actions = "<input class='center' type='image' 
414             src='images/editdelete.png' alt='"._("cut")."' name='cut_%KEY%' title='"._("Cut this entry")."'>&nbsp;";
415           $actions.= "<input class='center' type='image' 
416             src='images/editcopy.png' alt='"._("copy")."' name='copy_%KEY%' title='"._("Copy this entry")."'>&nbsp;";
417           $actions.= "<input class='center' type='image' 
418             src='images/edit.png' alt='"._("edit")."' name='appl_edit_%KEY%' title='"._("Edit this entry")."'>";
419           $actions.= "<input class='center' type='image' 
420             src='images/edittrash.png' alt='"._("delete")."' name='appl_del_%KEY%' title='"._("Delete this entry")."'>";
421         }else{
423           $actions = "<input class='center' type='image' 
424             src='images/edit.png' alt='"._("edit")."' name='appl_edit_%KEY%' title='"._("Edit this entry")."'>";
425           $actions.= "<input class='center' type='image' 
426             src='images/edittrash.png' alt='"._("delete")."' name='appl_del_%KEY%' title='"._("Delete this entry")."'>";
427         }
430         // Defining Links
431         $linkopen = "<a href='?plug=".$_GET['plug']."&amp;act=dep_open&amp;dep_id=%s'>%s</a>";
433         // image Buttons
434         $editlink = "<a href='?plug=".$_GET['plug']."&amp;id=%s&amp;act=edit_entry'>%s</a>";
435         $userimg  = "<img class='center' src='images/select_groups.png' alt='User'    title='%s'>";
437         // Extension images
438         $applimg  = "<img class='center' src='images/select_application.png' alt='A'  title='"._("Application")."'>";
440         // Space
441         $empty    = "<img class='center' src='images/empty.png' style='width:16px;height:16px;' alt=''>";
444         $divlist = new divlist("applicationtabs");
445         $divlist->SetHeader(array(
446               array("string" => "&nbsp;", "attach" => "style='text-align:center;width:20px;'"),
447               array("string" => _("Application name")." / "._("Department"), "attach" => "style=''"),
448               array("string" => _("Actions"), "attach" => "style='width:80px;border-right:0px;text-align:right;'")
449               ));
452         $divlist->SetSummary(_("This table displays all groups, in the selected tree."));
453         $divlist->SetEntriesPerPage(0);
455         foreach($this->departments as $key=> $val){
457           if(!isset($this->config->departments[trim($key)])){
458             $this->config->departments[trim($key)]="";
459           }
461           $non_empty="";
462           $keys= str_replace("/","\/",$key);
463           foreach($this->config->departments as $keyd=>$vald ){
464             if(preg_match("/".$keys."\/.*/",$keyd)){
465               $non_empty="full";
466             }
467           }
469           $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
470           $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
471           $field3 = array("string" => "&nbsp;", "attach" => "style='width:80px;border-right:0px;text-align:right;'");
473           $divlist->AddEntry(array($field1,$field2,$field3));
474         }
476         foreach($this->applications as $key => $val){
477           $title = "title='dn : ".$val['dn']."'";
479           if(!isset($val['description'][0])){
480             $desc = "";
481           }else{
482             $desc = " - [ ".$val['description'][0]." ]";
483           }
484           $field1 = array("string" => sprintf($applimg,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
485           $field2 = array("string" => sprintf($editlink,$key,($val['cn']['0'].$desc)), "attach" => "style='' ".$title);
486           $field3 = array("string" => preg_replace("/%KEY%/", $key, $actions), "attach" => "style='width:80px;border-right:0px;text-align:right;'");
488           $divlist->AddEntry(array($field1,$field2,$field3));
489         }
491         $smarty->assign("applicationshead", $listhead);
492         $smarty->assign("applications", $divlist->DrawList());
493         $smarty->assign("search_image", get_template_path('images/search.png'));
494         $smarty->assign("tree_image", get_template_path('images/tree.png'));
495         $smarty->assign("infoimage", get_template_path('images/info.png'));
496         $smarty->assign("launchimage", get_template_path('images/launch.png'));
497         $smarty->assign("deplist", $this->config->idepartments);
498         $smarty->assign("regex", $appfilter['regex']);
500         /* Extend if we are not using javascript */
501         $smarty->assign("apply", apply_filter());
502         $smarty->assign("alphabet", generate_alphabet());
503         $smarty->assign("hint", print_sizelimit_warning());
505         return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
506   }
509   function reload()
510   {
511         /* Get config */
512         $appfilter= get_global('appfilter');
514         /* Set base for all searches */
515         $base= $appfilter['depselect'];
517         /* Regex filter? */
518         if ($appfilter['regex'] != ""){
519                 $regex= $appfilter['regex'];
520         } else {
521                 $regex= "*";
522         }
523       
524         /* Generate application list */
525         $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)(objectClass=gosaApplication))", FALSE, "ou=apps,".$base, array("*"), TRUE);
526         $this->applications= array();
527         foreach ($res as $value){
528           $this->applications[]= $value;
529         }
530         reset ($this->applications);
534         /* NEW LIST MANAGMENT
535          * We also need to search for the departments
536          * So we are able to navigate like in konquerer
537          */
538         $peopleOU = get_people_ou();
540         if(empty($peopleOU)){
541           $base2 = $base;
542         }else{
543           $base2 = preg_replace("/".$peopleOU."/i","",$base);    
544         }
546         $res3 =  get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
547             TRUE, $base2, array("ou", "description"), TRUE);
549         $this->departments= array();
550         $tmp = array();
551         foreach ($res3 as $value){
552           $tmp[strtolower($value['dn']).$value['dn']]=$value;
553         }
554         ksort($tmp);
555         foreach($tmp as $value){
556           if(isset($value["description"][0])){
557             $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
558           }else{
559             $this->departments[$value['dn']]=convert_department_dn2($value['dn']);//$value["description"][0];
560           }
561         }
564         /* END NEW LIST MANAGMENT
565          */
567       $tmp=array();
568     foreach($this->applications as $tkey => $val ){
569       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
570     }
571     ksort($tmp);
572     $this->applications=array();
573     foreach($tmp as $val){
574       $this->applications[]=$val;
575     }
576     reset ($this->applications);
579   }
581   function remove_from_parent()
582   {
583         /* Optionally execute a command after we're done */
584         $this->postremove();
585   }
588   /* Save data to object */
589   function save_object()
590   {
591   }
594   /* Check values */
595   function check()
596   {
597   }
600   /* Save to LDAP */
601   function save()
602   {
603         /* Optionally execute a command after we're done */
604         $this->postcreate();
605   }
607   function adapt_from_template($dn)
608   {
609   }
611   function password_change_needed()
612   {
613   }
615   function show_header($button_text, $text, $disabled= FALSE)
616   {
617   }
619   function remove_lock()
620   {
621     if (isset($this->apptabs->dn)){
622       del_lock ($this->apptabs->dn);
623     }
624   }
627 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
628 ?>