a415e19e9ade5d4ef7638c602f28537667b44c05
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);
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 if($str = $this->showSnapshotDialog($this->DivListBlocklist->selectedBase,
326 array("ou=gofax,ou=systems,". $this->DivListBlocklist->selectedBase))){
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 /* Clear garbage from edited lists */
346 function clear_fields()
347 {
348 $this->dn= "";
349 $this->description= "";
350 $this->cn= "";
351 $this->base= $_SESSION['CurrentMainBase'];
352 $this->goFaxBlocklist= array();
353 }
356 /* Reload the list of known blocklists */
357 function reload()
358 {
359 /* Init some vars */
360 $filter = $filter2 = "";
361 $base = $this->DivListBlocklist->selectedBase;
362 $Regex = $this->DivListBlocklist->Regex;
363 $SubSearch = $this->DivListBlocklist->SubSearch;
364 $ShowSendBocklists = $this->DivListBlocklist->ShowSendBocklists;
365 $ShowReceiveBlocklists = $this->DivListBlocklist->ShowReceiveBlocklists;
366 $Flags = GL_SIZELIMIT;
367 $res = $res2 = array();
369 /* Append subsearch to Flags */
370 if($SubSearch){
371 $Flags |= GL_SUBSEARCH;
372 }else{
373 $base = "ou=gofax,ou=systems,".$base;
374 }
376 /* Create filter */
377 if ($ShowSendBocklists){
378 $filter = "(&(objectClass=goFaxSBlock)(|(cn=".$Regex.")(goFaxSBlocklist=".$Regex.")))";
379 $res= get_list($filter, $this->ui->subtreeACL, $base,array("*"), $Flags);
380 }
381 if ($ShowReceiveBlocklists){
382 $filter2= "(&(objectClass=goFaxRBlock)(|(cn=".$Regex.")(goFaxRBlocklist=".$Regex.")))";
383 $res2= get_list($filter2, $this->ui->subtreeACL, $base,array("*"), $Flags);
384 }
386 $this->blocklists = array_merge($res,$res2);
388 /* appen && sort */
389 $tmp=array();
390 foreach($this->blocklists as $tkey => $val ){
391 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
392 }
393 ksort($tmp);
394 $this->blocklists=array();
395 foreach($tmp as $val){
396 $this->blocklists[]=$val;
397 }
398 reset ($this->blocklists);
399 }
402 function remove_from_parent()
403 {
404 $ldap= $this->config->get_ldap_link();
405 $ldap->rmDir($this->dn);
406 show_ldap_error($ldap->get_error(), _("Removing blocklist object failed"));
407 $this->clear_fields();
408 $this->handle_post_events("remove");
409 }
412 /* Save data to object */
413 function save_object()
414 {
415 $this->DivListBlocklist->save_object();
416 plugin::save_object();
418 if(isset($_POST['base'])){
419 $this->base = $_POST['base'];
420 }
421 foreach($this->attributes as $attr){
422 if(isset($_POST[$attr])){
423 $this->$attr = $_POST[$attr];
424 }
425 }
426 }
429 /* Check values */
430 function check()
431 {
432 /* Call common method to give check the hook */
433 $message= plugin::check();
435 /* Permissions for that base? */
436 if ($this->base != ""){
437 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
438 } else {
439 $new_dn= $this->dn;
440 }
442 if (chkacl($this->acl, "create") != ""){
443 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
444 }
447 /* check syntax: must cn */
448 if ($this->cn == ""){
449 $message[]= _("Required field 'Name' is not set.");
450 } else {
451 if (!is_uid($this->cn)){
452 $message[]= _("Required field 'Name' contains invalid characters");
453 }
454 if ($this->dn == 'new'){
455 $ldap= $this->config->get_ldap_link();
456 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
457 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
458 if ($ldap->count() != 0){
459 $message[]= _("Specified name is already used.");
460 }
461 }
462 }
464 /* Is user allowed to create in 'base'? */
465 if (chkacl($this->acl, "create") != ""){
466 $message[]= _("No permission to create a blocklist on this base.");
467 }
469 return $message;
470 }
473 /* Save to LDAP */
474 function save()
475 {
476 plugin::save();
479 /* Type selection */
480 if ($this->type == 0){
481 $type= "goFaxSBlock";
482 } else {
483 $type= "goFaxRBlock";
484 }
486 /* Add list */
487 $this->attrs['objectClass']= $type;
488 if (count($this->goFaxBlocklist)){
489 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
490 }
492 /* Write back to ldap */
493 $ldap= $this->config->get_ldap_link();
494 $ldap->cd($this->base);
495 $ldap->search("cn=$this->cn",array("cn"));
496 $ldap->cat($this->dn, array('dn'));
497 $a= $ldap->fetch();
499 if (count($a)){
500 if (!isset($this->attrs["$type"."list"])){
501 $this->attrs["$type"."list"]= array();
502 }
503 $ldap->cd($this->dn);
504 $this->cleanup();
505 $ldap->modify($this->attrs);
506 $this->handle_post_events("modify");
507 } else {
508 $ldap->cd($this->config->current['BASE']);
509 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
510 $ldap->cd($this->dn);
511 $ldap->add($this->attrs);
512 $this->handle_post_events("add");
513 }
514 show_ldap_error($ldap->get_error(), _("Saving blocklist object failed"));
515 }
518 /* Add number */
519 function addNumber($number)
520 {
521 if (!in_array($number, $this->goFaxBlocklist)){
522 $this->goFaxBlocklist[]= $number;
523 sort($this->goFaxBlocklist);
524 }
525 }
528 /* Remove number from list */
529 function delNumber($numbers)
530 {
531 $tmp= array();
532 foreach ($this->goFaxBlocklist as $val){
533 if (!in_array($val, $numbers)){
534 $tmp[]= $val;
535 }
536 }
537 $this->goFaxBlocklist= $tmp;
538 }
541 /* Delete lock */
542 function remove_lock()
543 {
544 if (isset($this->dn)){
545 del_lock ($this->dn);
546 }
547 }
549 }
550 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
551 ?>