Code

Fixed broken tags
[gosa.git] / plugins / admin / systems / class_goSpamServer.inc
1 <?php
3 class gospamserver extends plugin{
5   /* CLI vars */
6   var $cli_summary= "Manage server base objects";
7   var $cli_description= "Some longer text\nfor help";
8   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
10   /* This plugin only writes its objectClass */
11   var $objectclasses    = array("goSpamServer");
12   var $attributes       = array("saRewriteHeader","saTrustedNetworks","saRequiredScore","saFlags","saRule");
13   var $StatusFlag       = "saStatus";
14  
15   /* This class can't be assigned twice so it conflicts with itsself */
16   var $conflicts        = array("goSpamServer");
17   var $Flags            = array("B","b","C","R","D","P");
19   var $DisplayName      = "";
20   var $dn               = NULL;
21   var $cn               = "";
22   var $saStatus         = "";
24   var $saRewriteHeader  = "";
25   var $saTrustedNetworks= array();
26   var $TrustedNetworks  = array();
27   var $saRequiredScore  = 0;
28   var $saFlags          = "";
29   var $Rules            = array();
30   var $saRule           = array();
32   var $saFlagsB         = false;
33   var $saFlagsb         = false;
34   var $saFlagsC         = false;
35   var $saFlagsR         = false;
36   var $saFlagsD         = false;
37   var $saFlagsP         = false;
38  
39   var $dialog           = NULL;
40   var $ui               = NULL;
41   var $acl              = NULL;
43   function gospamserver($config,$dn, $parent= NULL)
44   {
45     /* Init class */
46     plugin::plugin($config,$dn, $parent);
47     $this->DisplayName = _("Spamassassin");
49     /* Get userinfo & acls */
50     $this->ui = get_userinfo();
52     /* Set up the users ACL's for this 'dn' */
53     $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
54     $this->acl= get_module_permission($acl, "goSpamServer", $this->ui->dn);
56     /* Get Flags */
57     foreach($this->Flags as $flag){
58       $var = "saFlags".$flag;
59       if(preg_match("/".$flag."/",$this->saFlags)){
60         $this->$var = TRUE;
61       }
62     }
63     
64     /* Get trusted networks */
65     $this->TrustedNetworks = array();
66     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
67       $var = $this->attrs['saTrustedNetworks'];
68       for($i = 0 ; $i < $var['count'] ; $i ++ ){
69         $var2 = $this->attrs['saTrustedNetworks'][$i];
70         $this->TrustedNetworks[ $var2 ] = $var2; 
71       }
72     }
74     /* Get rules */
75     $this->Rules = array();
76     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
77       $var = $this->attrs['saRule'];
78       for($i = 0 ; $i < $var['count'] ; $i ++ ){
79         $var2 = $this->attrs['saRule'][$i];
80         $name = preg_replace("/:.*$/","",$var2);
81         $value= base64_decode(preg_replace("/^.*:/","",$var2));
82         $this->Rules[ $name ] = $value;
83       }
84     }
85   }
88   function execute()
89   {
90     $smarty = get_smarty(); 
91     if(get_class($this->parent) == "servtabs"){
93       $smarty->assign("servtabs",true);
94       /* Do we need to flip is_account state? */
95       if (isset($_POST['modify_state'])) {
96         $this->is_account = !$this->is_account;
97       }
99       /* Show tab dialog headers */
100       if ($this->is_account) {
101         /* call Add Acoount to add account */
102         $display = $this->show_header(_("Remove spamassassin extension"), 
103             _("This server has spamassassin features enabled. You can disable them by clicking below."));
104       } else {
105         /* call remove Account */
106         $display = $this->show_header(_("Add spamassassin service"), 
107             _("This server has spamassassin features disabled. You can enable them by clicking below."));
108         return ($display);
109       }
110     }else{
111       $this->is_account =true;
112       $display ="";
113       $smarty->assign("servtabs",false);
114     }
115   
116     /* Add new trusted network */
117     if(isset($_POST['AddNewTrust'])){
118       $this->AddTrust($_POST['NewTrustName']);
119     }
120   
121     /* Delete selected trusted network */
122     if(isset($_POST['DelTrust'])){
123       $this->DelTrust($_POST['TrustedNetworks']);
124     }
126     /* Add a new rule */
127     if(isset($_POST['AddRule'])){
128       $this->dialog = new goSpamServerRule($this->config,$this->dn);
129     }
130   
131     /* Cancel adding/editing specified rule */
132     if(isset($_POST['CancelRule'])){
133       $this->dialog = NULL;
134     }
136     /* Handle post to delete rules */
137     $once = true;
138     foreach($_POST as $name => $value){
139       if(preg_match("/^editRule_/",$name) && $once ){
140         $once = false;
141         $entry = preg_replace("/^editRule_/","",$name);
142         $entry = preg_replace("/_(x|y)$/","",$entry);
143         $rule = $this->Rules[$entry];
144         $name = $entry;
145         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
146       }
147       if(preg_match("/^delRule_/",$name) && $once ){
148         $once = false;
149         $entry = preg_replace("/^delRule_/","",$name);
150         $entry = preg_replace("/_(x|y)$/","",$entry);
151         unset($this->Rules[$entry]);
152       }
153     }
155     /* Save rules */
156     if(isset($_POST['SaveRule'])){
157       $this->dialog->save_object();
158       $msgs = $this->dialog->check();
159       if(count($msgs)){
160         foreach($msgs as $msg){
161           print_red($msg);
162         }
163       }else{
164         $ret = $this->dialog->save();
165         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
166           unset($this->Rules[$ret['orig_name']]);
167         }
168         $this->Rules[$ret['name']] = $ret['rule'];
169         $this->dialog = NULL;
170       }
171     }
172    
173     /* Display dialog if available */ 
174     if($this->dialog && $this->dialog->config){
175       $this->dialog->save_object();
176       return($this->dialog->execute());
177     }
179     /* Assign smarty vars */
180     foreach($this->attributes as $attr){
181       $smarty->assign($attr,$this->$attr);
182       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
183     }
185     /* Assign checkbox states */
186     foreach($this->Flags as $Flag){
187       $var = "saFlags".$Flag;
188       $smarty->assign("saFlags".$Flag."ACL",chkacl($this->acl,$Flag));
189       if($this->$var){
190         $smarty->assign("saFlags".$Flag."CHK"," checked " );
191       }else{
192         $smarty->assign("saFlags".$Flag."CHK","");
193       }
194     }
196     /* Create divlist */
197     $DivRules = new divSelectBox("SpamRules");
198     $DivRules->SetHeight(130);
200     if(preg_match("/disabled/",chkacl($this->acl,"saTrustedNetworks"))){
201       $actions = "";
202     }else{
203       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
204       $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
205     }
207     foreach($this->Rules as $key => $net){
208       $field1 = array("string" => $key );
209       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
210       $DivRules->AddEntry(array($field1,$field2));
211     }
212     $smarty->assign("divRules",$DivRules->DrawList()); 
213     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
215     /* Create Spam score select box entries */
216     $tmp = array();
217     for($i = 0 ; $i <= 20 ; $i ++ ){
218       $tmp[$i] = $i;
219     }
220     $smarty->assign("SpamScore",$tmp);
222     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
223   }
226   /* Add $post to list of configured trusted */
227   function AddTrust($post)
228   {
229     if(!empty($post)){
230       if(is_ip($post) || is_domain($post) || (is_ip_with_subnetmask($post))){
231         $this->TrustedNetworks[$post] = $post;
232       }else{
233         print_red(_("Specified value is not a valid 'trusted network' value."));
234       }
235     }
236   }
239   /* Delete trusted network */
240   function DelTrust($posts)
241   {
242     foreach($posts as $post){
243       if(isset($this->TrustedNetworks[$post])){
244         unset($this->TrustedNetworks[$post]);     
245       }
246     }
247   }
250   /* remove this extension */
251   function remove_from_parent()
252   {
254     if(!$this->is_account && $this->initially_was_account){
256       plugin::remove_from_parent();
258       /* Remove status flag, it is not a memeber of
259          this->attributes, so ensure that it is deleted too */
260       if(!empty($this->StatusFlag)){
261         $this->attrs[$this->StatusFlag] = array();
262       }
264       /* Check if this is a new entry ... add/modify */
265       $ldap = $this->config->get_ldap_link();
266       $ldap->cat($this->dn,array("objectClass"));
267       if($ldap->count()){
268         $ldap->cd($this->dn);
269         $ldap->modify($this->attrs);
270       }else{
271         $ldap->cd($this->dn);
272         $ldap->add($this->attrs);
273       }
274       show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
275       $this->handle_post_events("remove");
276     }
277   }
280   function save()
281   {
282     if(!$this->is_account) return;
283     plugin::save();
285     /* Create Flags */     
286     $this->attrs['saFlags'] = array();
287     foreach($this->Flags as $flag){
288       $var = "saFlags".$flag;
289       if($this->$var){
290         $this->attrs['saFlags'].=$flag;
291       }
292     }
294     /* Create trusted network entries */
295     $this->attrs['saTrustedNetworks'] = array();
296     foreach($this->TrustedNetworks as $net){
297       $this->attrs['saTrustedNetworks'][] = $net; 
298     }    
300     /* Rules */
301     $this->attrs['saRule'] = array();
302     foreach($this->Rules as $name => $rule){
303       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
304     }
306     /* Check if this is a new entry ... add/modify */
307     $ldap = $this->config->get_ldap_link();
308     $ldap->cat($this->dn,array("objectClass"));
309     if($ldap->count()){
310       $ldap->cd($this->dn);
311       $ldap->modify($this->attrs);
312     }else{
313       $ldap->cd($this->dn);
314       $ldap->add($this->attrs);
315     }
316     if($this->initially_was_account){
317       $this->handle_post_events("modify");
318     }else{
319       $this->handle_post_events("add");
320     }
322     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
323   }
325   function check()
326   { 
327     $message = plugin::check();
329     /* Check if required score is numeric */
330     if(!is_numeric($this->saRequiredScore)){
331       $message[] = _("Required score must be a numeric value.");
332     }
334     return($message);
335   }
336   
338   function save_object()
339   {
340     if(isset($_POST['goSpamServer'])){
342       plugin::save_object();
344       /* Check flags */
345       foreach($this->Flags as $flag){
346         $var = "saFlags".$flag;
347         if(isset($_POST[$var])){
348           $this->$var = TRUE;
349         }else{
350           $this->$var = FALSE;
351         }
352       }
353     }    
354   }  
356   
357   /* Return plugin informations for acl handling 
358   function plInfo()
359   {
360     return (array(
361           "plShortName"   => _("Spamassassin"),
362           "plDescription" => _("Spamassassin service"),
363           "plSelfModify"  => FALSE,
364           "plDepends"     => array(),
365           "plPriority"    => 0,
366           "plSection"     => array("administration"),
367           "plCategory"    => array("server"),
368           "plProvidedAcls"=> array(
370             "saRewriteHeader"   => _("Rewrite header"),
371             "saTrustedNetworks" => _("Trusted networks"),
372             "saRequiredScore"   => _("Required score"),
373             "saRule"            => _("Rules"),
375             "saFlagB"           => _("Enable use of bayes filtering"),
376             "saFlagb"           => _("Enabled bayes auto learning"),
377             "saFlagC"           => _("Enable RBL checks"),
378             "saFlagR"           => _("Enable use of Razor"),
379             "saFlagD"           => _("Enable use of DDC"),
380             "saFlagP"           => _("Enable use of Pyzor"))
381           ));
382   }
383   */
385   /* For newer service management dialogs */
386   function getListEntry()
387   {
388     $this->updateStatusState();
389     $flag                   = $this->StatusFlag;
390     $fields['Status']       = $this->$flag;
391     $fields['Message']      = _("Spamassassin");
392     $fields['AllowStart']   = true;
393     $fields['AllowStop']    = true;
394     $fields['AllowRestart'] = true;
395     $fields['AllowRemove']  = true;
396     $fields['AllowEdit']    = true;
397     return($fields);
398   }
400   function updateStatusState()
401   {
402     if(empty($this->StatusFlag)) return;
404     $attrs = array();
405     $flag = $this->StatusFlag;
406     $ldap = $this->config->get_ldap_link();
407     $ldap->cd($this->cn);
408     $ldap->cat($this->dn,array($flag));
409     if($ldap->count()){
410       $attrs = $ldap->fetch();
411     }
412     if(isset($attrs[$flag][0])){
413       $this->$flag = $attrs[$flag][0];
414     }
415   }
416   function action_hook($add_attrs= array())
417   {
418     /* Find postcreate entries for this class */
419     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
420     if ($command == "" && isset($this->config->data['TABS'])){
421       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
422     }
423     if ($command != ""){
424       /* Walk through attribute list */
425       foreach ($this->attributes as $attr){
426         if (!is_array($this->$attr)){
427           $command= preg_replace("/%$attr/", $this->$attr, $command);
428         }
429       }
430       $command= preg_replace("/%dn/", $this->dn, $command);
431       /* Additional attributes */
432       foreach ($add_attrs as $name => $value){
433         $command= preg_replace("/%$name/", $value, $command);
434       }
436       /* If there are still some %.. in our command, try to fill these with some other class vars */
437       if(preg_match("/%/",$command)){
438         $attrs = get_object_vars($this);
439         foreach($attrs as $name => $value){
440           if(!is_string($value)) continue;
441           $command= preg_replace("/%$name/", $value, $command);
442         }
443       }
445       if (check_command($command)){
446         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
447             $command, "Execute");
449         exec($command);
450       } else {
451         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
452         print_red ($message);
453       }
454     }
455   }
457   /* Directly save new status flag */
458   function setStatus($value)
459   {
460     if($value == "none") return;
461     if(!$this->initially_was_account) return;
462     $ldap = $this->config->get_ldap_link();
463     $ldap->cd($this->dn);
464     $ldap->cat($this->dn,array("objectClass"));
465     if($ldap->count()){
467       $tmp = $ldap->fetch();
468       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
469         $attrs['objectClass'][] = $tmp['objectClass'][$i];
470       }
471       $flag = $this->StatusFlag;
472       $attrs[$flag] = $value;
473       $this->$flag = $value;
474       $ldap->modify($attrs);
475       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
476       $this->action_hook();
477     }
478   }
482 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
483 ?>