Code

Added logging to fax extensions
[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;
26   function blocklist ($config, $ui)
27   {
28     /* Init class */
29     $this->ui     = $ui;
30     $this->dn     = "";
31     $this->config = $config;
32     $this->base = $_SESSION['CurrentMainBase']; 
33     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
34     $this->set_acl_category("gofaxlist");
35     $this->set_acl_base($this->base);
36   }
38   function execute()
39   {
40     /* Call parent execute */
41     plugin::execute();
43     $_SESSION['LOCK_VARS_TO_USE'] = array("/^goFaxBlocklist_/","/^act$/","/^id$/","/^remove_multiple_blocklists/","/^item_selected/");
45     /***************
46       Init vars 
47      ***************/
49     /* Get global smarty instance */
50     $smarty       = get_smarty();
51     $s_action     = "";                       // Contains the action to proceed
52     $s_entry      = "";                       // The value for s_action
53     $base_back    = "";                       // The Link for Backbutton
56     /***************
57       Fetch posts 
58      ***************/
60     /* Test Posts */
61     foreach($_POST as $key => $val){
62       // Post for delete
63       if(preg_match("/^goFaxBlocklist_del.*/",$key)){
64         $s_action = "del";
65         $s_entry  = preg_replace("/^goFaxBlocklist_del_/i","",$key);
66         // Post for edit
67       }elseif(preg_match("/^goFaxBlocklist_edit_.*/",$key)){
68         $s_action="edit";
69         $s_entry  = preg_replace("/^goFaxBlocklist_edit_/i","",$key);
70         // Post for new
71       }elseif(preg_match("/^goFaxBlocklist_new.*/",$key)){
72         $s_action="new";
73       }elseif(preg_match("/^remove_multiple_blocklists/",$key)){
74         $s_action="del_multiple";
75       }
76     }
78     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
79       $s_action ="edit";
80       $s_entry  = $_GET['id'];
81     }
83     $s_entry  = preg_replace("/_.$/","",$s_entry);
86     /***************
87       Cancel some dialogs  
88      ***************/
90     /* Cancel dialog */
91     if (isset($_POST['edit_cancel']) || 
92         isset($_POST['delete_blocklist_cancel']) ||
93         isset($_POST['delete_blocklist_confirm']) || 
94         isset($_POST['delete_lock'])){
96       del_lock ($this->dn);
97       if(!isset($_POST['delete_blocklist_confirm'])){
98         $this->clear_fields();
99       }
100       unset($_SESSION['objectinfo']);
101     }
104     /***************
105       Create a new blocklist (dialog)
106      ***************/
108     /* Create new blocklist? */
109     if ($s_action=="new"){
110       $this->clear_fields();
111       $this->dn= "new";
112       plugin::plugin ($this->config, $this->dn);
113     }
116     /***************
117       Edit blocklist 
118      ***************/
120     /* Edit selected blocklist? */
121     if (($s_action=="edit") && (empty($this->dn))){
122       $this->clear_fields();
123       $this->dn=$this->blocklists[$s_entry]['dn']; 
124       if (($user= get_lock($this->dn)) != ""){
125         $_SESSION['dn']= $this->dn;
126         //$this->dn ="";
127         return(gen_locked_message($user, $this->dn));
128       } else {
129 # Lock this dn for editing
130         add_lock ($this->dn, $this->ui->dn);
132         plugin::plugin ($this->config, $this->dn);
134         /* Load missing lists */
135         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
136           if(isset($this->attrs["goFaxSBlocklist"])){
137             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
138               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
139             }
140           }
141           $this->type= 0;
142         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
143           if(isset($this->attrs["goFaxRBlocklist"])){
144             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
145               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
146             }
147           }
148           $this->type= 1;
149         }
150         $_SESSION['objectinfo']= trim($this->dn);
151       }
152     }
155     /********************
156       Delete MULTIPLE entries requested, display confirm dialog
157      ********************/
159     if ($s_action=="del_multiple"){
161       $this->dns = array();
162       $ids = $this->list_get_selected_items();
164       if(count($ids)){
165         foreach($ids as $id){
166           $dn = $this->blocklists[$id]['dn'];
167           if (($user= get_lock($dn)) != ""){
168             return(gen_locked_message ($user, $dn));
169           }
170           $this->dns[$id] = $dn;
171         }
173         $dns_names = "<br><pre>";
174         foreach($this->dns as $dn){
175           add_lock ($dn, $this->ui->dn);
176           $dns_names .= $dn."\n";
177         }
178         $dns_names .="</pre>";
180         /* Lock the current entry, so nobody will edit it during deletion */
181         $smarty->assign("info",     sprintf(_("You're about to delete the following blocklists(s) %s"), @LDAP::fix($dns_names)));
182         $smarty->assign("multiple", true);
183         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
184       }
185     }
188     /********************
189       Delete MULTIPLE entries confirmed
190      ********************/
192     /* Confirmation for deletion has been passed. Users should be deleted. */
193     if (isset($_POST['delete_multiple_blocklist_confirm'])){
195       /* Remove user by user and check acls before removeing them */
196       foreach($this->dns as $key => $dn){
197         $acl = $this->ui->get_permissions($dn, "blocklist/blocklist");
198         if (preg_match('/d/', $acl)){
199           $this->dn = $dn;
200           $this->remove_from_parent();
201         } else {
202           print_red (sprintf(_("You are not allowed to delete the blocklist '%s'!"),$dn));
203         }
204         /* Remove lock file after successfull deletion */
205         del_lock ($dn);
206         unset($this->dns[$key]);
207       }
208       $this->reload();
209     }
212     /********************
213       Delete MULTIPLE entries Canceled
214      ********************/
216     /* Remove lock */
217     if(isset($_POST['delete_multiple_blocklist_cancel'])){
218       foreach($this->dns as $key => $dn){
219         del_lock ($dn);
220         unset($this->dns[$key]);
221       }
222     }
225     /***************
226       Remove blocklist
227      ***************/
229     /* Delete blocklist requested */
230     if ($s_action=="del"){
231       $this->dn=$this->blocklists[$s_entry]['dn'];
232       /* Check locking */
233       if (($user= get_lock($this->dn)) != ""){
234         $_SESSION['dn']= $this->dn;
235         return(gen_locked_message($user, $this->dn));
236       } else {
237 # Lock this dn for editing
238         add_lock ($this->dn, $this->ui->dn);
240         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
241         $smarty->assign("multiple", false);
242         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
243       }
244     }
247     /***************
248       Remove blocklist confirmed
249      ***************/
251     /* Finally delete blocklist */
252     if (isset($_POST['delete_blocklist_confirm'])){
253       if ($this->acl_is_removeable()){
254         $this->remove_from_parent();
255         gosa_log ("Blocklist object'".$this->dn."' has been removed");
256         $this->reload ();
257       } else {
258         print_red (_("You have no permission to remove this blocklist."));
259       }
260     }
263     /***************
264       Add numer to blocklist
265      ***************/
268     /* Handle interactions: add */
269     if (isset($_POST['add_number']) && $_POST['number'] != ""){
270       if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
271         $this->addNumber ($_POST['number']);
272       } else {
273         print_red (_("Please specify a valid phone number."));
274       }
275     }
278     /***************
279       Delete number from list
280      ***************/
282     /* Handle interactions: delete */
283     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
284       $this->delNumber ($_POST['numbers']);
285     }
288     /***************
289       Edit finished
290      ***************/
292     /* What about finish? */
293     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
294       $message= $this->check();
295       $this->remove_lock();
297       /* No errors, save object */
298       if (count ($message) == 0){
300         /* Perpare 'dn' in case of new entries */
301         if ($this->dn == "new"){
302           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
303         }
305         $this->save ();
306         gosa_log ("Blocklist object'".$this->dn."' has been saved");
307       
308         if (!isset($_POST['edit_apply'])){
309           $this->dn= "";
310           del_lock ($this->ui->dn);
311           unset($_SESSION['objectinfo']);
312         }
313       } else {
314         /* Errors found, show message */
315         show_errors ($message);
316       }
317     }
320     /***************
321       Object currently in edit  
322      ***************/
324     if($this->dn){
326       $dn = $this->dn;
328       /* Log view */
329       if(!$this->view_logged){
330         $this->view_logged = TRUE;
331         @log::log("view","users/".get_class($this),$this->dn);
332       }
333  
334       /* set Account states, this is nescessary for get_allowed_bases */
335       $this->initially_was_account= true;
336       $this->is_account = true;
337       if($dn == "new"){
338         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
339         $this->initially_was_account= false;
340       }
342       $this->set_acl_category("gofaxlist");
343       $this->set_acl_base($dn);
345       /* Base select dialog */
346       $once = true;
347       foreach($_POST as $name => $value){
348         if(preg_match("/^chooseBase/",$name) && $once){
349           $once = false;
350           $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
351           $this->dialog->setCurrentBase($this->base);
352         }
353       }
355       /* Dialog handling */
356       if(is_object($this->dialog)){
357         /* Must be called before save_object */
358         $this->dialog->save_object();
360         if($this->dialog->isClosed()){
361           $this->dialog = false;
362         }elseif($this->dialog->isSelected()){
364           /* A new base was selected, check if it is a valid one */
365           $tmp = $this->get_allowed_bases();
366           if(isset($tmp[$this->dialog->isSelected()])){
367             $this->base = $this->dialog->isSelected();
368           }
369           $this->dialog= false;
371         }else{
372           return($this->dialog->execute());
373         }
374       }
376       /* Set base */
377       if ($this->base == ""){
378         if ($this->dn == "new"){
379           $ui= get_userinfo();
380           $this->base= dn2base($ui->dn);
381         } else {
382           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
383         }
384       }
386       /* Fill templating stuff */
387       $smarty->assign("bases", $this->get_allowed_bases());
389       $smarty->assign("base_select", $this->base);
390       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
391       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
392         $smarty->assign("selectmode", "");
393         $smarty->assign("mode", "");
394         $smarty->assign("apply", "0");
395       } else {
396         $smarty->assign("selectmode", "disabled");
397         $smarty->assign("mode", "readonly");
398         $smarty->assign("apply", "1");
399       }
401       $tmp = $this->plInfo();
402       foreach($tmp['plProvidedAcls'] as $name => $translation){
403         $smarty->assign($name."ACL",$this->getacl($name));
404       }
406       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
407         $smarty->assign("$val", $this->$val);
408       }
410       /* Lock blocklist type for non new entries */
411       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
413       /* Show main page */
414       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
416     }else{
418       /***************
419         Divlist dialog 
420        ***************/
422       /* Check if there is a snapshot dialog open */
423       $base = $this->DivListBlocklist->selectedBase;
424       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
425         return($str);
426       }
428       /* Display dialog with system list */
429       $this->DivListBlocklist->parent = $this;
430       $this->DivListBlocklist->execute();
432       /* Add departments if subsearch is disabled */
433       if(!$this->DivListBlocklist->SubSearch){
434         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase,3,1);
435       }
436       $this->reload();
437       $this->DivListBlocklist->setEntries($this->blocklists);
438       return($this->DivListBlocklist->Draw());
439     }
440   }
443   function list_get_selected_items()
444   {
445     $ids = array();
446     foreach($_POST as $name => $value){
447       if(preg_match("/^item_selected_[0-9]*$/",$name)){
448         $id   = preg_replace("/^item_selected_/","",$name);
449         $ids[$id] = $id;
450       }
451     }
452     return($ids);
453   }
456   /* Return departments, that will be included within snapshot detection */
457   function get_used_snapshot_bases()
458   {
459     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
460   }
463   /* Clear garbage from edited lists */
464   function clear_fields()
465   {
466     $this->dn= "";
467     $this->description= "";
468     $this->cn= "";
469     $this->base= $_SESSION['CurrentMainBase'];
470     $this->view_logged = FALSE;
471     $this->goFaxBlocklist= array();
472   }
475   /* Reload the list of known blocklists */
476   function reload()
477   {
478     /* Init some vars */
479     $filter = $filter2      = "";
480     $base                   = $this->DivListBlocklist->selectedBase;
481     $Regex                  = $this->DivListBlocklist->Regex;
482     $SubSearch              = $this->DivListBlocklist->SubSearch;
483     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
484     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
485     $Flags                  = GL_SIZELIMIT;
486     $res = $res2            = array();
488     /* Append subsearch to Flags */
489     if($SubSearch){
490       $Flags |= GL_SUBSEARCH;
491     }else{
492       $base = "ou=gofax,ou=systems,".$base;
493     }  
495     /* Create filter */
496     if ($ShowSendBocklists){
497       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
498       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
499     }
500     if ($ShowReceiveBlocklists){
501       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
502       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
503     }
504    
505     $this->blocklists = array_merge($res,$res2);
507     /* appen && sort */
508     $tmp=array();
509     foreach($this->blocklists as $tkey => $val ){
511       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
512       if(preg_match("/r/",$acl)){
513         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
514       }
515     }
516     ksort($tmp);
517     $this->blocklists=array();
518     foreach($tmp as $val){
519       $this->blocklists[]=$val;
520     }
521     reset ($this->blocklists);
522   }
525   function remove_from_parent()
526   {
527     $ldap= $this->config->get_ldap_link();
528     $ldap->rmDir($this->dn);
530     @log::log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
531   
532     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
533     $this->clear_fields();
534     $this->handle_post_events("remove");
535   }
538   /* Save data to object */
539   function save_object()
540   {
541     $this->DivListBlocklist->save_object();
543     /* Create a base backup and reset the
544        base directly after calling plugin::save_object();
545        Base will be set seperatly a few lines below */
546     $base_tmp = $this->base;
547     plugin::save_object();
548     $this->base = $base_tmp;
550     /* Save base, since this is no LDAP attribute */
551     $tmp = $this->get_allowed_bases();
552     if(isset($_POST['base'])){
553       if(isset($tmp[$_POST['base']])){
554         $this->base= $_POST['base'];
555       }
556     }
558     foreach($this->attributes as $attr){
559       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
560         $this->$attr = $_POST[$attr];
561       }
562     }
563     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
564       $this->type = $_POST['type'];
565     }
566   }
569   /* Check values */
570   function check()
571   {
572     /* Call common method to give check the hook */
573     $message= plugin::check();
575     /* Permissions for that base? */
576     if ($this->base != ""){
577       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
578     } else {
579       $new_dn= $this->dn;
580     }
582     /* check syntax: must cn */
583     if ($this->cn == ""){
584       $message[]= _("Required field 'Name' is not set.");
585     } else {
586       if (!is_uid($this->cn)){
587         $message[]= _("Required field 'Name' contains invalid characters");
588       }
589       if ($this->dn == 'new'){
590         $ldap= $this->config->get_ldap_link();
591         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
592         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
593         if ($ldap->count() != 0){
594           $message[]= _("Specified name is already used.");
595         }
596       }
597     }
599     return $message;
600   }
603   /* Save to LDAP */
604   function save()
605   {
606     plugin::save();
609     /* Type selection */
610     if ($this->type == 0){
611       $type= "goFaxSBlock";
612     } else {
613       $type= "goFaxRBlock";
614     }
616     /* Add list */
617     $this->attrs['objectClass']= $type;
618     if (count($this->goFaxBlocklist)){
619       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
620     }
622     /* Write back to ldap */
623     $ldap= $this->config->get_ldap_link();
624     $ldap->cd($this->base);
625     $ldap->search("cn=$this->cn",array("cn"));
626     $ldap->cat($this->dn, array('dn'));
627     $a= $ldap->fetch();
629     if (count($a)){
630       if (!isset($this->attrs["$type"."list"])){
631         $this->attrs["$type"."list"]= array();
632       }
633       $ldap->cd($this->dn);
634       $this->cleanup();
635       $ldap->modify($this->attrs);
636       @log::log("modify","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
637       $this->handle_post_events("modify");
638     } else {
639       $ldap->cd($this->config->current['BASE']);
640       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
641       $ldap->cd($this->dn);
642       $ldap->add($this->attrs);
643       @log::log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
644       $this->handle_post_events("add");
645     }
646     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
647   }
650   /* Add number */
651   function addNumber($number)
652   {
653     if (!in_array($number, $this->goFaxBlocklist)){
654       $this->goFaxBlocklist[]= $number;
655       sort($this->goFaxBlocklist);
656     }
657   }
660   /* Remove number from list */
661   function delNumber($numbers)
662   {
663     $tmp= array();
664     foreach ($this->goFaxBlocklist as $val){
665       if (!in_array($val, $numbers)){
666         $tmp[]= $val;
667       }
668     }
669     $this->goFaxBlocklist= $tmp;
670   }
673   /* Delete lock */
674   function remove_lock()
675   {
676     if (isset($this->dn)){
677       del_lock ($this->dn);
678     }
679   }
681   /* Return plugin informations for acl handling */
682   function plInfo()
683   {
684     return (array(  
685           "plShortName"       => _("Fax"),
686           "plDescription"     => _("Fax blocklists"),
687           "plSelfModify"      => FALSE,
688           "plDepends"         => array(),
690           "plPriority"    => 0,
691           "plSection"     => array("administration" => _("FAX Blocklists")),
692           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
693                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
694           "plProvidedAcls" => array(
695             "cn"              => _("Name"),
696             "description"     => _("Description"),
697             "base"            => _("Base"),
698             "goFaxBlocklist"  => _("Blocklist"),
699             "type"            => _("Blocklist type"))
700           ));
701   }
704 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
705 ?>