Code

9b946ee97cce5792d6b571dd05d4f497d11cd463
[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;
32     $this->DivListBlocklist = new divListBlocklist($this->config,$this);
33   }
35   function execute()
36   {
37     /* Call parent execute */
38     plugin::execute();
40     /***************
41       Init vars 
42      ***************/
44     /* Get global smarty instance */
45     $smarty       = get_smarty();
46     $s_action     = "";                       // Contains the action to proceed
47     $s_entry      = "";                       // The value for s_action
48     $base_back    = "";                       // The Link for Backbutton
51     /***************
52       Fetch posts 
53      ***************/
55     /* Test Posts */
56     foreach($_POST as $key => $val){
57       // Post for delete
58       if(preg_match("/user_del.*/",$key)){
59         $s_action = "del";
60         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
61         // Post for edit
62       }elseif(preg_match("/user_edit_.*/",$key)){
63         $s_action="edit";
64         $s_entry  = preg_replace("/user_".$s_action."_/i","",$key);
65         // Post for new
66       }elseif(preg_match("/user_new.*/",$key)){
67         $s_action="new";
68       }elseif(preg_match("/user_tplnew.*/i",$key)){
69         $s_action="new_tpl";
70       }elseif(preg_match("/user_chgpw.*/i",$key)){
71         $s_action="change_pw";
72         $s_entry  = preg_replace("/user_chgpw_/i","",$key);
73       }
74     }
76     if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
77       $s_action ="edit";
78       $s_entry  = $_GET['id'];
79     }
81     $s_entry  = preg_replace("/_.$/","",$s_entry);
84     /***************
85       Cancel some dialogs  
86      ***************/
88     /* Cancel dialog */
89     if (isset($_POST['edit_cancel']) || 
90         isset($_POST['delete_blocklist_cancel']) ||
91         isset($_POST['delete_blocklist_confirm']) || 
92         isset($_POST['delete_lock'])){
94       if(!isset($_POST['delete_blocklist_confirm'])){
95         $this->clear_fields();
96       }
97       del_lock ($this->dn);
98       unset($_SESSION['objectinfo']);
99     }
102     /***************
103       Cancel some dialogs  
104      ***************/
106     /* Create new blocklist? */
107     if ($s_action=="new"){
108       $this->clear_fields();
109       $this->dn= "new";
110       plugin::plugin ($this->config, $this->dn);
111     }
114     /***************
115       Edit blocklist 
116      ***************/
118     /* Edit selected blocklist? */
119     if (($s_action=="edit") && (empty($this->dn))){
120       $this->clear_fields();
121       $this->dn=$this->blocklists[$s_entry]['dn']; 
122       if (($user= get_lock($this->dn)) != ""){
123         $_SESSION['dn']= $this->dn;
124         //$this->dn ="";
125         return(gen_locked_message($user, $this->dn));
126       } else {
127 # Lock this dn for editing
128         add_lock ($this->dn, $this->ui->dn);
130         plugin::plugin ($this->config, $this->dn);
132         /* Load missing lists */
133         if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
134           if(isset($this->attrs["goFaxSBlocklist"])){
135             for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
136               $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
137             }
138           }
139           $this->type= 0;
140         } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
141           if(isset($this->attrs["goFaxRBlocklist"])){
142             for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
143               $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
144             }
145           }
146           $this->type= 1;
147         }
148         $_SESSION['objectinfo']= trim($this->dn);
149       }
150     }
153     /***************
154       Remove blocklist
155      ***************/
157     /* Delete blocklist requested */
158     if ($s_action=="del"){
159       $this->dn=$this->blocklists[$s_entry]['dn'];
160       /* Check locking */
161       if (($user= get_lock($this->dn)) != ""){
162         $_SESSION['dn']= $this->dn;
163         return(gen_locked_message($user, $this->dn));
164       } else {
165 # Lock this dn for editing
166         add_lock ($this->dn, $this->ui->dn);
168         $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
169         return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
170       }
171     }
174     /***************
175       Remove blocklist confirmed
176      ***************/
178     /* Finally delete blocklist */
179     if (isset($_POST['delete_blocklist_confirm'])){
180       if (chkacl($this->acl, "delete") == ""){
181         $this->remove_from_parent();
182         gosa_log ("Blocklist object'".$this->dn."' has been removed");
183         $this->reload ();
184       } else {
185         print_red (_("You have no permission to remove this blocklist."));
186       }
187     }
190     /***************
191       Add numer to blocklist
192      ***************/
195     /* Handle interactions: add */
196     if (isset($_POST['add_number']) && $_POST['number'] != ""){
197       if (is_phone_nr($_POST['number'])){
198         $this->addNumber ($_POST['number']);
199       } else {
200         print_red (_("Please specify a valid phone number."));
201       }
202     }
205     /***************
206       Delete number from list
207      ***************/
209     /* Handle interactions: delete */
210     if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
211       $this->delNumber ($_POST['numbers']);
212     }
215     /***************
216       Edit finished
217      ***************/
219     /* What about finish? */
220     if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
221       $message= $this->check();
222       $this->remove_lock();
224       /* No errors, save object */
225       if (count ($message) == 0){
227         /* Perpare 'dn' in case of new entries */
228         if ($this->dn == "new"){
229           $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
230         }
232         $this->save ();
233         gosa_log ("Blocklist object'".$this->dn."' has been saved");
234       
235         if (!isset($_POST['edit_apply'])){
236           $this->dn= "";
237           del_lock ($this->ui->dn);
238           unset($_SESSION['objectinfo']);
239         }
240       } else {
241         /* Errors found, show message */
242         show_errors ($message);
243       }
244     }
247     /***************
248       Object currently in edit  
249      ***************/
251     if($this->dn){
253       /* Base select dialog */
254       $once = true;
255       foreach($_POST as $name => $value){
256         if(preg_match("/^chooseBase/",$name) && $once){
257           $once = false;
258           $this->dialog = new baseSelectDialog($this->config,$this);
259           $this->dialog->setCurrentBase($this->base);
260         }
261       }
263       /* Dialog handling */
264       if(is_object($this->dialog)){
265         /* Must be called before save_object */
266         $this->dialog->save_object();
268         if($this->dialog->isClosed()){
269           $this->dialog = false;
270         }elseif($this->dialog->isSelected()){
271           $this->base = $this->dialog->isSelected();
272           $this->dialog= false;
273         }else{
274           return($this->dialog->execute());
275         }
276       }
278       /* Set base */
279       if ($this->base == ""){
280         if ($this->dn == "new"){
281           $ui= get_userinfo();
282           $this->base= dn2base($ui->dn);
283         } else {
284           $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
285         }
286       }
288       /* Fill templating stuff */
289       $smarty->assign("bases", $this->config->idepartments);
290       $smarty->assign("base_select", $this->base);
291       $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
292       if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
293         $smarty->assign("selectmode", "");
294         $smarty->assign("mode", "");
295         $smarty->assign("apply", "0");
296       } else {
297         $smarty->assign("selectmode", "disabled");
298         $smarty->assign("mode", "readonly");
299         $smarty->assign("apply", "1");
300       }
301       foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
302         $smarty->assign("$val", $this->$val);
303         $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
304       }
306       /* Lock blocklist type for non new entries */
307       if ($this->dn != "new"){
308         $smarty->assign('typeACL', "disabled");
309       }
311       /* Show main page */
312       return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
314     }else{
316       /***************
317         Divlist dialog 
318        ***************/
320       /* Check if there is a snapshot dialog open */
321       $base = $this->DivListBlocklist->selectedBase;
322       if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
323         return($str);
324       }
326       /* Display dialog with system list */
327       $this->DivListBlocklist->parent = $this;
328       $this->DivListBlocklist->execute();
330       /* Add departments if subsearch is disabled */
331       if(!$this->DivListBlocklist->SubSearch){
332         $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
333       }
334       $this->reload();
335       $this->DivListBlocklist->setEntries($this->blocklists);
336       return($this->DivListBlocklist->Draw());
337     }
338   }
341   /* Return departments, that will be included within snapshot detection */
342   function get_used_snapshot_bases()
343   {
344     return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
345   }
348   /* Clear garbage from edited lists */
349   function clear_fields()
350   {
351     $this->dn= "";
352     $this->description= "";
353     $this->cn= "";
354     $this->base= $_SESSION['CurrentMainBase'];
355     $this->goFaxBlocklist= array();
356   }
359   /* Reload the list of known blocklists */
360   function reload()
361   {
362     /* Init some vars */
363     $filter = $filter2      = "";
364     $base                   = $this->DivListBlocklist->selectedBase;
365     $Regex                  = $this->DivListBlocklist->Regex;
366     $SubSearch              = $this->DivListBlocklist->SubSearch;
367     $ShowSendBocklists      = $this->DivListBlocklist->ShowSendBocklists;
368     $ShowReceiveBlocklists  = $this->DivListBlocklist->ShowReceiveBlocklists;
369     $Flags                  = GL_SIZELIMIT;
370     $res = $res2            = array();
372     /* Append subsearch to Flags */
373     if($SubSearch){
374       $Flags |= GL_SUBSEARCH;
375     }else{
376       $base = "ou=gofax,ou=systems,".$base;
377     }  
379     /* Create filter */
380     if ($ShowSendBocklists){
381       $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
382       $res= get_list($filter, "gofax", $base,array("*"), $Flags);
383     }
384     if ($ShowReceiveBlocklists){
385       $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
386       $res2= get_list($filter2, "gofax", $base,array("*"), $Flags);
387     }
388    
389     $this->blocklists = array_merge($res,$res2);
391     /* appen && sort */
392     $tmp=array();
393     foreach($this->blocklists as $tkey => $val ){
394       $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
395     }
396     ksort($tmp);
397     $this->blocklists=array();
398     foreach($tmp as $val){
399       $this->blocklists[]=$val;
400     }
401     reset ($this->blocklists);
402   }
405   function remove_from_parent()
406   {
407     $ldap= $this->config->get_ldap_link();
408     $ldap->rmDir($this->dn);
409     show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
410     $this->clear_fields();
411     $this->handle_post_events("remove");
412   }
415   /* Save data to object */
416   function save_object()
417   {
418     $this->DivListBlocklist->save_object();
419     plugin::save_object();
421     if(isset($_POST['base'])){
422       $this->base = $_POST['base'];
423     }
424     foreach($this->attributes as $attr){
425       if(isset($_POST[$attr])){
426         $this->$attr = $_POST[$attr];
427       }
428     }
429     if(isset($_POST['type'])){
430       $this->type = $_POST['type'];
431     }
432   }
435   /* Check values */
436   function check()
437   {
438     /* Call common method to give check the hook */
439     $message= plugin::check();
441     /* Permissions for that base? */
442     if ($this->base != ""){
443       $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
444     } else {
445       $new_dn= $this->dn;
446     }
448     if (chkacl($this->acl, "create") != ""){
449       $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
450     }
453     /* check syntax: must cn */
454     if ($this->cn == ""){
455       $message[]= _("Required field 'Name' is not set.");
456     } else {
457       if (!is_uid($this->cn)){
458         $message[]= _("Required field 'Name' contains invalid characters");
459       }
460       if ($this->dn == 'new'){
461         $ldap= $this->config->get_ldap_link();
462         $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
463         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
464         if ($ldap->count() != 0){
465           $message[]= _("Specified name is already used.");
466         }
467       }
468     }
470     /* Is user allowed to create in 'base'? */
471     if (chkacl($this->acl, "create") != ""){
472       $message[]= _("No permission to create a blocklist on this base.");
473     }
475     return $message;
476   }
479   /* Save to LDAP */
480   function save()
481   {
482     plugin::save();
485     /* Type selection */
486     if ($this->type == 0){
487       $type= "goFaxSBlock";
488     } else {
489       $type= "goFaxRBlock";
490     }
492     /* Add list */
493     $this->attrs['objectClass']= $type;
494     if (count($this->goFaxBlocklist)){
495       $this->attrs["$type"."list"]= $this->goFaxBlocklist;
496     }
498     /* Write back to ldap */
499     $ldap= $this->config->get_ldap_link();
500     $ldap->cd($this->base);
501     $ldap->search("cn=$this->cn",array("cn"));
502     $ldap->cat($this->dn, array('dn'));
503     $a= $ldap->fetch();
505     if (count($a)){
506       if (!isset($this->attrs["$type"."list"])){
507         $this->attrs["$type"."list"]= array();
508       }
509       $ldap->cd($this->dn);
510       $this->cleanup();
511       $ldap->modify($this->attrs);
512       $this->handle_post_events("modify");
513     } else {
514       $ldap->cd($this->config->current['BASE']);
515       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
516       $ldap->cd($this->dn);
517       $ldap->add($this->attrs);
518       $this->handle_post_events("add");
519     }
520     show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
521   }
524   /* Add number */
525   function addNumber($number)
526   {
527     if (!in_array($number, $this->goFaxBlocklist)){
528       $this->goFaxBlocklist[]= $number;
529       sort($this->goFaxBlocklist);
530     }
531   }
534   /* Remove number from list */
535   function delNumber($numbers)
536   {
537     $tmp= array();
538     foreach ($this->goFaxBlocklist as $val){
539       if (!in_array($val, $numbers)){
540         $tmp[]= $val;
541       }
542     }
543     $this->goFaxBlocklist= $tmp;
544   }
547   /* Delete lock */
548   function remove_lock()
549   {
550     if (isset($this->dn)){
551       del_lock ($this->dn);
552     }
553   }
555   /* Return plugin informations for acl handling */
556   function plInfo()
557   {
558     return (array(  
559           "plShortName"       => _("Fax"),
560           "plDescription"     => _("Fax Blocklists"),
561           "plSelfModify"      => TRUE,
562           "plDepends"         => array(),
564           "plPriority"    => 0,
565           "plSection"     => array("administration" => _("FAX Blocklists")),
566           "plCategory"    => array("gofax"          => array("description" => _("Fax"),
567                                                              "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
568       
569   
571           "plProvidedAcls" => array(
572             "cn"              => _("Name"),
573             "description"     => _("Description"),
574             "base"            => _("Base"),
575             "type"            => _("Blocklist type"))
576           ));
577   }
580 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
581 ?>