Code

6507ba07a6529bb23a4764fd1d64927057ba289c
[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 blacklists";
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();
29   var $baseSelector;
31   
32   function __construct($config,$dn = "new")
33   {
34     plugin::plugin($config,$dn);
36     /* Set default list type */
37     $this->type = BLOCK_LIST_SEND;
39     /* Load defined numbers */
40     if($dn != "new"){
41   
42       /* We will not be able to rename edited blocklists */
43       $this->readonly = TRUE;
44   
45       /* Get blocklist type and defined numbers */
46       if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
47         if(isset($this->attrs["goFaxSBlocklist"])){
48           for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
49             $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
50           }
51         }
52         $this->type= BLOCK_LIST_SEND;
53       } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
54         if(isset($this->attrs["goFaxRBlocklist"])){
55           for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
56             $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
57           }
58         }
59         $this->type= BLOCK_LIST_RECEIVE;
60       }
61     }
63     /* Set base */
64     if ($this->dn == "new"){
65       if(session::is_set('CurrentMainBase')){
66         $this->base = session::get('CurrentMainBase');
67       }else{
68         $ui= get_userinfo();
69         $this->base= dn2base($ui->dn);
70       }
71     } else {
72       $this->base= preg_replace ("/^[^,]+,".preg_quote(get_ou("blocklistGeneric", "faxBlocklistRDN"), '/')."/i", "", $this->dn);
73     }
74     
75     $this->orig_base = $this->base;
76     $this->orig_dn   = $this->dn;
78     /* Instanciate base selector */
79     $this->baseSelector= new baseSelector($this->get_allowed_bases(), $this->base);
80     $this->baseSelector->setSubmitButton(false);
81     $this->baseSelector->setHeight(300);
82     $this->baseSelector->update(true);
83   }
85   public function execute()
86   {
87     plugin::execute();
89     /* Log view */
90     if(!$this->view_logged){
91       $this->view_logged = TRUE;
92       new log("view","blocklist/".get_class($this),$this->dn);
93     }
96     /***************
97       Add numer to blocklist
98      ***************/
101     /* Handle interactions: add */
102     if (isset($_POST['add_number']) && $_POST['number'] != ""){
103       if (tests::is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
104         $acl =  ($this->type == 1) ? "goFaxSBlocklist" : "goFaxRBlocklist";
105         if($this->acl_is_writeable($acl)){
106           $this->addNumber ($_POST['number']);
107         }
108       } else {
109         msg_dialog::display(_("Error"), msgPool::invalid(_("Phone number")), ERROR_DIALOG);
110       }
111     }
114     /***************
115       Delete number from list
116      ***************/
118     /* Handle interactions: delete */
119     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
121       $acl = ($this->type == 1) ? "goFaxSBlocklist" : "goFaxRBlocklist";
122       if($this->acl_is_writeable($acl)){
123         $this->delNumber ($_POST['numbers']);
124       }
125     }
128     /***************
129       Template output
130      ***************/
132     $smarty = get_smarty();
134     foreach($this->attributes as $name){
135       $smarty->assign($name,set_post($this->$name));
136     }
137     $tmp = $this->plInfo();
138     foreach($tmp['plProvidedAcls'] as $name => $translation){
139       $smarty->assign($name."ACL",$this->getacl($name));
140     }
141     
142     if($this->type == 1){
143       $smarty->assign("goFaxBlocklistACL", $this->getacl('goFaxSBlocklist'));
144     }else{
145       $smarty->assign("goFaxBlocklistACL", $this->getacl('goFaxRBlocklist'));
146     }
148     $smarty->assign("goFaxBlocklist", set_post($this->goFaxBlocklist));
149     $smarty->assign("cnACL",$this->getacl("cn",$this->readonly));
150     $smarty->assign("typeACL",$this->getacl("type",$this->readonly));
151     $smarty->assign("base", $this->baseSelector->render());
152     $smarty->assign("types", array(BLOCK_LIST_SEND => _("send"), BLOCK_LIST_RECEIVE => _("receive")));
153     $smarty->assign("type", set_post($this->type));
154     $smarty->assign("dn", $this->dn);
155     $smarty->assign("read_only",$this->read_only);
156     return($smarty->fetch(get_template_path('generic.tpl', TRUE)));
157   }
160   public function save_object()
161   {
162     if(isset($_POST['blocklist_posted'])){
163       $tmp_cn = $this->cn;
164       plugin::save_object();
165       $this->cn = $tmp_cn;
167       /* Refresh base */
168       if ($this->acl_is_moveable($this->base)){
169         if (!$this->baseSelector->update()) {
170           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
171         }
172         if ($this->base != $this->baseSelector->getBase()) {
173           $this->base= $this->baseSelector->getBase();
174           $this->is_modified= TRUE;
175         }
176       }
178       /* Save attrbutes */
179       $tmp = $this->attributes;
180       $tmp[] = "type";
181       foreach($tmp as $attr){
182         if(in_array($attr,array("cn","type")) && $this->readonly){
183           continue;
184         }elseif(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
185           $this->$attr = get_post($attr);
186         }
187       }
188     }
189   }
192   function remove_from_parent()
193   {
194     $ldap= $this->config->get_ldap_link();
195     $ldap->rmDir($this->dn);
196     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
197     if (!$ldap->success()){
198       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
199     }
200     $this->handle_post_events("remove");
201   }
204   /* Check values */
205   function check()
206   {
207     /* Call common method to give check the hook */
208     $message= plugin::check();
210     /* check syntax: must cn */
211     if ($this->cn == ""){
212       $message[]= msgPool::required(_("Name"));
213     } else {
214       if (!tests::is_uid($this->cn)){
215         $message[]= msgPool::invalid(_("Name"));
216       }
217       if ($this->dn == 'new'){
218         $ldap= $this->config->get_ldap_link();
219         $ldap->cd (get_ou("blocklistGeneric", "faxBlocklistRDN").$this->config->current["BASE"]);
220         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
221         if ($ldap->count() != 0){
222           $message[]= msgPool::duplicated(_("Name"));
223         }
224       }
225     }
227     // Check if a wrong base was supplied
228     if(!$this->baseSelector->checkLastBaseUpdate()){
229       $message[]= msgPool::check_base();;
230     }
231     
232     /* Check if we are allowed to create or move this object
233      */
234     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
235       $message[] = msgPool::permCreate();
236     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
237       $message[] = msgPool::permMove();
238     }
239   
240     return $message;
241   }
244   /* Save to LDAP */
245   function save()
246   {
247     /* Type selection */
248     if ($this->type == BLOCK_LIST_SEND){
249       $type= "goFaxSBlocklist";
250       $this->objectclasses = array("goFaxSBlock");
251     } else {
252       $type= "goFaxRBlocklist";
253       $this->objectclasses = array("goFaxRBlock");
254     }
256     plugin::save();
258     /* Let clenaup() know what attributes to handle 
259      */ 
260     $this->attrs[$type] = $this->goFaxBlocklist;
261     $this->attributes [] = $type;
263     /* Write back to ldap */
264     $ldap= $this->config->get_ldap_link();
265     $ldap->cd($this->base);
266     $ldap->cat($this->dn, array('dn'));
267     if ($ldap->count()){
268       $ldap->cd($this->dn);
269       $this->cleanup();
270       $ldap->modify($this->attrs);
271       new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
272       $this->handle_post_events("modify");
273     } else {
274       $ldap->cd($this->config->current['BASE']);
275       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
276       $ldap->cd($this->dn);
277       $this->cleanup();
278       $ldap->add($this->attrs);
279       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
280       $this->handle_post_events("add");
281     }
283     if (!$ldap->success()){
284       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
285     }
286   }
289   /* Add number */
290   function addNumber($number)
291   {
292     if (!in_array($number, $this->goFaxBlocklist)){
293       $this->goFaxBlocklist[]= $number;
294       sort($this->goFaxBlocklist);
295     }
296   }
299   /* Remove number from list */
300   function delNumber($numbers)
301   {
302     $tmp= array();
303     foreach ($this->goFaxBlocklist as $val){
304       if (!in_array($val, $numbers)){
305         $tmp[]= $val;
306       }
307     }
308     $this->goFaxBlocklist= $tmp;
309   }
312   function getCopyDialog()
313   { 
314     $smarty = get_smarty();
315     $smarty->assign("cn",set_post($this->cn));
316     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
317     $ret = array();
318     $ret['string'] = $str;
319     $ret['status'] = "";
320     return($ret);
321   }
323   function saveCopyDialog()
324   {
325     if(isset($_POST['cn'])){
326       $this->cn = get_post('cn');
327     }
328   }
330   
331   function PrepareForCopyPaste($source)
332   {
333     plugin::PrepareForCopyPaste($source);
335     /* We will not be able to rename edited blocklists */
336     $this->readonly = TRUE;
338     /* Get blocklist type and defined numbers */
339     if (in_array("goFaxSBlock",$source['objectClass'])){
340       if(isset($source["goFaxSBlocklist"])){
341         for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
342           $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
343         }
344       }
345       $this->type= BLOCK_LIST_SEND;
346     } elseif (in_array("goFaxRBlock",$source['objectClass'])){
347       if(isset($source["goFaxRBlocklist"])){
348         for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
349           $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
350         }
351       }
352       $this->type= BLOCK_LIST_RECEIVE;
353     }
355     /* Set base */
356     if ($this->dn == "new"){
357       if(session::is_set('CurrentMainBase')){
358         $this->base = session::get('CurrentMainBase');
359       }else{
360         $ui= get_userinfo();
361         $this->base= dn2base($ui->dn);
362       }
363     } else {
364       $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
365     }
366   }
369   /* Return plugin informations for acl handling */
370   static function plInfo()
371   {
372       return (array(
373                   "plShortName"       => _("Fax"),
374                   "plDescription"     => _("Fax blacklists"),
375                   "plSelfModify"      => FALSE,
376                   "plDepends"         => array(),
377                   "plRequirements"=> array(
378                       'ldapSchema' => array('goFaxRBlock' => '>=1.0.4','goFaxSBlock' => '>=1.0.4'),
379                       'onFailureDisablePlugin' => array(get_class(),'blocklist')
380                       ),
381                   "plPriority"    => 0,
382                   "plSection"     => array("administration" => _("Fax blacklists")),
383                   "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blacklists"),
384                           "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
385                   "plProperties" =>
386                   array(
387                       array(
388                           "name"          => "faxBlocklistRDN",
389                           "type"          => "rdn",
390                           "default"       => "ou=gofax,ou=systems,",
391                           "description"   => _("RDN for facsimile blocklist storage."),
392                           "check"         => "gosaProperty::isRdn",
393                           "migrate"       => "migrate_faxBlocklistRDN",
394                           "group"         => "plugin",
395                           "mandatory"     => FALSE
396                           )
397                       ),
399           "plProvidedAcls" => array(
400             "cn"              => _("Name"),
401             "description"     => _("Description"),
402             "base"            => _("Base"),
403             "goFaxSBlocklist"  => _("Send blacklist entries"),
404             "goFaxRBlocklist"  => _("Receive blacklist entries"),
405             "type"            => _("Blacklist type"))
406           ));
407   }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>