Code

Moved folder image
[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     }
112   
113     /* Cancel adding/editing specified rule */
114     if(isset($_POST['CancelRule'])){
115       $this->dialog = FALSE;
116     }
118     /* Handle post to delete rules */
119     $once = true;
120     foreach($_POST as $name => $value){
121       if(preg_match("/^editRule_/",$name) && $once && $this->acl_is_readable("saRule")){
122         $once = false;
123         $entry = preg_replace("/^editRule_/","",$name);
124         $entry = preg_replace("/_(x|y)$/","",$entry);
125         $rule = $this->Rules[$entry];
126         $name = $entry;
127         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
128       }
129       if(preg_match("/^delRule_/",$name) && $once && $this->acl_is_writeable("saRule")){
130         $once = false;
131         $entry = preg_replace("/^delRule_/","",$name);
132         $entry = preg_replace("/_(x|y)$/","",$entry);
133         unset($this->Rules[$entry]);
134       }
135     }
137     /* Save rules */
138     if(isset($_POST['SaveRule'])){
139       $this->dialog->save_object();
140       $msgs = $this->dialog->check();
141       if(count($msgs)){
142         foreach($msgs as $msg){
143           msg_dialog::display(_("Error"), $msg, ERROR_DIALOG);
144         }
145       }elseif($this->acl_is_writeable("saRule")){
146         $ret = $this->dialog->save();
147         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
148           unset($this->Rules[$ret['orig_name']]);
149         }
150         $this->Rules[$ret['name']] = $ret['rule'];
151         $this->dialog = FALSE;
152       }
153     }
154    
155     /* Display dialog if available */ 
156     if($this->dialog && $this->dialog->config){
157       $this->dialog->save_object();
158       return($this->dialog->execute());
159     }
161     /* Assign smarty vars */
162     foreach($this->attributes as $attr){
163       $smarty->assign($attr,$this->$attr);
164     }
166     /* Assign checkbox states */
167     foreach($this->Flags as $Flag){
168       $var = "saFlags".$Flag;
169       $smarty->assign("saFlags".$Flag."ACL", $this->getacl($Flag));
170       if($this->$var){
171         $smarty->assign("saFlags".$Flag."CHK"," checked " );
172       }else{
173         $smarty->assign("saFlags".$Flag."CHK","");
174       }
175     }
177     /* Create divlist */
178     $DivRules = new divSelectBox("SpamRules");
179     $DivRules->SetHeight(130);
181     if($this->acl_is_writeable("saTrustedNetworks")){
182       $actions = "";
183     }else{
184     
185       $actions = "<input type='image' src='images/lists/edit.png'      name='editRule_%s'>";
186       if($this->acl_is_writeable("saRule")){
187         $actions.= "<input type='image' src='images/lists/trash.png' name='delRule_%s'>";
188       }
189     }
191     foreach($this->Rules as $key => $net){
192       $field1 = array("string" => $key );
193       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
194       $DivRules->AddEntry(array($field1,$field2));
195     }
196     $smarty->assign("divRules",$DivRules->DrawList()); 
197     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
199     /* Create Spam score select box entries */
200     $tmp = array();
201     for($i = 0 ; $i <= 20 ; $i ++ ){
202       $tmp[$i] = $i;
203     }
204     $smarty->assign("SpamScore",$tmp);
206     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
207   }
210   /* Add $post to list of configured trusted */
211   function AddTrust($post)
212   {
213     if(!empty($post)){
214       if(tests::is_ip($post) || tests::is_domain($post) || (tests::is_ip_with_subnetmask($post))){
215         $this->TrustedNetworks[$post] = $post;
216       }else{
217         msg_dialog::display(_("Error"), msgPool::invalid(_("Trusted network")), ERROR_DIALOG);
218       }
219     }
220   }
223   /* Delete trusted network */
224   function DelTrust($posts)
225   {
226     foreach($posts as $post){
227       if(isset($this->TrustedNetworks[$post])){
228         unset($this->TrustedNetworks[$post]);     
229       }
230     }
231   }
233   function save()
234   {
235     if(!$this->is_account) return;
236     plugin::save();
238     /* Create Flags */     
239     $this->attrs['saFlags'] = array();
240     foreach($this->Flags as $flag){
241       $var = "saFlags".$flag;
242       if($this->$var){
243         $this->attrs['saFlags'].=$flag;
244       }
245     }
247     /* Create trusted network entries */
248     $this->attrs['saTrustedNetworks'] = array();
249     foreach($this->TrustedNetworks as $net){
250       $this->attrs['saTrustedNetworks'][] = $net; 
251     }    
253     /* Rules */
254     $this->attrs['saRule'] = array();
255     foreach($this->Rules as $name => $rule){
256       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
257     }
259     /* Check if this is a new entry ... add/modify */
260     $ldap = $this->config->get_ldap_link();
261     $ldap->cat($this->dn,array("objectClass"));
262     if($ldap->count()){
263       $ldap->cd($this->dn);
264       $ldap->modify($this->attrs);
265     }else{
266       $ldap->cd($this->dn);
267       $ldap->add($this->attrs);
268     }
269     if($this->initially_was_account){
270       $this->handle_post_events("modify");
271       new log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
272     }else{
273       $this->handle_post_events("add");
274       new log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
275     }
277     if (!$ldap->success()){
278       msg_dialog::display(_("LDAP error"), msgPool::ldaperror($ldap->get_error(), $this->dn, 0, get_class()));
279     }
280   }
282   function check()
283   { 
284     $message = plugin::check();
286     /* Check if required score is numeric */
287     if(!is_numeric($this->saRequiredScore)){
288       $message[] = msgPool::invalid(_("Score"),$this->saRequiredScore,"/[0-9]/");
289     }
291     return($message);
292   }
293   
295   function save_object()
296   {
297     if(isset($_POST['goSpamServer'])){
299       plugin::save_object();
301       /* Check flags */
302       foreach($this->Flags as $flag){
303         $var = "saFlags".$flag;
305         if($this->acl_is_writeable($var)){
306           if(isset($_POST[$var])){
307             $this->$var = TRUE;
308           }else{
309             $this->$var = FALSE;
310           }
311         }
312       }
313     }    
314   }  
316   
317   /* Return plugin informations for acl handling  */
318   static function plInfo()
319   {
320     return (array(
321           "plShortName"   => _("Spamassassin"),
322           "plDescription" => _("Spamassassin")." ("._("Services").")",
323           "plSelfModify"  => FALSE,
324           "plDepends"     => array(),
325           "plPriority"    => 89,
326           "plSection"     => array("administration"),
327           "plCategory"    => array("server"),
328           "plProvidedAcls"=> array(
330             "saRewriteHeader"   => _("Rewrite header"),
331             "saTrustedNetworks" => _("Trusted networks"),
332             "saRequiredScore"   => _("Required score"),
333             "saRule"            => _("Rules"),
335             "saFlagB"           => _("Enable use of bayes filtering"),
336             "saFlagb"           => _("Enabled bayes auto learning"),
337             "saFlagC"           => _("Enable RBL checks"),
338             "saFlagR"           => _("Enable use of Razor"),
339             "saFlagD"           => _("Enable use of DDC"),
340             "saFlagP"           => _("Enable use of Pyzor"))
341           ));
342   }
344   /* For newer service management dialogs */
345   function getListEntry()
346   {
347     $fields                 = goService::getListEntry();
348     $fields['Message']      = _("Spamassassin");
349     $fields['AllowEdit']    = true;
350     return($fields);
351   }
353 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
354 ?>