6786efdc6dd8ae66a080fbe79b5af4a745b6ef14
1 <?php
2 class blocklist extends plugin
3 {
4 /* Definitions */
5 var $plHeadline= "FAX Blocklists";
6 var $plDescription= "This does something";
8 /* CLI vars */
9 var $cli_summary= "Handling of GOfax block lists";
10 var $cli_description= "Some longer text\nfor help";
11 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
13 /* blocklist attributes */
14 var $cn= "";
15 var $description= "";
16 var $type= "";
17 var $goFaxBlocklist= array();
18 var $base= "";
20 /* Headpage attributes */
21 var $blocklists= array();
23 /* attribute list for save action */
24 var $attributes= array("cn", "description");
25 var $objectclasses= array();
27 function blocklist ($config, $ui)
28 {
29 $this->ui= $ui;
30 $this->dn= "";
31 $this->config= $config;
33 /* Get global filter config */
34 if (!is_global("blockfilter")){
35 $uii= get_userinfo();
36 $base= get_base_from_people($ui->dn);
37 $blockfilter= array("sendlists" => "checked",
38 "receivelists" => "checked",
39 "entry" => "*",
40 "depselect" => $base,
41 "regex" => "*");
42 register_global("blockfilter", $blockfilter);
43 }else{
44 $blockfilter = $_SESSION["blockfilter"];
45 $this->base = $blockfilter['depselect'];
46 }
47 }
49 function execute()
50 {
51 /* Call parent execute */
52 plugin::execute();
54 /* Get global smarty instance */
55 $smarty= get_smarty();
56 $blockfilter = get_global("blockfilter"); // contains Filter Settings
57 $s_action = ""; // Contains the action to proceed
58 $s_entry = ""; // The value for s_action
59 $base_back = ""; // The Link for Backbutton
61 /* Start for New List Managment */
62 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
63 $s_action="open";
64 $s_entry = base64_decode($_GET['dep_id']);
65 $blockfilter['depselect']= "".$this->config->departments[trim($s_entry)];
66 $this->reload();
67 }
69 /* Test Posts */
70 foreach($_POST as $key => $val){
71 // Post for delete
72 if(preg_match("/user_del.*/",$key)){
73 $s_action = "del";
74 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
75 // Post for edit
76 }elseif(preg_match("/user_edit_.*/",$key)){
77 $s_action="edit";
78 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
79 // Post for new
80 }elseif(preg_match("/dep_back.*/i",$key)){
81 $s_action="back";
82 }elseif(preg_match("/user_new.*/",$key)){
83 $s_action="new";
84 }elseif(preg_match("/dep_home.*/i",$key)){
85 $s_action="home";
86 }elseif(preg_match("/user_tplnew.*/i",$key)){
87 $s_action="new_tpl";
88 }elseif(preg_match("/user_chgpw.*/i",$key)){
89 $s_action="change_pw";
90 $s_entry = preg_replace("/user_chgpw_/i","",$key);
91 }elseif(preg_match("/dep_root.*/i",$key)){
92 $s_action="root";
93 }
94 }
98 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
99 $s_action ="edit";
100 $s_entry = $_GET['id'];
101 }
103 $s_entry = preg_replace("/_.$/","",$s_entry);
105 /* Department changed? */
106 if(isset($_POST['depselect']) && $_POST['depselect']){
107 $blockfilter['depselect']= $_POST['depselect'];
108 $this->reload();
109 }
111 /* Homebutton is posted */
112 if($s_action=="home"){
113 $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
114 $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$blockfilter['depselect']));
115 $this->reload();
116 }
118 if($s_action=="root"){
119 $blockfilter['depselect']=($this->config->current['BASE']);
120 $this->reload();
121 }
123 /* If Backbutton is Posted */
124 if($s_action=="back"){
125 $base_back = preg_replace("/^[^,]+,/","",$blockfilter['depselect']);
126 $base_back = convert_department_dn($base_back);
128 if(isset($this->config->departments[trim($base_back)])){
129 $blockfilter['depselect']= $this->config->departments[trim($base_back)];
130 }else{
131 $blockfilter['depselect']= $this->config->departments["/"];
132 }
133 $this->reload();
134 }
136 register_global("blockfilter", $blockfilter);
138 /* Save data */
139 $blockfilter= get_global("blockfilter");
140 foreach( array( "entry", "regex") as $type){
141 if (isset($_POST[$type])){
142 $blockfilter[$type]= $_POST[$type];
143 }
144 }
145 if (isset($_POST['depselect'])){
146 foreach( array("sendlists", "receivelists") as $type){
148 if (isset($_POST[$type])) {
149 $blockfilter[$type]= "checked";
150 } else {
151 $blockfilter[$type]= "";
152 }
153 }
154 }
155 if (isset($_GET['search'])){
156 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
157 if ($s == "**"){
158 $s= "*";
159 }
160 $blockfilter['regex']= $s;
161 }
162 register_global("blockfilter", $blockfilter);
164 /* Create new blocklist? */
165 if ($s_action=="new"){
166 $this->clear_fields();
167 $this->acl= "#all#";
168 $this->dn= "new";
169 plugin::plugin ($this->config, $this->dn);
170 }
172 /* Edit selected blocklist? */
173 if ($s_action=="edit"){
174 $this->clear_fields();
175 $this->dn=$this->blocklists[$s_entry]['dn'];
176 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
177 $this->acl= get_module_permission($this->acl, "blocklists", $this->dn);
178 plugin::plugin ($this->config, $this->dn);
180 /* Load missing lists */
181 if (isset($this->attrs["goFaxSBlocklist"])){
182 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
183 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
184 }
185 $this->type= 0;
186 } elseif (isset($this->attrs["goFaxRBlocklist"])){
187 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
188 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
189 }
190 $this->type= 1;
191 }
192 $_SESSION['objectinfo']= trim($this->dn);
193 }
195 /* Delete blocklist requested */
196 if ($s_action=="del"){
197 $this->dn=$this->blocklists[$s_entry]['dn'];
198 /* Check locking */
199 if (($user= get_lock($this->dn)) != ""){
200 $_SESSION['dn']= $this->dn;
201 return(gen_locked_message($user, $this->dn));
202 } else {
203 # Lock this dn for editing
204 add_lock ($this->dn, $this->ui->dn);
206 $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
207 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
208 }
209 }
211 /* Finally delete blocklist */
212 if (isset($_POST['delete_blocklist_confirm'])){
213 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
214 $acl= get_module_permission($acl, "blocklists", $this->dn);
215 if (chkacl($acl, "all") == ""){
216 $this->remove_from_parent();
217 gosa_log ("Blocklist object'".$this->dn."' has been removed");
218 $this->reload ();
219 } else {
220 print_red (_("You have no permission to remove this blocklist."));
221 }
222 }
224 /* Cancel dialog */
225 if (isset($_POST['edit_cancel']) || isset($_POST['delete_blocklist_cancel']) ||
226 isset($_POST['delete_blocklist_confirm']) || isset($_POST['delete_lock'])){
228 del_lock ($this->dn);
229 $this->clear_fields();
230 unset($_SESSION['objectinfo']);
231 }
233 /* Handle interactions: add */
234 if (isset($_POST['add_number']) && $_POST['number'] != ""){
235 if (is_phone_nr($_POST['number'])){
236 $this->addNumber ($_POST['number']);
237 } else {
238 print_red (_("Please specify a valid phone number."));
239 }
240 }
242 /* Handle interactions: delete */
243 if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
244 $this->delNumber ($_POST['numbers']);
245 }
247 /* What about finish? */
248 if (isset($_POST['edit_finish'])){
249 $message= $this->check();
251 /* No errors, save object */
252 if (count ($message) == 0){
254 /* Perpare 'dn' in case of new entries */
255 if ($this->dn == "new"){
256 $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
257 }
259 $this->save ();
260 gosa_log ("Blocklist object'".$this->dn."' has been saved");
261 $this->acl= "#none#";
262 $this->dn= "";
263 del_lock ($this->ui->dn);
264 unset($_SESSION['objectinfo']);
265 } else {
266 /* Errors found, show message */
267 show_errors ($message);
268 }
269 }
271 /* Headpage or normal plugin screen? */
272 if ($this->dn == ""){
273 /* Check sorting variable */
274 $this->reload();
276 /* Check for exeeded sizelimit */
277 if (($message= check_sizelimit()) != ""){
278 return($message);
279 }
281 $options= "";
282 foreach ($this->config->idepartments as $key => $value){
283 if ($blockfilter['depselect'] == $key){
284 $options.= "<option selected value='$key'>$value</option>";
285 } else {
286 $options.= "<option value='$key'>$value</option>";
287 }
288 }
292 /* NEW LIST MANAGMENT */
293 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
294 " <input type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
295 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
296 " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
297 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
298 " <input type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'> ".
299 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> "._("Current base")." <select name='depselect' onChange='mainform.submit()'>$options</select>".
300 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
301 "</div>";
303 $action= "<input type='image' src='images/edit.png' alt='"._("edit")."' name='user_edit_%KEY%' title='"._("Edit user")."'>";
304 $action.= "<input type='image' src='images/edittrash.png' alt='"._("delete")."' name='user_del_%KEY%' title='"._("Delete user")."'>";
307 $divlist = new divlist("faxtabs");
308 $divlist->SetHeader(array(
309 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
310 array("string" => _("Blocklist name")." / "._("Department"), "attach" => "style=''"),
311 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
314 $divlist->SetSummary(_("This table displays faxblocklists for the selected tree."));
315 $divlist->SetEntriesPerPage(0);
317 // Defining Links
318 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
319 foreach($this->departments as $key=> $val){
320 if(!isset($this->config->departments[trim($key)])){
321 $this->config->departments[trim($key)]="";
322 }
324 $non_empty="";
325 $keys= str_replace("/","\/",$key);
326 foreach($this->config->departments as $keyd=>$vald ){
327 if(preg_match("/".$keys."\/.*/",$keyd)){
328 $non_empty="full";
329 }
330 }
332 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
333 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
334 $field3 = array("string" => " ", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
335 $divlist->AddEntry(array($field1,$field2,$field3));
336 }
338 // User and Template Images
339 $blockimg = "<img src='images/list_blocklist.png' alt='User' title='%s'>";
340 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
342 foreach($this->blocklists as $key => $val){
343 // Generate Array to Add
344 $display= "[".$val["cn"][0]."]";
345 $field1 = array("string" => sprintf($blockimg,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
346 $field2 = array("string" => sprintf($editlink,$key,$display), "attach" => "style='' title='".$val['dn']."'");
347 $field3 = array("string" => preg_replace("/%KEY%/", "$key", $action), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
348 $divlist->AddEntry( array($field1,$field2,$field3));
349 }
351 /* Show main page */
352 $smarty->assign("blocklistshead", $listhead);
353 $smarty->assign("blocklists", $divlist->DrawList());
354 $smarty->assign("search_image", get_template_path('images/search.png'));
355 $smarty->assign("tree_image", get_template_path('images/tree.png'));
356 $smarty->assign("infoimage", get_template_path('images/info.png'));
357 $smarty->assign("launchimage", get_template_path('images/launch.png'));
358 $smarty->assign("apply", apply_filter());
359 foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
360 $smarty->assign("$type", $blockfilter[$type]);
361 }
362 $smarty->assign("deplist", $this->config->idepartments);
363 $smarty->assign("alphabet", generate_alphabet());
364 $smarty->assign("hint", print_sizelimit_warning());
366 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
367 }
369 /* Set base */
370 if ($this->base == ""){
371 if ($this->dn == "new"){
372 $ui= get_userinfo();
373 $this->base= dn2base($ui->dn);
374 } else {
375 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
376 }
377 }
379 /* Fill templating stuff */
380 $smarty->assign("bases", $this->config->idepartments);
381 $smarty->assign("base_select", $_SESSION['blockfilter']['depselect']);//$this->base);
382 $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
383 if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
384 $smarty->assign("selectmode", "");
385 $smarty->assign("mode", "");
386 } else {
387 $smarty->assign("selectmode", "disabled");
388 $smarty->assign("mode", "readonly");
389 }
390 foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
391 $smarty->assign("$val", $this->$val);
392 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
393 }
395 /* Lock blocklist type for non new entries */
396 if ($this->dn != "new"){
397 $smarty->assign('typeACL', "disabled");
398 }
400 /* Show main page */
401 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
402 }
404 function clear_fields()
405 {
406 $this->dn= "";
407 $this->description= "";
408 $this->cn= "";
409 $this->base= "";
410 $this->goFaxBlocklist= array();
411 $this->acl= "#none#";
412 }
414 function reload()
415 {
416 /* Get config */
417 $blockfilter= get_global('blockfilter');
419 /* Set base for all searches */
420 $base= $blockfilter['depselect'];
421 $filter= "";
423 /* Regex filter? */
424 if ($blockfilter['regex'] != ""){
425 $regex= $blockfilter['regex'];
426 } else {
427 $regex= "*";
428 }
430 /* Entry filter? */
431 /* Get list of blocklists to be shown */
432 if ($blockfilter['sendlists'] == "checked"){
433 $filter.= "(objectClass=goFaxSBlock)";
434 }
435 if ($blockfilter['receivelists'] == "checked"){
436 $filter.= "(objectClass=goFaxRBlock)";
437 }
438 if ($filter != ""){
439 $filter= "(|$filter)";
440 }
442 if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
443 $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
444 }
446 /* Generate blocklists */
447 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", FALSE, "ou=gofax,ou=systems,".$base, array("*"));
449 $this->blocklists=$res;
451 /* NEW LIST MANAGMENT
452 * We also need to search for the departments
453 * So we are able to navigate like in konquerer
454 */
455 $base2 = preg_replace("/ou=people,/i","",$base);
456 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
457 TRUE, $base2, array("ou", "description"), TRUE);
459 $this->departments= array();
460 $tmp = array();
461 foreach ($res3 as $value){
462 $tmp[strtolower($value['dn']).$value['dn']]=$value;
463 }
464 ksort($tmp);
465 foreach($tmp as $value){
466 if($value["description"][0]!=".."){
467 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
468 }else{
469 $this->departments[$value['dn']]=$value["description"][0];
470 }
471 }
473 /* END NEW LIST MANAGMENT
474 */
475 $tmp=array();
476 foreach($this->blocklists as $tkey => $val ){
477 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
478 }
479 ksort($tmp);
480 $this->blocklists=array();
481 foreach($tmp as $val){
482 $this->blocklists[]=$val;
483 }
484 reset ($this->blocklists);
485 }
487 function remove_from_parent()
488 {
489 $ldap= $this->config->get_ldap_link();
490 $ldap->rmDir($this->dn);
491 $this->handle_post_events("remove");
492 }
495 /* Save data to object */
496 function save_object()
497 {
498 plugin::save_object();
500 /* Save type, needed to detect objectClass */
501 if (isset($_POST['type']) && chkacl($this->acl, "cn") == ""){
502 $this->type= $_POST['type'];
503 }
505 /* Save base, since this is no LDAP attribute */
506 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
507 $this->base= $_POST['base'];
508 }
509 }
512 /* Check values */
513 function check()
514 {
515 $message= array();
517 /* Permissions for that base? */
518 if ($this->base != ""){
519 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
520 } else {
521 $new_dn= $this->dn;
522 }
524 $ui= get_userinfo();
525 $acl= get_permissions ($new_dn, $ui->subtreeACL);
526 $acl= get_module_permission($acl, "blocklist", $new_dn);
527 if (chkacl($acl, "create") != ""){
528 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
529 }
532 /* check syntax: must cn */
533 if ($this->cn == ""){
534 $message[]= _("Required field 'Name' is not set.");
535 } else {
536 if (!is_uid($this->cn)){
537 $message[]= _("Required field 'Name' contains invalid characters");
538 }
539 if ($this->dn == 'new'){
540 $ldap= $this->config->get_ldap_link();
541 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
542 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))".
543 "(cn=".$this->cn."))", array("cn"));
544 if ($ldap->count() != 0){
545 $message[]= _("Specified name is already used.");
546 }
547 }
548 }
550 /* Is user allowed to create in 'base'? */
551 if (chkacl($this->acl, "create") != ""){
552 $message[]= _("No permission to create a blocklist on this base.");
553 }
555 return $message;
556 }
559 /* Save to LDAP */
560 function save()
561 {
562 plugin::save();
564 /* Type selection */
565 if ($this->type == 0){
566 $type= "goFaxSBlock";
567 } else {
568 $type= "goFaxRBlock";
569 }
571 /* Add list */
572 $this->attrs['objectClass']= $type;
573 if (count($this->goFaxBlocklist)){
574 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
575 }
577 /* Write back to ldap */
578 $ldap= $this->config->get_ldap_link();
579 $ldap->cd($ldap->getParentDir($this->dn));
580 $ldap->search("cn=$this->cn");
581 $a= $ldap->fetch();
582 if (count($a)){
583 if (!isset($this->attrs["$type"."list"])){
584 $this->attrs["$type"."list"]= array();
585 }
586 $ldap->cd($this->dn);
587 $ldap->modify($this->attrs);
588 $this->handle_post_events("modify");
589 } else {
590 $ldap->cd($this->config->current['BASE']);
591 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
592 $ldap->cd($this->dn);
593 $ldap->add($this->attrs);
594 $this->handle_post_events("add");
595 }
596 show_ldap_error($ldap->get_error());
597 }
600 function addNumber($number)
601 {
602 if (!in_array($number, $this->goFaxBlocklist)){
603 $this->goFaxBlocklist[]= $number;
604 sort($this->goFaxBlocklist);
605 }
606 }
608 function delNumber($numbers)
609 {
610 $tmp= array();
611 foreach ($this->goFaxBlocklist as $val){
612 if (!in_array($val, $numbers)){
613 $tmp[]= $val;
614 }
615 }
616 $this->goFaxBlocklist= $tmp;
617 }
619 function remove_lock()
620 {
621 if (isset($this->dn)){
622 del_lock ($this->dn);
623 }
624 }
626 }
627 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
628 ?>