79b5b3c72ef0352c581f6417945b9cf94e9f4815
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 $goFaxBlocklist= array();
18 var $base= "";
20 /* Headpage attributes */
21 var $blocklists= array();
23 /* attribute list for save action */
24 var $attributes= array("cn", "description");
25 var $objectclasses= array();
27 function blocklist ($config, $ui)
28 {
29 $this->ui= $ui;
30 $this->dn= "";
31 $this->config= $config;
33 /* Get global filter config */
34 if (!is_global("blockfilter")){
35 $ui= get_userinfo();
36 $this->base= get_base_from_people($ui->dn);
37 $blockfilter= array("sendlists" => "checked",
38 "receivelists" => "checked",
39 "entry" => "*",
40 "depselect" => $this->base,
41 "regex" => "*");
42 register_global("blockfilter", $blockfilter);
43 }else{
44 $blockfilter = $_SESSION["blockfilter"];
45 $this->base = $blockfilter['depselect'];
46 }
48 $ui= get_userinfo();
49 $acl= get_permissions ($ui->dn, $ui->subtreeACL);
50 $this->acl= get_module_permission($acl, "blocklists", $ui->dn);
51 }
53 function execute()
54 {
55 /* Call parent execute */
56 plugin::execute();
58 /* Get global smarty instance */
59 $smarty= get_smarty();
60 $blockfilter = get_global("blockfilter"); // contains Filter Settings
61 $s_action = ""; // Contains the action to proceed
62 $s_entry = ""; // The value for s_action
63 $base_back = ""; // The Link for Backbutton
65 /* Start for New List Managment */
66 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
67 $s_action="open";
68 $s_entry = base64_decode($_GET['dep_id']);
69 $blockfilter['depselect']= "".$this->config->departments[trim($s_entry)];
70 $this->reload();
71 }
73 /* Test Posts */
74 foreach($_POST as $key => $val){
75 // Post for delete
76 if(preg_match("/user_del.*/",$key)){
77 $s_action = "del";
78 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
79 // Post for edit
80 }elseif(preg_match("/user_edit_.*/",$key)){
81 $s_action="edit";
82 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
83 // Post for new
84 }elseif(preg_match("/dep_back.*/i",$key)){
85 $s_action="back";
86 }elseif(preg_match("/user_new.*/",$key)){
87 $s_action="new";
88 }elseif(preg_match("/dep_home.*/i",$key)){
89 $s_action="home";
90 }elseif(preg_match("/user_tplnew.*/i",$key)){
91 $s_action="new_tpl";
92 }elseif(preg_match("/user_chgpw.*/i",$key)){
93 $s_action="change_pw";
94 $s_entry = preg_replace("/user_chgpw_/i","",$key);
95 }elseif(preg_match("/dep_root.*/i",$key)){
96 $s_action="root";
97 }
98 }
102 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
103 $s_action ="edit";
104 $s_entry = $_GET['id'];
105 }
107 $s_entry = preg_replace("/_.$/","",$s_entry);
109 /* Department changed? */
110 if(isset($_POST['depselect']) && $_POST['depselect']){
111 $blockfilter['depselect']= $_POST['depselect'];
112 $this->reload();
113 }
115 /* Homebutton is posted */
116 if($s_action=="home"){
117 $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
118 $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$blockfilter['depselect']));
119 $this->reload();
120 }
122 if($s_action=="root"){
123 $blockfilter['depselect']=($this->config->current['BASE']);
124 $this->reload();
125 }
127 /* If Backbutton is Posted */
128 if($s_action=="back"){
129 $base_back = preg_replace("/^[^,]+,/","",$blockfilter['depselect']);
130 $base_back = convert_department_dn($base_back);
132 if(isset($this->config->departments[trim($base_back)])){
133 $blockfilter['depselect']= $this->config->departments[trim($base_back)];
134 }else{
135 $blockfilter['depselect']= $this->config->departments["/"];
136 }
137 $this->reload();
138 }
140 register_global("blockfilter", $blockfilter);
142 /* Save data */
143 $blockfilter= get_global("blockfilter");
144 foreach( array( "entry", "regex") as $type){
145 if (isset($_POST[$type])){
146 $blockfilter[$type]= $_POST[$type];
147 }
148 }
149 if (isset($_POST['depselect'])){
150 foreach( array("sendlists", "receivelists") as $type){
152 if (isset($_POST[$type])) {
153 $blockfilter[$type]= "checked";
154 } else {
155 $blockfilter[$type]= "";
156 }
157 }
158 }
159 if (isset($_GET['search'])){
160 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
161 if ($s == "**"){
162 $s= "*";
163 }
164 $blockfilter['regex']= $s;
165 }
166 register_global("blockfilter", $blockfilter);
168 /* Create new blocklist? */
169 if ($s_action=="new"){
170 $this->clear_fields();
171 $this->dn= "new";
172 plugin::plugin ($this->config, $this->dn);
173 }
175 /* Edit selected blocklist? */
176 if ($s_action=="edit"){
177 $this->clear_fields();
178 $this->dn=$this->blocklists[$s_entry]['dn'];
179 plugin::plugin ($this->config, $this->dn);
181 /* Load missing lists */
182 if (isset($this->attrs["goFaxSBlocklist"])){
183 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
184 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
185 }
186 $this->type= 0;
187 } elseif (isset($this->attrs["goFaxRBlocklist"])){
188 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
189 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
190 }
191 $this->type= 1;
192 }
193 $_SESSION['objectinfo']= trim($this->dn);
194 }
196 /* Delete blocklist requested */
197 if ($s_action=="del"){
198 $this->dn=$this->blocklists[$s_entry]['dn'];
199 /* Check locking */
200 if (($user= get_lock($this->dn)) != ""){
201 $_SESSION['dn']= $this->dn;
202 return(gen_locked_message($user, $this->dn));
203 } else {
204 # Lock this dn for editing
205 add_lock ($this->dn, $this->ui->dn);
207 $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
208 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
209 }
210 }
212 /* Finally delete blocklist */
213 if (isset($_POST['delete_blocklist_confirm'])){
214 if (chkacl($this->acl, "delete") == ""){
215 $this->remove_from_parent();
216 gosa_log ("Blocklist object'".$this->dn."' has been removed");
217 $this->reload ();
218 } else {
219 print_red (_("You have no permission to remove this blocklist."));
220 }
221 }
223 /* Cancel dialog */
224 if (isset($_POST['edit_cancel']) || isset($_POST['delete_blocklist_cancel']) ||
225 isset($_POST['delete_blocklist_confirm']) || isset($_POST['delete_lock'])){
227 del_lock ($this->dn);
228 $this->clear_fields();
229 unset($_SESSION['objectinfo']);
230 }
232 /* Handle interactions: add */
233 if (isset($_POST['add_number']) && $_POST['number'] != ""){
234 if (is_phone_nr($_POST['number'])){
235 $this->addNumber ($_POST['number']);
236 } else {
237 print_red (_("Please specify a valid phone number."));
238 }
239 }
241 /* Handle interactions: delete */
242 if (isset($_POST['delete_number']) && count($_POST['numbers']) > 0){
243 $this->delNumber ($_POST['numbers']);
244 }
246 /* What about finish? */
247 if (isset($_POST['edit_finish'])){
248 $message= $this->check();
250 /* No errors, save object */
251 if (count ($message) == 0){
253 /* Perpare 'dn' in case of new entries */
254 if ($this->dn == "new"){
255 $this->dn= "cn=$this->cn,ou=gofax,ou=systems,".$this->base;
256 }
258 $this->save ();
259 gosa_log ("Blocklist object'".$this->dn."' has been saved");
260 $this->dn= "";
261 del_lock ($this->ui->dn);
262 unset($_SESSION['objectinfo']);
263 } else {
264 /* Errors found, show message */
265 show_errors ($message);
266 }
267 }
269 /* Headpage or normal plugin screen? */
270 if ($this->dn == ""){
271 /* Check sorting variable */
272 $this->reload();
274 /* Check for exeeded sizelimit */
275 if (($message= check_sizelimit()) != ""){
276 return($message);
277 }
279 $options= "";
280 foreach ($this->config->idepartments as $key => $value){
281 if ($blockfilter['depselect'] == $key){
282 $options.= "<option selected value='$key'>$value</option>";
283 } else {
284 $options.= "<option value='$key'>$value</option>";
285 }
286 }
290 /* NEW LIST MANAGMENT */
291 $listhead = "<div style='background:#F0F0F9;padding:5px;'>".
292 " <input class='center' type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
293 " <input class='center' type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
294 " <input class='center' type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
295 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
296 " <input class='center' type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'> ".
297 " <img class='center' src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> "._("Base")." <select name='depselect' onChange='mainform.submit()' class='center'>$options</select>".
298 " <input class='center' type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
299 "</div>";
301 $action= "<input class='center' type='image' src='images/edit.png' alt='"._("edit")."' name='user_edit_%KEY%' title='"._("Edit user")."'>";
302 $action.= "<input class='center' type='image' src='images/edittrash.png' alt='"._("delete")."' name='user_del_%KEY%' title='"._("Delete user")."'>";
305 $divlist = new divlist("faxtabs");
306 $divlist->SetHeader(array(
307 array("string" => " ", "attach" => "style='text-align:center;width:20px;'"),
308 array("string" => _("Blocklist name")." / "._("Department"), "attach" => "style=''"),
309 array("string" => _("Actions"), "attach" => "style='width:60px;border-right:0px;text-align:right;'" )));
312 $divlist->SetSummary(_("This table displays faxblocklists for the selected tree."));
313 $divlist->SetEntriesPerPage(0);
315 // Defining Links
316 $linkopen = "<a href='?plug=".$_GET['plug']."&act=dep_open&dep_id=%s'>%s</a>";
317 foreach($this->departments as $key=> $val){
318 if(!isset($this->config->departments[trim($key)])){
319 $this->config->departments[trim($key)]="";
320 }
322 $non_empty="";
323 $keys= str_replace("/","\/",$key);
324 foreach($this->config->departments as $keyd=>$vald ){
325 if(preg_match("/".$keys."\/.*/",$keyd)){
326 $non_empty="full";
327 }
328 }
330 $field1 = array("string" => "<img src='images/".$non_empty."folder.png' alt='department'>", "attach" => "style='text-align:center;width:20px;'");
331 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val), "attach" => "style=''");
332 $field3 = array("string" => " ", "attach" => "style='width:60px;border-right:0px;text-align:right;'");
333 $divlist->AddEntry(array($field1,$field2,$field3));
334 }
336 // User and Template Images
337 $blockimg = "<img class='center' src='images/list_blocklist.png' alt='User' title='%s'>";
338 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
340 foreach($this->blocklists as $key => $val){
341 // Generate Array to Add
342 $display= "[".$val["cn"][0]."]";
343 $field1 = array("string" => sprintf($blockimg,$val['dn']), "attach" => "style='text-align:center;width:20px;'");
344 $field2 = array("string" => sprintf($editlink,$key,$display), "attach" => "style='' title='".$val['dn']."'");
345 $field3 = array("string" => preg_replace("/%KEY%/", "$key", $action), "attach" => "style='width:60px;border-right:0px;text-align:right;'");
346 $divlist->AddEntry( array($field1,$field2,$field3));
347 }
349 /* Show main page */
350 $smarty->assign("blocklistshead", $listhead);
351 $smarty->assign("blocklists", $divlist->DrawList());
352 $smarty->assign("search_image", get_template_path('images/search.png'));
353 $smarty->assign("tree_image", get_template_path('images/tree.png'));
354 $smarty->assign("infoimage", get_template_path('images/info.png'));
355 $smarty->assign("launchimage", get_template_path('images/launch.png'));
356 $smarty->assign("apply", apply_filter());
357 foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
358 $smarty->assign("$type", $blockfilter[$type]);
359 }
360 $smarty->assign("deplist", $this->config->idepartments);
361 $smarty->assign("alphabet", generate_alphabet());
362 $smarty->assign("hint", print_sizelimit_warning());
364 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
365 }
367 /* Set base */
368 if ($this->base == ""){
369 if ($this->dn == "new"){
370 $ui= get_userinfo();
371 $this->base= dn2base($ui->dn);
372 } else {
373 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
374 }
375 }
377 /* Fill templating stuff */
378 $smarty->assign("bases", $this->config->idepartments);
379 $smarty->assign("base_select", $this->base);
380 $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
381 if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
382 $smarty->assign("selectmode", "");
383 $smarty->assign("mode", "");
384 } else {
385 $smarty->assign("selectmode", "disabled");
386 $smarty->assign("mode", "readonly");
387 }
388 foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
389 $smarty->assign("$val", $this->$val);
390 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
391 }
393 /* Lock blocklist type for non new entries */
394 if ($this->dn != "new"){
395 $smarty->assign('typeACL', "disabled");
396 }
398 /* Show main page */
399 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
400 }
402 function clear_fields()
403 {
404 $this->dn= "";
405 $this->description= "";
406 $this->cn= "";
407 $this->base= $_SESSION['blockfilter']['depselect'];
408 $this->goFaxBlocklist= array();
409 }
411 function reload()
412 {
413 /* Get config */
414 $blockfilter= get_global('blockfilter');
416 /* Set base for all searches */
417 $base= $blockfilter['depselect'];
418 $filter= "";
420 /* Regex filter? */
421 if ($blockfilter['regex'] != ""){
422 $regex= $blockfilter['regex'];
423 } else {
424 $regex= "*";
425 }
427 /* Entry filter? */
428 /* Get list of blocklists to be shown */
429 if ($blockfilter['sendlists'] == "checked"){
430 $filter.= "(objectClass=goFaxSBlock)";
431 }
432 if ($blockfilter['receivelists'] == "checked"){
433 $filter.= "(objectClass=goFaxRBlock)";
434 }
435 if ($filter != ""){
436 $filter= "(|$filter)";
437 }
439 if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
440 $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
441 }
443 /* Generate blocklists */
444 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", FALSE, "ou=gofax,ou=systems,".$base, array("*"));
446 $this->blocklists=$res;
448 /* NEW LIST MANAGMENT
449 * We also need to search for the departments
450 * So we are able to navigate like in konquerer
451 */
452 $peopleBase = get_people_ou();
453 if(empty($peopleBase)){
454 $base2 = $base;
455 }else{
456 $base2 = preg_replace("/".$peopleBase.",/i","",$base);
457 }
459 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
460 TRUE, $base2, array("ou", "description"), TRUE);
462 $this->departments= array();
463 $tmp = array();
464 foreach ($res3 as $value){
465 $tmp[strtolower($value['dn']).$value['dn']]=$value;
466 }
467 ksort($tmp);
468 foreach($tmp as $value){
469 if(isset($value["description"][0])){
470 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
471 }else{
472 $this->departments[$value['dn']]=convert_department_dn2($value['dn']);//$value["description"][0];
473 }
474 }
476 /* END NEW LIST MANAGMENT
477 */
478 $tmp=array();
479 foreach($this->blocklists as $tkey => $val ){
480 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
481 }
482 ksort($tmp);
483 $this->blocklists=array();
484 foreach($tmp as $val){
485 $this->blocklists[]=$val;
486 }
487 reset ($this->blocklists);
488 }
490 function remove_from_parent()
491 {
492 $ldap= $this->config->get_ldap_link();
493 $ldap->rmDir($this->dn);
494 $this->handle_post_events("remove");
495 }
498 /* Save data to object */
499 function save_object()
500 {
501 plugin::save_object();
503 if(isset($_POST['base'])){
504 $this->base = $_POST['base'];
505 }
507 foreach($this->attributes as $attr){
508 if(isset($_POST[$attr])){
509 $this->$attr = $_POST[$attr];
510 }
511 }
512 }
514 /* Check values */
515 function check()
516 {
517 $message= array();
519 /* Permissions for that base? */
520 if ($this->base != ""){
521 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
522 } else {
523 $new_dn= $this->dn;
524 }
526 if (chkacl($this->acl, "create") != ""){
527 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
528 }
531 /* check syntax: must cn */
532 if ($this->cn == ""){
533 $message[]= _("Required field 'Name' is not set.");
534 } else {
535 if (!is_uid($this->cn)){
536 $message[]= _("Required field 'Name' contains invalid characters");
537 }
538 if ($this->dn == 'new'){
539 $ldap= $this->config->get_ldap_link();
540 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
541 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))(cn=".$this->cn."))", array("cn"));
542 if ($ldap->count() != 0){
543 $message[]= _("Specified name is already used.");
544 }
545 }
546 }
548 /* Is user allowed to create in 'base'? */
549 if (chkacl($this->acl, "create") != ""){
550 $message[]= _("No permission to create a blocklist on this base.");
551 }
553 return $message;
554 }
557 /* Save to LDAP */
558 function save()
559 {
560 plugin::save();
563 /* Type selection */
564 if ($this->type == 0){
565 $type= "goFaxSBlock";
566 } else {
567 $type= "goFaxRBlock";
568 }
570 /* Add list */
571 $this->attrs['objectClass']= $type;
572 if (count($this->goFaxBlocklist)){
573 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
574 }
576 /* Write back to ldap */
577 $ldap= $this->config->get_ldap_link();
578 $ldap->cd($this->base);
579 $ldap->search("cn=$this->cn",array("cn"));
580 $ldap->cat($this->dn);
581 $a= $ldap->fetch();
583 if (count($a)){
584 if (!isset($this->attrs["$type"."list"])){
585 $this->attrs["$type"."list"]= array();
586 }
587 $ldap->cd($this->dn);
588 $this->cleanup();
589 $ldap->modify($this->attrs);
590 $this->handle_post_events("modify");
591 } else {
592 $ldap->cd($this->config->current['BASE']);
593 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
594 $ldap->cd($this->dn);
595 $ldap->add($this->attrs);
596 $this->handle_post_events("add");
597 }
598 show_ldap_error($ldap->get_error());
599 }
602 function addNumber($number)
603 {
604 if (!in_array($number, $this->goFaxBlocklist)){
605 $this->goFaxBlocklist[]= $number;
606 sort($this->goFaxBlocklist);
607 }
608 }
610 function delNumber($numbers)
611 {
612 $tmp= array();
613 foreach ($this->goFaxBlocklist as $val){
614 if (!in_array($val, $numbers)){
615 $tmp[]= $val;
616 }
617 }
618 $this->goFaxBlocklist= $tmp;
619 }
621 function remove_lock()
622 {
623 if (isset($this->dn)){
624 del_lock ($this->dn);
625 }
626 }
628 }
629 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
630 ?>