Code

removed duplicated check msgs
[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;
25   function blocklist ($config, $ui)
26   {
27     /* Init class */
28     $this->ui     = $ui;
29     $this->dn     = "";
30     $this->config = $config;
31     $this->base = $_SESSION['CurrentMainBase']; 
32     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
33     $this->set_acl_category("gofaxlist");
34     $this->set_acl_base($this->base);
35   }
37   function execute()
38   {
39     /* Call parent execute */
40     plugin::execute();
42     $_SESSION['LOCK_VARS_TO_USE'] = array("/^user_/");
44     /***************
45       Init vars 
46      ***************/
48     /* Get global smarty instance */
49     $smarty       = get_smarty();
50     $s_action     = "";                       // Contains the action to proceed
51     $s_entry      = "";                       // The value for s_action
52     $base_back    = "";                       // The Link for Backbutton
55     /***************
56       Fetch posts 
57      ***************/
59     /* Test Posts */
60     foreach($_POST as $key => $val){
61       // Post for delete
62       if(preg_match("/user_del.*/",$key)){
63         $s_action = "del";
64         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
65         // Post for edit
66       }elseif(preg_match("/user_edit_.*/",$key)){
67         $s_action="edit";
68         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
69         // Post for new
70       }elseif(preg_match("/user_new.*/",$key)){
71         $s_action="new";
72       }
73     }
75     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
76       $s_action ="edit";
77       $s_entry  = $_GET['id'];
78     }
80     $s_entry  = preg_replace("/_.$/","",$s_entry);
83     /***************
84       Cancel some dialogs  
85      ***************/
87     /* Cancel dialog */
88     if (isset($_POST['edit_cancel']) || 
89         isset($_POST['delete_blocklist_cancel']) ||
90         isset($_POST['delete_blocklist_confirm']) || 
91         isset($_POST['delete_lock'])){
93       del_lock ($this->dn);
94       if(!isset($_POST['delete_blocklist_confirm'])){
95         $this->clear_fields();
96       }
97       unset($_SESSION['objectinfo']);
98     }
101     /***************
102       Create a new blocklist (dialog)
103      ***************/
105     /* Create new blocklist? */
106     if ($s_action=="new"){
107       $this->clear_fields();
108       $this->dn= "new";
109       plugin::plugin ($this->config, $this->dn);
110     }
113     /***************
114       Edit blocklist 
115      ***************/
117     /* Edit selected blocklist? */
118     if (($s_action=="edit") && (empty($this->dn))){
119       $this->clear_fields();
120       $this->dn=$this->blocklists[$s_entry]['dn']; 
121       if (($user= get_lock($this->dn)) != ""){
122         $_SESSION['dn']= $this->dn;
123         //$this->dn ="";
124         return(gen_locked_message($user, $this->dn));
125       } else {
126 # Lock this dn for editing
127         add_lock ($this->dn, $this->ui->dn);
129         plugin::plugin ($this->config, $this->dn);
131         /* Load missing lists */
132         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
133           if(isset($this->attrs["goFaxSBlocklist"])){
134             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
135               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
136             }
137           }
138           $this->type= 0;
139         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
140           if(isset($this->attrs["goFaxRBlocklist"])){
141             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
142               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
143             }
144           }
145           $this->type= 1;
146         }
147         $_SESSION['objectinfo']= trim($this->dn);
148       }
149     }
152     /***************
153       Remove blocklist
154      ***************/
156     /* Delete blocklist requested */
157     if ($s_action=="del"){
158       $this->dn=$this->blocklists[$s_entry]['dn'];
159       /* Check locking */
160       if (($user= get_lock($this->dn)) != ""){
161         $_SESSION['dn']= $this->dn;
162         return(gen_locked_message($user, $this->dn));
163       } else {
164 # Lock this dn for editing
165         add_lock ($this->dn, $this->ui->dn);
167         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
168         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
169       }
170     }
173     /***************
174       Remove blocklist confirmed
175      ***************/
177     /* Finally delete blocklist */
178     if (isset($_POST['delete_blocklist_confirm'])){
179       if ($this->acl_is_removeable()){
180         $this->remove_from_parent();
181         gosa_log ("Blocklist object'".$this->dn."' has been removed");
182         $this->reload ();
183       } else {
184         print_red (_("You have no permission to remove this blocklist."));
185       }
186     }
189     /***************
190       Add numer to blocklist
191      ***************/
194     /* Handle interactions: add */
195     if (isset($_POST['add_number']) && $_POST['number'] != ""){
196       if (is_phone_nr($_POST['number'])){
197         $this->addNumber ($_POST['number']);
198       } else {
199         print_red (_("Please specify a valid phone number."));
200       }
201     }
204     /***************
205       Delete number from list
206      ***************/
208     /* Handle interactions: delete */
209     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
210       $this->delNumber ($_POST['numbers']);
211     }
214     /***************
215       Edit finished
216      ***************/
218     /* What about finish? */
219     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
220       $message= $this->check();
221       $this->remove_lock();
223       /* No errors, save object */
224       if (count ($message) == 0){
226         /* Perpare 'dn' in case of new entries */
227         if ($this->dn == "new"){
228           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
229         }
231         $this->save ();
232         gosa_log ("Blocklist object'".$this->dn."' has been saved");
233       
234         if (!isset($_POST['edit_apply'])){
235           $this->dn= "";
236           del_lock ($this->ui->dn);
237           unset($_SESSION['objectinfo']);
238         }
239       } else {
240         /* Errors found, show message */
241         show_errors ($message);
242       }
243     }
246     /***************
247       Object currently in edit  
248      ***************/
250     if($this->dn){
252       $dn = $this->dn;
253   
254       /* set Account states, this is nescessary for get_allowed_bases */
255       $this->initially_was_account= true;
256       $this->is_account = true;
257       if($dn == "new"){
258         $dn = "cn=dummy,ou=gofax,ou=systems,".$this->base; 
259         $this->initially_was_account= false;
260       }
262       $this->set_acl_category("gofaxlist");
263       $this->set_acl_base($dn);
265       /* Base select dialog */
266       $once = true;
267       foreach($_POST as $name => $value){
268         if(preg_match("/^chooseBase/",$name) && $once){
269           $once = false;
270           $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
271           $this->dialog->setCurrentBase($this->base);
272         }
273       }
275       /* Dialog handling */
276       if(is_object($this->dialog)){
277         /* Must be called before save_object */
278         $this->dialog->save_object();
280         if($this->dialog->isClosed()){
281           $this->dialog = false;
282         }elseif($this->dialog->isSelected()){
284           /* A new base was selected, check if it is a valid one */
285           $tmp = $this->get_allowed_bases();
286           if(isset($tmp[$this->dialog->isSelected()])){
287             $this->base = $this->dialog->isSelected();
288           }
289           $this->dialog= false;
291         }else{
292           return($this->dialog->execute());
293         }
294       }
296       /* Set base */
297       if ($this->base == ""){
298         if ($this->dn == "new"){
299           $ui= get_userinfo();
300           $this->base= dn2base($ui->dn);
301         } else {
302           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
303         }
304       }
306       /* Fill templating stuff */
307       $smarty->assign("bases", $this->get_allowed_bases());
309       $smarty->assign("base_select", $this->base);
310       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
311       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
312         $smarty->assign("selectmode", "");
313         $smarty->assign("mode", "");
314         $smarty->assign("apply", "0");
315       } else {
316         $smarty->assign("selectmode", "disabled");
317         $smarty->assign("mode", "readonly");
318         $smarty->assign("apply", "1");
319       }
321       $tmp = $this->plInfo();
322       foreach($tmp['plProvidedAcls'] as $name => $translation){
323         $smarty->assign($name."ACL",$this->getacl($name));
324       }
326       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
327         $smarty->assign("$val", $this->$val);
328       }
330       /* Lock blocklist type for non new entries */
331       $smarty->assign('typeACL',$this->getacl("type", ($this->dn != "new")));
333       /* Show main page */
334       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
336     }else{
338       /***************
339         Divlist dialog 
340        ***************/
342       /* Check if there is a snapshot dialog open */
343       $base = $this->DivListBlocklist->selectedBase;
344       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
345         return($str);
346       }
348       /* Display dialog with system list */
349       $this->DivListBlocklist->parent = $this;
350       $this->DivListBlocklist->execute();
352       /* Add departments if subsearch is disabled */
353       if(!$this->DivListBlocklist->SubSearch){
354         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
355       }
356       $this->reload();
357       $this->DivListBlocklist->setEntries($this->blocklists);
358       return($this->DivListBlocklist->Draw());
359     }
360   }
363   /* Return departments, that will be included within snapshot detection */
364   function get_used_snapshot_bases()
365   {
366     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
367   }
370   /* Clear garbage from edited lists */
371   function clear_fields()
372   {
373     $this->dn= "";
374     $this->description= "";
375     $this->cn= "";
376     $this->base= $_SESSION['CurrentMainBase'];
377     $this->goFaxBlocklist= array();
378   }
381   /* Reload the list of known blocklists */
382   function reload()
383   {
384     /* Init some vars */
385     $filter = $filter2      = "";
386     $base                   = $this->DivListBlocklist->selectedBase;
387     $Regex                  = $this->DivListBlocklist->Regex;
388     $SubSearch              = $this->DivListBlocklist->SubSearch;
389     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
390     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
391     $Flags                  = GL_SIZELIMIT;
392     $res = $res2            = array();
394     /* Append subsearch to Flags */
395     if($SubSearch){
396       $Flags |= GL_SUBSEARCH;
397     }else{
398       $base = "ou=gofax,ou=systems,".$base;
399     }  
401     /* Create filter */
402     if ($ShowSendBocklists){
403       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
404       $res= get_list($filter, "gofaxlist", $base,array("*"), $Flags);
405     }
406     if ($ShowReceiveBlocklists){
407       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
408       $res2= get_list($filter2, "gofaxlist", $base,array("*"), $Flags);
409     }
410    
411     $this->blocklists = array_merge($res,$res2);
413     /* appen && sort */
414     $tmp=array();
415     foreach($this->blocklists as $tkey => $val ){
417       $acl = $this->ui ->get_permissions($base,"gofaxlist/blocklist");
418       if(preg_match("/r/",$acl)){
419         $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
420       }
421     }
422     ksort($tmp);
423     $this->blocklists=array();
424     foreach($tmp as $val){
425       $this->blocklists[]=$val;
426     }
427     reset ($this->blocklists);
428   }
431   function remove_from_parent()
432   {
433     $ldap= $this->config->get_ldap_link();
434     $ldap->rmDir($this->dn);
435     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
436     $this->clear_fields();
437     $this->handle_post_events("remove");
438   }
441   /* Save data to object */
442   function save_object()
443   {
444     $this->DivListBlocklist->save_object();
446     /* Create a base backup and reset the
447        base directly after calling plugin::save_object();
448        Base will be set seperatly a few lines below */
449     $base_tmp = $this->base;
450     plugin::save_object();
451     $this->base = $base_tmp;
453     /* Save base, since this is no LDAP attribute */
454     $tmp = $this->get_allowed_bases();
455     if(isset($_POST['base'])){
456       if(isset($tmp[$_POST['base']])){
457         $this->base= $_POST['base'];
458       }
459     }
461     foreach($this->attributes as $attr){
462       if(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
463         $this->$attr = $_POST[$attr];
464       }
465     }
466     if(isset($_POST['type']) && $this->acl_is_writeable("type",($this->dn != "new"))){
467       $this->type = $_POST['type'];
468     }
469   }
472   /* Check values */
473   function check()
474   {
475     /* Call common method to give check the hook */
476     $message= plugin::check();
478     /* Permissions for that base? */
479     if ($this->base != ""){
480       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
481     } else {
482       $new_dn= $this->dn;
483     }
485     /* check syntax: must cn */
486     if ($this->cn == ""){
487       $message[]= _("Required field 'Name' is not set.");
488     } else {
489       if (!is_uid($this->cn)){
490         $message[]= _("Required field 'Name' contains invalid characters");
491       }
492       if ($this->dn == 'new'){
493         $ldap= $this->config->get_ldap_link();
494         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
495         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
496         if ($ldap->count() != 0){
497           $message[]= _("Specified name is already used.");
498         }
499       }
500     }
502     return $message;
503   }
506   /* Save to LDAP */
507   function save()
508   {
509     plugin::save();
512     /* Type selection */
513     if ($this->type == 0){
514       $type= "goFaxSBlock";
515     } else {
516       $type= "goFaxRBlock";
517     }
519     /* Add list */
520     $this->attrs['objectClass']= $type;
521     if (count($this->goFaxBlocklist)){
522       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
523     }
525     /* Write back to ldap */
526     $ldap= $this->config->get_ldap_link();
527     $ldap->cd($this->base);
528     $ldap->search("cn=$this->cn",array("cn"));
529     $ldap->cat($this->dn, array('dn'));
530     $a= $ldap->fetch();
532     if (count($a)){
533       if (!isset($this->attrs["$type"."list"])){
534         $this->attrs["$type"."list"]= array();
535       }
536       $ldap->cd($this->dn);
537       $this->cleanup();
538       $ldap->modify($this->attrs);
539       $this->handle_post_events("modify");
540     } else {
541       $ldap->cd($this->config->current['BASE']);
542       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
543       $ldap->cd($this->dn);
544       $ldap->add($this->attrs);
545       $this->handle_post_events("add");
546     }
547     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
548   }
551   /* Add number */
552   function addNumber($number)
553   {
554     if (!in_array($number, $this->goFaxBlocklist)){
555       $this->goFaxBlocklist[]= $number;
556       sort($this->goFaxBlocklist);
557     }
558   }
561   /* Remove number from list */
562   function delNumber($numbers)
563   {
564     $tmp= array();
565     foreach ($this->goFaxBlocklist as $val){
566       if (!in_array($val, $numbers)){
567         $tmp[]= $val;
568       }
569     }
570     $this->goFaxBlocklist= $tmp;
571   }
574   /* Delete lock */
575   function remove_lock()
576   {
577     if (isset($this->dn)){
578       del_lock ($this->dn);
579     }
580   }
582   /* Return plugin informations for acl handling */
583   function plInfo()
584   {
585     return (array(  
586           "plShortName"       => _("Fax"),
587           "plDescription"     => _("Fax blocklists"),
588           "plSelfModify"      => FALSE,
589           "plDepends"         => array(),
591           "plPriority"    => 0,
592           "plSection"     => array("administration" => _("FAX Blocklists")),
593           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
594                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
595           "plProvidedAcls" => array(
596             "cn"              => _("Name"),
597             "description"     => _("Description"),
598             "base"            => _("Base"),
599             "goFaxBlocklist"  => _("Blocklist"),
600             "type"            => _("Blocklist type"))
601           ));
602   }
605 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
606 ?>