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 }
46 }
48 function execute()
49 {
50 /* Call parent execute */
51 plugin::execute();
52 /* Save data */
53 $macrofilter= get_global("macrofilter");
54 foreach( array("depselect", "regex") as $type){
55 if (isset($_POST[$type])){
56 $macrofilter[$type]= $_POST[$type];
57 }
58 }
59 if (isset($_GET['search'])){
60 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
61 if ($s == "**"){
62 $s= "*";
63 }
64 $macrofilter['regex']= $s;
65 }
67 $s_action = ""; // Contains the action to proceed
68 $s_entry = ""; // The value for s_action
69 $base_back = ""; // The Link for Backbutton
71 /* Start for New List Managment */
72 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
73 $s_action="open";
74 $s_entry = base64_decode($_GET['dep_id']);
75 $macrofilter['depselect']= "".$this->config->departments[trim($s_entry)];
76 }
78 /* Test Posts */
79 foreach($_POST as $key => $val){
80 // Post for delete
81 if(preg_match("/user_del.*/",$key)){
82 $s_action = "del";
83 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
84 // Post for edit
85 }elseif(preg_match("/user_edit_.*/",$key)){
86 $s_action="edit";
87 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
88 // Post for new
89 }elseif(preg_match("/dep_back.*/i",$key)){
90 $s_action="back";
91 }elseif(preg_match("/user_new.*/",$key)){
92 $s_action="new";
93 }elseif(preg_match("/dep_home.*/i",$key)){
94 $s_action="home";
95 }elseif(preg_match("/user_chgpw.*/i",$key)){
96 $s_action="change_pw";
97 $s_entry = preg_replace("/user_chgpw_/i","",$key);
98 }elseif(preg_match("/dep_root.*/i",$key)){
99 $s_action="root";
100 }
101 }
102 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
103 $s_action ="edit";
104 $s_entry = $_GET['id'];
105 }
107 $s_entry = preg_replace("/_.$/","",$s_entry);
109 /* Department changed? */
110 if(isset($_POST['depselect']) && $_POST['depselect']){
111 $macrofilter['depselect']= $_POST['depselect'];
112 }
114 /* Homebutton is posted */
115 if($s_action=="home"){
116 $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
117 $macrofilter['depselect']=(preg_replace("/^[^,]+,/","",$macrofilter['depselect']));
118 }
120 if($s_action=="root"){
121 $macrofilter['depselect']=($this->config->current['BASE']);
122 }
125 /* If Backbutton is Posted */
126 if($s_action=="back"){
127 $base_back = preg_replace("/^[^,]+,/","",$macrofilter['depselect']);
128 $base_back = convert_department_dn($base_back);
130 if(isset($this->config->departments[trim($base_back)])){
131 $macrofilter['depselect']= $this->config->departments[trim($base_back)];
132 }else{
133 $macrofilter['depselect']= $this->config->departments["/"];
134 }
135 }
137 register_global("macrofilter", $macrofilter);
138 $this->reload();
140 $smarty= get_smarty();
142 /* Check for exeeded sizelimit */
143 if (($message= check_sizelimit()) != ""){
144 return($message);
145 }
147 /* New macro? */
148 if ($s_action=="new"){
150 /* By default we set 'dn' to 'new', all relevant plugins will
151 react on this. */
152 $this->dn= "new";
154 /* Create new usertab object */
155 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
156 $this->macrotabs->set_acl(array(':all'));
157 }
159 /* Cancel dialogs */
160 if (isset($_POST['edit_cancel']) || isset($_POST['password_cancel'])){
161 del_lock ($this->macrotabs->dn);
162 unset ($this->macrotabs);
163 $this->macrotabs= NULL;
164 unset ($_SESSION['objectinfo']);
165 }
167 /* Finish mac edit is triggered by the tabulator dialog, so
168 the user wants to save edited data. Check and save at this
169 point. */
170 if (isset($_POST['edit_finish'])){
172 /* Check tabs, will feed message array */
173 $this->macrotabs->last= $this->macrotabs->current;
174 $this->macrotabs->save_object();
175 $message= $this->macrotabs->check();
177 /* Save, or display error message? */
178 if (count($message) == 0){
180 /* Save data data to ldap */
181 $this->macrotabs->save();
182 gosa_log ("Macro object'".$this->dn."' has been saved");
184 /* macro has been saved successfully, remove lock from
185 LDAP. */
186 if ($this->dn != "new"){
187 del_lock ($this->dn);
188 }
190 /* There's no page reload so we have to read new mac at
191 this point. */
192 $this->reload ();
193 unset ($this->macrotabs);
194 $this->macrotabs= NULL;
195 unset ($_SESSION['objectinfo']);
196 } else {
197 /* Ok. There seem to be errors regarding to the tab data,
198 show message and continue as usual. */
199 show_errors($message);
200 }
201 }
203 /* User wants to edit data? */
204 if ($s_action=="edit"){
206 /* Get 'dn' from posted 'macrolists', must be unique */
207 $this->dn= $this->macros[$s_entry]['dn'];
209 /* Check locking, save current plugin in 'back_plugin', so
210 the dialog knows where to return. */
211 if (($user= get_lock($this->dn)) != ""){
212 return(gen_locked_message ($user, $this->dn));
213 }
215 /* Lock the current entry, so everyone will get the
216 above dialog */
217 add_lock ($this->dn, $this->ui->dn);
219 /* Set up the users ACL's for this 'dn' */
220 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
222 /* Register macrotabs to trigger edit dialog */
223 $this->macrotabs= new macrotabs($this->config,
224 $this->config->data['TABS']['MACROTABS'], $this->dn);
225 $this->macrotabs->set_acl($acl);
226 $_SESSION['objectinfo']= $this->dn;
227 }
229 /* Remove user was requested */
230 if ($s_action=="del"){
232 /* Get 'dn' from posted 'uid' */
233 $this->dn= $this->macros[$s_entry]['dn'];
235 /* Load permissions for selected 'dn' and check if
236 we're allowed to remove this 'dn' */
237 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
238 $this->acl= get_module_permission($acl, "macro", $this->dn);
239 if (chkacl($this->acl, "delete") == ""){
241 /* Check locking, save current plugin in 'back_plugin', so
242 the dialog knows where to return. */
243 if (($user= get_lock($this->dn)) != ""){
244 return (gen_locked_message ($user, $this->dn));
245 }
247 /* Lock the current entry, so nobody will edit it during deletion */
248 add_lock ($this->dn, $this->ui->dn);
249 $smarty= get_smarty();
250 $smarty->assign("intro", sprintf(_("You're about to delete the macro '%s'."), $this->dn));
251 return($smarty->fetch (get_template_path('remove.tpl', TRUE)));
252 } else {
254 /* Obviously the user isn't allowed to delete. Show message and
255 clean session. */
256 print_red (_("You are not allowed to delete this macro!"));
257 }
258 }
260 /* Confirmation for deletion has been passed. Group should be deleted. */
261 if (isset($_POST['delete_macro_confirm'])){
263 /* Some nice guy may send this as POST, so we've to check
264 for the permissions again. */
265 if (chkacl($this->acl, "delete") == ""){
267 /* Delete request is permitted, perform LDAP action */
268 $this->macrotabs= new macrotabs($this->config,$this->config->data['TABS']['MACROTABS'], $this->dn);
269 $this->macrotabs->set_acl(array($this->acl));
271 $this->macrotabs->delete ();
272 gosa_log ("macro object'".$this->dn."' has been removed");
273 unset ($this->macrotabs);
274 $this->macrotabs= NULL;
276 /* Group list has changed, reload it. */
277 $this->reload ();
278 } else {
279 /* Normally this shouldn't be reached, send some extra
280 logs to notify the administrator */
281 print_red (_("You are not allowed to delete this macro!"));
282 gosa_log ("Warning: '".$this->ui->uid."' tried to trick group deletion.");
283 }
284 /* Remove lock file after successfull deletion */
285 del_lock ($this->dn);
286 }
289 /* Delete macro canceled? */
290 if (isset($_POST['delete_cancel'])){
291 del_lock ($this->dn);
292 unset($_SESSION['objectinfo']);
293 }
295 /* Show tab dialog if object is present */
296 if ($this->macrotabs){
297 $display= $this->macrotabs->execute();
299 /* Don't show buttons if tab dialog requests this */
300 if (!$this->macrotabs->by_object[$this->macrotabs->current]->dialog){
301 $display.= "<p style=\"text-align:right\">\n";
302 $display.= "<input type=submit name=\"edit_finish\" value=\""._("Finish")."\">\n";
303 $display.= " \n";
304 $display.= "<input type=submit name=\"edit_cancel\" value=\""._("Cancel")."\">\n";
305 $display.= "</p>";
306 }
307 return ($display);
308 }
313 /* Prepare departments */
314 $options= "";
315 foreach ($this->config->idepartments as $key => $value){
316 if ($macrofilter['depselect'] == $key){
317 $options.= "<option selected value='$key'>$value</option>";
318 } else {
319 $options.= "<option value='$key'>$value</option>";
320 }
321 }
324 /* NEW LIST MANAGMENT */
325 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
326 " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
327 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root"). "'> ".
328 " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
329 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
330 " <input type='image' align='middle' src='images/list_new_macro.png' title='"._("Create new phone macro")."' alt='"._("New")."' name='user_new'> ".
331 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
332 _("Current base")." <select name='depselect' onChange='mainform.submit()'>$options</select>".
333 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
334 "</div>";
337 /* Options */
338 $action= "<input type='image' src='images/edit.png' alt='"._("edit")."' name='user_edit_%KEY%' title='"._("Edit user")."'>";
339 $action.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='user_del_%KEY%' title='"._("Delete user")."'>";
341 $divlist = new divlist("macrotabs");
342 $divlist->SetHeader(array(
343 array("string" => " "),
344 array("string" => _("Username")." / "._("Department")),
345 array("string" => _("Visible"), "attach" => "style='width:30px;'"),
346 array("string" => _("Actions"), "attach" => "style='border:none'" )));
349 $divlist->SetSummary(_("This table displays all phone macros, in the selected tree."));
350 $divlist->SetEntriesPerPage(0);
352 // Defining Links
353 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
355 foreach($this->departments as $key=> $val){
357 if(!isset($this->config->departments[trim($key)])){
358 $this->config->departments[trim($key)]="";
359 }
361 $non_empty="";
362 $keys= str_replace("/","\/",$key);
363 foreach($this->config->departments as $keyd=>$vald ){
364 if(preg_match("/".$keys."\/.*/",$keyd)){
365 $non_empty="full";
366 }
367 }
369 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>");
370 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
371 $field3 = array("string" => " ");
372 $field4 = array("string" => " ", "attach" => "style='border:none'");
373 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
374 }
376 // Pictures for Extensions
377 $empty = "<img src='images/nothing.png' style='width:16px;height:16px;' alt=''>";
379 // User and Template Images
380 $macroimg = "<img src='images/list_macro.png' alt='"._("Macro")."' title='%s'>";
381 $visible = "<img src='images/true.png' alt='"._("yes")."' title='"._("visible")."'>";
382 $invisible = "<img src='images/false.png' alt='"._("no")."'title='"._("invisible")."'>";
384 // Link for edit
385 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
387 // Test Every Entry and generate divlist Array
388 foreach($this->macros as $key => $val){
389 // Generate Array to Add
390 $display= $val["displayName"][0];
393 if(isset($val['goFonMacroVisible']['0'])&&($val['goFonMacroVisible']['0'] == "1")){
394 $pic1 = $visible;
395 }else{
396 $pic1 = $invisible;
397 }
399 $field1 = array("string" => sprintf($macroimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
400 $field2 = array("string" => sprintf($editlink,$key,$display),"attach"=>"title='".$val['dn']."'");
401 $field3 = array("string" => $pic1, "attach" => "style='width:20px;text-align:right;'");
402 $field4 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='border:none;text-align:right;width:48px'");
404 $divlist->AddEntry(array($field1,$field2,$field3,$field4));
405 }
407 /* Show main page */
408 $smarty->assign("macroshead", $listhead);
409 $smarty->assign("macros", $divlist->DrawList());
410 $smarty->assign("search_image", get_template_path('images/search.png'));
411 $smarty->assign("tree_image", get_template_path('images/tree.png'));
412 $smarty->assign("infoimage", get_template_path('images/info.png'));
413 $smarty->assign("launchimage", get_template_path('images/launch.png'));
414 $smarty->assign("deplist", $this->config->idepartments);
415 foreach( array("depselect", "regex") as $type){
416 $smarty->assign("$type", $macrofilter[$type]);
417 }
419 /* Extend if we are not using javascript */
420 $smarty->assign("apply", apply_filter());
421 $smarty->assign("alphabet", generate_alphabet());
422 $smarty->assign("hint", print_sizelimit_warning());
424 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
425 }
428 function reload()
429 {
430 /* Get config */
431 $macrofilter= get_global('macrofilter');
433 /* Set base for all searches */
434 $base= $macrofilter['depselect'];
436 /* Regex filter? */
437 if ($macrofilter['regex'] != ""){
438 $regex= $macrofilter['regex'];
439 } else {
440 $regex= "*";
441 }
443 /* Generate macro list */
444 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)(objectClass=goFonMacro))", FALSE, "ou=macros,ou=asterisk,ou=configs,ou=systems,".$base, array("*"), TRUE);
445 $this->macros= $res;
447 /* NEW LIST MANAGMENT
448 * We also need to search for the departments
449 * So we are able to navigate like in konquerer
450 */
451 $base2 = preg_replace("/ou=people,/i","",$base);
453 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
454 TRUE, $base2, array("ou", "description"), TRUE);
456 $this->departments= array();
457 $tmp = array();
458 foreach ($res3 as $value){
459 $tmp[strtolower($value['dn']).$value['dn']]=$value;
460 }
461 ksort($tmp);
462 foreach($tmp as $value){
463 if($value["description"][0]!=".."){
464 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
465 }else{
466 $this->departments[$value['dn']]=$value["description"][0];
467 }
468 }
470 /* END NEW LIST MANAGMENT
471 */
473 $tmp=array();
474 foreach($this->macros as $tkey => $val ){
475 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
476 }
477 ksort($tmp);
478 $this->macros=array();
479 foreach($tmp as $val){
480 $this->macros[]=$val;
481 }
483 reset ($this->macros);
484 }
486 function remove_from_parent()
487 {
488 /* Optionally execute a command after we're done */
489 $this->postremove();
490 }
493 /* Save data to object */
494 function save_object()
495 {
496 }
499 /* Check values */
500 function check()
501 {
502 }
505 /* Save to LDAP */
506 function save()
507 {
508 /* Optionally execute a command after we're done */
509 $this->postcreate();
510 }
512 function adapt_from_template($dn)
513 {
514 }
516 function password_change_needed()
517 {
518 }
520 function show_header($button_text, $text, $disabled= FALSE)
521 {
522 }
524 function remove_lock()
525 {
526 if (isset($this->macrotabs->dn)){
527 del_lock ($this->macrotabs->dn);
528 }
529 }
531 }
532 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
533 ?>