Code

Added initial (NOT WORKING) logging classes
[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         $this->reload ();
256       } else {
257         print_red (_("You have no permission to remove this blocklist."));
258       }
259     }
262     /***************
263       Add numer to blocklist
264      ***************/
267     /* Handle interactions: add */
268     if (isset($_POST['add_number']) && $_POST['number'] != ""){
269       if (is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
270         $this->addNumber ($_POST['number']);
271       } else {
272         print_red (_("Please specify a valid phone number."));
273       }
274     }
277     /***************
278       Delete number from list
279      ***************/
281     /* Handle interactions: delete */
282     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
283       $this->delNumber ($_POST['numbers']);
284     }
287     /***************
288       Edit finished
289      ***************/
291     /* What about finish? */
292     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
293       $message= $this->check();
294       $this->remove_lock();
296       /* No errors, save object */
297       if (count ($message) == 0){
299         /* Perpare 'dn' in case of new entries */
300         if ($this->dn == "new"){
301           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
302         }
304         $this->save ();
305       
306         if (!isset($_POST['edit_apply'])){
307           $this->dn= "";
308           del_lock ($this->ui->dn);
309           unset($_SESSION['objectinfo']);
310         }
311       } else {
312         /* Errors found, show message */
313         show_errors ($message);
314       }
315     }
318     /***************
319       Object currently in edit  
320      ***************/
322     if($this->dn){
324       $dn = $this->dn;
326       /* Log view */
327       if(!$this->view_logged){
328         $this->view_logged = TRUE;
329         new log("view","users/".get_class($this),$this->dn);
330       }
331  
332       /* set Account states, this is nescessary for get_allowed_bases */
333       $this->initially_was_account= true;
334       $this->is_account = true;
335       if($dn == "new"){
336         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
337         $this->initially_was_account= false;
338       }
340       $this->set_acl_category("gofaxlist");
341       $this->set_acl_base($dn);
343       /* Base select dialog */
344       $once = true;
345       foreach($_POST as $name => $value){
346         if(preg_match("/^chooseBase/",$name) && $once){
347           $once = false;
348           $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
349           $this->dialog->setCurrentBase($this->base);
350         }
351       }
353       /* Dialog handling */
354       if(is_object($this->dialog)){
355         /* Must be called before save_object */
356         $this->dialog->save_object();
358         if($this->dialog->isClosed()){
359           $this->dialog = false;
360         }elseif($this->dialog->isSelected()){
362           /* A new base was selected, check if it is a valid one */
363           $tmp = $this->get_allowed_bases();
364           if(isset($tmp[$this->dialog->isSelected()])){
365             $this->base = $this->dialog->isSelected();
366           }
367           $this->dialog= false;
369         }else{
370           return($this->dialog->execute());
371         }
372       }
374       /* Set base */
375       if ($this->base == ""){
376         if ($this->dn == "new"){
377           $ui= get_userinfo();
378           $this->base= dn2base($ui->dn);
379         } else {
380           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
381         }
382       }
384       /* Fill templating stuff */
385       $smarty->assign("bases", $this->get_allowed_bases());
387       $smarty->assign("base_select", $this->base);
388       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
389       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
390         $smarty->assign("selectmode", "");
391         $smarty->assign("mode", "");
392         $smarty->assign("apply", "0");
393       } else {
394         $smarty->assign("selectmode", "disabled");
395         $smarty->assign("mode", "readonly");
396         $smarty->assign("apply", "1");
397       }
399       $tmp = $this->plInfo();
400       foreach($tmp['plProvidedAcls'] as $name => $translation){
401         $smarty->assign($name."ACL",$this->getacl($name));
402       }
404       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
405         $smarty->assign("$val", $this->$val);
406       }
408       /* Lock blocklist type for non new entries */
409       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
411       /* Show main page */
412       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
414     }else{
416       /***************
417         Divlist dialog 
418        ***************/
420       /* Check if there is a snapshot dialog open */
421       $base = $this->DivListBlocklist->selectedBase;
422       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
423         return($str);
424       }
426       /* Display dialog with system list */
427       $this->DivListBlocklist->parent = $this;
428       $this->DivListBlocklist->execute();
430       /* Add departments if subsearch is disabled */
431       if(!$this->DivListBlocklist->SubSearch){
432         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase,3,1);
433       }
434       $this->reload();
435       $this->DivListBlocklist->setEntries($this->blocklists);
436       return($this->DivListBlocklist->Draw());
437     }
438   }
441   function list_get_selected_items()
442   {
443     $ids = array();
444     foreach($_POST as $name => $value){
445       if(preg_match("/^item_selected_[0-9]*$/",$name)){
446         $id   = preg_replace("/^item_selected_/","",$name);
447         $ids[$id] = $id;
448       }
449     }
450     return($ids);
451   }
454   /* Return departments, that will be included within snapshot detection */
455   function get_used_snapshot_bases()
456   {
457     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
458   }
461   /* Clear garbage from edited lists */
462   function clear_fields()
463   {
464     $this->dn= "";
465     $this->description= "";
466     $this->cn= "";
467     $this->base= $_SESSION['CurrentMainBase'];
468     $this->view_logged = FALSE;
469     $this->goFaxBlocklist= array();
470   }
473   /* Reload the list of known blocklists */
474   function reload()
475   {
476     /* Init some vars */
477     $filter = $filter2      = "";
478     $base                   = $this->DivListBlocklist->selectedBase;
479     $Regex                  = $this->DivListBlocklist->Regex;
480     $SubSearch              = $this->DivListBlocklist->SubSearch;
481     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
482     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
483     $Flags                  = GL_SIZELIMIT;
484     $res = $res2            = array();
486     /* Append subsearch to Flags */
487     if($SubSearch){
488       $Flags |= GL_SUBSEARCH;
489     }else{
490       $base = "ou=gofax,ou=systems,".$base;
491     }  
493     /* Create filter */
494     if ($ShowSendBocklists){
495       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
496       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
497     }
498     if ($ShowReceiveBlocklists){
499       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
500       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
501     }
502    
503     $this->blocklists = array_merge($res,$res2);
505     /* appen && sort */
506     $tmp=array();
507     foreach($this->blocklists as $tkey => $val ){
509       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
510       if(preg_match("/r/",$acl)){
511         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
512       }
513     }
514     ksort($tmp);
515     $this->blocklists=array();
516     foreach($tmp as $val){
517       $this->blocklists[]=$val;
518     }
519     reset ($this->blocklists);
520   }
523   function remove_from_parent()
524   {
525     $ldap= $this->config->get_ldap_link();
526     $ldap->rmDir($this->dn);
528     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
529   
530     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
531     $this->clear_fields();
532     $this->handle_post_events("remove");
533   }
536   /* Save data to object */
537   function save_object()
538   {
539     $this->DivListBlocklist->save_object();
541     /* Create a base backup and reset the
542        base directly after calling plugin::save_object();
543        Base will be set seperatly a few lines below */
544     $base_tmp = $this->base;
545     plugin::save_object();
546     $this->base = $base_tmp;
548     /* Save base, since this is no LDAP attribute */
549     $tmp = $this->get_allowed_bases();
550     if(isset($_POST['base'])){
551       if(isset($tmp[$_POST['base']])){
552         $this->base= $_POST['base'];
553       }
554     }
556     foreach($this->attributes as $attr){
557       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
558         $this->$attr = $_POST[$attr];
559       }
560     }
561     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
562       $this->type = $_POST['type'];
563     }
564   }
567   /* Check values */
568   function check()
569   {
570     /* Call common method to give check the hook */
571     $message= plugin::check();
573     /* Permissions for that base? */
574     if ($this->base != ""){
575       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
576     } else {
577       $new_dn= $this->dn;
578     }
580     /* check syntax: must cn */
581     if ($this->cn == ""){
582       $message[]= _("Required field 'Name' is not set.");
583     } else {
584       if (!is_uid($this->cn)){
585         $message[]= _("Required field 'Name' contains invalid characters");
586       }
587       if ($this->dn == 'new'){
588         $ldap= $this->config->get_ldap_link();
589         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
590         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
591         if ($ldap->count() != 0){
592           $message[]= _("Specified name is already used.");
593         }
594       }
595     }
597     return $message;
598   }
601   /* Save to LDAP */
602   function save()
603   {
604     plugin::save();
607     /* Type selection */
608     if ($this->type == 0){
609       $type= "goFaxSBlock";
610     } else {
611       $type= "goFaxRBlock";
612     }
614     /* Add list */
615     $this->attrs['objectClass']= $type;
616     if (count($this->goFaxBlocklist)){
617       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
618     }
620     /* Write back to ldap */
621     $ldap= $this->config->get_ldap_link();
622     $ldap->cd($this->base);
623     $ldap->search("cn=$this->cn",array("cn"));
624     $ldap->cat($this->dn, array('dn'));
625     $a= $ldap->fetch();
627     if (count($a)){
628       if (!isset($this->attrs["$type"."list"])){
629         $this->attrs["$type"."list"]= array();
630       }
631       $ldap->cd($this->dn);
632       $this->cleanup();
633       $ldap->modify($this->attrs);
634       new log("modify","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
635       $this->handle_post_events("modify");
636     } else {
637       $ldap->cd($this->config->current['BASE']);
638       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
639       $ldap->cd($this->dn);
640       $ldap->add($this->attrs);
641       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
642       $this->handle_post_events("add");
643     }
644     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
645   }
648   /* Add number */
649   function addNumber($number)
650   {
651     if (!in_array($number, $this->goFaxBlocklist)){
652       $this->goFaxBlocklist[]= $number;
653       sort($this->goFaxBlocklist);
654     }
655   }
658   /* Remove number from list */
659   function delNumber($numbers)
660   {
661     $tmp= array();
662     foreach ($this->goFaxBlocklist as $val){
663       if (!in_array($val, $numbers)){
664         $tmp[]= $val;
665       }
666     }
667     $this->goFaxBlocklist= $tmp;
668   }
671   /* Delete lock */
672   function remove_lock()
673   {
674     if (isset($this->dn)){
675       del_lock ($this->dn);
676     }
677   }
679   /* Return plugin informations for acl handling */
680   function plInfo()
681   {
682     return (array(  
683           "plShortName"       => _("Fax"),
684           "plDescription"     => _("Fax blocklists"),
685           "plSelfModify"      => FALSE,
686           "plDepends"         => array(),
688           "plPriority"    => 0,
689           "plSection"     => array("administration" => _("FAX Blocklists")),
690           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
691                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
692           "plProvidedAcls" => array(
693             "cn"              => _("Name"),
694             "description"     => _("Description"),
695             "base"            => _("Base"),
696             "goFaxBlocklist"  => _("Blocklist"),
697             "type"            => _("Blocklist type"))
698           ));
699   }
702 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
703 ?>