Code

Updated table summary
[gosa.git] / gosa-plugins / mail / admin / systems / services / spam / class_goSpamServer.inc
1 <?php
3 class gospamserver extends goService{
5   /* This plugin only writes its objectClass */
6   var $objectclasses    = array("goSpamServer");
7   var $attributes       = array("saRewriteHeader","saTrustedNetworks","saRequiredScore","saFlags","saRule");
8   var $StatusFlag       = "saStatus";
9  
10   /* This class can't be assigned twice so it conflicts with itsself */
11   var $conflicts        = array("goSpamServer");
12   var $Flags            = array("B","b","C","R","D","P");
14   var $DisplayName      = "";
15   var $dn               = NULL;
16   var $cn               = "";
17   var $saStatus         = "";
19   var $saRewriteHeader  = "";
20   var $saTrustedNetworks= array();
21   var $TrustedNetworks  = array();
22   var $saRequiredScore  = 0;
23   var $saFlags          = "";
24   var $Rules            = array();
25   var $saRule           = array();
27   var $saFlagsB         = false;
28   var $saFlagsb         = false;
29   var $saFlagsC         = false;
30   var $saFlagsR         = false;
31   var $saFlagsD         = false;
32   var $saFlagsP         = false;
33  
34   var $ui               = NULL;
35   var $acl              = NULL;
36   var $view_logged  =FALSE;
38   function gospamserver(&$config,$dn, $parent= NULL)
39   {
40     /* Init class */
41     goService::goService($config,$dn, $parent);
42     $this->DisplayName = _("Spamassassin");
44     /* Get userinfo & acls */
45     $this->ui = get_userinfo();
47     /* Get Flags */
48     foreach($this->Flags as $flag){
49       $var = "saFlags".$flag;
50       if(preg_match("/".$flag."/",$this->saFlags)){
51         $this->$var = TRUE;
52       }
53     }
54     
55     /* Get trusted networks */
56     $this->TrustedNetworks = array();
57     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
58       $var = $this->attrs['saTrustedNetworks'];
59       for($i = 0 ; $i < $var['count'] ; $i ++ ){
60         $var2 = $this->attrs['saTrustedNetworks'][$i];
61         $this->TrustedNetworks[ $var2 ] = $var2; 
62       }
63     }
65     /* Get rules */
66     $this->Rules = array();
67     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
68       $var = $this->attrs['saRule'];
69       for($i = 0 ; $i < $var['count'] ; $i ++ ){
70         $var2 = $this->attrs['saRule'][$i];
71         $name = preg_replace("/:.*$/","",$var2);
72         $value= base64_decode(preg_replace("/^.*:/","",$var2));
73         $this->Rules[ $name ] = $value;
74       }
75     }
76   }
79   function execute()
80   {
81     $display ="";
82     $smarty = get_smarty(); 
83     
84     if($this->is_account && !$this->view_logged){
85       $this->view_logged = TRUE;
86       new log("view","server/".get_class($this),$this->dn);
87     }
89     /* If displayed, it is ever true*/
90     $this->is_account =true;
92     /* Get acls */
93     $tmp = $this->plinfo();
94     foreach($tmp['plProvidedAcls'] as $name => $translation){
95       $smarty->assign($name."ACL",$this->getacl($name));
96     }
98     /* Add new trusted network */
99     if(isset($_POST['AddNewTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
100       $this->AddTrust($_POST['NewTrustName']);
101     }
102   
103     /* Delete selected trusted network */
104     if(isset($_POST['DelTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
105       $this->DelTrust($_POST['TrustedNetworks']);
106     }
108     /* Add a new rule */
109     if(isset($_POST['AddRule']) && $this->acl_is_writeable("saRule")){
110       $this->dialog = new goSpamServerRule($this->config,$this->dn);
111       $this->dialog->acl_base = $this->acl_base;
112       $this->dialog->acl_category = $this->acl_category;
113     }
114   
115     /* Cancel adding/editing specified rule */
116     if(isset($_POST['CancelRule'])){
117       $this->dialog = FALSE;
118     }
120     /* Handle post to delete rules */
121     $once = true;
122     foreach($_POST as $name => $value){
123       if(preg_match("/^editRule_/",$name) && $once && $this->acl_is_readable("saRule")){
124         $once = false;
125         $entry = preg_replace("/^editRule_/","",$name);
126         $rule = $this->Rules[$entry];
127         $name = $entry;
128         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
129         $this->dialog->acl_base = $this->acl_base;
130         $this->dialog->acl_category = $this->acl_category;
131       }
132       if(preg_match("/^delRule_/",$name) && $once && $this->acl_is_writeable("saRule")){
133         $once = false;
134         $entry = preg_replace("/^delRule_/","",$name);
135         unset($this->Rules[$entry]);
136       }
137     }
139     /* Save rules */
140     if(isset($_POST['SaveRule'])){
141       $this->dialog->save_object();
142       $msgs = $this->dialog->check();
143       if(count($msgs)){
144         foreach($msgs as $msg){
145           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
146         }
147       }elseif($this->acl_is_writeable("saRule")){
148         $ret = $this->dialog->save();
149         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
150           unset($this->Rules[$ret['orig_name']]);
151         }
152         $this->Rules[$ret['name']] = $ret['rule'];
153         $this->dialog = FALSE;
154       }
155     }
156    
157     /* Display dialog if available */ 
158     if($this->dialog && $this->dialog->config){
159       $this->dialog->save_object();
160       return($this->dialog->execute());
161     }
163     /* Assign smarty vars */
164     foreach($this->attributes as $attr){
165       $smarty->assign($attr,$this->$attr);
166     }
168     /* Assign checkbox states */
169     foreach($this->Flags as $Flag){
170       $var = "saFlags".$Flag;
171       $smarty->assign("saFlags".$Flag."ACL", $this->getacl($var));
172       if($this->$var){
173         $smarty->assign("saFlags".$Flag."CHK"," checked " );
174       }else{
175         $smarty->assign("saFlags".$Flag."CHK","");
176       }
177     }
179     /* Create divlist */
180     $DivRules = new divSelectBox("SpamRules");
181     $DivRules->SetHeight(130);
183     if(!$this->acl_is_writeable("saTrustedNetworks")){
184       $actions = "";
185     }else{
186       $actions = image('images/lists/edit.png', 'editRule_%s');
187       if($this->acl_is_writeable("saRule")){
188         $actions.= image('images/lists/trash.png','delRule_%s');
189       }
190     }
192     foreach($this->Rules as $key => $net){
193       $field1 = array("string" => $key );
194       $field2 = array("string" => preg_replace("/%s/",$key,$actions) , "attach" => "style='border-right:0px;width:36px;'");
195       $DivRules->AddEntry(array($field1,$field2));
196     }
197     $smarty->assign("divRules",$DivRules->DrawList()); 
198     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
200     /* Create Spam score select box entries */
201     $tmp = array();
202     for($i = 0 ; $i <= 20 ; $i ++ ){
203       $tmp[$i] = $i;
204     }
205     $smarty->assign("SpamScore",$tmp);
207     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
208   }
211   /* Add $post to list of configured trusted */
212   function AddTrust($post)
213   {
214     if(!empty($post)){
215       if(tests::is_ip($post) || tests::is_domain($post) || (tests::is_ip_with_subnetmask($post))){
216         $this->TrustedNetworks[$post] = $post;
217       }else{
218         msg_dialog::display(_("Error"), msgPool::invalid(_("Trusted network")), ERROR_DIALOG);
219       }
220     }
221   }
224   /* Delete trusted network */
225   function DelTrust($posts)
226   {
227     foreach($posts as $post){
228       if(isset($this->TrustedNetworks[$post])){
229         unset($this->TrustedNetworks[$post]);     
230       }
231     }
232   }
234   function save()
235   {
236     if(!$this->is_account) return;
237     plugin::save();
239     /* Create Flags */     
240     $this->attrs['saFlags'] = array();
241     foreach($this->Flags as $flag){
242       $var = "saFlags".$flag;
243       if($this->$var){
244         $this->attrs['saFlags'].=$flag;
245       }
246     }
248     /* Create trusted network entries */
249     $this->attrs['saTrustedNetworks'] = array();
250     foreach($this->TrustedNetworks as $net){
251       $this->attrs['saTrustedNetworks'][] = $net; 
252     }    
254     /* Rules */
255     $this->attrs['saRule'] = array();
256     foreach($this->Rules as $name => $rule){
257       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
258     }
260     /* Check if this is a new entry ... add/modify */
261     $ldap = $this->config->get_ldap_link();
262     $ldap->cat($this->dn,array("objectClass"));
263     if($ldap->count()){
264       $ldap->cd($this->dn);
265       $ldap->modify($this->attrs);
266     }else{
267       $ldap->cd($this->dn);
268       $ldap->add($this->attrs);
269     }
270     if($this->initially_was_account){
271       $this->handle_post_events("modify");
272       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
273     }else{
274       $this->handle_post_events("add");
275       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
276     }
278     if (!$ldap->success()){
279       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
280     }
281   }
283   function check()
284   { 
285     $message = plugin::check();
287     /* Check if required score is numeric */
288     if(!is_numeric($this->saRequiredScore)){
289       $message[] = msgPool::invalid(_("Score"),$this->saRequiredScore,"/[0-9]/");
290     }
292     return($message);
293   }
294   
296   function save_object()
297   {
298     if(isset($_POST['goSpamServer'])){
300       plugin::save_object();
302       /* Check flags */
303       foreach($this->Flags as $flag){
304         $var = "saFlags".$flag;
306         if($this->acl_is_writeable($var)){
307           if(isset($_POST[$var])){
308             $this->$var = TRUE;
309           }else{
310             $this->$var = FALSE;
311           }
312         }
313       }
314     }    
315   }  
317   
318   /* Return plugin informations for acl handling  */
319   static function plInfo()
320   {
321     return (array(
322           "plShortName"   => _("Spamassassin"),
323           "plDescription" => _("Spamassassin")." ("._("Services").")",
324           "plSelfModify"  => FALSE,
325           "plDepends"     => array(),
326           "plPriority"    => 89,
327           "plSection"     => array("administration"),
328           "plCategory"    => array("server"),
329           "plProvidedAcls"=> array(
331             "saRewriteHeader"   => _("Rewrite header"),
332             "saTrustedNetworks" => _("Trusted networks"),
333             "saRequiredScore"   => _("Required score"),
334             "saRule"            => _("Rules"),
337             "start"           => _("Start"),
338             "stop"            => _("Stop"),
339             "restart"         => _("Restart"),
341             "saFlagsB"           => _("Enable use of bayes filtering"),
342             "saFlagsb"           => _("Enabled bayes auto learning"),
343             "saFlagsC"           => _("Enable RBL checks"),
344             "saFlagsR"           => _("Enable use of Razor"),
345             "saFlagsD"           => _("Enable use of DDC"),
346             "saFlagsP"           => _("Enable use of Pyzor"))
347           ));
348   }
350   /* For newer service management dialogs */
351   function getListEntry()
352   {
353     $fields                 = goService::getListEntry();
354     $fields['Message']      = _("Spamassassin");
355     #$fields['AllowEdit']    = true;
356     return($fields);
357   }
359 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
360 ?>