1 <?php
2 class blocklist extends plugin
3 {
4 /* Definitions */
5 var $plHeadline= "FAX Blocklists";
6 var $plDescription= "This does something";
8 /* CLI vars */
9 var $cli_summary= "Handling of GOfax block lists";
10 var $cli_description= "Some longer text\nfor help";
11 var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
13 /* blocklist attributes */
14 var $cn = "";
15 var $description = "";
16 var $type = "";
17 var $base = "";
19 var $goFaxBlocklist = array();
20 var $blocklists = array();
21 var $attributes = array("cn", "description");
22 var $objectclasses = array();
23 var $DivListBlocklist = NULL;
25 function blocklist ($config, $ui)
26 {
27 /* Init class */
28 $this->ui = $ui;
29 $this->dn = "";
30 $this->config = $config;
32 $ui = get_userinfo();
33 $acl = get_permissions ($ui->dn, $ui->subtreeACL);
34 $this->acl = get_module_permission($acl, "blocklists", $ui->dn);
36 $this->DivListBlocklist = new divListBlocklist($this->config,$this);
37 }
39 function execute()
40 {
41 /* Call parent execute */
42 plugin::execute();
44 /***************
45 Init vars
46 ***************/
48 /* Get global smarty instance */
49 $smarty = get_smarty();
50 $s_action = ""; // Contains the action to proceed
51 $s_entry = ""; // The value for s_action
52 $base_back = ""; // The Link for Backbutton
55 /***************
56 Fetch posts
57 ***************/
59 /* Test Posts */
60 foreach($_POST as $key => $val){
61 // Post for delete
62 if(preg_match("/user_del.*/",$key)){
63 $s_action = "del";
64 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
65 // Post for edit
66 }elseif(preg_match("/user_edit_.*/",$key)){
67 $s_action="edit";
68 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
69 // Post for new
70 }elseif(preg_match("/user_new.*/",$key)){
71 $s_action="new";
72 }elseif(preg_match("/user_tplnew.*/i",$key)){
73 $s_action="new_tpl";
74 }elseif(preg_match("/user_chgpw.*/i",$key)){
75 $s_action="change_pw";
76 $s_entry = preg_replace("/user_chgpw_/i","",$key);
77 }
78 }
80 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
81 $s_action ="edit";
82 $s_entry = $_GET['id'];
83 }
85 $s_entry = preg_replace("/_.$/","",$s_entry);
88 /***************
89 Cancel some dialogs
90 ***************/
92 /* Cancel dialog */
93 if (isset($_POST['edit_cancel']) ||
94 isset($_POST['delete_blocklist_cancel']) ||
95 isset($_POST['delete_blocklist_confirm']) ||
96 isset($_POST['delete_lock'])){
98 if(!isset($_POST['delete_blocklist_confirm'])){
99 $this->clear_fields();
100 }
101 del_lock ($this->dn);
102 unset($_SESSION['objectinfo']);
103 }
106 /***************
107 Cancel some dialogs
108 ***************/
110 /* Create new blocklist? */
111 if ($s_action=="new"){
112 $this->clear_fields();
113 $this->dn= "new";
114 plugin::plugin ($this->config, $this->dn);
115 }
118 /***************
119 Edit blocklist
120 ***************/
122 /* Edit selected blocklist? */
123 if (($s_action=="edit") && (empty($this->dn))){
124 $this->clear_fields();
125 $this->dn=$this->blocklists[$s_entry]['dn'];
126 if (($user= get_lock($this->dn)) != ""){
127 $_SESSION['dn']= $this->dn;
128 //$this->dn ="";
129 return(gen_locked_message($user, $this->dn));
130 } else {
131 # Lock this dn for editing
132 add_lock ($this->dn, $this->ui->dn);
134 plugin::plugin ($this->config, $this->dn);
136 /* Load missing lists */
137 if (in_array("goFaxSBlock",$this->attrs['objectClass'])){
138 if(isset($this->attrs["goFaxSBlocklist"])){
139 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
140 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
141 }
142 }
143 $this->type= 0;
144 } elseif (in_array("goFaxRBlock",$this->attrs['objectClass'])){
145 if(isset($this->attrs["goFaxRBlocklist"])){
146 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
147 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
148 }
149 }
150 $this->type= 1;
151 }
152 $_SESSION['objectinfo']= trim($this->dn);
153 }
154 }
157 /***************
158 Remove blocklist
159 ***************/
161 /* Delete blocklist requested */
162 if ($s_action=="del"){
163 $this->dn=$this->blocklists[$s_entry]['dn'];
164 /* Check locking */
165 if (($user= get_lock($this->dn)) != ""){
166 $_SESSION['dn']= $this->dn;
167 return(gen_locked_message($user, $this->dn));
168 } else {
169 # Lock this dn for editing
170 add_lock ($this->dn, $this->ui->dn);
172 $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
173 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
174 }
175 }
178 /***************
179 Remove blocklist confirmed
180 ***************/
182 /* Finally delete blocklist */
183 if (isset($_POST['delete_blocklist_confirm'])){
184 if (chkacl($this->acl, "delete") == ""){
185 $this->remove_from_parent();
186 gosa_log ("Blocklist object'".$this->dn."' has been removed");
187 $this->reload ();
188 } else {
189 print_red (_("You have no permission to remove this blocklist."));
190 }
191 }
194 /***************
195 Add numer to blocklist
196 ***************/
199 /* Handle interactions: add */
200 if (isset($_POST['add_number']) && $_POST['number'] != ""){
201 if (is_phone_nr($_POST['number'])){
202 $this->addNumber ($_POST['number']);
203 } else {
204 print_red (_("Please specify a valid phone number."));
205 }
206 }
209 /***************
210 Delete number from list
211 ***************/
213 /* Handle interactions: delete */
214 if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
215 $this->delNumber ($_POST['numbers']);
216 }
219 /***************
220 Edit finished
221 ***************/
223 /* What about finish? */
224 if ((isset($_POST['edit_finish']) || isset($_POST['edit_apply'])) && (!empty($this->dn))){
225 $message= $this->check();
226 $this->remove_lock();
228 /* No errors, save object */
229 if (count ($message) == 0){
231 /* Perpare 'dn' in case of new entries */
232 if ($this->dn == "new"){
233 $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
234 }
236 $this->save ();
237 gosa_log ("Blocklist object'".$this->dn."' has been saved");
239 if (!isset($_POST['edit_apply'])){
240 $this->dn= "";
241 del_lock ($this->ui->dn);
242 unset($_SESSION['objectinfo']);
243 }
244 } else {
245 /* Errors found, show message */
246 show_errors ($message);
247 }
248 }
251 /***************
252 Object currently in edit
253 ***************/
255 if($this->dn){
257 /* Base select dialog */
258 $once = true;
259 foreach($_POST as $name => $value){
260 if(preg_match("/^chooseBase/",$name) && $once){
261 $once = false;
262 $this->dialog = new baseSelectDialog($this->config,$this);
263 $this->dialog->setCurrentBase($this->base);
264 }
265 }
267 /* Dialog handling */
268 if(is_object($this->dialog)){
269 /* Must be called before save_object */
270 $this->dialog->save_object();
272 if($this->dialog->isClosed()){
273 $this->dialog = false;
274 }elseif($this->dialog->isSelected()){
275 $this->base = $this->dialog->isSelected();
276 $this->dialog= false;
277 }else{
278 return($this->dialog->execute());
279 }
280 }
282 /* Set base */
283 if ($this->base == ""){
284 if ($this->dn == "new"){
285 $ui= get_userinfo();
286 $this->base= dn2base($ui->dn);
287 } else {
288 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
289 }
290 }
292 /* Fill templating stuff */
293 $smarty->assign("bases", $this->config->idepartments);
294 $smarty->assign("base_select", $this->base);
295 $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
296 if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
297 $smarty->assign("selectmode", "");
298 $smarty->assign("mode", "");
299 $smarty->assign("apply", "0");
300 } else {
301 $smarty->assign("selectmode", "disabled");
302 $smarty->assign("mode", "readonly");
303 $smarty->assign("apply", "1");
304 }
305 foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
306 $smarty->assign("$val", $this->$val);
307 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
308 }
310 /* Lock blocklist type for non new entries */
311 if ($this->dn != "new"){
312 $smarty->assign('typeACL', "disabled");
313 }
315 /* Show main page */
316 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
318 }else{
320 /***************
321 Divlist dialog
322 ***************/
324 /* Check if there is a snapshot dialog open */
325 $base = $this->DivListBlocklist->selectedBase;
326 if($str = $this->showSnapshotDialog($base,$this->get_used_snapshot_bases())){
327 return($str);
328 }
330 /* Display dialog with system list */
331 $this->DivListBlocklist->parent = $this;
332 $this->DivListBlocklist->execute();
334 /* Add departments if subsearch is disabled */
335 if(!$this->DivListBlocklist->SubSearch){
336 $this->DivListBlocklist->AddDepartments($this->DivListBlocklist->selectedBase);
337 }
338 $this->reload();
339 $this->DivListBlocklist->setEntries($this->blocklists);
340 return($this->DivListBlocklist->Draw());
341 }
342 }
345 /* Return departments, that will be included within snapshot detection */
346 function get_used_snapshot_bases()
347 {
348 return(array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase));
349 }
352 /* Clear garbage from edited lists */
353 function clear_fields()
354 {
355 $this->dn= "";
356 $this->description= "";
357 $this->cn= "";
358 $this->base= $_SESSION['CurrentMainBase'];
359 $this->goFaxBlocklist= array();
360 }
363 /* Reload the list of known blocklists */
364 function reload()
365 {
366 /* Init some vars */
367 $filter = $filter2 = "";
368 $base = $this->DivListBlocklist->selectedBase;
369 $Regex = $this->DivListBlocklist->Regex;
370 $SubSearch = $this->DivListBlocklist->SubSearch;
371 $ShowSendBocklists = $this->DivListBlocklist->ShowSendBocklists;
372 $ShowReceiveBlocklists = $this->DivListBlocklist->ShowReceiveBlocklists;
373 $Flags = GL_SIZELIMIT;
374 $res = $res2 = array();
376 /* Append subsearch to Flags */
377 if($SubSearch){
378 $Flags |= GL_SUBSEARCH;
379 }else{
380 $base = "ou=gofax,ou=systems,".$base;
381 }
383 /* Create filter */
384 if ($ShowSendBocklists){
385 $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
386 $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
387 }
388 if ($ShowReceiveBlocklists){
389 $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
390 $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
391 }
393 $this->blocklists = array_merge($res,$res2);
395 /* appen && sort */
396 $tmp=array();
397 foreach($this->blocklists as $tkey => $val ){
398 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
399 }
400 ksort($tmp);
401 $this->blocklists=array();
402 foreach($tmp as $val){
403 $this->blocklists[]=$val;
404 }
405 reset ($this->blocklists);
406 }
409 function remove_from_parent()
410 {
411 $ldap= $this->config->get_ldap_link();
412 $ldap->rmDir($this->dn);
413 show_ldap_error($ldap->get_error(), sprintf(_("Removing of blocklist with dn '%s' failed."),$this->dn));
414 $this->clear_fields();
415 $this->handle_post_events("remove");
416 }
419 /* Save data to object */
420 function save_object()
421 {
422 $this->DivListBlocklist->save_object();
423 plugin::save_object();
425 if(isset($_POST['base'])){
426 $this->base = $_POST['base'];
427 }
428 foreach($this->attributes as $attr){
429 if(isset($_POST[$attr])){
430 $this->$attr = $_POST[$attr];
431 }
432 }
433 if(isset($_POST['type'])){
434 $this->type = $_POST['type'];
435 }
436 }
439 /* Check values */
440 function check()
441 {
442 /* Call common method to give check the hook */
443 $message= plugin::check();
445 /* Permissions for that base? */
446 if ($this->base != ""){
447 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
448 } else {
449 $new_dn= $this->dn;
450 }
452 if (chkacl($this->acl, "create") != ""){
453 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
454 }
457 /* check syntax: must cn */
458 if ($this->cn == ""){
459 $message[]= _("Required field 'Name' is not set.");
460 } else {
461 if (!is_uid($this->cn)){
462 $message[]= _("Required field 'Name' contains invalid characters");
463 }
464 if ($this->dn == 'new'){
465 $ldap= $this->config->get_ldap_link();
466 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
467 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
468 if ($ldap->count() != 0){
469 $message[]= _("Specified name is already used.");
470 }
471 }
472 }
474 /* Is user allowed to create in 'base'? */
475 if (chkacl($this->acl, "create") != ""){
476 $message[]= _("No permission to create a blocklist on this base.");
477 }
479 return $message;
480 }
483 /* Save to LDAP */
484 function save()
485 {
486 plugin::save();
489 /* Type selection */
490 if ($this->type == 0){
491 $type= "goFaxSBlock";
492 } else {
493 $type= "goFaxRBlock";
494 }
496 /* Add list */
497 $this->attrs['objectClass']= $type;
498 if (count($this->goFaxBlocklist)){
499 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
500 }
502 /* Write back to ldap */
503 $ldap= $this->config->get_ldap_link();
504 $ldap->cd($this->base);
505 $ldap->search("cn=$this->cn",array("cn"));
506 $ldap->cat($this->dn, array('dn'));
507 $a= $ldap->fetch();
509 if (count($a)){
510 if (!isset($this->attrs["$type"."list"])){
511 $this->attrs["$type"."list"]= array();
512 }
513 $ldap->cd($this->dn);
514 $this->cleanup();
515 $ldap->modify($this->attrs);
516 $this->handle_post_events("modify");
517 } else {
518 $ldap->cd($this->config->current['BASE']);
519 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
520 $ldap->cd($this->dn);
521 $ldap->add($this->attrs);
522 $this->handle_post_events("add");
523 }
524 show_ldap_error($ldap->get_error(), sprintf(_("Saving of blocklist with dn '%s' failed."),$this->dn));
525 }
528 /* Add number */
529 function addNumber($number)
530 {
531 if (!in_array($number, $this->goFaxBlocklist)){
532 $this->goFaxBlocklist[]= $number;
533 sort($this->goFaxBlocklist);
534 }
535 }
538 /* Remove number from list */
539 function delNumber($numbers)
540 {
541 $tmp= array();
542 foreach ($this->goFaxBlocklist as $val){
543 if (!in_array($val, $numbers)){
544 $tmp[]= $val;
545 }
546 }
547 $this->goFaxBlocklist= $tmp;
548 }
551 /* Delete lock */
552 function remove_lock()
553 {
554 if (isset($this->dn)){
555 del_lock ($this->dn);
556 }
557 }
559 /* Return plugin informations for acl handling */
560 function plInfo()
561 {
562 return (array(
563 "plShortName" => _("Fax"),
564 "plDescription" => _("Fax Blocklists"),
565 "plSelfModify" => TRUE,
566 "plDepends" => array(),
568 "plPriority" => 0,
569 "plSection" => array("administration" => _("FAX Blocklists")),
570 "plCategory" => array("gofax" => array("description" => _("Fax"),
571 "objectClass" => array("goFaxRBlock","goFaxSBlock"))),
575 "plProvidedAcls" => array(
576 "cn" => _("Name"),
577 "description" => _("Description"),
578 "base" => _("Base"),
579 "type" => _("Blocklist type"))
580 ));
581 }
583 }
584 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
585 ?>