Code

Fixed blocklistm remove acl check.
[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");
22   var $ignore_account = TRUE;
23   
24   function __construct($config,$dn = "new")
25   {
26     plugin::plugin($config,$dn);
28     /* Set default list type */
29     $this->type = BLOCK_LIST_SEND;
31     /* Load defined numbers */
32     if($dn != "new"){
33   
34       /* We will not be able to rename edited blocklists */
35       $this->readonly = TRUE;
36   
37       /* Get blocklist type and defined numbers */
38       if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
39         if(isset($this->attrs["goFaxSBlocklist"])){
40           for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
41             $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
42           }
43         }
44         $this->type= BLOCK_LIST_SEND;
45       } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
46         if(isset($this->attrs["goFaxRBlocklist"])){
47           for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
48             $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
49           }
50         }
51         $this->type= BLOCK_LIST_RECEIVE;
52       }
53     }
55     /* Set base */
56     if ($this->dn == "new"){
57       if(session::is_set('CurrentMainBase')){
58         $this->base = session::get('CurrentMainBase');
59       }else{
60         $ui= get_userinfo();
61         $this->base= dn2base($ui->dn);
62       }
63     } else {
64       $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
65     }
66   }
68   public function execute()
69   {
70     /* Log view */
71     if(!$this->view_logged){
72       $this->view_logged = TRUE;
73       new log("view","blocklist/".get_class($this),$this->dn);
74     }
77     /**************
78      * Base select dialog 
79      **************/
81     $once = true;
82     foreach($_POST as $name => $value){
83       if(preg_match("/^chooseBase/",$name) && $once){
84         $once = false;
85         $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
86         $this->dialog->setCurrentBase($this->base);
87       }
88     }
90     /* Dialog handling */
91     if(is_object($this->dialog)){
92       /* Must be called before save_object */
93       $this->dialog->save_object();
95       if($this->dialog->isClosed()){
96         $this->dialog = false;
97       }elseif($this->dialog->isSelected()){
99         /* A new base was selected, check if it is a valid one */
100         $tmp = $this->get_allowed_bases();
101         if(isset($tmp[$this->dialog->isSelected()])){
102           $this->base = $this->dialog->isSelected();
103         }
104         $this->dialog= false;
106       }else{
107         return($this->dialog->execute());
108       }
109     }
112     /***************
113       Add numer to blocklist
114      ***************/
117     /* Handle interactions: add */
118     if (isset($_POST['add_number']) && $_POST['number'] != ""){
119       if (tests::is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
120         $this->addNumber ($_POST['number']);
121       } else {
122         msg_dialog::display(_("Error"), msgPool::invalid(_("Phone number")), ERROR_DIALOG);
123       }
124     }
127     /***************
128       Delete number from list
129      ***************/
131     /* Handle interactions: delete */
132     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
133       $this->delNumber ($_POST['numbers']);
134     }
137     /***************
138       Template output
139      ***************/
141     $smarty = get_smarty();
142     foreach($this->attributes as $name){
143       $smarty->assign($name,$this->$name);
144     }
145     $tmp = $this->plInfo();
146     foreach($tmp['plProvidedAcls'] as $name => $translation){
147       $smarty->assign($name."ACL",$this->getacl($name));
148     }
149     $smarty->assign("goFaxBlocklist",$this->goFaxBlocklist);
150     $smarty->assign("cnACL",$this->getacl("cn",$this->readonly));
151     $smarty->assign("typeACL",$this->getacl("type",$this->readonly));
152     $smarty->assign("base",$this->base);
153     $smarty->assign("bases", $this->get_allowed_bases());
154     $smarty->assign("types", array(BLOCK_LIST_SEND => _("send"), BLOCK_LIST_RECEIVE => _("receive")));
155     $smarty->assign("type", $this->type);
156     $smarty->assign("dn", $this->dn);
157     return($smarty->fetch(get_template_path('generic.tpl', TRUE)));
158   }
161   public function save_object()
162   {
163     if(isset($_POST['blocklist_posted'])){
164       $tmp_cn = $this->cn;
165       plugin::save_object();
166       $this->cn = $tmp_cn;
168       /* Save base, since this is no LDAP attribute */
169       $tmp = $this->get_allowed_bases();
170       if(isset($_POST['base'])){
171         if(isset($tmp[$_POST['base']])){
172           $this->base= $_POST['base'];
173         }
174       }
175       $tmp = $this->attributes;
176       $tmp[] = "type";
177       foreach($tmp as $attr){
178         if(in_array($attr,array("cn","type")) && $this->readonly){
179           continue;
180         }elseif(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
181           $this->$attr = $_POST[$attr];
182         }
183       }
184     }
185   }
188   function remove_from_parent()
189   {
190     $ldap= $this->config->get_ldap_link();
191     $ldap->rmDir($this->dn);
192     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
193     if (!$ldap->success()){
194       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
195     }
196     $this->handle_post_events("remove");
197   }
200   /* Check values */
201   function check()
202   {
203     /* Call common method to give check the hook */
204     $message= plugin::check();
206     /* check syntax: must cn */
207     if ($this->cn == ""){
208       $message[]= msgPool::required(_("Name"));
209     } else {
210       if (!tests::is_uid($this->cn)){
211         $message[]= msgPool::invalid(_("Name"));
212       }
213       if ($this->dn == 'new'){
214         $ldap= $this->config->get_ldap_link();
215         $ldap->cd (get_ou('blocklistou').$this->config->current["BASE"]);
216         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
217         if ($ldap->count() != 0){
218           $message[]= msgPool::duplicated(_("Name"));
219         }
220       }
221     }
222     return $message;
223   }
226   /* Save to LDAP */
227   function save()
228   {
229     /* Type selection */
230     if ($this->type == BLOCK_LIST_SEND){
231       $type= "goFaxSBlocklist";
232       $this->objectclasses = array("goFaxSBlock");
233     } else {
234       $type= "goFaxRBlocklist";
235       $this->objectclasses = array("goFaxRBlock");
236     }
238     plugin::save();
240     /* Add list */
241     if (count($this->goFaxBlocklist)){
242       $this->attrs[$type]= $this->goFaxBlocklist;
243     }
245     /* Write back to ldap */
246     $ldap= $this->config->get_ldap_link();
247     $ldap->cd($this->base);
248     $ldap->cat($this->dn, array('dn'));
249     if ($ldap->count()){
250       if (!isset($this->attrs[$type])){
251         $this->attrs[$type]= array();
252       }
253       $ldap->cd($this->dn);
254       $this->cleanup();
255       $ldap->modify($this->attrs);
256       new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
257       $this->handle_post_events("modify");
258     } else {
259       $ldap->cd($this->config->current['BASE']);
260       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
261       $ldap->cd($this->dn);
262       $ldap->add($this->attrs);
263       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
264       $this->handle_post_events("add");
265     }
267     if (!$ldap->success()){
268       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
269     }
270   }
273   /* Add number */
274   function addNumber($number)
275   {
276     if (!in_array($number, $this->goFaxBlocklist)){
277       $this->goFaxBlocklist[]= $number;
278       sort($this->goFaxBlocklist);
279     }
280   }
283   /* Remove number from list */
284   function delNumber($numbers)
285   {
286     $tmp= array();
287     foreach ($this->goFaxBlocklist as $val){
288       if (!in_array($val, $numbers)){
289         $tmp[]= $val;
290       }
291     }
292     $this->goFaxBlocklist= $tmp;
293   }
296   function getCopyDialog()
297   { 
298     $smarty = get_smarty();
299     $smarty->assign("cn",$this->cn);
300     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
301     $ret = array();
302     $ret['string'] = $str;
303     $ret['status'] = "";
304     return($ret);
305   }
307   function saveCopyDialog()
308   {
309     if(isset($_POST['cn'])){
310       $this->cn = get_post('cn');
311     }
312   }
314   
315   function PrepareForCopyPaste($source)
316   {
317     plugin::PrepareForCopyPaste($source);
319     /* We will not be able to rename edited blocklists */
320     $this->readonly = TRUE;
322     /* Get blocklist type and defined numbers */
323     if (in_array("goFaxSBlock",$source['objectClass'])){
324       if(isset($source["goFaxSBlocklist"])){
325         for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
326           $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
327         }
328       }
329       $this->type= BLOCK_LIST_SEND;
330     } elseif (in_array("goFaxRBlock",$source['objectClass'])){
331       if(isset($source["goFaxRBlocklist"])){
332         for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
333           $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
334         }
335       }
336       $this->type= BLOCK_LIST_RECEIVE;
337     }
339     /* Set base */
340     if ($this->dn == "new"){
341       if(session::is_set('CurrentMainBase')){
342         $this->base = session::get('CurrentMainBase');
343       }else{
344         $ui= get_userinfo();
345         $this->base= dn2base($ui->dn);
346       }
347     } else {
348       $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
349     }
350   }
353   /* Return plugin informations for acl handling */
354   static function plInfo()
355   {
356     return (array(
357           "plShortName"       => _("Fax"),
358           "plDescription"     => _("Fax blocklists"),
359           "plSelfModify"      => FALSE,
360           "plDepends"         => array(),
362           "plPriority"    => 0,
363           "plSection"     => array("administration" => _("Fax blocklists")),
364           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
365               "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
366           "plProvidedAcls" => array(
367             "cn"              => _("Name"),
368             "description"     => _("Description"),
369             "base"            => _("Base"),
370             "goFaxBlocklist"  => _("Blocklist"),
371             "type"            => _("Blocklist type"))
372           ));
373   }
376 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
377 ?>