Code

Fixed logging
[gosa.git] / plugins / admin / systems / class_goSpamServer.inc
1 <?php
3 require_once("class_goService.inc");
5 class gospamserver extends goService{
7   /* CLI vars */
8   var $cli_summary= "Manage server base objects";
9   var $cli_description= "Some longer text\nfor help";
10   var $cli_parameters= array("eins" => "Eins ist toll", "zwei" => "Zwei ist noch besser");
12   /* This plugin only writes its objectClass */
13   var $objectclasses    = array("goSpamServer");
14   var $attributes       = array("saRewriteHeader","saTrustedNetworks","saRequiredScore","saFlags","saRule");
15   var $StatusFlag       = "saStatus";
16  
17   /* This class can't be assigned twice so it conflicts with itsself */
18   var $conflicts        = array("goSpamServer");
19   var $Flags            = array("B","b","C","R","D","P");
21   var $DisplayName      = "";
22   var $dn               = NULL;
23   var $cn               = "";
24   var $saStatus         = "";
26   var $saRewriteHeader  = "";
27   var $saTrustedNetworks= array();
28   var $TrustedNetworks  = array();
29   var $saRequiredScore  = 0;
30   var $saFlags          = "";
31   var $Rules            = array();
32   var $saRule           = array();
34   var $saFlagsB         = false;
35   var $saFlagsb         = false;
36   var $saFlagsC         = false;
37   var $saFlagsR         = false;
38   var $saFlagsD         = false;
39   var $saFlagsP         = false;
40  
41   var $dialog           = NULL;
42   var $ui               = NULL;
43   var $acl              = NULL;
44   var $view_logged  =FALSE;
46   function gospamserver($config,$dn, $parent= NULL)
47   {
48     /* Init class */
49     goService::goService($config,$dn, $parent);
50     $this->DisplayName = _("Spamassassin");
52     /* Get userinfo & acls */
53     $this->ui = get_userinfo();
55     /* Get Flags */
56     foreach($this->Flags as $flag){
57       $var = "saFlags".$flag;
58       if(preg_match("/".$flag."/",$this->saFlags)){
59         $this->$var = TRUE;
60       }
61     }
62     
63     /* Get trusted networks */
64     $this->TrustedNetworks = array();
65     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
66       $var = $this->attrs['saTrustedNetworks'];
67       for($i = 0 ; $i < $var['count'] ; $i ++ ){
68         $var2 = $this->attrs['saTrustedNetworks'][$i];
69         $this->TrustedNetworks[ $var2 ] = $var2; 
70       }
71     }
73     /* Get rules */
74     $this->Rules = array();
75     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
76       $var = $this->attrs['saRule'];
77       for($i = 0 ; $i < $var['count'] ; $i ++ ){
78         $var2 = $this->attrs['saRule'][$i];
79         $name = preg_replace("/:.*$/","",$var2);
80         $value= base64_decode(preg_replace("/^.*:/","",$var2));
81         $this->Rules[ $name ] = $value;
82       }
83     }
84   }
87   function execute()
88   {
89     $display ="";
90     $smarty = get_smarty(); 
91     
92     if($this->is_account && !$this->view_logged){
93       $this->view_logged = TRUE;
94       @log::log("view","server/".get_class($this),$this->dn);
95     }
97     /* If displayed, it is ever true*/
98     $this->is_account =true;
100     /* Get acls */
101     $tmp = $this->plinfo();
102     foreach($tmp['plProvidedAcls'] as $name => $translation){
103       $smarty->assign($name."ACL",$this->getacl($name));
104     }
106     /* Add new trusted network */
107     if(isset($_POST['AddNewTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
108       $this->AddTrust($_POST['NewTrustName']);
109     }
110   
111     /* Delete selected trusted network */
112     if(isset($_POST['DelTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
113       $this->DelTrust($_POST['TrustedNetworks']);
114     }
116     /* Add a new rule */
117     if(isset($_POST['AddRule']) && $this->acl_is_writeable("saRule")){
118       $this->dialog = new goSpamServerRule($this->config,$this->dn);
119     }
120   
121     /* Cancel adding/editing specified rule */
122     if(isset($_POST['CancelRule'])){
123       $this->dialog = NULL;
124     }
126     /* Handle post to delete rules */
127     $once = true;
128     foreach($_POST as $name => $value){
129       if(preg_match("/^editRule_/",$name) && $once && $this->acl_is_readable("saRule")){
130         $once = false;
131         $entry = preg_replace("/^editRule_/","",$name);
132         $entry = preg_replace("/_(x|y)$/","",$entry);
133         $rule = $this->Rules[$entry];
134         $name = $entry;
135         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
136       }
137       if(preg_match("/^delRule_/",$name) && $once && $this->acl_is_writeable("saRule")){
138         $once = false;
139         $entry = preg_replace("/^delRule_/","",$name);
140         $entry = preg_replace("/_(x|y)$/","",$entry);
141         unset($this->Rules[$entry]);
142       }
143     }
145     /* Save rules */
146     if(isset($_POST['SaveRule'])){
147       $this->dialog->save_object();
148       $msgs = $this->dialog->check();
149       if(count($msgs)){
150         foreach($msgs as $msg){
151           print_red($msg);
152         }
153       }elseif($this->acl_is_writeable("saRule")){
154         $ret = $this->dialog->save();
155         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
156           unset($this->Rules[$ret['orig_name']]);
157         }
158         $this->Rules[$ret['name']] = $ret['rule'];
159         $this->dialog = NULL;
160       }
161     }
162    
163     /* Display dialog if available */ 
164     if($this->dialog && $this->dialog->config){
165       $this->dialog->save_object();
166       return($this->dialog->execute());
167     }
169     /* Assign smarty vars */
170     foreach($this->attributes as $attr){
171       $smarty->assign($attr,$this->$attr);
172     }
174     /* Assign checkbox states */
175     foreach($this->Flags as $Flag){
176       $var = "saFlags".$Flag;
177       $smarty->assign("saFlags".$Flag."ACL", $this->getacl($Flag));
178       if($this->$var){
179         $smarty->assign("saFlags".$Flag."CHK"," checked " );
180       }else{
181         $smarty->assign("saFlags".$Flag."CHK","");
182       }
183     }
185     /* Create divlist */
186     $DivRules = new divSelectBox("SpamRules");
187     $DivRules->SetHeight(130);
189     if($this->acl_is_writeable("saTrustedNetworks")){
190       $actions = "";
191     }else{
192     
193       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
194       if($this->acl_is_writeable("saRule")){
195         $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
196       }
197     }
199     foreach($this->Rules as $key => $net){
200       $field1 = array("string" => $key );
201       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
202       $DivRules->AddEntry(array($field1,$field2));
203     }
204     $smarty->assign("divRules",$DivRules->DrawList()); 
205     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
207     /* Create Spam score select box entries */
208     $tmp = array();
209     for($i = 0 ; $i <= 20 ; $i ++ ){
210       $tmp[$i] = $i;
211     }
212     $smarty->assign("SpamScore",$tmp);
214     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
215   }
218   /* Add $post to list of configured trusted */
219   function AddTrust($post)
220   {
221     if(!empty($post)){
222       if(is_ip($post) || is_domain($post) || (is_ip_with_subnetmask($post))){
223         $this->TrustedNetworks[$post] = $post;
224       }else{
225         print_red(_("Specified value is not a valid 'trusted network' value."));
226       }
227     }
228   }
231   /* Delete trusted network */
232   function DelTrust($posts)
233   {
234     foreach($posts as $post){
235       if(isset($this->TrustedNetworks[$post])){
236         unset($this->TrustedNetworks[$post]);     
237       }
238     }
239   }
241   function save()
242   {
243     if(!$this->is_account) return;
244     plugin::save();
246     /* Create Flags */     
247     $this->attrs['saFlags'] = array();
248     foreach($this->Flags as $flag){
249       $var = "saFlags".$flag;
250       if($this->$var){
251         $this->attrs['saFlags'].=$flag;
252       }
253     }
255     /* Create trusted network entries */
256     $this->attrs['saTrustedNetworks'] = array();
257     foreach($this->TrustedNetworks as $net){
258       $this->attrs['saTrustedNetworks'][] = $net; 
259     }    
261     /* Rules */
262     $this->attrs['saRule'] = array();
263     foreach($this->Rules as $name => $rule){
264       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
265     }
267     /* Check if this is a new entry ... add/modify */
268     $ldap = $this->config->get_ldap_link();
269     $ldap->cat($this->dn,array("objectClass"));
270     if($ldap->count()){
271       $ldap->cd($this->dn);
272       $ldap->modify($this->attrs);
273     }else{
274       $ldap->cd($this->dn);
275       $ldap->add($this->attrs);
276     }
277     if($this->initially_was_account){
278       $this->handle_post_events("modify");
279       @log::log("modify","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
280     }else{
281       $this->handle_post_events("add");
282       @log::log("create","server/".get_class($this),$this->dn,array_keys($this->attrs),$ldap->get_error());
283     }
285     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
286   }
288   function check()
289   { 
290     $message = plugin::check();
292     /* Check if required score is numeric */
293     if(!is_numeric($this->saRequiredScore)){
294       $message[] = _("Required score must be a numeric value.");
295     }
297     return($message);
298   }
299   
301   function save_object()
302   {
303     if(isset($_POST['goSpamServer'])){
305       plugin::save_object();
307       /* Check flags */
308       foreach($this->Flags as $flag){
309         $var = "saFlags".$flag;
311         if($this->acl_is_writeable($var)){
312           if(isset($_POST[$var])){
313             $this->$var = TRUE;
314           }else{
315             $this->$var = FALSE;
316           }
317         }
318       }
319     }    
320   }  
322   
323   /* Return plugin informations for acl handling  */
324   function plInfo()
325   {
326     return (array(
327           "plShortName"   => _("Spamassassin"),
328           "plDescription" => _("Spamassassin")." ("._("Services").")",
329           "plSelfModify"  => FALSE,
330           "plDepends"     => array(),
331           "plPriority"    => 89,
332           "plSection"     => array("administration"),
333           "plCategory"    => array("server"),
334           "plProvidedAcls"=> array(
336             "saRewriteHeader"   => _("Rewrite header"),
337             "saTrustedNetworks" => _("Trusted networks"),
338             "saRequiredScore"   => _("Required score"),
339             "saRule"            => _("Rules"),
341             "saFlagB"           => _("Enable use of bayes filtering"),
342             "saFlagb"           => _("Enabled bayes auto learning"),
343             "saFlagC"           => _("Enable RBL checks"),
344             "saFlagR"           => _("Enable use of Razor"),
345             "saFlagD"           => _("Enable use of DDC"),
346             "saFlagP"           => _("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 ?>