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;
24 var $orig_base = "";
25 var $orig_dn = "";
28 function __construct($config,$dn = "new")
29 {
30 plugin::plugin($config,$dn);
32 /* Set default list type */
33 $this->type = BLOCK_LIST_SEND;
35 /* Load defined numbers */
36 if($dn != "new"){
38 /* We will not be able to rename edited blocklists */
39 $this->readonly = TRUE;
41 /* Get blocklist type and defined numbers */
42 if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
43 if(isset($this->attrs["goFaxSBlocklist"])){
44 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
45 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
46 }
47 }
48 $this->type= BLOCK_LIST_SEND;
49 } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
50 if(isset($this->attrs["goFaxRBlocklist"])){
51 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
52 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
53 }
54 }
55 $this->type= BLOCK_LIST_RECEIVE;
56 }
57 }
59 /* Set base */
60 if ($this->dn == "new"){
61 if(session::is_set('CurrentMainBase')){
62 $this->base = session::get('CurrentMainBase');
63 }else{
64 $ui= get_userinfo();
65 $this->base= dn2base($ui->dn);
66 }
67 } else {
68 $this->base =preg_replace ("/^[^,]+,[^,]+,[^,]+,/","",$this->dn);
69 }
71 $this->orig_base = $this->base;
72 $this->orig_dn = $this->dn;
73 }
75 public function execute()
76 {
77 /* Log view */
78 if(!$this->view_logged){
79 $this->view_logged = TRUE;
80 new log("view","blocklist/".get_class($this),$this->dn);
81 }
84 /**************
85 * Base select dialog
86 **************/
88 $once = true;
89 foreach($_POST as $name => $value){
90 if(preg_match("/^chooseBase/",$name) && $once){
91 $once = false;
92 $this->dialog = new baseSelectDialog($this->config,$this,$this->get_allowed_bases());
93 $this->dialog->setCurrentBase($this->base);
94 }
95 }
97 /* Dialog handling */
98 if(is_object($this->dialog)){
99 /* Must be called before save_object */
100 $this->dialog->save_object();
102 if($this->dialog->isClosed()){
103 $this->dialog = false;
104 }elseif($this->dialog->isSelected()){
106 /* A new base was selected, check if it is a valid one */
107 $tmp = $this->get_allowed_bases();
108 if(isset($tmp[$this->dialog->isSelected()])){
109 $this->base = $this->dialog->isSelected();
110 }
111 $this->dialog= false;
113 }else{
114 return($this->dialog->execute());
115 }
116 }
119 /***************
120 Add numer to blocklist
121 ***************/
124 /* Handle interactions: add */
125 if (isset($_POST['add_number']) && $_POST['number'] != ""){
126 if (tests::is_phone_nr($_POST['number']) || preg_match ("/^[\/0-9 ()\^\.\$+*-]+$/",$_POST['number'])){
127 $this->addNumber ($_POST['number']);
128 } else {
129 msg_dialog::display(_("Error"), msgPool::invalid(_("Phone number")), ERROR_DIALOG);
130 }
131 }
134 /***************
135 Delete number from list
136 ***************/
138 /* Handle interactions: delete */
139 if (isset($_POST['delete_number']) && isset($_POST['numbers']) && count($_POST['numbers']) > 0){
140 $this->delNumber ($_POST['numbers']);
141 }
144 /***************
145 Template output
146 ***************/
148 $smarty = get_smarty();
149 foreach($this->attributes as $name){
150 $smarty->assign($name,$this->$name);
151 }
152 $tmp = $this->plInfo();
153 foreach($tmp['plProvidedAcls'] as $name => $translation){
154 $smarty->assign($name."ACL",$this->getacl($name));
155 }
156 $smarty->assign("goFaxBlocklist",$this->goFaxBlocklist);
157 $smarty->assign("cnACL",$this->getacl("cn",$this->readonly));
158 $smarty->assign("typeACL",$this->getacl("type",$this->readonly));
159 $smarty->assign("base",$this->base);
160 $smarty->assign("bases", $this->get_allowed_bases());
161 $smarty->assign("types", array(BLOCK_LIST_SEND => _("send"), BLOCK_LIST_RECEIVE => _("receive")));
162 $smarty->assign("type", $this->type);
163 $smarty->assign("dn", $this->dn);
164 return($smarty->fetch(get_template_path('generic.tpl', TRUE)));
165 }
168 public function save_object()
169 {
170 if(isset($_POST['blocklist_posted'])){
171 $tmp_cn = $this->cn;
172 plugin::save_object();
173 $this->cn = $tmp_cn;
175 /* Save base, since this is no LDAP attribute */
176 $tmp = $this->get_allowed_bases();
177 if(isset($_POST['base'])){
178 if(isset($tmp[$_POST['base']])){
179 $this->base= $_POST['base'];
180 }
181 }
182 $tmp = $this->attributes;
183 $tmp[] = "type";
184 foreach($tmp as $attr){
185 if(in_array($attr,array("cn","type")) && $this->readonly){
186 continue;
187 }elseif(isset($_POST[$attr]) && $this->acl_is_writeable($attr)){
188 $this->$attr = $_POST[$attr];
189 }
190 }
191 }
192 }
195 function remove_from_parent()
196 {
197 $ldap= $this->config->get_ldap_link();
198 $ldap->rmDir($this->dn);
199 new log("remove","gofaxlist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
200 if (!$ldap->success()){
201 msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
202 }
203 $this->handle_post_events("remove");
204 }
207 /* Check values */
208 function check()
209 {
210 /* Call common method to give check the hook */
211 $message= plugin::check();
213 /* check syntax: must cn */
214 if ($this->cn == ""){
215 $message[]= msgPool::required(_("Name"));
216 } else {
217 if (!tests::is_uid($this->cn)){
218 $message[]= msgPool::invalid(_("Name"));
219 }
220 if ($this->dn == 'new'){
221 $ldap= $this->config->get_ldap_link();
222 $ldap->cd (get_ou('blocklistou').$this->config->current["BASE"]);
223 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
224 if ($ldap->count() != 0){
225 $message[]= msgPool::duplicated(_("Name"));
226 }
227 }
228 }
230 /* Check if we are allowed to create or move this object
231 */
232 if($this->orig_dn == "new" && !$this->acl_is_createable($this->base)){
233 $message[] = msgPool::permCreate();
234 }elseif($this->base != $this->orig_base && !$this->acl_is_moveable($this->base)){
235 $message[] = msgPool::permMove();
236 }
238 return $message;
239 }
242 /* Save to LDAP */
243 function save()
244 {
245 /* Type selection */
246 if ($this->type == BLOCK_LIST_SEND){
247 $type= "goFaxSBlocklist";
248 $this->objectclasses = array("goFaxSBlock");
249 } else {
250 $type= "goFaxRBlocklist";
251 $this->objectclasses = array("goFaxRBlock");
252 }
254 plugin::save();
256 /* Add list */
257 if (count($this->goFaxBlocklist)){
258 $this->attrs[$type]= $this->goFaxBlocklist;
259 }
261 /* Write back to ldap */
262 $ldap= $this->config->get_ldap_link();
263 $ldap->cd($this->base);
264 $ldap->cat($this->dn, array('dn'));
265 if ($ldap->count()){
266 if (!isset($this->attrs[$type])){
267 $this->attrs[$type]= array();
268 }
269 $ldap->cd($this->dn);
270 $this->cleanup();
271 $ldap->modify($this->attrs);
272 new log("modify","faxblocklist/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
273 $this->handle_post_events("modify");
274 } else {
275 $ldap->cd($this->config->current['BASE']);
276 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
277 $ldap->cd($this->dn);
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",$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 }
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 blocklists"),
375 "plSelfModify" => FALSE,
376 "plDepends" => array(),
378 "plPriority" => 0,
379 "plSection" => array("administration" => _("Fax blocklists")),
380 "plCategory" => array("gofaxlist" => array("description" => _("Fax blocklists"),
381 "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
382 "plProvidedAcls" => array(
383 "cn" => _("Name"),
384 "description" => _("Description"),
385 "base" => _("Base"),
386 "goFaxBlocklist" => _("Blocklist"),
387 "type" => _("Blocklist type"))
388 ));
389 }
391 }
392 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
393 ?>