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