Code

6c6cd0057b740e913c732cac66273335b097bddc
[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();
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("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         $this->addNumber ($_POST['number']);
105       } else {
106         msg_dialog::display(_("Error"), msgPool::invalid(_("Phone number")), ERROR_DIALOG);
107       }
108     }
111     /***************
112       Delete number from list
113      ***************/
115     /* Handle interactions: delete */
116     if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
117       $this->delNumber ($_POST['numbers']);
118     }
121     /***************
122       Template output
123      ***************/
125     $smarty = get_smarty();
126     $smarty->assign("usePrototype", "true");
127     foreach($this->attributes as $name){
128       $smarty->assign($name,$this->$name);
129     }
130     $tmp = $this->plInfo();
131     foreach($tmp['plProvidedAcls'] as $name => $translation){
132       $smarty->assign($name."ACL",$this->getacl($name));
133     }
134     $smarty->assign("goFaxBlocklist",$this->goFaxBlocklist);
135     $smarty->assign("cnACL",$this->getacl("cn",$this->readonly));
136     $smarty->assign("typeACL",$this->getacl("type",$this->readonly));
137     $smarty->assign("base", $this->baseSelector->render());
138     $smarty->assign("types", array(BLOCK_LIST_SEND => _("send"), BLOCK_LIST_RECEIVE => _("receive")));
139     $smarty->assign("type", $this->type);
140     $smarty->assign("dn", $this->dn);
141     $smarty->assign("read_only",$this->read_only);
142     return($smarty->fetch(get_template_path('generic.tpl', TRUE)));
143   }
146   public function save_object()
147   {
148     if(isset($_POST['blocklist_posted'])){
149       $tmp_cn = $this->cn;
150       plugin::save_object();
151       $this->cn = $tmp_cn;
153       /* Refresh base */
154       if ($this->acl_is_moveable($this->base)){
155         if (!$this->baseSelector->update()) {
156           msg_dialog::display(_("Error"), msgPool::permMove(), ERROR_DIALOG);
157         }
158         if ($this->base != $this->baseSelector->getBase()) {
159           $this->base= $this->baseSelector->getBase();
160           $this->is_modified= TRUE;
161         }
162       }
164       /* Save attrbutes */
165       $tmp = $this->attributes;
166       $tmp[] = "type";
167       foreach($tmp as $attr){
168         if(in_array($attr,array("cn","type")) && $this->readonly){
169           continue;
170         }elseif(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
171           $this->$attr = $_POST[$attr];
172         }
173       }
174     }
175   }
178   function remove_from_parent()
179   {
180     $ldap= $this->config->get_ldap_link();
181     $ldap->rmDir($this->dn);
182     new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
183     if (!$ldap->success()){
184       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
185     }
186     $this->handle_post_events("remove");
187   }
190   /* Check values */
191   function check()
192   {
193     /* Call common method to give check the hook */
194     $message= plugin::check();
196     /* check syntax: must cn */
197     if ($this->cn == ""){
198       $message[]= msgPool::required(_("Name"));
199     } else {
200       if (!tests::is_uid($this->cn)){
201         $message[]= msgPool::invalid(_("Name"));
202       }
203       if ($this->dn == 'new'){
204         $ldap= $this->config->get_ldap_link();
205         $ldap->cd (get_ou('faxBlocklistRDN').$this->config->current["BASE"]);
206         $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
207         if ($ldap->count() != 0){
208           $message[]= msgPool::duplicated(_("Name"));
209         }
210       }
211     }
213     // Check if a wrong base was supplied
214     if(!$this->baseSelector->checkLastBaseUpdate()){
215       $message[]= msgPool::check_base();;
216     }
217     
218     /* Check if we are allowed to create or move this object
219      */
220     if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
221       $message[] = msgPool::permCreate();
222     }elseif($this->orig_dn != "new" && $this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
223       $message[] = msgPool::permMove();
224     }
225   
226     return $message;
227   }
230   /* Save to LDAP */
231   function save()
232   {
233     /* Type selection */
234     if ($this->type == BLOCK_LIST_SEND){
235       $type= "goFaxSBlocklist";
236       $this->objectclasses = array("goFaxSBlock");
237     } else {
238       $type= "goFaxRBlocklist";
239       $this->objectclasses = array("goFaxRBlock");
240     }
242     plugin::save();
244     /* Let clenaup() know what attributes to handle 
245      */ 
246     $this->attrs[$type] = $this->goFaxBlocklist;
247     $this->attributes [] = $type;
249     /* Write back to ldap */
250     $ldap= $this->config->get_ldap_link();
251     $ldap->cd($this->base);
252     $ldap->cat($this->dn, array('dn'));
253     if ($ldap->count()){
254       $ldap->cd($this->dn);
255       $this->cleanup();
256       $ldap->modify($this->attrs);
257       new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
258       $this->handle_post_events("modify");
259     } else {
260       $ldap->cd($this->config->current['BASE']);
261       $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
262       $ldap->cd($this->dn);
263       $this->cleanup();
264       $ldap->add($this->attrs);
265       new log("create","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
266       $this->handle_post_events("add");
267     }
269     if (!$ldap->success()){
270       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
271     }
272   }
275   /* Add number */
276   function addNumber($number)
277   {
278     if (!in_array($number, $this->goFaxBlocklist)){
279       $this->goFaxBlocklist[]= $number;
280       sort($this->goFaxBlocklist);
281     }
282   }
285   /* Remove number from list */
286   function delNumber($numbers)
287   {
288     $tmp= array();
289     foreach ($this->goFaxBlocklist as $val){
290       if (!in_array($val, $numbers)){
291         $tmp[]= $val;
292       }
293     }
294     $this->goFaxBlocklist= $tmp;
295   }
298   function getCopyDialog()
299   { 
300     $smarty = get_smarty();
301     $smarty->assign("cn",$this->cn);
302     $str = $smarty->fetch(get_template_path("paste_generic.tpl",TRUE,dirname(__FILE__)));
303     $ret = array();
304     $ret['string'] = $str;
305     $ret['status'] = "";
306     return($ret);
307   }
309   function saveCopyDialog()
310   {
311     if(isset($_POST['cn'])){
312       $this->cn = get_post('cn');
313     }
314   }
316   
317   function PrepareForCopyPaste($source)
318   {
319     plugin::PrepareForCopyPaste($source);
321     /* We will not be able to rename edited blocklists */
322     $this->readonly = TRUE;
324     /* Get blocklist type and defined numbers */
325     if (in_array("goFaxSBlock",$source['objectClass'])){
326       if(isset($source["goFaxSBlocklist"])){
327         for ($i= 0; $i<$source["goFaxSBlocklist"]["count"]; $i++){
328           $this->goFaxBlocklist[]= $source["goFaxSBlocklist"][$i];
329         }
330       }
331       $this->type= BLOCK_LIST_SEND;
332     } elseif (in_array("goFaxRBlock",$source['objectClass'])){
333       if(isset($source["goFaxRBlocklist"])){
334         for ($i= 0; $i<$source["goFaxRBlocklist"]["count"]; $i++){
335           $this->goFaxBlocklist[]= $source["goFaxRBlocklist"][$i];
336         }
337       }
338       $this->type= BLOCK_LIST_RECEIVE;
339     }
341     /* Set base */
342     if ($this->dn == "new"){
343       if(session::is_set('CurrentMainBase')){
344         $this->base = session::get('CurrentMainBase');
345       }else{
346         $ui= get_userinfo();
347         $this->base= dn2base($ui->dn);
348       }
349     } else {
350       $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
351     }
352   }
355   /* Return plugin informations for acl handling */
356   static function plInfo()
357   {
358     return (array(
359           "plShortName"       => _("Fax"),
360           "plDescription"     => _("Fax blocklists"),
361           "plSelfModify"      => FALSE,
362           "plDepends"         => array(),
364           "plPriority"    => 0,
365           "plSection"     => array("administration" => _("Fax blocklists")),
366           "plCategory"    => array("gofaxlist"      => array("description" => _("Fax blocklists"),
367               "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
368           "plProvidedAcls" => array(
369             "cn"              => _("Name"),
370             "description"     => _("Description"),
371             "base"            => _("Base"),
372             "goFaxBlocklist"  => _("Blocklist"),
373             "type"            => _("Blocklist type"))
374           ));
375   }
378 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
379 ?>