Code

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