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_macros.inc";
22 class gofonMacro extends plugin
23 {
24 /* Definitions */
25 var $plHeadline = "Phone macros";
26 var $plDescription = "This does something";
28 /* Dialog attributes */
29 var $macrotabs = NULL;
30 var $macros = array();
31 var $ui = NULL;
32 var $acl = "";
34 function gofonMacro($config, $ui)
35 {
36 /* Save configuration for internal use */
37 $this->config= $config;
38 $this->ui= $ui;
40 /* Get global filter config */
41 if (!is_global("macrofilter")){
42 $base= get_base_from_people($ui->dn);
43 $macrofilter= array("depselect" => $base, "regex" => "*");
44 register_global("macrofilter", $macrofilter);
45 }
47 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
48 $this->acl= get_module_permission($acl, "goFonMacro", $ui->dn);
49 }
51 function execute()
52 {
53 /* Call parent execute */
54 plugin::execute();
56 /* Save data */
57 $macrofilter= get_global("macrofilter");
58 foreach( array("depselect", "regex") as $type){
59 if (isset($_POST[$type])){
60 $macrofilter[$type]= $_POST[$type];
61 }
62 }
63 if (isset($_GET['search'])){
64 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
65 if ($s == "**"){
66 $s= "*";
67 }
68 $macrofilter['regex']= $s;
69 }
71 $s_action = ""; // Contains the action to proceed
72 $s_entry = ""; // The value for s_action
73 $base_back = ""; // The Link for Backbutton
75 /* Start for New List Managment */
76 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
77 $s_action="open";
78 $s_entry = base64_decode($_GET['dep_id']);
79 $macrofilter['depselect']= "".$this->config->departments[trim($s_entry)];
80 }
82 /* Test Posts */
83 foreach($_POST as $key => $val){
84 // Post for delete
85 if(preg_match("/user_del.*/",$key)){
86 $s_action = "del";
87 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
88 // Post for edit
89 }elseif(preg_match("/user_edit_.*/",$key)){
90 $s_action="edit";
91 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
92 // Post for new
93 }elseif(preg_match("/dep_back.*/i",$key)){
94 $s_action="back";
95 }elseif(preg_match("/user_new.*/",$key)){
96 $s_action="new";
97 }elseif(preg_match("/dep_home.*/i",$key)){
98 $s_action="home";
99 }elseif(preg_match("/user_chgpw.*/i",$key)){
100 $s_action="change_pw";
101 $s_entry = preg_replace("/user_chgpw_/i","",$key);
102 }elseif(preg_match("/dep_root.*/i",$key)){
103 $s_action="root";
104 }
105 }
106 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
107 $s_action ="edit";
108 $s_entry = $_GET['id'];
109 }
111 $s_entry = preg_replace("/_.$/","",$s_entry);
113 /* Department changed? */
114 if(isset($_POST['depselect']) && $_POST['depselect']){
115 $macrofilter['depselect']= $_POST['depselect'];
116 }
118 /* Homebutton is posted */
119 if($s_action=="home"){
120 $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
121 $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$macrofilter['depselect']));
122 }
124 if($s_action=="root"){
125 $macrofilter['depselect']=($this->config->current['BASE']);
126 }
129 /* If Backbutton is Posted */
130 if($s_action=="back"){
131 $base_back = preg_replace("/^[^,]+,/","",$macrofilter['depselect']);
132 $base_back = convert_department_dn($base_back);
134 if(isset($this->config->departments[trim($base_back)])){
135 $macrofilter['depselect']= $this->config->departments[trim($base_back)];
136 }else{
137 $macrofilter['depselect']= $this->config->departments["/"];
138 }
139 }
141 register_global("macrofilter", $macrofilter);
142 $this->reload();
144 $smarty= get_smarty();
146 /* Check for exeeded sizelimit */
147 if (($message= check_sizelimit()) != ""){
148 return($message);
149 }
151 /* New macro? */
152 if ($s_action=="new"){
154 /* By default we set 'dn' to 'new', all relevant plugins will
155 react on this. */
156 $this->dn= "new";
158 /* Create new usertab object */
159 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
160 }
162 /* Cancel dialogs */
163 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
164 del_lock ($this->macrotabs->dn);
165 unset ($this->macrotabs);
166 $this->macrotabs= NULL;
167 unset ($_SESSION['objectinfo']);
168 }
170 /* Finish mac edit is triggered by the tabulator dialog, so
171 the user wants to save edited data. Check and save at this
172 point. */
173 if ((isset($_POST['edit_finish'])) && (isset($this->macrotabs->config))){
175 /* Check tabs, will feed message array */
176 $this->macrotabs->last= $this->macrotabs->current;
177 $this->macrotabs->save_object();
178 $message= $this->macrotabs->check();
180 /* Save, or display error message? */
181 if (count($message) == 0){
183 /* Save data data to ldap */
184 $this->macrotabs->save();
185 gosa_log ("Macro object'".$this->dn."' has been saved");
187 /* macro has been saved successfully, remove lock from
188 LDAP. */
189 if ($this->dn != "new"){
190 del_lock ($this->dn);
191 }
193 /* There's no page reload so we have to read new mac at
194 this point. */
195 $this->reload ();
196 unset ($this->macrotabs);
197 $this->macrotabs= NULL;
198 unset ($_SESSION['objectinfo']);
199 } else {
200 /* Ok. There seem to be errors regarding to the tab data,
201 show message and continue as usual. */
202 show_errors($message);
203 }
204 }
206 /* User wants to edit data? */
207 if (($s_action=="edit") && (!isset($this->macrotabs->config))){
209 /* Get 'dn' from posted 'macrolists', must be unique */
210 $this->dn= $this->macros[$s_entry]['dn'];
212 /* Check locking, save current plugin in 'back_plugin', so
213 the dialog knows where to return. */
214 if (($user= get_lock($this->dn)) != ""){
215 return(gen_locked_message ($user, $this->dn));
216 }
218 /* Lock the current entry, so everyone will get the
219 above dialog */
220 add_lock ($this->dn, $this->ui->dn);
221 /* Register macrotabs to trigger edit dialog */
222 $this->macrotabs= new macrotabs($this->config,
223 $this->config->data['TABS']['MACROTABS'], $this->dn);
224 $_SESSION['objectinfo']= $this->dn;
225 }
227 /* Remove user was requested */
228 if ($s_action=="del"){
230 /* Get 'dn' from posted 'uid' */
231 $this->dn= $this->macros[$s_entry]['dn'];
233 /* Load permissions for selected 'dn' and check if
234 we're allowed to remove this 'dn' */
235 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
236 $this->acl= get_module_permission($acl, "goFonMacro", $this->dn);
237 if (chkacl($this->acl, "delete") == ""){
239 /* Check locking, save current plugin in 'back_plugin', so
240 the dialog knows where to return. */
241 if (($user= get_lock($this->dn)) != ""){
242 return (gen_locked_message ($user, $this->dn));
243 }
245 /* Lock the current entry, so nobody will edit it during deletion */
246 add_lock ($this->dn, $this->ui->dn);
247 $smarty= get_smarty();
248 $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
249 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
250 } else {
252 /* Obviously the user isn't allowed to delete. Show message and
253 clean session. */
254 print_red (_("You are not allowed to delete this macro!"));
255 }
256 }
258 /* Confirmation for deletion has been passed. Group should be deleted. */
259 if (isset($_POST['delete_macro_confirm'])){
261 /* Some nice guy may send this as POST, so we've to check
262 for the permissions again. */
263 if (chkacl($this->acl, "delete") == ""){
265 /* Delete request is permitted, perform LDAP action */
266 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
267 $this->macrotabs->set_acl(array($this->acl));
269 $this->macrotabs->delete ();
270 gosa_log ("macro object'".$this->dn."' has been removed");
271 unset ($this->macrotabs);
272 $this->macrotabs= NULL;
274 /* Group list has changed, reload it. */
275 $this->reload ();
276 } else {
277 /* Normally this shouldn't be reached, send some extra
278 logs to notify the administrator */
279 print_red (_("You are not allowed to delete this macro!"));
280 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
281 }
282 /* Remove lock file after successfull deletion */
283 del_lock ($this->dn);
284 }
287 /* Delete macro canceled? */
288 if (isset($_POST['delete_cancel'])){
289 del_lock ($this->dn);
290 unset($_SESSION['objectinfo']);
291 }
293 /* Show tab dialog if object is present */
294 if (($this->macrotabs) && (isset($this->macrotabs->config))){
295 $display= $this->macrotabs->execute();
297 /* Don't show buttons if tab dialog requests this */
298 if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
299 $display.= "<p style=\"text-align:right\">\n";
300 $display.= "<input type=\"submit\" name=\"edit_finish\" value=\""._("Finish")."\">\n";
301 $display.= " \n";
302 $display.= "<input type=\"submit\" name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
303 $display.= "</p>";
304 }
305 return ($display);
306 }
311 /* Prepare departments */
312 $options= "";
313 foreach ($this->config->idepartments as $key => $value){
314 if ($macrofilter['depselect'] == $key){
315 $options.= "<option selected='selected' value='$key'>$value</option>";
316 } else {
317 $options.= "<option value='$key'>$value</option>";
318 }
319 }
322 /* NEW LIST MANAGMENT */
323 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
324 " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root"). "'> ".
325 " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
326 " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
327 " <input class='center' type='image' src='images/list_reload.png' align='middle' title='"._("Reload list")."' name='submit_department' alt='"._("Submit")."'> ".
328 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
329 " <input class='center' type='image' align='middle' src='images/list_new_macro.png' title='"._("Create new phone macro")."' alt='"._("New")."' name='user_new'> ".
330 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
331 _("Base")." <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
332 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
333 "</div>";
336 /* Options */
337 $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='user_edit_%KEY%' title='"._("Edit user")."'>";
338 $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='user_del_%KEY%' title='"._("Delete user")."'>";
340 $divlist = new divlist("macrotabs");
341 $divlist->SetHeader(array(
342 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
343 array("string" => _("Username")." / "._("Department"), "attach" => "style=''"),
344 array("string" => _("Visible"), "attach" => "style='width:50px;'"),
345 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
348 $divlist->SetSummary(_("This table displays all phone macros, in the selected tree."));
349 $divlist->SetEntriesPerPage(0);
351 // Defining Links
352 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
354 foreach($this->departments as $key=> $val){
356 if(!isset($this->config->departments[trim($key)])){
357 $this->config->departments[trim($key)]="";
358 }
360 $non_empty="";
361 $keys= str_replace("/","\/",$key);
362 foreach($this->config->departments as $keyd=>$vald ){
363 if(preg_match("/".$keys."\/.*/",$keyd)){
364 $non_empty="full";
365 }
366 }
368 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
369 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
370 $field3 = array("string" => " ", "attach" => "style='width:50px;'");
371 $field4 = array("string" => " ", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
372 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
373 }
375 // Pictures for Extensions
376 $empty = "<img class='center' src='images/nothing.png' style='width:16px;height:16px;' alt=''>";
378 // User and Template Images
379 $macroimg = "<img class='center' src='images/list_macro.png' alt='"._("Macro")."' title='%s'>";
380 $visible = "<img class='center' src='images/true.png' alt='"._("yes")."' title='"._("visible")."'>";
381 $invisible = "<img class='center' src='images/false.png' alt='"._("no")."'title='"._("invisible")."'>";
383 // Link for edit
384 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
386 // Test Every Entry and generate divlist Array
387 foreach($this->macros as $key => $val){
388 // Generate Array to Add
389 $display= $val["displayName"][0];
392 if(isset($val['goFonMacroVisible']['0'])&&($val['goFonMacroVisible']['0'] == "1")){
393 $pic1 = $visible;
394 }else{
395 $pic1 = $invisible;
396 }
398 $field1 = array("string" => sprintf($macroimg,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
399 $field2 = array("string" => sprintf($editlink,$key,$display), "attach" => "style='' title='".$val['dn']."'");
400 $field3 = array("string" => $pic1, "attach" => "style='width:50px;'");
401 $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
403 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
404 }
406 /* Show main page */
407 $smarty->assign("macroshead", $listhead);
408 $smarty->assign("macros", $divlist->DrawList());
409 $smarty->assign("search_image", get_template_path('images/search.png'));
410 $smarty->assign("tree_image", get_template_path('images/tree.png'));
411 $smarty->assign("infoimage", get_template_path('images/info.png'));
412 $smarty->assign("launchimage", get_template_path('images/launch.png'));
413 $smarty->assign("deplist", $this->config->idepartments);
414 foreach( array("depselect", "regex") as $type){
415 $smarty->assign("$type", $macrofilter[$type]);
416 }
418 /* Extend if we are not using javascript */
419 $smarty->assign("apply", apply_filter());
420 $smarty->assign("alphabet", generate_alphabet());
421 $smarty->assign("hint", print_sizelimit_warning());
423 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
424 }
427 function reload()
428 {
429 /* Get config */
430 $macrofilter= get_global('macrofilter');
432 /* Set base for all searches */
433 $base= $macrofilter['depselect'];
435 /* Regex filter? */
436 if ($macrofilter['regex'] != ""){
437 $regex= $macrofilter['regex'];
438 } else {
439 $regex= "*";
440 }
442 /* Generate macro list */
443 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)(objectClass=goFonMacro))", FALSE, "ou=macros,ou=asterisk,ou=configs,ou=systems,".$base, array("*"), TRUE);
444 $this->macros= $res;
446 /* NEW LIST MANAGMENT
447 * We also need to search for the departments
448 * So we are able to navigate like in konquerer
449 */
450 $peopleOU = get_people_ou();
452 if(empty($peopleOU)){
453 $base2 = $base;
454 }else{
455 $base2 = preg_replace("/".$peopleOU."/i","",$base);
456 }
458 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
459 TRUE, $base2, array("ou", "description"), TRUE);
461 $this->departments= array();
462 $tmp = array();
463 foreach ($res3 as $value){
464 $tmp[strtolower($value['dn']).$value['dn']]=$value;
465 }
466 ksort($tmp);
467 foreach($tmp as $value){
468 if(isset($value["description"][0])){
469 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
470 }else{
471 $this->departments[$value['dn']]=convert_department_dn2($value['dn']);//$value["description"][0];
472 }
473 }
475 /* END NEW LIST MANAGMENT
476 */
478 $tmp=array();
479 foreach($this->macros as $tkey => $val ){
480 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
481 }
482 ksort($tmp);
483 $this->macros=array();
484 foreach($tmp as $val){
485 $this->macros[]=$val;
486 }
488 reset ($this->macros);
489 }
491 function remove_from_parent()
492 {
493 /* Optionally execute a command after we're done */
494 $this->postremove();
495 }
498 /* Save data to object */
499 function save_object()
500 {
501 }
504 /* Check values */
505 function check()
506 {
507 }
510 /* Save to LDAP */
511 function save()
512 {
513 /* Optionally execute a command after we're done */
514 $this->postcreate();
515 }
517 function adapt_from_template($dn)
518 {
519 }
521 function password_change_needed()
522 {
523 }
525 function show_header($button_text, $text, $disabled= FALSE)
526 {
527 }
529 function remove_lock()
530 {
531 if (isset($this->macrotabs->dn)){
532 del_lock ($this->macrotabs->dn);
533 }
534 }
536 }
537 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
538 ?>