Code

8c7df503314b40986a6a055610f6bd732255cd75
[gosa.git] / plugins / gofax / blocklists / class_blocklistManagement.inc
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 $base             = "";
19   var $goFaxBlocklist   = array();
20   var $blocklists       = array();
21   var $attributes       = array("cn", "description");
22   var $objectclasses    = array();
23   var $DivListBlocklist = NULL;
24   var $view_logged = FALSE;
25   var $CopyPasteHandler = NULL;
27   function blocklist (&$config, $ui)
28   {
29     /* Init class */
30     $this->ui     = $ui;
31     $this->dn     = "";
32     $this->config = &$config;
33     $this->base = $_SESSION['CurrentMainBase']; 
34     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
35     $this->set_acl_category("gofaxlist");
36     $this->set_acl_base($this->base);
37   }
39   function execute()
40   {
41     /* Call parent execute */
42     plugin::execute();
44     $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFaxBlocklist_/","/^act$/","/^id$/","/^remove_multiple_blocklists/","/^item_selected/");
46     /***************
47       Init vars 
48      ***************/
50     /* Get global smarty instance */
51     $smarty       = get_smarty();
52     $s_action     = "";                       // Contains the action to proceed
53     $s_entry      = "";                       // The value for s_action
54     $base_back    = "";                       // The Link for Backbutton
57     /***************
58       Fetch posts 
59      ***************/
61     /* Test Posts */
62     foreach($_POST as $key => $val){
63       // Post for delete
64       if(preg_match("/^goFaxBlocklist_del.*/",$key)){
65         $s_action = "del";
66         $s_entry  = preg_replace("/^goFaxBlocklist_del_/i","",$key);
67         // Post for edit
68       }elseif(preg_match("/^goFaxBlocklist_edit_.*/",$key)){
69         $s_action="edit";
70         $s_entry  = preg_replace("/^goFaxBlocklist_edit_/i","",$key);
71         // Post for new
72       }elseif(preg_match("/^goFaxBlocklist_new.*/",$key)){
73         $s_action="new";
74       }elseif(preg_match("/^remove_multiple_blocklists/",$key)){
75         $s_action="del_multiple";
76       }
77     }
79     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
80       $s_action ="edit";
81       $s_entry  = $_GET['id'];
82     }
84     $s_entry  = preg_replace("/_.$/","",$s_entry);
86     
89     
90     
91     /* handle C&P from layers menu */
92 #   if(isset($_POST['menu_action']) && preg_match("/^multiple_copy_systems/",$_POST['menu_action'])){
93 #     $s_action = "copy_multiple";
94 #   }
95 #   if(isset($_POST['menu_action']) && preg_match("/^multiple_cut_systems/",$_POST['menu_action'])){
96 #     $s_action = "cut_multiple";
97 #   }
98 #   if(isset($_POST['menu_action']) && preg_match("/^editPaste/",$_POST['menu_action'])){
99 #     $s_action = "editPaste";
100 #   }
103     /* Create options */
104     if(isset($_POST['menu_action']) && $_POST['menu_action'] == "goFaxBlocklist_new"){
105       $s_action = "new";
106     }
108     /* handle remove from layers menu */
109     if(isset($_POST['menu_action']) && preg_match("/^remove_multiple/",$_POST['menu_action'])){
110       $s_action = "del_multiple";
111     }
114     /***************
115       Cancel some dialogs  
116      ***************/
118     /* Cancel dialog */
119     if (isset($_POST['edit_cancel']) || 
120         isset($_POST['delete_blocklist_cancel']) ||
121         isset($_POST['delete_blocklist_confirm']) || 
122         isset($_POST['delete_lock'])){
124       del_lock ($this->dn);
125       if(!isset($_POST['delete_blocklist_confirm'])){
126         $this->clear_fields();
127       }
128       unset($_SESSION['objectinfo']);
129     }
132     /***************
133       Create a new blocklist (dialog)
134      ***************/
136     /* Create new blocklist? */
137     if ($s_action=="new"){
138       $this->clear_fields();
139       $this->dn= "new";
140       plugin::plugin ($this->config, $this->dn);
141     }
144     /***************
145       Edit blocklist 
146      ***************/
148     /* Edit selected blocklist? */
149     if (($s_action=="edit") && (empty($this->dn))){
150       $this->clear_fields();
151       $this->dn=$this->blocklists[$s_entry]['dn']; 
152       if (($user= get_lock($this->dn)) != ""){
153         $_SESSION['dn']= $this->dn;
154         //$this->dn ="";
155         return(gen_locked_message($user, $this->dn));
156       } else {
157 # Lock this dn for editing
158         add_lock ($this->dn, $this->ui->dn);
160         plugin::plugin ($this->config, $this->dn);
162         /* Load missing lists */
163         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
164           if(isset($this->attrs["goFaxSBlocklist"])){
165             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
166               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
167             }
168           }
169           $this->type= 0;
170         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
171           if(isset($this->attrs["goFaxRBlocklist"])){
172             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
173               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
174             }
175           }
176           $this->type= 1;
177         }
178         $_SESSION['objectinfo']= trim($this->dn);
179       }
180     }
183     /********************
184       Delete MULTIPLE entries requested, display confirm dialog
185      ********************/
187     if ($s_action=="del_multiple"){
189       $this->dns = array();
190       $ids = $this->list_get_selected_items();
192       if(count($ids)){
193         foreach($ids as $id){
194           $dn = $this->blocklists[$id]['dn'];
195           if (($user= get_lock($dn)) != ""){
196             return(gen_locked_message ($user, $dn));
197           }
198           $this->dns[$id] = $dn;
199         }
201         $dns_names = "<br><pre>";
202         foreach($this->dns as $dn){
203           add_lock ($dn, $this->ui->dn);
204           $dns_names .= $dn."\n";
205         }
206         $dns_names .="</pre>";
208         /* Lock the current entry, so nobody will edit it during deletion */
209         $smarty->assign("info",     sprintf(_("You're about to delete the following blocklists(s) %s"), @LDAP::fix($dns_names)));
210         $smarty->assign("multiple", true);
211         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
212       }
213     }
216     /********************
217       Delete MULTIPLE entries confirmed
218      ********************/
220     /* Confirmation for deletion has been passed. Users should be deleted. */
221     if (isset($_POST['delete_multiple_blocklist_confirm'])){
223       /* Remove user by user and check acls before removeing them */
224       foreach($this->dns as $key => $dn){
225         $acl = $this->ui->get_permissions($dn, "blocklist/blocklist");
226         if (preg_match('/d/', $acl)){
227           $this->dn = $dn;
228           $this->remove_from_parent();
229         } else {
230           print_red (sprintf(_("You are not allowed to delete the blocklist '%s'!"),$dn));
231         }
232         /* Remove lock file after successfull deletion */
233         del_lock ($dn);
234         unset($this->dns[$key]);
235       }
236       $this->reload();
237     }
240     /********************
241       Delete MULTIPLE entries Canceled
242      ********************/
244     /* Remove lock */
245     if(isset($_POST['delete_multiple_blocklist_cancel'])){
246       foreach($this->dns as $key => $dn){
247         del_lock ($dn);
248         unset($this->dns[$key]);
249       }
250     }
253     /***************
254       Remove blocklist
255      ***************/
257     /* Delete blocklist requested */
258     if ($s_action=="del"){
259       $this->dn=$this->blocklists[$s_entry]['dn'];
260       /* Check locking */
261       if (($user= get_lock($this->dn)) != ""){
262         $_SESSION['dn']= $this->dn;
263         return(gen_locked_message($user, $this->dn));
264       } else {
265 # Lock this dn for editing
266         add_lock ($this->dn, $this->ui->dn);
268         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
269         $smarty->assign("multiple", false);
270         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
271       }
272     }
275     /***************
276       Remove blocklist confirmed
277      ***************/
279     /* Finally delete blocklist */
280     if (isset($_POST['delete_blocklist_confirm'])){
281       if ($this->acl_is_removeable()){
282         $this->remove_from_parent();
283         $this->reload ();
284       } else {
285         print_red (_("You have no permission to remove this blocklist."));
286       }
287     }
290     /***************
291       Add numer to blocklist
292      ***************/
295     /* Handle interactions: add */
296     if (isset($_POST['add_number']) && $_POST['number'] != ""){
297       if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
298         $this->addNumber ($_POST['number']);
299       } else {
300         print_red (_("Please specify a valid phone number."));
301       }
302     }
305     /***************
306       Delete number from list
307      ***************/
309     /* Handle interactions: delete */
310     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
311       $this->delNumber ($_POST['numbers']);
312     }
315     /***************
316       Edit finished
317      ***************/
319     /* What about finish? */
320     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
321       $message= $this->check();
322       $this->remove_lock();
324       /* No errors, save object */
325       if (count ($message) == 0){
327         /* Perpare 'dn' in case of new entries */
328         if ($this->dn == "new"){
329           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
330         }
332         $this->save ();
333       
334         if (!isset($_POST['edit_apply'])){
335           $this->dn= "";
336           del_lock ($this->ui->dn);
337           unset($_SESSION['objectinfo']);
338         }
339       } else {
340         /* Errors found, show message */
341         show_errors ($message);
342       }
343     }
346     /***************
347       Object currently in edit  
348      ***************/
350     if($this->dn){
352       $dn = $this->dn;
354       /* Log view */
355       if(!$this->view_logged){
356         $this->view_logged = TRUE;
357         new log("view","users/".get_class($this),$this->dn);
358       }
359  
360       /* set Account states, this is nescessary for get_allowed_bases */
361       $this->initially_was_account= true;
362       $this->is_account = true;
363       if($dn == "new"){
364         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
365         $this->initially_was_account= false;
366       }
368       $this->set_acl_category("gofaxlist");
369       $this->set_acl_base($dn);
371       /* Base select dialog */
372       $once = true;
373       foreach($_POST as $name => $value){
374         if(preg_match("/^chooseBase/",$name) && $once){
375           $once = false;
376           $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
377           $this->dialog->setCurrentBase($this->base);
378         }
379       }
381       /* Dialog handling */
382       if(is_object($this->dialog)){
383         /* Must be called before save_object */
384         $this->dialog->save_object();
386         if($this->dialog->isClosed()){
387           $this->dialog = false;
388         }elseif($this->dialog->isSelected()){
390           /* A new base was selected, check if it is a valid one */
391           $tmp = $this->get_allowed_bases();
392           if(isset($tmp[$this->dialog->isSelected()])){
393             $this->base = $this->dialog->isSelected();
394           }
395           $this->dialog= false;
397         }else{
398           return($this->dialog->execute());
399         }
400       }
402       /* Set base */
403       if ($this->base == ""){
404         if ($this->dn == "new"){
405           $ui= get_userinfo();
406           $this->base= dn2base($ui->dn);
407         } else {
408           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
409         }
410       }
412       /* Fill templating stuff */
413       $smarty->assign("bases", $this->get_allowed_bases());
415       $smarty->assign("base_select", $this->base);
416       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
417       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
418         $smarty->assign("selectmode", "");
419         $smarty->assign("mode", "");
420         $smarty->assign("apply", "0");
421       } else {
422         $smarty->assign("selectmode", "disabled");
423         $smarty->assign("mode", "readonly");
424         $smarty->assign("apply", "1");
425       }
427       $tmp = $this->plInfo();
428       foreach($tmp['plProvidedAcls'] as $name => $translation){
429         $smarty->assign($name."ACL",$this->getacl($name));
430       }
432       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
433         $smarty->assign("$val", $this->$val);
434       }
436       /* Lock blocklist type for non new entries */
437       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
439       /* Show main page */
440       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
442     }else{
444       /***************
445         Divlist dialog 
446        ***************/
448       /* Check if there is a snapshot dialog open */
449       $base = $this->DivListBlocklist->selectedBase;
450       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
451         return($str);
452       }
454       /* Display dialog with system list */
455       $this->DivListBlocklist->parent = $this;
456       $this->DivListBlocklist->execute();
458       /* Add departments if subsearch is disabled */
459       if(!$this->DivListBlocklist->SubSearch){
460         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase,3,1);
461       }
462       $this->reload();
463       $this->DivListBlocklist->setEntries($this->blocklists);
464       return($this->DivListBlocklist->Draw());
465     }
466   }
469   function list_get_selected_items()
470   {
471     $ids = array();
472     foreach($_POST as $name => $value){
473       if(preg_match("/^item_selected_[0-9]*$/",$name)){
474         $id   = preg_replace("/^item_selected_/","",$name);
475         $ids[$id] = $id;
476       }
477     }
478     return($ids);
479   }
482   /* Return departments, that will be included within snapshot detection */
483   function get_used_snapshot_bases()
484   {
485     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
486   }
489   /* Clear garbage from edited lists */
490   function clear_fields()
491   {
492     $this->dn= "";
493     $this->description= "";
494     $this->cn= "";
495     $this->base= $_SESSION['CurrentMainBase'];
496     $this->view_logged = FALSE;
497     $this->goFaxBlocklist= array();
498   }
501   /* Reload the list of known blocklists */
502   function reload()
503   {
504     /* Init some vars */
505     $filter = $filter2      = "";
506     $base                   = $this->DivListBlocklist->selectedBase;
507     $Regex                  = $this->DivListBlocklist->Regex;
508     $SubSearch              = $this->DivListBlocklist->SubSearch;
509     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
510     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
511     $Flags                  = GL_SIZELIMIT;
512     $res = $res2            = array();
514     /* Append subsearch to Flags */
515     if($SubSearch){
516       $Flags |= GL_SUBSEARCH;
517     }else{
518       $base = "ou=gofax,ou=systems,".$base;
519     }  
521     /* Create filter */
522     if ($ShowSendBocklists){
523       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
524       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
525     }
526     if ($ShowReceiveBlocklists){
527       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
528       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
529     }
530    
531     $this->blocklists = array_merge($res,$res2);
533     /* appen && sort */
534     $tmp=array();
535     foreach($this->blocklists as $tkey => $val ){
537       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
538       if(preg_match("/r/",$acl)){
539         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
540       }
541     }
542     ksort($tmp);
543     $this->blocklists=array();
544     foreach($tmp as $val){
545       $this->blocklists[]=$val;
546     }
547     reset ($this->blocklists);
548   }
551   function remove_from_parent()
552   {
553     $ldap= $this->config->get_ldap_link();
554     $ldap->rmDir($this->dn);
556     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
557   
558     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
559     $this->clear_fields();
560     $this->handle_post_events("remove");
561   }
564   /* Save data to object */
565   function save_object()
566   {
567     $this->DivListBlocklist->save_object();
569     /* Create a base backup and reset the
570        base directly after calling plugin::save_object();
571        Base will be set seperatly a few lines below */
572     $base_tmp = $this->base;
573     plugin::save_object();
574     $this->base = $base_tmp;
576     /* Save base, since this is no LDAP attribute */
577     $tmp = $this->get_allowed_bases();
578     if(isset($_POST['base'])){
579       if(isset($tmp[$_POST['base']])){
580         $this->base= $_POST['base'];
581       }
582     }
584     foreach($this->attributes as $attr){
585       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
586         $this->$attr = $_POST[$attr];
587       }
588     }
589     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
590       $this->type = $_POST['type'];
591     }
592   }
595   /* Check values */
596   function check()
597   {
598     /* Call common method to give check the hook */
599     $message= plugin::check();
601     /* Permissions for that base? */
602     if ($this->base != ""){
603       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
604     } else {
605       $new_dn= $this->dn;
606     }
608     /* check syntax: must cn */
609     if ($this->cn == ""){
610       $message[]= _("Required field 'Name' is not set.");
611     } else {
612       if (!is_uid($this->cn)){
613         $message[]= _("Required field 'Name' contains invalid characters");
614       }
615       if ($this->dn == 'new'){
616         $ldap= $this->config->get_ldap_link();
617         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
618         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
619         if ($ldap->count() != 0){
620           $message[]= _("Specified name is already used.");
621         }
622       }
623     }
625     return $message;
626   }
629   /* Save to LDAP */
630   function save()
631   {
632     plugin::save();
635     /* Type selection */
636     if ($this->type == 0){
637       $type= "goFaxSBlock";
638     } else {
639       $type= "goFaxRBlock";
640     }
642     /* Add list */
643     $this->attrs['objectClass']= $type;
644     if (count($this->goFaxBlocklist)){
645       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
646     }
648     /* Write back to ldap */
649     $ldap= $this->config->get_ldap_link();
650     $ldap->cd($this->base);
651     $ldap->search("cn=$this->cn",array("cn"));
652     $ldap->cat($this->dn, array('dn'));
653     $a= $ldap->fetch();
655     if (count($a)){
656       if (!isset($this->attrs["$type"."list"])){
657         $this->attrs["$type"."list"]= array();
658       }
659       $ldap->cd($this->dn);
660       $this->cleanup();
661       $ldap->modify($this->attrs);
662       new log("modify","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
663       $this->handle_post_events("modify");
664     } else {
665       $ldap->cd($this->config->current['BASE']);
666       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
667       $ldap->cd($this->dn);
668       $ldap->add($this->attrs);
669       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
670       $this->handle_post_events("add");
671     }
672     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
673   }
676   /* Add number */
677   function addNumber($number)
678   {
679     if (!in_array($number, $this->goFaxBlocklist)){
680       $this->goFaxBlocklist[]= $number;
681       sort($this->goFaxBlocklist);
682     }
683   }
686   /* Remove number from list */
687   function delNumber($numbers)
688   {
689     $tmp= array();
690     foreach ($this->goFaxBlocklist as $val){
691       if (!in_array($val, $numbers)){
692         $tmp[]= $val;
693       }
694     }
695     $this->goFaxBlocklist= $tmp;
696   }
699   /* Delete lock */
700   function remove_lock()
701   {
702     if (isset($this->dn)){
703       del_lock ($this->dn);
704     }
705   }
707   /* Return plugin informations for acl handling */
708   function plInfo()
709   {
710     return (array(  
711           "plShortName"       => _("Fax"),
712           "plDescription"     => _("Fax blocklists"),
713           "plSelfModify"      => FALSE,
714           "plDepends"         => array(),
716           "plPriority"    => 0,
717           "plSection"     => array("administration" => _("FAX Blocklists")),
718           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
719                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
720           "plProvidedAcls" => array(
721             "cn"              => _("Name"),
722             "description"     => _("Description"),
723             "base"            => _("Base"),
724             "goFaxBlocklist"  => _("Blocklist"),
725             "type"            => _("Blocklist type"))
726           ));
727   }
730 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
731 ?>