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 $Release = "";
37 var $Releases = array();
39 var $enableCopyPaste = false;
41 function applicationManagement ($config, $ui)
42 {
43 /* Save configuration for internal use */
44 $this->config= $config;
45 $this->ui= $ui;
47 if( (isset($this->config->data['MAIN']['ENABLECOPYPASTE']))
48 &&
49 (preg_match("/true/i",$this->config->data['MAIN']['ENABLECOPYPASTE'] ))){
50 $this->enableCopyPaste = true;
51 }
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);
59 $appfilter= array("depselect" => $base,
60 "regex" => "*" ,
61 "release" => "ou=apps,".$base);
63 register_global("appfilter", $appfilter);
64 }
66 $appfilter = get_global("appfilter");
67 $this->Releases = $this->getReleases($appfilter['depselect']);
68 $this->Release = $appfilter['release'];
69 }
71 function getReleases($base)
72 {
73 $dn = "ou=apps,".$base;
75 $ret = array();
77 $ret ["ou=apps,".$base] = "/";
79 $ldap = $this->config->get_ldap_link();
80 $ldap->cd($dn);
82 $ldap->search("objectClass=organizationalUnit",array("ou"));
84 while($attrs = $ldap->fetch()){
85 $str = str_replace($dn,"",$attrs['dn']);
86 $tmp = array_reverse( split("ou=",$str));
87 $str = "";
88 foreach($tmp as $val){
89 $val = trim(preg_replace("/,/","",$val));
90 if($val == "apps") break;
91 $str .= "/".$val;
92 }
93 $ret[$attrs['dn']]= $str;
94 }
95 return($ret);
96 }
98 function execute()
99 {
100 /* Call parent execute */
101 plugin::execute();
103 $_SESSION['LOCK_VARS_TO_USE'] = array("/^act$/","/^id$/","/^appl_edit_/","/^appl_del_/");
105 /* Save data */
106 $appfilter = get_global("appfilter");
107 $smarty = get_smarty(); // Smarty instance
108 $s_action = ""; // Contains the action to proceed
109 $s_entry = ""; // The value for s_action
110 $base_back = ""; // The Link for Backbutton
112 /* Start for New List Managment */
113 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
114 $s_action="open";
115 $s_entry = base64_decode($_GET['dep_id']);
116 $appfilter['depselect']= "".$this->config->departments[trim($s_entry)];
117 }
119 /* Get posted release */
120 if(isset($_POST['select_release'])){
121 $r = $_POST['select_release'];
122 if((isset($this->Releases[$r])) && ($this->Release != $r)){
123 $this->Release = $r;
124 $appfilter = get_global("appfilter");
125 $appfilter['release'] = $r;
126 register_global("appfilter",$appfilter);
127 }
128 }
130 /* Test Posts */
131 foreach($_POST as $key => $val){
132 // Post for delete
133 if(preg_match("/appl_del.*/",$key)){
134 $s_action = "del";
135 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
136 // Post for edit
137 }elseif(preg_match("/appl_edit_.*/",$key)){
138 $s_action="edit";
139 $s_entry = preg_replace("/appl_".$s_action."_/i","",$key);
140 // Post for new
141 }elseif(preg_match("/^copy_.*/",$key)){
142 $s_action="copy";
143 $s_entry = preg_replace("/^copy_/i","",$key);
144 }elseif(preg_match("/^cut_.*/",$key)){
145 $s_action="cut";
146 $s_entry = preg_replace("/^cut_/i","",$key);
147 // Post for new
148 }elseif(preg_match("/^dep_back.*/i",$key)){
149 $s_action="back";
150 }elseif(preg_match("/^appl_new.*/",$key)){
151 $s_action="new";
152 }elseif(preg_match("/^dep_home.*/i",$key)){
153 $s_action="home";
154 }elseif(preg_match("/^dep_root.*/i",$key)){
155 $s_action="root";
156 }elseif(preg_match("/^editPaste.*/i",$key)){
157 $s_action="editPaste";
158 }
159 }
161 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
162 $s_action ="edit";
163 $s_entry = $_GET['id'];
164 }
166 $s_entry = preg_replace("/_.$/","",$s_entry);
168 /* Department changed? */
169 if(isset($_POST['depselect']) && $_POST['depselect']){
170 $appfilter['depselect']= $_POST['depselect'];
171 }
173 /* Homebutton is posted */
174 if($s_action=="home"){
175 $appfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
176 $appfilter['depselect']=(preg_replace("/^[^,]+,/","",$appfilter['depselect']));
177 }
179 if($s_action=="root"){
180 $appfilter['depselect']=($this->config->current['BASE']);
181 }
183 /* If Backbutton is Posted */
184 if($s_action=="back"){
185 $base_back = preg_replace("/^[^,]+,/","",$appfilter['depselect']);
186 $base_back = convert_department_dn($base_back);
188 if(isset($this->config->departments[trim($base_back)])){
189 $appfilter['depselect']= $this->config->departments[trim($base_back)];
190 }else{
191 $appfilter['depselect']= $this->config->departments["/"];
192 }
193 }
195 if (isset($_POST['regex'])){
196 $appfilter['regex']= $_POST['regex'];
197 }
198 if (isset($_GET['search'])){
199 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
200 if ($s == "**"){
201 $s= "*";
202 }
203 $appfilter['regex']= $s;
204 }
206 $this->Releases = $this->getReleases($appfilter['depselect']);
207 if(!isset($this->Releases[$this->Release])){
208 $this->Release = key($this->Releases);
209 $appfilter['release'] = $this->Release;
210 register_global("appfilter",$appfilter);
211 }
213 register_global("appfilter", $appfilter);
215 /* Check sorting variable */
216 $this->reload();
217 $smarty= get_smarty();
219 /* Check for exeeded sizelimit */
220 if (($message= check_sizelimit()) != ""){
221 return($message);
222 }
225 /* Only perform copy / paste if it is enabled
226 */
227 if($this->enableCopyPaste){
229 /* Paste copied/cutted object in here
230 */
231 if(($s_action == "editPaste") || ($this->CopyPasteHandler->stillOpen())){
232 $this->CopyPasteHandler->save_object();
233 $this->CopyPasteHandler->SetVar("base",$appfilter['depselect']);
234 return($this->CopyPasteHandler->execute());
235 }
238 /* Copy current object to CopyHandler
239 */
240 if($s_action == "copy"){
241 $this->CopyPasteHandler->Clear();
242 $dn = $this->applications[$s_entry]['dn'];
243 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
244 $objNew = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], "new");
245 $this->CopyPasteHandler->Copy($obj,$objNew);
246 }
249 /* Copy current object to CopyHandler
250 */
251 if($s_action == "cut"){
252 $this->CopyPasteHandler->Clear();
253 $dn = $this->applications[$s_entry]['dn'];
254 $obj = new apptabs($this->config, $this->config->data['TABS']['APPSTABS'], $dn);
255 $this->CopyPasteHandler->Cut($obj);
256 }
257 }
259 /* New application? */
260 if ($s_action=="new"){
262 /* By default we set 'dn' to 'new', all relevant plugins will
263 react on this. */
264 $this->dn= "new";
266 /* Create new usertab object */
267 $this->apptabs= new apptabs($this->config,
268 $this->config->data['TABS']['APPSTABS'], $this->dn);
269 $this->apptabs->set_acl(array(':all'));
270 }
272 /* Cancel dialogs */
273 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
274 del_lock ($this->apptabs->dn);
275 unset ($this->apptabs);
276 $this->apptabs= NULL;
277 unset ($_SESSION['objectinfo']);
278 }
280 /* Finish apps edit is triggered by the tabulator dialog, so
281 the user wants to save edited data. Check and save at this
282 point. */
283 if ((isset($_POST['edit_finish'])) && (isset($this->apptabs->config))){
285 /* Check tabs, will feed message array */
286 $this->apptabs->last= $this->apptabs->current;
287 $this->apptabs->save_object();
288 $message= $this->apptabs->check();
290 /* Save, or display error message? */
291 if (count($message) == 0){
293 /* Save data data to ldap */
294 $this->apptabs->save();
295 gosa_log ("Application object'".$this->dn."' has been saved");
297 /* Application has been saved successfully, remove lock from
298 LDAP. */
299 if ($this->dn != "new"){
300 del_lock ($this->dn);
301 }
303 /* There's no page reload so we have to read new apps at
304 this point. */
305 $this->reload ();
306 unset ($this->apptabs);
307 $this->apptabs= NULL;
308 unset ($_SESSION['objectinfo']);
309 } else {
310 /* Ok. There seem to be errors regarding to the tab data,
311 show message and continue as usual. */
312 show_errors($message);
313 }
314 }
316 /* User wants to edit data? */
317 if (($s_action=="edit") && (!isset($this->apptabs->config))){
319 /* Get 'dn' from posted 'applist', must be unique */
320 $this->dn= $this->applications[$s_entry]['dn'];
322 /* Check locking, save current plugin in 'back_plugin', so
323 the dialog knows where to return. */
324 if (($user= get_lock($this->dn)) != ""){
325 return(gen_locked_message ($user, $this->dn));
326 }
328 /* Lock the current entry, so everyone will get the
329 above dialog */
330 add_lock ($this->dn, $this->ui->dn);
332 /* Set up the users ACL's for this 'dn' */
333 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
335 /* Register apptabs to trigger edit dialog */
336 $this->apptabs= new apptabs($this->config,
337 $this->config->data['TABS']['APPSTABS'], $this->dn);
338 $this->apptabs->set_acl($acl);
339 $_SESSION['objectinfo']= $this->dn;
340 }
342 /* Remove user was requested */
343 if ($s_action == "del"){
345 /* Get 'dn' from posted 'uid' */
346 $this->dn= $this->applications[$s_entry]['dn'];
348 /* Load permissions for selected 'dn' and check if
349 we're allowed to remove this 'dn' */
350 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
351 $this->acl= get_module_permission($acl, "application", $this->dn);
352 if (chkacl($this->acl, "delete") == ""){
354 /* Check locking, save current plugin in 'back_plugin', so
355 the dialog knows where to return. */
356 if (($user= get_lock($this->dn)) != ""){
357 return (gen_locked_message ($user, $this->dn));
358 }
360 /* Lock the current entry, so nobody will edit it during deletion */
361 add_lock ($this->dn, $this->ui->dn);
362 $smarty= get_smarty();
363 $smarty->assign("intro", sprintf(_("You're about to delete the application '%s'."), LDAP::fix($this->dn)));
364 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
365 } else {
367 /* Obviously the user isn't allowed to delete. Show message and
368 clean session. */
369 print_red (_("You are not allowed to delete this application!"));
370 }
371 }
373 /* Confirmation for deletion has been passed. Group should be deleted. */
374 if (isset($_POST['delete_app_confirm'])){
376 /* Some nice guy may send this as POST, so we've to check
377 for the permissions again. */
378 if (chkacl($this->acl, "delete") == ""){
380 /* Delete request is permitted, perform LDAP action */
381 $this->apptabs= new apptabs($this->config,
382 $this->config->data['TABS']['APPSTABS'], $this->dn);
383 $this->apptabs->set_acl(array($this->acl));
384 $this->apptabs->delete ();
385 gosa_log ("Application object'".$this->dn."' has been removed");
386 unset ($this->apptabs);
387 $this->apptabs= NULL;
389 /* Group list has changed, reload it. */
390 $this->reload ();
391 } else {
393 /* Normally this shouldn't be reached, send some extra
394 logs to notify the administrator */
395 print_red (_("You are not allowed to delete this application!"));
396 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
397 }
399 /* Remove lock file after successfull deletion */
400 del_lock ($this->dn);
401 }
404 /* Delete application canceled? */
405 if (isset($_POST['delete_cancel'])){
406 del_lock ($this->dn);
407 unset($_SESSION['objectinfo']);
408 }
410 /* Show tab dialog if object is present */
411 if (($this->apptabs) && (isset($this->apptabs->config))){
412 $display= $this->apptabs->execute();
414 /* Don't show buttons if tab dialog requests this */
415 if (!$this->apptabs->by_object[$this->apptabs->current]->dialog){
416 $display.= "<p style=\"text-align:right\">\n";
417 $display.= "<input type=\"submit\" name=\"edit_finish\" value=\""._("Finish")."\">\n";
418 $display.= " \n";
419 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
420 $display.= "</p>";
421 }
422 return ($display);
423 }
425 /* Show main page */
427 /* Prepare departments */
428 $options= "";
429 foreach ($this->config->idepartments as $key => $value){
430 if ($appfilter['depselect'] == $key){
431 $options.= "<option selected='selected' value='$key'>$value</option>";
432 } else {
433 $options.= "<option value='$key'>$value</option>";
434 }
435 }
437 if($this->enableCopyPaste){
438 $Copy_Paste = " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ";
440 if($this->CopyPasteHandler->isCurrentObjectPastAble()){
442 if($this->CopyPasteHandler->isCurrentCutted()){
443 $img = "images/cutpaste.png";
444 }else{
445 $img = "images/copypaste.png";
446 }
448 $Copy_Paste .= "<input type='image' name='editPaste' class='center'
449 src='".$img."' alt='"._("Paste")."' title='".$this->CopyPasteHandler->GetCurrentDn()."'> ";
450 }else{
451 $Copy_Paste .= "<img class='center' src='images/cant_editpaste.png' alt='"._("Can't paste")."'> ";
452 }
453 }else{
454 $Copy_Paste ="";
455 }
457 // Managment
458 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
459 " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
460 " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
461 " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
462 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
463 " <input class='center' type='image' align='middle' src='images/list_new_app.png' alt='"._("new")."' title='"._("Create new application")."' name='appl_new'> ".
464 $Copy_Paste.
465 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
466 _("Base")." <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
467 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
468 "</div>";
472 if($this->enableCopyPaste){
473 $actions = "<input class='center' type='image'
474 src='images/editcut.png' alt='"._("cut")."' name='cut_%KEY%' title='"._("Cut this entry")."'> ";
475 $actions.= "<input class='center' type='image'
476 src='images/editcopy.png' alt='"._("copy")."' name='copy_%KEY%' title='"._("Copy this entry")."'> ";
477 $actions.= "<input class='center' type='image'
478 src='images/edit.png' alt='"._("edit")."' name='appl_edit_%KEY%' title='"._("Edit this entry")."'>";
479 $actions.= "<input class='center' type='image'
480 src='images/edittrash.png' alt='"._("delete")."' name='appl_del_%KEY%' title='"._("Delete this entry")."'>";
481 }else{
483 $actions = "<input class='center' type='image'
484 src='images/edit.png' alt='"._("edit")."' name='appl_edit_%KEY%' title='"._("Edit this entry")."'>";
485 $actions.= "<input class='center' type='image'
486 src='images/edittrash.png' alt='"._("delete")."' name='appl_del_%KEY%' title='"._("Delete this entry")."'>";
487 }
490 // Defining Links
491 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
493 // image Buttons
494 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
495 $userimg = "<img class='center' src='images/select_groups.png' alt='User' title='%s'>";
497 // Extension images
498 $applimg = "<img class='center' src='images/select_application.png' alt='A' title='"._("Application")."'>";
500 // Space
501 $empty = "<img class='center' src='images/empty.png' style='width:16px;height:16px;' alt=''>";
504 $divlist = new divlist("applicationtabs");
505 $divlist->SetHeader(array(
506 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
507 array("string" => _("Application name")." / "._("Department"), "attach" => "style=''"),
508 array("string" => _("Actions"), "attach" => "style='width:80px;border-right:0px;text-align:right;'")
509 ));
512 $divlist->SetSummary(_("This table displays all groups, in the selected tree."));
513 $divlist->SetEntriesPerPage(0);
515 foreach($this->departments as $key=> $val){
517 if(!isset($this->config->departments[trim($key)])){
518 $this->config->departments[trim($key)]="";
519 }
521 $non_empty="";
522 $keys= str_replace("/","\/",$key);
523 foreach($this->config->departments as $keyd=>$vald ){
524 if(preg_match("/".$keys."\/.*/",$keyd)){
525 $non_empty="full";
526 }
527 }
529 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
530 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
531 $field3 = array("string" => " ", "attach" => "style='width:80px;border-right:0px;text-align:right;'");
533 $divlist->AddEntry(array($field1,$field2,$field3));
534 }
536 foreach($this->applications as $key => $val){
537 $title = "title='dn : ".$val['dn']."'";
539 if(!isset($val['description'][0])){
540 $desc = "";
541 }else{
542 $desc = " - [ ".$val['description'][0]." ]";
543 }
544 $field1 = array("string" => sprintf($applimg,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
545 $field2 = array("string" => sprintf($editlink,$key,($val['cn']['0'].$desc)), "attach" => "style='' ".$title);
546 $field3 = array("string" => preg_replace("/%KEY%/", $key, $actions), "attach" => "style='width:80px;border-right:0px;text-align:right;'");
548 $divlist->AddEntry(array($field1,$field2,$field3));
549 }
551 $smarty->assign("applicationshead", $listhead);
552 $smarty->assign("applications", $divlist->DrawList());
553 $smarty->assign("search_image", get_template_path('images/search.png'));
554 $smarty->assign("tree_image", get_template_path('images/tree.png'));
555 $smarty->assign("infoimage", get_template_path('images/info.png'));
556 $smarty->assign("launchimage", get_template_path('images/launch.png'));
557 $smarty->assign("releaseimage" , get_template_path('images/branch.png'));
558 $smarty->assign("deplist", $this->config->idepartments);
559 $smarty->assign("regex", $appfilter['regex']);
561 $smarty->assign("releases", $this->Releases );
562 $smarty->assign("releaseKeys", array_flip($this->Releases));
563 $smarty->assign("select_release",$this->Release);
565 /* Extend if we are not using javascript */
566 $smarty->assign("apply", apply_filter());
567 $smarty->assign("alphabet", generate_alphabet());
568 $smarty->assign("hint", print_sizelimit_warning());
570 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
571 }
574 function reload()
575 {
576 /* Get config */
577 $appfilter= get_global('appfilter');
579 /* Set base for all searches */
580 $base= $this->Release;
582 /* Regex filter? */
583 if ($appfilter['regex'] != ""){
584 $regex= $appfilter['regex'];
585 } else {
586 $regex= "*";
587 }
589 /* Generate application list */
590 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)(objectClass=gosaApplication))", FALSE, $base, array("*"), FALSE);
591 $this->applications= array();
592 foreach ($res as $value){
593 $this->applications[]= $value;
594 }
595 reset ($this->applications);
599 /* NEW LIST MANAGMENT
600 * We also need to search for the departments
601 * So we are able to navigate like in konquerer
602 */
603 $peopleOU = get_people_ou();
605 $base2 = $appfilter['depselect'];
607 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
608 TRUE, $base2, array("ou", "description"), TRUE);
610 $this->departments= array();
611 $tmp = array();
612 foreach ($res3 as $value){
613 $tmp[strtolower($value['dn']).$value['dn']]=$value;
614 }
615 ksort($tmp);
616 foreach($tmp as $value){
617 if(isset($value["description"][0])){
618 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
619 }else{
620 $this->departments[$value['dn']]=convert_department_dn2($value['dn']);//$value["description"][0];
621 }
622 }
625 /* END NEW LIST MANAGMENT
626 */
628 $tmp=array();
629 foreach($this->applications as $tkey => $val ){
630 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
631 }
632 ksort($tmp);
633 $this->applications=array();
634 foreach($tmp as $val){
635 $this->applications[]=$val;
636 }
637 reset ($this->applications);
640 }
642 function remove_from_parent()
643 {
644 /* Optionally execute a command after we're done */
645 $this->postremove();
646 }
649 /* Save data to object */
650 function save_object()
651 {
652 }
655 /* Check values */
656 function check()
657 {
658 }
661 /* Save to LDAP */
662 function save()
663 {
664 /* Optionally execute a command after we're done */
665 $this->postcreate();
666 }
668 function adapt_from_template($dn)
669 {
670 }
672 function password_change_needed()
673 {
674 }
676 function show_header($button_text, $text, $disabled= FALSE)
677 {
678 }
680 function remove_lock()
681 {
682 if (isset($this->apptabs->dn)){
683 del_lock ($this->apptabs->dn);
684 }
685 }
687 }
688 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
689 ?>