Code

Updated an error message. fixed server acl string
[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;
45   function gospamserver($config,$dn, $parent= NULL)
46   {
47     /* Init class */
48     plugin::plugin($config,$dn, $parent);
49     $this->DisplayName = _("Spamassassin");
51     /* Get userinfo & acls */
52     $this->ui = get_userinfo();
54     /* Get Flags */
55     foreach($this->Flags as $flag){
56       $var = "saFlags".$flag;
57       if(preg_match("/".$flag."/",$this->saFlags)){
58         $this->$var = TRUE;
59       }
60     }
61     
62     /* Get trusted networks */
63     $this->TrustedNetworks = array();
64     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
65       $var = $this->attrs['saTrustedNetworks'];
66       for($i = 0 ; $i < $var['count'] ; $i ++ ){
67         $var2 = $this->attrs['saTrustedNetworks'][$i];
68         $this->TrustedNetworks[ $var2 ] = $var2; 
69       }
70     }
72     /* Get rules */
73     $this->Rules = array();
74     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
75       $var = $this->attrs['saRule'];
76       for($i = 0 ; $i < $var['count'] ; $i ++ ){
77         $var2 = $this->attrs['saRule'][$i];
78         $name = preg_replace("/:.*$/","",$var2);
79         $value= base64_decode(preg_replace("/^.*:/","",$var2));
80         $this->Rules[ $name ] = $value;
81       }
82     }
83   }
86   function execute()
87   {
88     $display ="";
89     $smarty = get_smarty(); 
90     
91     /* If displayed, it is ever true*/
92     $this->is_account =true;
94     /* Get acls */
95     $tmp = $this->plinfo();
96     foreach($tmp['plProvidedAcls'] as $name => $translation){
97       $smarty->assign($name."ACL",$this->getacl($name));
98     }
100     /* Add new trusted network */
101     if(isset($_POST['AddNewTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
102       $this->AddTrust($_POST['NewTrustName']);
103     }
104   
105     /* Delete selected trusted network */
106     if(isset($_POST['DelTrust']) && ($this->acl_is_writeable("saTrustedNetworks"))){
107       $this->DelTrust($_POST['TrustedNetworks']);
108     }
110     /* Add a new rule */
111     if(isset($_POST['AddRule']) && $this->acl_is_writeable("saRule")){
112       $this->dialog = new goSpamServerRule($this->config,$this->dn);
113     }
114   
115     /* Cancel adding/editing specified rule */
116     if(isset($_POST['CancelRule'])){
117       $this->dialog = NULL;
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         $entry = preg_replace("/_(x|y)$/","",$entry);
127         $rule = $this->Rules[$entry];
128         $name = $entry;
129         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
130       }
131       if(preg_match("/^delRule_/",$name) && $once && $this->acl_is_writeable("saRule")){
132         $once = false;
133         $entry = preg_replace("/^delRule_/","",$name);
134         $entry = preg_replace("/_(x|y)$/","",$entry);
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           print_red($msg);
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 = NULL;
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($Flag));
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     
187       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
188       if($this->acl_is_writeable("saRule")){
189         $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
190       }
191     }
193     foreach($this->Rules as $key => $net){
194       $field1 = array("string" => $key );
195       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
196       $DivRules->AddEntry(array($field1,$field2));
197     }
198     $smarty->assign("divRules",$DivRules->DrawList()); 
199     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
201     /* Create Spam score select box entries */
202     $tmp = array();
203     for($i = 0 ; $i <= 20 ; $i ++ ){
204       $tmp[$i] = $i;
205     }
206     $smarty->assign("SpamScore",$tmp);
208     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
209   }
212   /* Add $post to list of configured trusted */
213   function AddTrust($post)
214   {
215     if(!empty($post)){
216       if(is_ip($post) || is_domain($post) || (is_ip_with_subnetmask($post))){
217         $this->TrustedNetworks[$post] = $post;
218       }else{
219         print_red(_("Specified value is not a valid 'trusted network' value."));
220       }
221     }
222   }
225   /* Delete trusted network */
226   function DelTrust($posts)
227   {
228     foreach($posts as $post){
229       if(isset($this->TrustedNetworks[$post])){
230         unset($this->TrustedNetworks[$post]);     
231       }
232     }
233   }
235   function save()
236   {
237     if(!$this->is_account) return;
238     plugin::save();
240     /* Create Flags */     
241     $this->attrs['saFlags'] = array();
242     foreach($this->Flags as $flag){
243       $var = "saFlags".$flag;
244       if($this->$var){
245         $this->attrs['saFlags'].=$flag;
246       }
247     }
249     /* Create trusted network entries */
250     $this->attrs['saTrustedNetworks'] = array();
251     foreach($this->TrustedNetworks as $net){
252       $this->attrs['saTrustedNetworks'][] = $net; 
253     }    
255     /* Rules */
256     $this->attrs['saRule'] = array();
257     foreach($this->Rules as $name => $rule){
258       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
259     }
261     /* Check if this is a new entry ... add/modify */
262     $ldap = $this->config->get_ldap_link();
263     $ldap->cat($this->dn,array("objectClass"));
264     if($ldap->count()){
265       $ldap->cd($this->dn);
266       $ldap->modify($this->attrs);
267     }else{
268       $ldap->cd($this->dn);
269       $ldap->add($this->attrs);
270     }
271     if($this->initially_was_account){
272       $this->handle_post_events("modify");
273     }else{
274       $this->handle_post_events("add");
275     }
277     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
278   }
280   function check()
281   { 
282     $message = plugin::check();
284     /* Check if required score is numeric */
285     if(!is_numeric($this->saRequiredScore)){
286       $message[] = _("Required score must be a numeric value.");
287     }
289     return($message);
290   }
291   
293   function save_object()
294   {
295     if(isset($_POST['goSpamServer'])){
297       plugin::save_object();
299       /* Check flags */
300       foreach($this->Flags as $flag){
301         $var = "saFlags".$flag;
303         if($this->acl_is_writeable($var)){
304           if(isset($_POST[$var])){
305             $this->$var = TRUE;
306           }else{
307             $this->$var = FALSE;
308           }
309         }
310       }
311     }    
312   }  
314   
315   /* Return plugin informations for acl handling  */
316   function plInfo()
317   {
318     return (array(
319           "plShortName"   => _("Spamassassin"),
320           "plDescription" => _("Spamassassin")." ("._("Services").")",
321           "plSelfModify"  => FALSE,
322           "plDepends"     => array(),
323           "plPriority"    => 89,
324           "plSection"     => array("administration"),
325           "plCategory"    => array("server"),
326           "plProvidedAcls"=> array(
328             "saRewriteHeader"   => _("Rewrite header"),
329             "saTrustedNetworks" => _("Trusted networks"),
330             "saRequiredScore"   => _("Required score"),
331             "saRule"            => _("Rules"),
333             "saFlagB"           => _("Enable use of bayes filtering"),
334             "saFlagb"           => _("Enabled bayes auto learning"),
335             "saFlagC"           => _("Enable RBL checks"),
336             "saFlagR"           => _("Enable use of Razor"),
337             "saFlagD"           => _("Enable use of DDC"),
338             "saFlagP"           => _("Enable use of Pyzor"))
339           ));
340   }
342   /* For newer service management dialogs */
343   function getListEntry()
344   {
345     $fields                 = goService::getListEntry();
346     $fields['Message']      = _("Spamassassin");
347     $fields['AllowEdit']    = true;
348     return($fields);
349   }
351 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
352 ?>