Code

Fixed Blocklist handling
[gosa.git] / gosa-plugins / gofax / gofax / blocklists / class_blocklistGeneric.inc
1 <?php
3 define("BLOCK_LIST_RECEIVE" , 0);
4 define("BLOCK_LIST_SEND" , 1);
6 class blocklistGeneric extends plugin
7 {
8   /* Definitions */
9   var $plHeadline= "Fax blocklists";
10   var $plDescription= "This does something";
12   var $cn ="";
13   var $description = "";
14   var $base = "";  
16   var $type;
17   var $goFaxBlocklist = array();
18   var $readonly = FALSE;
19   var $view_logged = FALSE;
20   var $attributes = array("cn","description","goFaxSBlocklist","goFaxRBlocklist");
22   var $ignore_account = TRUE;
24   var $orig_base = "";
25   var $orig_dn = "";
27   var $goFaxSBlocklist = array();
28   var $goFaxRBlocklist = array();
30   
31   function __construct($config,$dn = "new")
32   {
33     plugin::plugin($config,$dn);
35     /* Set default list type */
36     $this->type = BLOCK_LIST_SEND;
38     /* Load defined numbers */
39     if($dn != "new"){
40   
41       /* We will not be able to rename edited blocklists */
42       $this->readonly = TRUE;
43   
44       /* Get blocklist type and defined numbers */
45       if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
46         if(isset($this->attrs["goFaxSBlocklist"])){
47           for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
48             $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
49           }
50         }
51         $this->type= BLOCK_LIST_SEND;
52       } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
53         if(isset($this->attrs["goFaxRBlocklist"])){
54           for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
55             $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
56           }
57         }
58         $this->type= BLOCK_LIST_RECEIVE;
59       }
60     }
62     /* Set base */
63     if ($this->dn == "new"){
64       if(session::is_set('CurrentMainBase')){
65         $this->base = session::get('CurrentMainBase');
66       }else{
67         $ui= get_userinfo();
68         $this->base= dn2base($ui->dn);
69       }
70     } else {
71       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("faxBlocklistRDN"), '/')."/i", "", $this->dn);
72     }
73     
74     $this->orig_base = $this->base;
75     $this->orig_dn   = $this->dn;
76   }
78   public function execute()
79   {
80     /* Log view */
81     if(!$this->view_logged){
82       $this->view_logged = TRUE;
83       new log("view","blocklist/".get_class($this),$this->dn);
84     }
87     /**************
88      * Base select dialog 
89      **************/
91     $once = true;
92     foreach($_POST as $name => $value){
93       if(preg_match("/^chooseBase/",$name) && $once){
94         $once = false;
95         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
96         $this->dialog->setCurrentBase($this->base);
97       }
98     }
100     /* Dialog handling */
101     if(is_object($this->dialog)){
102       /* Must be called before save_object */
103       $this->dialog->save_object();
105       if($this->dialog->isClosed()){
106         $this->dialog = false;
107       }elseif($this->dialog->isSelected()){
109         /* A new base was selected, check if it is a valid one */
110         $tmp = $this->get_allowed_bases();
111         if(isset($tmp[$this->dialog->isSelected()])){
112           $this->base = $this->dialog->isSelected();
113         }
114         $this->dialog= false;
116       }else{
117         return($this->dialog->execute());
118       }
119     }
122     /***************
123       Add numer to blocklist
124      ***************/
127     /* Handle interactions: add */
128     if (isset($_POST['add_number']) && $_POST['number'] != ""){
129       if (tests::is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
130         $this->addNumber ($_POST['number']);
131       } else {
132         msg_dialog::display(_("Error"), msgPool::invalid(_("Phone number")), ERROR_DIALOG);
133       }
134     }
137     /***************
138       Delete number from list
139      ***************/
141     /* Handle interactions: delete */
142     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
143       $this->delNumber ($_POST['numbers']);
144     }
147     /***************
148       Template output
149      ***************/
151     $smarty = get_smarty();
152     foreach($this->attributes as $name){
153       $smarty->assign($name,$this->$name);
154     }
155     $tmp = $this->plInfo();
156     foreach($tmp['plProvidedAcls'] as $name => $translation){
157       $smarty->assign($name."ACL",$this->getacl($name));
158     }
159     $smarty->assign("goFaxBlocklist",$this->goFaxBlocklist);
160     $smarty->assign("cnACL",$this->getacl("cn",$this->readonly));
161     $smarty->assign("typeACL",$this->getacl("type",$this->readonly));
162     $smarty->assign("base",$this->base);
163     $smarty->assign("bases", $this->get_allowed_bases());
164     $smarty->assign("types", array(BLOCK_LIST_SEND => _("send"), BLOCK_LIST_RECEIVE => _("receive")));
165     $smarty->assign("type", $this->type);
166     $smarty->assign("dn", $this->dn);
167     $smarty->assign("read_only",$this->read_only);
168     return($smarty->fetch(get_template_path('generic.tpl', TRUE)));
169   }
172   public function save_object()
173   {
174     if(isset($_POST['blocklist_posted'])){
175       $tmp_cn = $this->cn;
176       plugin::save_object();
177       $this->cn = $tmp_cn;
179       /* Save base, since this is no LDAP attribute */
180       $tmp = $this->get_allowed_bases();
181       if(isset($_POST['base'])){
182         if(isset($tmp[$_POST['base']])){
183           $this->base= $_POST['base'];
184         }
185       }
186       $tmp = $this->attributes;
187       $tmp[] = "type";
188       foreach($tmp as $attr){
189         if(in_array($attr,array("cn","type")) && $this->readonly){
190           continue;
191         }elseif(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
192           $this->$attr = $_POST[$attr];
193         }
194       }
195     }
196   }
199   function remove_from_parent()
200   {
201     $ldap= $this->config->get_ldap_link();
202     $ldap->rmDir($this->dn);
203     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
204     if (!$ldap->success()){
205       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
206     }
207     $this->handle_post_events("remove");
208   }
211   /* Check values */
212   function check()
213   {
214     /* Call common method to give check the hook */
215     $message= plugin::check();
217     /* check syntax: must cn */
218     if ($this->cn == ""){
219       $message[]= msgPool::required(_("Name"));
220     } else {
221       if (!tests::is_uid($this->cn)){
222         $message[]= msgPool::invalid(_("Name"));
223       }
224       if ($this->dn == 'new'){
225         $ldap= $this->config->get_ldap_link();
226         $ldap->cd (get_ou('faxBlocklistRDN').$this->config->current["BASE"]);
227         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
228         if ($ldap->count() != 0){
229           $message[]= msgPool::duplicated(_("Name"));
230         }
231       }
232     }
233     
234     /* Check if we are allowed to create or move this object
235      */
236     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
237       $message[] = msgPool::permCreate();
238     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
239       $message[] = msgPool::permMove();
240     }
241   
242     return $message;
243   }
246   /* Save to LDAP */
247   function save()
248   {
249     /* Type selection */
250     if ($this->type == BLOCK_LIST_SEND){
251       $type= "goFaxSBlocklist";
252       $this->objectclasses = array("goFaxSBlock");
253     } else {
254       $type= "goFaxRBlocklist";
255       $this->objectclasses = array("goFaxRBlock");
256     }
258     plugin::save();
260     /* Let clenaup() know what attributes to handle 
261      */ 
262     $this->attrs[$type] = $this->goFaxBlocklist;
263     $this->attributes [] = $type;
265     /* Write back to ldap */
266     $ldap= $this->config->get_ldap_link();
267     $ldap->cd($this->base);
268     $ldap->cat($this->dn, array('dn'));
269     if ($ldap->count()){
270       $ldap->cd($this->dn);
271       $this->cleanup();
272       $ldap->modify($this->attrs);
273       new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
274       $this->handle_post_events("modify");
275     } else {
276       $ldap->cd($this->config->current['BASE']);
277       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
278       $ldap->cd($this->dn);
279       $this->cleanup();
280       $ldap->add($this->attrs);
281       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
282       $this->handle_post_events("add");
283     }
285     if (!$ldap->success()){
286       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
287     }
288   }
291   /* Add number */
292   function addNumber($number)
293   {
294     if (!in_array($number, $this->goFaxBlocklist)){
295       $this->goFaxBlocklist[]= $number;
296       sort($this->goFaxBlocklist);
297     }
298   }
301   /* Remove number from list */
302   function delNumber($numbers)
303   {
304     $tmp= array();
305     foreach ($this->goFaxBlocklist as $val){
306       if (!in_array($val, $numbers)){
307         $tmp[]= $val;
308       }
309     }
310     $this->goFaxBlocklist= $tmp;
311   }
314   function getCopyDialog()
315   { 
316     $smarty = get_smarty();
317     $smarty->assign("cn",$this->cn);
318     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
319     $ret = array();
320     $ret['string'] = $str;
321     $ret['status'] = "";
322     return($ret);
323   }
325   function saveCopyDialog()
326   {
327     if(isset($_POST['cn'])){
328       $this->cn = get_post('cn');
329     }
330   }
332   
333   function PrepareForCopyPaste($source)
334   {
335     plugin::PrepareForCopyPaste($source);
337     /* We will not be able to rename edited blocklists */
338     $this->readonly = TRUE;
340     /* Get blocklist type and defined numbers */
341     if (in_array("goFaxSBlock",$source['objectClass'])){
342       if(isset($source["goFaxSBlocklist"])){
343         for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
344           $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
345         }
346       }
347       $this->type= BLOCK_LIST_SEND;
348     } elseif (in_array("goFaxRBlock",$source['objectClass'])){
349       if(isset($source["goFaxRBlocklist"])){
350         for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
351           $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
352         }
353       }
354       $this->type= BLOCK_LIST_RECEIVE;
355     }
357     /* Set base */
358     if ($this->dn == "new"){
359       if(session::is_set('CurrentMainBase')){
360         $this->base = session::get('CurrentMainBase');
361       }else{
362         $ui= get_userinfo();
363         $this->base= dn2base($ui->dn);
364       }
365     } else {
366       $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
367     }
368   }
371   /* Return plugin informations for acl handling */
372   static function plInfo()
373   {
374     return (array(
375           "plShortName"       => _("Fax"),
376           "plDescription"     => _("Fax blocklists"),
377           "plSelfModify"      => FALSE,
378           "plDepends"         => array(),
380           "plPriority"    => 0,
381           "plSection"     => array("administration" => _("Fax blocklists")),
382           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
383               "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
384           "plProvidedAcls" => array(
385             "cn"              => _("Name"),
386             "description"     => _("Description"),
387             "base"            => _("Base"),
388             "goFaxBlocklist"  => _("Blocklist"),
389             "type"            => _("Blocklist type"))
390           ));
391   }
394 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
395 ?>