Code

Udpated gotomasses
[gosa.git] / plugins / admin / systems / class_goSpamServer.inc
1 <?php
3 class gospamserver extends goService{
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 $ui               = NULL;
40   var $acl              = NULL;
41   var $view_logged  =FALSE;
43   function gospamserver(&$config,$dn, $parent= NULL)
44   {
45     /* Init class */
46     goService::goService($config,$dn, $parent);
47     $this->DisplayName = _("Spamassassin");
49     /* Get userinfo & acls */
50     $this->ui = get_userinfo();
52     /* Get Flags */
53     foreach($this->Flags as $flag){
54       $var = "saFlags".$flag;
55       if(preg_match("/".$flag."/",$this->saFlags)){
56         $this->$var = TRUE;
57       }
58     }
59     
60     /* Get trusted networks */
61     $this->TrustedNetworks = array();
62     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
63       $var = $this->attrs['saTrustedNetworks'];
64       for($i = 0 ; $i < $var['count'] ; $i ++ ){
65         $var2 = $this->attrs['saTrustedNetworks'][$i];
66         $this->TrustedNetworks[ $var2 ] = $var2; 
67       }
68     }
70     /* Get rules */
71     $this->Rules = array();
72     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
73       $var = $this->attrs['saRule'];
74       for($i = 0 ; $i < $var['count'] ; $i ++ ){
75         $var2 = $this->attrs['saRule'][$i];
76         $name = preg_replace("/:.*$/","",$var2);
77         $value= base64_decode(preg_replace("/^.*:/","",$var2));
78         $this->Rules[ $name ] = $value;
79       }
80     }
81   }
84   function execute()
85   {
86     $display ="";
87     $smarty = get_smarty(); 
88     
89     if($this->is_account && !$this->view_logged){
90       $this->view_logged = TRUE;
91       new log("view","server/".get_class($this),$this->dn);
92     }
94     /* If displayed, it is ever true*/
95     $this->is_account =true;
97     /* Get acls */
98     $tmp = $this->plinfo();
99     foreach($tmp['plProvidedAcls'] as $name => $translation){
100       $smarty->assign($name."ACL",$this->getacl($name));
101     }
103     /* Add new trusted network */
104     if(isset($_POST['AddNewTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
105       $this->AddTrust($_POST['NewTrustName']);
106     }
107   
108     /* Delete selected trusted network */
109     if(isset($_POST['DelTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
110       $this->DelTrust($_POST['TrustedNetworks']);
111     }
113     /* Add a new rule */
114     if(isset($_POST['AddRule']) && $this->acl_is_writeable("saRule")){
115       $this->dialog = new goSpamServerRule($this->config,$this->dn);
116     }
117   
118     /* Cancel adding/editing specified rule */
119     if(isset($_POST['CancelRule'])){
120       $this->dialog = FALSE;
121     }
123     /* Handle post to delete rules */
124     $once = true;
125     foreach($_POST as $name => $value){
126       if(preg_match("/^editRule_/",$name) && $once && $this->acl_is_readable("saRule")){
127         $once = false;
128         $entry = preg_replace("/^editRule_/","",$name);
129         $entry = preg_replace("/_(x|y)$/","",$entry);
130         $rule = $this->Rules[$entry];
131         $name = $entry;
132         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
133       }
134       if(preg_match("/^delRule_/",$name) && $once && $this->acl_is_writeable("saRule")){
135         $once = false;
136         $entry = preg_replace("/^delRule_/","",$name);
137         $entry = preg_replace("/_(x|y)$/","",$entry);
138         unset($this->Rules[$entry]);
139       }
140     }
142     /* Save rules */
143     if(isset($_POST['SaveRule'])){
144       $this->dialog->save_object();
145       $msgs = $this->dialog->check();
146       if(count($msgs)){
147         foreach($msgs as $msg){
148           print_red($msg);
149         }
150       }elseif($this->acl_is_writeable("saRule")){
151         $ret = $this->dialog->save();
152         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
153           unset($this->Rules[$ret['orig_name']]);
154         }
155         $this->Rules[$ret['name']] = $ret['rule'];
156         $this->dialog = FALSE;
157       }
158     }
159    
160     /* Display dialog if available */ 
161     if($this->dialog && $this->dialog->config){
162       $this->dialog->save_object();
163       return($this->dialog->execute());
164     }
166     /* Assign smarty vars */
167     foreach($this->attributes as $attr){
168       $smarty->assign($attr,$this->$attr);
169     }
171     /* Assign checkbox states */
172     foreach($this->Flags as $Flag){
173       $var = "saFlags".$Flag;
174       $smarty->assign("saFlags".$Flag."ACL", $this->getacl($Flag));
175       if($this->$var){
176         $smarty->assign("saFlags".$Flag."CHK"," checked " );
177       }else{
178         $smarty->assign("saFlags".$Flag."CHK","");
179       }
180     }
182     /* Create divlist */
183     $DivRules = new divSelectBox("SpamRules");
184     $DivRules->SetHeight(130);
186     if($this->acl_is_writeable("saTrustedNetworks")){
187       $actions = "";
188     }else{
189     
190       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
191       if($this->acl_is_writeable("saRule")){
192         $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
193       }
194     }
196     foreach($this->Rules as $key => $net){
197       $field1 = array("string" => $key );
198       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
199       $DivRules->AddEntry(array($field1,$field2));
200     }
201     $smarty->assign("divRules",$DivRules->DrawList()); 
202     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
204     /* Create Spam score select box entries */
205     $tmp = array();
206     for($i = 0 ; $i <= 20 ; $i ++ ){
207       $tmp[$i] = $i;
208     }
209     $smarty->assign("SpamScore",$tmp);
211     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
212   }
215   /* Add $post to list of configured trusted */
216   function AddTrust($post)
217   {
218     if(!empty($post)){
219       if(is_ip($post) || is_domain($post) || (is_ip_with_subnetmask($post))){
220         $this->TrustedNetworks[$post] = $post;
221       }else{
222         print_red(_("Specified value is not a valid 'trusted network' value."));
223       }
224     }
225   }
228   /* Delete trusted network */
229   function DelTrust($posts)
230   {
231     foreach($posts as $post){
232       if(isset($this->TrustedNetworks[$post])){
233         unset($this->TrustedNetworks[$post]);     
234       }
235     }
236   }
238   function save()
239   {
240     if(!$this->is_account) return;
241     plugin::save();
243     /* Create Flags */     
244     $this->attrs['saFlags'] = array();
245     foreach($this->Flags as $flag){
246       $var = "saFlags".$flag;
247       if($this->$var){
248         $this->attrs['saFlags'].=$flag;
249       }
250     }
252     /* Create trusted network entries */
253     $this->attrs['saTrustedNetworks'] = array();
254     foreach($this->TrustedNetworks as $net){
255       $this->attrs['saTrustedNetworks'][] = $net; 
256     }    
258     /* Rules */
259     $this->attrs['saRule'] = array();
260     foreach($this->Rules as $name => $rule){
261       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
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     if($this->initially_was_account){
275       $this->handle_post_events("modify");
276       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
277     }else{
278       $this->handle_post_events("add");
279       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
280     }
282     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
283   }
285   function check()
286   { 
287     $message = plugin::check();
289     /* Check if required score is numeric */
290     if(!is_numeric($this->saRequiredScore)){
291       $message[] = _("Required score must be a numeric value.");
292     }
294     return($message);
295   }
296   
298   function save_object()
299   {
300     if(isset($_POST['goSpamServer'])){
302       plugin::save_object();
304       /* Check flags */
305       foreach($this->Flags as $flag){
306         $var = "saFlags".$flag;
308         if($this->acl_is_writeable($var)){
309           if(isset($_POST[$var])){
310             $this->$var = TRUE;
311           }else{
312             $this->$var = FALSE;
313           }
314         }
315       }
316     }    
317   }  
319   
320   /* Return plugin informations for acl handling  */
321   function plInfo()
322   {
323     return (array(
324           "plShortName"   => _("Spamassassin"),
325           "plDescription" => _("Spamassassin")." ("._("Services").")",
326           "plSelfModify"  => FALSE,
327           "plDepends"     => array(),
328           "plPriority"    => 89,
329           "plSection"     => array("administration"),
330           "plCategory"    => array("server"),
331           "plProvidedAcls"=> array(
333             "saRewriteHeader"   => _("Rewrite header"),
334             "saTrustedNetworks" => _("Trusted networks"),
335             "saRequiredScore"   => _("Required score"),
336             "saRule"            => _("Rules"),
338             "saFlagB"           => _("Enable use of bayes filtering"),
339             "saFlagb"           => _("Enabled bayes auto learning"),
340             "saFlagC"           => _("Enable RBL checks"),
341             "saFlagR"           => _("Enable use of Razor"),
342             "saFlagD"           => _("Enable use of DDC"),
343             "saFlagP"           => _("Enable use of Pyzor"))
344           ));
345   }
347   /* For newer service management dialogs */
348   function getListEntry()
349   {
350     $fields                 = goService::getListEntry();
351     $fields['Message']      = _("Spamassassin");
352     $fields['AllowEdit']    = true;
353     return($fields);
354   }
356 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
357 ?>