bd8f9a7a8359d645b0c869fb206a45e298fbb17c
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 print_a($_SESSION['blocklist']);
36 $uii= get_userinfo();
37 $base= get_base_from_people($ui->dn);
38 $blockfilter= array("sendlists" => "checked",
39 "receivelists" => "checked",
40 "entry" => "*",
41 "depselect" => $base,
42 "regex" => "*");
43 register_global("blockfilter", $blockfilter);
44 }else{
45 $blockfilter = $_SESSION["blockfilter"];
46 $this->base = $blockfilter['depselect'];
47 }
48 }
50 function execute()
51 {
52 /* Get global smarty instance */
53 $smarty= get_smarty();
54 $blockfilter = get_global("blockfilter"); // contains Filter Settings
55 $s_action = ""; // Contains the action to proceed
56 $s_entry = ""; // The value for s_action
57 $base_back = ""; // The Link for Backbutton
59 /* Start for New List Managment */
60 if(isset($_GET['act'])&&($_GET['act']=="dep_open")){
61 $s_action="open";
62 $s_entry = base64_decode($_GET['dep_id']);
63 $blockfilter['depselect']= "".$this->config->departments[trim($s_entry)];
64 $this->reload();
65 }
67 /* Test Posts */
68 foreach($_POST as $key => $val){
69 // Post for delete
70 if(preg_match("/user_del.*/",$key)){
71 $s_action = "del";
72 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
73 // Post for edit
74 }elseif(preg_match("/user_edit_.*/",$key)){
75 $s_action="edit";
76 $s_entry = preg_replace("/user_".$s_action."_/i","",$key);
77 // Post for new
78 }elseif(preg_match("/dep_back.*/i",$key)){
79 $s_action="back";
80 }elseif(preg_match("/user_new.*/",$key)){
81 $s_action="new";
82 }elseif(preg_match("/dep_home.*/i",$key)){
83 $s_action="home";
84 }elseif(preg_match("/user_tplnew.*/i",$key)){
85 $s_action="new_tpl";
86 }elseif(preg_match("/user_chgpw.*/i",$key)){
87 $s_action="change_pw";
88 $s_entry = preg_replace("/user_chgpw_/i","",$key);
89 }elseif(preg_match("/dep_root.*/i",$key)){
90 $s_action="root";
91 }
92 }
96 if((isset($_GET['act']))&&($_GET['act']=="edit_entry")){
97 $s_action ="edit";
98 $s_entry = $_GET['id'];
99 }
101 $s_entry = preg_replace("/_.$/","",$s_entry);
103 /* Department changed? */
104 if(isset($_POST['depselect']) && $_POST['depselect']){
105 $blockfilter['depselect']= $_POST['depselect'];
106 $this->reload();
107 }
109 /* Homebutton is posted */
110 if($s_action=="home"){
111 $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$this->ui->dn));
112 $blockfilter['depselect']=(preg_replace("/^[^,]+,/","",$blockfilter['depselect']));
113 $this->reload();
114 }
116 if($s_action=="root"){
117 $blockfilter['depselect']=($this->config->current['BASE']);
118 $this->reload();
119 }
121 /* If Backbutton is Posted */
122 if($s_action=="back"){
123 $base_back = preg_replace("/^[^,]+,/","",$blockfilter['depselect']);
124 $base_back = convert_department_dn($base_back);
126 if(isset($this->config->departments[trim($base_back)])){
127 $blockfilter['depselect']= $this->config->departments[trim($base_back)];
128 }else{
129 $blockfilter['depselect']= $this->config->departments["/"];
130 }
131 $this->reload();
132 }
134 register_global("blockfilter", $blockfilter);
136 /* Save data */
137 $blockfilter= get_global("blockfilter");
138 foreach( array( "entry", "regex") as $type){
139 if (isset($_POST[$type])){
140 $blockfilter[$type]= $_POST[$type];
141 }
142 }
143 if (isset($_POST['depselect'])){
144 foreach( array("sendlists", "receivelists") as $type){
146 if (isset($_POST[$type])) {
147 $blockfilter[$type]= "checked";
148 } else {
149 $blockfilter[$type]= "";
150 }
151 }
152 }
153 if (isset($_GET['search'])){
154 $s= mb_substr($_GET['search'], 0, 1, "UTF8")."*";
155 if ($s == "**"){
156 $s= "*";
157 }
158 $blockfilter['regex']= $s;
159 }
160 register_global("blockfilter", $blockfilter);
162 /* Create new blocklist? */
163 if ($s_action=="new"){
164 $this->clear_fields();
165 $this->acl= "#all#";
166 $this->dn= "new";
167 plugin::plugin ($this->config, $this->dn);
168 }
170 /* Edit selected blocklist? */
171 if ($s_action=="edit"){
172 $this->clear_fields();
173 $this->dn=$this->blocklists[$s_entry]['dn'];
174 $this->acl= get_permissions ($this->dn, $this->ui->subtreeACL);
175 $this->acl= get_module_permission($this->acl, "blocklists", $this->dn);
176 plugin::plugin ($this->config, $this->dn);
178 /* Load missing lists */
179 if (isset($this->attrs["goFaxSBlocklist"])){
180 for ($i= 0; $i<$this->attrs["goFaxSBlocklist"]["count"]; $i++){
181 $this->goFaxBlocklist[]= $this->attrs["goFaxSBlocklist"][$i];
182 }
183 $this->type= 0;
184 } elseif (isset($this->attrs["goFaxRBlocklist"])){
185 for ($i= 0; $i<$this->attrs["goFaxRBlocklist"]["count"]; $i++){
186 $this->goFaxBlocklist[]= $this->attrs["goFaxRBlocklist"][$i];
187 }
188 $this->type= 1;
189 }
190 $_SESSION['objectinfo']= trim($this->dn);
191 }
193 /* Delete blocklist requested */
194 if ($s_action=="del"){
195 $this->dn=$this->blocklists[$s_entry]['dn'];
196 /* Check locking */
197 if (($user= get_lock($this->dn)) != ""){
198 $_SESSION['dn']= $this->dn;
199 return(gen_locked_message($user, $this->dn));
200 } else {
201 # Lock this dn for editing
202 add_lock ($this->dn, $this->ui->dn);
204 $smarty->assign("info", sprintf(_("You're about to delete the blocklist '%s'."), $this->dn));
205 return($smarty->fetch(get_template_path('remove.tpl', TRUE)));
206 }
207 }
209 /* Finally delete blocklist */
210 if (isset($_POST['delete_blocklist_confirm'])){
211 $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
212 $acl= get_module_permission($acl, "blocklists", $this->dn);
213 if (chkacl($acl, "all") == ""){
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->acl= "#none#";
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 type='image' align='middle' src='images/list_back.png' title='"._("Go up one department")."' alt='"._("Up")."' name='dep_back'> ".
293 " <input type='image' src='images/list_root.png' align='middle' title='"._("Go to root department")."' name='dep_root' alt='"._("Root")."'> ".
294 " <input type='image' align='middle' src='images/list_home.png' title='"._("Go to users department")."' alt='"._("Home")."' name='dep_home'> ".
295 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> ".
296 " <input type='image' align='middle' src='images/list_new_blocklist.png' title='"._("Create new blocklist")."' alt='"._("New Blocklist")."' name='user_new'> ".
297 " <img src='images/list_seperator.png' align='middle' alt='' height='16' width='1'> "._("Current base")." <select name='depselect' onChange='mainform.submit()'>$options</select>".
298 " <input type='image' src='images/list_submit.png' align='middle' title='"._("Submit department")."' name='submit_department' alt='"._("Submit")."'> ".
299 "</div>";
301 $action= "<input type='image' src='images/edit.png' alt='"._("edit")."' name='user_edit_%KEY%' title='"._("Edit user")."'>";
302 $action.= "<input type='image' src='images/editdelete.png' alt='"._("delete")."' name='user_del_%KEY%' title='"._("Delete user")."'>";
305 $divlist = new divlist("faxtabs");
306 $divlist->SetHeader(array(
307 array("string" => " "),
308 array("string" => _("Blocklist name")." / "._("Department")),
309 array("string" => _("Actions"), "attach" => "style='border:none'" )));
312 $divlist->SetSummary(_("This table displays faxblocklists for the selected tree."));
313 $divlist->SetEntriesPerPage(20);
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 $field1 = array("string" => "<img src='images/folder.png' alt='department'>");
319 $field2 = array("string" => sprintf($linkopen,base64_encode($key),$val));
320 $field3 = array("string" => " ", "attach" => "style='border:none'");
321 $divlist->AddEntry(array($field1,$field2,$field3));
322 }
324 // User and Template Images
325 $blockimg = "<img src='images/list_blocklist.png' alt='User' title='%s'>";
326 $editlink = "<a href='?plug=".$_GET['plug']."&id=%s&act=edit_entry'>%s</a>";
328 foreach($this->blocklists as $key => $val){
329 // Generate Array to Add
330 $display= "[".$val["cn"][0]."]";
331 $field1 = array("string" => sprintf($blockimg,$val['dn']),"attach"=>"style='width:20px;align:middle;'");
332 $field2 = array("string" => sprintf($editlink,$key,$display),"attach"=>"title='".$val['dn']."'");
333 $field3 = array("string" => preg_replace("/%KEY%/", "$key", $action),"attach" => "style='border:none; text-align:right;width:40px'");
334 $divlist->AddEntry( array($field1,$field2,$field3));
335 }
337 /* Show main page */
338 $smarty->assign("blocklistshead", $listhead);
339 $smarty->assign("blocklists", $divlist->DrawList());
340 $smarty->assign("search_image", get_template_path('images/search.png'));
341 $smarty->assign("tree_image", get_template_path('images/tree.png'));
342 $smarty->assign("infoimage", get_template_path('images/info.png'));
343 $smarty->assign("launchimage", get_template_path('images/launch.png'));
344 $smarty->assign("apply", apply_filter());
345 foreach( array("depselect", "entry", "regex", "sendlists", "receivelists") as $type){
346 $smarty->assign("$type", $blockfilter[$type]);
347 }
348 $smarty->assign("deplist", $this->config->idepartments);
349 $smarty->assign("alphabet", generate_alphabet());
350 $smarty->assign("hint", print_sizelimit_warning());
352 return($smarty->fetch(get_template_path('headpage.tpl', TRUE)));
353 }
355 /* Set base */
356 if ($this->base == ""){
357 if ($this->dn == "new"){
358 $ui= get_userinfo();
359 $this->base= dn2base($ui->dn);
360 } else {
361 $this->base= preg_replace ("/^[^,]+,[^,]+,[^,]+,/", "", $this->dn);
362 }
363 }
365 /* Fill templating stuff */
366 $smarty->assign("bases", $this->config->idepartments);
367 $smarty->assign("base_select", $_SESSION['blockfilter']['depselect']);//$this->base);
368 $smarty->assign("types", array(0 => _("send"), 1 => _("receive")));
369 if ($this->dn == "new" || preg_match ("/,ou=incoming,/", $this->dn)){
370 $smarty->assign("selectmode", "");
371 $smarty->assign("mode", "");
372 } else {
373 $smarty->assign("selectmode", "disabled");
374 $smarty->assign("mode", "readonly");
375 }
376 foreach(array("cn", "description", "type", "goFaxBlocklist") as $val){
377 $smarty->assign("$val", $this->$val);
378 $smarty->assign("$val"."ACL", chkacl($this->acl, "$val"));
379 }
381 /* Lock blocklist type for non new entries */
382 if ($this->dn != "new"){
383 $smarty->assign('typeACL', "disabled");
384 }
386 /* Show main page */
387 return($smarty->fetch (get_template_path('generic.tpl', TRUE)));
388 }
390 function clear_fields()
391 {
392 $this->dn= "";
393 $this->description= "";
394 $this->cn= "";
395 $this->base= "";
396 $this->goFaxBlocklist= array();
397 $this->acl= "#none#";
398 }
400 function reload()
401 {
402 /* Get config */
403 $blockfilter= get_global('blockfilter');
405 /* Set base for all searches */
406 $base= $blockfilter['depselect'];
407 $filter= "";
409 /* Regex filter? */
410 if ($blockfilter['regex'] != ""){
411 $regex= $blockfilter['regex'];
412 } else {
413 $regex= "*";
414 }
416 /* Entry filter? */
417 /* Get list of blocklists to be shown */
418 if ($blockfilter['sendlists'] == "checked"){
419 $filter.= "(objectClass=goFaxSBlock)";
420 }
421 if ($blockfilter['receivelists'] == "checked"){
422 $filter.= "(objectClass=goFaxRBlock)";
423 }
424 if ($filter != ""){
425 $filter= "(|$filter)";
426 }
428 if ($blockfilter['entry'] != "" && $blockfilter['entry'] != "*"){
429 $filter.= "(|(goFaxSBlocklist=".$blockfilter['entry'].")(goFaxRBlocklist=".$blockfilter['entry']."))";
430 }
432 /* Generate blocklists */
433 $res= get_list($this->ui->subtreeACL, "(&(cn=$regex)$filter)", TRUE, $base, array("*"));
435 $this->blocklists=$res;
437 /* NEW LIST MANAGMENT
438 * We also need to search for the departments
439 * So we are able to navigate like in konquerer
440 */
441 $base2 = preg_replace("/ou=people,/i","",$base);
443 $res3 = get_list2($this->ui->subtreeACL, "(&(|(ou=$regex)(description=$regex))(objectClass=gosaDepartment))",
444 TRUE, $base2, array("ou", "description"), TRUE);
446 $this->departments= array();
447 $tmp = array();
448 foreach ($res3 as $value){
449 $tmp[strtolower($value['dn']).$value['dn']]=$value;
450 }
451 ksort($tmp);
452 foreach($tmp as $value){
453 if($value["description"][0]!=".."){
454 $this->departments[$value['dn']]=convert_department_dn2($value['dn'])." - [".$value["description"][0]."]";
455 }else{
456 $this->departments[$value['dn']]=$value["description"][0];
457 }
458 }
460 /* END NEW LIST MANAGMENT
461 */
462 $tmp=array();
463 foreach($this->blocklists as $tkey => $val ){
464 $tmp[strtolower($val['cn'][0]).$val['cn'][0]]=$val;
465 }
466 ksort($tmp);
467 $this->blocklists=array();
468 foreach($tmp as $val){
469 $this->blocklists[]=$val;
470 }
471 reset ($this->blocklists);
472 }
474 function remove_from_parent()
475 {
476 $ldap= $this->config->get_ldap_link();
477 $ldap->rmDir($this->dn);
478 $this->handle_post_events("remove");
479 }
482 /* Save data to object */
483 function save_object()
484 {
485 plugin::save_object();
487 /* Save type, needed to detect objectClass */
488 if (isset($_POST['type']) && chkacl($this->acl, "cn") == ""){
489 $this->type= $_POST['type'];
490 }
492 /* Save base, since this is no LDAP attribute */
493 if (isset($_POST['base']) && chkacl($this->acl, "create") == ""){
494 $this->base= $_POST['base'];
495 }
496 }
499 /* Check values */
500 function check()
501 {
502 $message= array();
504 /* Permissions for that base? */
505 if ($this->base != ""){
506 $new_dn= "cn=".$this->cn.",ou=gofax,ou=systems,".$this->base;
507 } else {
508 $new_dn= $this->dn;
509 }
511 $ui= get_userinfo();
512 $acl= get_permissions ($new_dn, $ui->subtreeACL);
513 $acl= get_module_permission($acl, "blocklist", $new_dn);
514 if (chkacl($acl, "create") != ""){
515 $message[]= _("You have no permissions to create a blocklist on this 'Base'.");
516 }
519 /* check syntax: must cn */
520 if ($this->cn == ""){
521 $message[]= _("Required field 'Name' is not set.");
522 } else {
523 if (!is_uid($this->cn)){
524 $message[]= _("Required field 'Name' contains invalid characters");
525 }
526 if ($this->dn == 'new'){
527 $ldap= $this->config->get_ldap_link();
528 $ldap->cd ("ou=gofax,ou=systems,".$this->config->current["BASE"]);
529 $ldap->search ("(&(|(objectClass=goFaxSBlock)(objectClass=goFaxRBlock))".
530 "(cn=".$this->cn."))", array("cn"));
531 if ($ldap->count() != 0){
532 $message[]= _("Specified name is already used.");
533 }
534 }
535 }
537 /* Is user allowed to create in 'base'? */
538 if (chkacl($this->acl, "create") != ""){
539 $message[]= _("No permission to create a blocklist on this base.");
540 }
542 return $message;
543 }
546 /* Save to LDAP */
547 function save()
548 {
549 plugin::save();
551 /* Type selection */
552 if ($this->type == 0){
553 $type= "goFaxSBlock";
554 } else {
555 $type= "goFaxRBlock";
556 }
558 /* Add list */
559 $this->attrs['objectClass']= $type;
560 if (count($this->goFaxBlocklist)){
561 $this->attrs["$type"."list"]= $this->goFaxBlocklist;
562 }
564 /* Write back to ldap */
565 $ldap= $this->config->get_ldap_link();
566 $ldap->cd($ldap->getParentDir($this->dn));
567 $ldap->search("cn=$this->cn");
568 $a= $ldap->fetch();
569 if (count($a)){
570 if (!isset($this->attrs["$type"."list"])){
571 $this->attrs["$type"."list"]= array();
572 }
573 $ldap->cd($this->dn);
574 $ldap->modify($this->attrs);
575 $this->handle_post_events("modify");
576 } else {
577 $ldap->cd($this->config->current['BASE']);
578 $ldap->create_missing_trees(preg_replace('/^[^,]+,/', '', $this->dn));
579 $ldap->cd($this->dn);
580 $ldap->add($this->attrs);
581 $this->handle_post_events("add");
582 }
583 show_ldap_error($ldap->get_error());
584 }
587 function addNumber($number)
588 {
589 if (!in_array($number, $this->goFaxBlocklist)){
590 $this->goFaxBlocklist[]= $number;
591 sort($this->goFaxBlocklist);
592 }
593 }
595 function delNumber($numbers)
596 {
597 $tmp= array();
598 foreach ($this->goFaxBlocklist as $val){
599 if (!in_array($val, $numbers)){
600 $tmp[]= $val;
601 }
602 }
603 $this->goFaxBlocklist= $tmp;
604 }
606 function remove_lock()
607 {
608 if (isset($this->dn)){
609 del_lock ($this->dn);
610 }
611 }
613 }
614 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
615 ?>