Code

Made new service tabs (Spam / Virus) compatible with serverservice and servtabs
[gosa.git] / plugins / admin / systems / class_goSpamServer.inc
1 <?php
3 class gospamserver extends plugin{
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;
43   function gospamserver($config,$dn)
44   {
45     /* Init class */
46     plugin::plugin($config,$dn);
47     $this->DisplayName = _("Spamassassin");
49     /* Get userinfo & acls */
50     $this->ui = get_userinfo();
52     /* Set up the users ACL's for this 'dn' */
53     $acl= get_permissions ($this->dn, $this->ui->subtreeACL);
54     $this->acl= get_module_permission($acl, "goSpamServer", $this->ui->dn);
56     /* Get Flags */
57     foreach($this->Flags as $flag){
58       $var = "saFlags".$flag;
59       if(preg_match("/".$flag."/",$this->saFlags)){
60         $this->$var = TRUE;
61       }
62     }
63     
64     /* Get trusted networks */
65     $this->TrustedNetworks = array();
66     if(isset($this->attrs['saTrustedNetworks']) && is_array($this->attrs['saTrustedNetworks'])){
67       $var = $this->attrs['saTrustedNetworks'];
68       for($i = 0 ; $i < $var['count'] ; $i ++ ){
69         $var2 = $this->attrs['saTrustedNetworks'][$i];
70         $this->TrustedNetworks[ $var2 ] = $var2; 
71       }
72     }
74     /* Get rules */
75     $this->Rules = array();
76     if(isset($this->attrs['saRule']) && is_array($this->attrs['saRule'])){  
77       $var = $this->attrs['saRule'];
78       for($i = 0 ; $i < $var['count'] ; $i ++ ){
79         $var2 = $this->attrs['saRule'][$i];
80         $name = preg_replace("/:.*$/","",$var2);
81         $value= base64_decode(preg_replace("/^.*:/","",$var2));
82         $this->Rules[ $name ] = $value;
83       }
84     }
85   }
88   function execute()
89   {
90     $smarty = get_smarty(); 
91     if(get_class($this->parent) == "servtabs"){
93       $smarty->assign("servtabs",true);
94       /* Do we need to flip is_account state? */
95       if (isset($_POST['modify_state'])) {
96         $this->is_account = !$this->is_account;
97       }
99       /* Show tab dialog headers */
100       if ($this->is_account) {
101         /* call Add Acoount to add account */
102         $display = $this->show_header(_("Remove spamassassin extension"), 
103             _("This server has spamassassin features enabled. You can disable them by clicking below."));
104       } else {
105         /* call remove Account */
106         $display = $this->show_header(_("Add spamassassin service"), 
107             _("This server has spamassassin features disabled. You can enable them by clicking below."));
108         return ($display);
109       }
110     }else{
111       $this->is_account =true;
112       $display ="";
113       $smarty->assign("servtabs",false);
114     }
115   
116     /* Add new trusted network */
117     if(isset($_POST['AddNewTrust'])){
118       $this->AddTrust($_POST['NewTrustName']);
119     }
120   
121     /* Delete selected trusted network */
122     if(isset($_POST['DelTrust'])){
123       $this->DelTrust($_POST['TrustedNetworks']);
124     }
126     /* Add a new rule */
127     if(isset($_POST['AddRule'])){
128       $this->dialog = new goSpamServerRule($this->config,$this->dn);
129     }
130   
131     /* Cancel adding/editing specified rule */
132     if(isset($_POST['CancelRule'])){
133       $this->dialog = NULL;
134     }
136     /* Handle post to delete rules */
137     $once = true;
138     foreach($_POST as $name => $value){
139       if(preg_match("/^editRule_/",$name) && $once ){
140         $once = false;
141         $entry = preg_replace("/^editRule_/","",$name);
142         $entry = preg_replace("/_(x|y)$/","",$entry);
143         $rule = $this->Rules[$entry];
144         $name = $entry;
145         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
146       }
147       if(preg_match("/^delRule_/",$name) && $once ){
148         $once = false;
149         $entry = preg_replace("/^delRule_/","",$name);
150         $entry = preg_replace("/_(x|y)$/","",$entry);
151         unset($this->Rules[$entry]);
152       }
153     }
155     /* Save rules */
156     if(isset($_POST['SaveRule'])){
157       $this->dialog->save_object();
158       $msgs = $this->dialog->check();
159       if(count($msgs)){
160         foreach($msgs as $msg){
161           print_red($msg);
162         }
163       }else{
164         $ret = $this->dialog->save();
165         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
166           unset($this->Rules[$ret['orig_name']]);
167         }
168         $this->Rules[$ret['name']] = $ret['rule'];
169         $this->dialog = NULL;
170       }
171     }
172    
173     /* Display dialog if available */ 
174     if($this->dialog && $this->dialog->config){
175       $this->dialog->save_object();
176       return($this->dialog->execute());
177     }
179     /* Assign smarty vars */
180     foreach($this->attributes as $attr){
181       $smarty->assign($attr,$this->$attr);
182       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
183     }
185     /* Assign checkbox states */
186     foreach($this->Flags as $Flag){
187       $var = "saFlags".$Flag;
188       $smarty->assign("saFlags".$Flag."ACL",chkacl($this->acl,$Flag));
189       if($this->$var){
190         $smarty->assign("saFlags".$Flag."CHK"," checked " );
191       }else{
192         $smarty->assign("saFlags".$Flag."CHK","");
193       }
194     }
196     /* Create divlist */
197     $DivRules = new divSelectBox("SpamRules");
198     $DivRules->SetHeight(130);
200     if(preg_match("/disabled/",chkacl($this->acl,"saTrustedNetworks"))){
201       $actions = "";
202     }else{
203       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
204       $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
205     }
207     foreach($this->Rules as $key => $net){
208       $field1 = array("string" => $key );
209       $field2 = array("string" => sprintf($actions,$key,$key) , "attach" => "style='border-right:0px;width:36px;'");
210       $DivRules->AddEntry(array($field1,$field2));
211     }
212     $smarty->assign("divRules",$DivRules->DrawList()); 
213     $smarty->assign("TrustedNetworks",$this->TrustedNetworks); 
215     /* Create Spam score select box entries */
216     $tmp = array();
217     for($i = 0 ; $i <= 20 ; $i ++ ){
218       $tmp[$i] = $i;
219     }
220     $smarty->assign("SpamScore",$tmp);
222     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
223   }
226   /* Add $post to list of configured trusted */
227   function AddTrust($post)
228   {
229     if(!empty($post)){
230       $this->TrustedNetworks[$post] = $post;
231     }
232   }
235   /* Delete trusted network */
236   function DelTrust($posts)
237   {
238     foreach($posts as $post){
239       if(isset($this->TrustedNetworks[$post])){
240         unset($this->TrustedNetworks[$post]);     
241       }
242     }
243   }
246   /* remove this extension */
247   function remove_from_parent()
248   {
249     if(!$this->is_account && $this->initially_was_account){
250       plugin::remove_from_parent();
252       /* Check if this is a new entry ... add/modify */
253       $ldap = $this->config->get_ldap_link();
254       $ldap->cat($this->dn,array("objectClass"));
255       if($ldap->count()){
256         $ldap->cd($this->dn);
257         $ldap->modify($this->attrs);
258       }else{
259         $ldap->cd($this->dn);
260         $ldap->add($this->attrs);
261       }
262       show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
263       $this->handle_post_events("remove");
264     }
265   }
268   function save()
269   {
270     if(!$this->is_account) return;
271     plugin::save();
273     /* Create Flags */     
274     $this->attrs['saFlags'] = "";
275     foreach($this->Flags as $flag){
276       $var = "saFlags".$flag;
277       if($this->$var){
278         $this->attrs['saFlags'].=$flag;
279       }
280     }
282     /* Create trusted network entries */
283     $this->attrs['saTrustedNetworks'] = array();
284     foreach($this->TrustedNetworks as $net){
285       $this->attrs['saTrustedNetworks'][] = $net; 
286     }    
288     /* Rules */
289     $this->attrs['saRule'] = array();
290     foreach($this->Rules as $name => $rule){
291       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
292     }
294     /* Check if this is a new entry ... add/modify */
295     $ldap = $this->config->get_ldap_link();
296     $ldap->cat($this->dn,array("objectClass"));
297     if($ldap->count()){
298       $ldap->cd($this->dn);
299       $ldap->modify($this->attrs);
300     }else{
301       $ldap->cd($this->dn);
302       $ldap->add($this->attrs);
303     }
304     if($this->initially_was_account){
305       $this->handle_post_events("modify");
306     }else{
307       $this->handle_post_events("add");
308     }
310     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
311   }
313   function check()
314   { 
315     $message = plugin::check();
317     /* Check if required score is numeric */
318     if(!is_numeric($this->saRequiredScore)){
319       $message[] = _("Required score must be a numeric value.");
320     }
322     return($message);
323   }
324   
326   function save_object()
327   {
328     if(isset($_POST['goSpamServer'])){
330       plugin::save_object();
332       /* Check flags */
333       foreach($this->Flags as $flag){
334         $var = "saFlags".$flag;
335         if(isset($_POST[$var])){
336           $this->$var = TRUE;
337         }else{
338           $this->$var = FALSE;
339         }
340       }
341     }    
342   }  
344   
345   /* Return plugin informations for acl handling 
346   function plInfo()
347   {
348     return (array(
349           "plShortName"   => _("Spamassassin"),
350           "plDescription" => _("Spamassassin service"),
351           "plSelfModify"  => FALSE,
352           "plDepends"     => array(),
353           "plPriority"    => 0,
354           "plSection"     => array("administration"),
355           "plCategory"    => array("server"),
356           "plProvidedAcls"=> array(
358             "saRewriteHeader"   => _("Rewrite header"),
359             "saTrustedNetworks" => _("Trusted networks"),
360             "saRequiredScore"   => _("Required score"),
361             "saRule"            => _("Rules"),
363             "saFlagB"           => _("Enable use of bayes filtering"),
364             "saFlagb"           => _("Enabled bayes auto learning"),
365             "saFlagC"           => _("Enable RBL checks"),
366             "saFlagR"           => _("Enable use of Razor"),
367             "saFlagD"           => _("Enable use of DDC"),
368             "saFlagP"           => _("Enable use of Pyzor"))
369           ));
370   }
371   */
373   /* For newer service management dialogs */
374   function getListEntry()
375   {
376     $this->updateStatusState();
377     $flag                   = $this->StatusFlag;
378     $fields['Status']       = $this->$flag;
379     $fields['Message']      = _("Spamassassin");
380     $fields['AllowStart']   = true;
381     $fields['AllowStop']    = true;
382     $fields['AllowRestart'] = true;
383     $fields['AllowRemove']  = true;
384     $fields['AllowEdit']    = true;
385     return($fields);
386   }
388   function updateStatusState()
389   {
390     if(empty($this->StatusFlag)) return;
392     $attrs = array();
393     $flag = $this->StatusFlag;
394     $ldap = $this->config->get_ldap_link();
395     $ldap->cd($this->cn);
396     $ldap->cat($this->dn,array($flag));
397     if($ldap->count()){
398       $attrs = $ldap->fetch();
399     }
400     if(isset($attrs[$flag][0])){
401       $this->$flag = $attrs[$flag][0];
402     }
403   }
404   function action_hook($add_attrs= array())
405   {
406     /* Find postcreate entries for this class */
407     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
408     if ($command == "" && isset($this->config->data['TABS'])){
409       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
410     }
411     if ($command != ""){
412       /* Walk through attribute list */
413       foreach ($this->attributes as $attr){
414         if (!is_array($this->$attr)){
415           $command= preg_replace("/%$attr/", $this->$attr, $command);
416         }
417       }
418       $command= preg_replace("/%dn/", $this->dn, $command);
419       /* Additional attributes */
420       foreach ($add_attrs as $name => $value){
421         $command= preg_replace("/%$name/", $value, $command);
422       }
424       /* If there are still some %.. in our command, try to fill these with some other class vars */
425       if(preg_match("/%/",$command)){
426         $attrs = get_object_vars($this);
427         foreach($attrs as $name => $value){
428           if(!is_string($value)) continue;
429           $command= preg_replace("/%$name/", $value, $command);
430         }
431       }
433       if (check_command($command)){
434         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
435             $command, "Execute");
437         exec($command);
438       } else {
439         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
440         print_red ($message);
441       }
442     }
443   }
445   /* Directly save new status flag */
446   function setStatus($value)
447   {
448     if($value == "none") return;
449     if(!$this->initially_was_account) return;
450     $ldap = $this->config->get_ldap_link();
451     $ldap->cd($this->dn);
452     $ldap->cat($this->dn,array("objectClass"));
453     if($ldap->count()){
455       $tmp = $ldap->fetch();
456       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
457         $attrs['objectClass'][] = $tmp['objectClass'][$i];
458       }
459       $flag = $this->StatusFlag;
460       $attrs[$flag] = $value;
461       $this->$flag = $value;
462       $ldap->modify($attrs);
463       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
464       $this->action_hook();
465     }
466   }
470 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
471 ?>