Code

Some fixes
[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     /* Do we need to flip is_account state? */
91     if (isset($_POST['modify_state'])) {
92       $this->is_account = !$this->is_account;
93     }
94  
95     /* Show tab dialog headers */
96     if ($this->is_account) {
97       /* call Add Acoount to add account */
98       $display = $this->show_header(_("Remove spamassassin extension"), 
99         _("This server has spamassassin features enabled. You can disable them by clicking below."));
100     } else {
101       /* call remove Account */
102       $display = $this->show_header(_("Add spamassassin service"), 
103         _("This server has spamassassin features disabled. You can enable them by clicking below."));
104       return ($display);
105     }
107     /* Add new trusted network */
108     if(isset($_POST['AddNewTrust'])){
109       $this->AddTrust($_POST['NewTrustName']);
110     }
111   
112     /* Delete selected trusted network */
113     if(isset($_POST['DelTrust'])){
114       $this->DelTrust($_POST['TrustedNetworks']);
115     }
117     /* Add a new rule */
118     if(isset($_POST['AddRule'])){
119       $this->dialog = new goSpamServerRule($this->config,$this->dn);
120     }
121   
122     /* Cancel adding/editing specified rule */
123     if(isset($_POST['CancelRule'])){
124       $this->dialog = NULL;
125     }
127     /* Handle post to delete rules */
128     $once = true;
129     foreach($_POST as $name => $value){
130       if(preg_match("/^editRule_/",$name) && $once ){
131         $once = false;
132         $entry = preg_replace("/^editRule_/","",$name);
133         $entry = preg_replace("/_(x|y)$/","",$entry);
134         $rule = $this->Rules[$entry];
135         $name = $entry;
136         $this->dialog = new goSpamServerRule($this->config,$this->dn,$name,$rule);
137       }
138       if(preg_match("/^delRule_/",$name) && $once ){
139         $once = false;
140         $entry = preg_replace("/^delRule_/","",$name);
141         $entry = preg_replace("/_(x|y)$/","",$entry);
142         unset($this->Rules[$entry]);
143       }
144     }
146     /* Save rules */
147     if(isset($_POST['SaveRule'])){
148       $this->dialog->save_object();
149       $msgs = $this->dialog->check();
150       if(count($msgs)){
151         foreach($msgs as $msg){
152           print_red($msg);
153         }
154       }else{
155         $ret = $this->dialog->save();
156         if((!empty($ret['orig_name'])) && isset($this->Rules[$ret['orig_name']])){
157           unset($this->Rules[$ret['orig_name']]);
158         }
159         $this->Rules[$ret['name']] = $ret['rule'];
160         $this->dialog = NULL;
161       }
162     }
163    
164     /* Display dialog if available */ 
165     if($this->dialog && $this->dialog->config){
166       $this->dialog->save_object();
167       return($this->dialog->execute());
168     }
170     /* Assign smarty vars */
171     $smarty = get_smarty(); 
172     foreach($this->attributes as $attr){
173       $smarty->assign($attr,$this->$attr);
174       $smarty->assign($attr."ACL",chkacl($this->acl,$attr));
175     }
177     /* Assign checkbox states */
178     foreach($this->Flags as $Flag){
179       $var = "saFlags".$Flag;
180       $smarty->assign("saFlags".$Flag."ACL",chkacl($this->acl,$Flag));
181       if($this->$var){
182         $smarty->assign("saFlags".$Flag."CHK"," checked " );
183       }else{
184         $smarty->assign("saFlags".$Flag."CHK","");
185       }
186     }
188     /* Create divlist */
189     $DivRules = new divSelectBox("SpamRules");
190     $DivRules->SetHeight(130);
192     if(preg_match("/disabled/",chkacl($this->acl,"saTrustedNetworks"))){
193       $actions = "";
194     }else{
195       $actions = "<input type='image' src='images/edit.png'      name='editRule_%s'>";
196       $actions.= "<input type='image' src='images/edittrash.png' name='delRule_%s'>";
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     return($display.$smarty->fetch(get_template_path("goSpamServer.tpl",TRUE,dirname(__FILE__))));
208   }
211   /* Add $post to list of configured trusted */
212   function AddTrust($post)
213   {
214     if(!empty($post)){
215       $this->TrustedNetworks[$post] = $post;
216     }
217   }
220   /* Delete trusted network */
221   function DelTrust($posts)
222   {
223     foreach($posts as $post){
224       if(isset($this->TrustedNetworks[$post])){
225         unset($this->TrustedNetworks[$post]);     
226       }
227     }
228   }
231   /* remove this extension */
232   function remove_from_parent()
233   {
234     if(!$this->is_account && $this->initially_was_account){
235       plugin::remove_from_parent();
237       /* Check if this is a new entry ... add/modify */
238       $ldap = $this->config->get_ldap_link();
239       $ldap->cat($this->dn,array("objectClass"));
240       if($ldap->count()){
241         $ldap->cd($this->dn);
242         $ldap->modify($this->attrs);
243       }else{
244         $ldap->cd($this->dn);
245         $ldap->add($this->attrs);
246       }
247       show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
248       $this->handle_post_events("remove");
249     }
250   }
253   function save()
254   {
255     if(!$this->is_account) return;
256     plugin::save();
258     /* Create Flags */     
259     $this->attrs['saFlags'] = "";
260     foreach($this->Flags as $flag){
261       $var = "saFlags".$flag;
262       if($this->$var){
263         $this->attrs['saFlags'].=$flag;
264       }
265     }
267     /* Create trusted network entries */
268     $this->attrs['saTrustedNetworks'] = array();
269     foreach($this->TrustedNetworks as $net){
270       $this->attrs['saTrustedNetworks'][] = $net; 
271     }    
273     /* Rules */
274     $this->attrs['saRule'] = array();
275     foreach($this->Rules as $name => $rule){
276       $this->attrs['saRule'][] = $name.":".base64_encode($rule);
277     }
279     /* Check if this is a new entry ... add/modify */
280     $ldap = $this->config->get_ldap_link();
281     $ldap->cat($this->dn,array("objectClass"));
282     if($ldap->count()){
283       $ldap->cd($this->dn);
284       $ldap->modify($this->attrs);
285     }else{
286       $ldap->cd($this->dn);
287       $ldap->add($this->attrs);
288     }
289     if($this->initially_was_account){
290       $this->handle_post_events("modify");
291     }else{
292       $this->handle_post_events("add");
293     }
295     show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
296   }
298   function check()
299   { 
300     $message = plugin::check();
302     /* Check if required score is numeric */
303     if(!is_numeric($this->saRequiredScore)){
304       $message[] = _("Required score must be a numeric value.");
305     }
307     return($message);
308   }
309   
311   function save_object()
312   {
313     if(isset($_POST['goSpamServer'])){
315       plugin::save_object();
317       /* Check flags */
318       foreach($this->Flags as $flag){
319         $var = "saFlags".$flag;
320         if(isset($_POST[$var])){
321           $this->$var = TRUE;
322         }else{
323           $this->$var = FALSE;
324         }
325       }
326     }    
327   }  
329   
330   /* Return plugin informations for acl handling 
331   function plInfo()
332   {
333     return (array(
334           "plShortName"   => _("Spamassassin"),
335           "plDescription" => _("Spamassassin service"),
336           "plSelfModify"  => FALSE,
337           "plDepends"     => array(),
338           "plPriority"    => 0,
339           "plSection"     => array("administration"),
340           "plCategory"    => array("server"),
341           "plProvidedAcls"=> array(
343             "saRewriteHeader"   => _("Rewrite header"),
344             "saTrustedNetworks" => _("Trusted networks"),
345             "saRequiredScore"   => _("Required score"),
346             "saRule"            => _("Rules"),
348             "saFlagB"           => _("Enable use of bayes filtering"),
349             "saFlagb"           => _("Enabled bayes auto learning"),
350             "saFlagC"           => _("Enable RBL checks"),
351             "saFlagR"           => _("Enable use of Razor"),
352             "saFlagD"           => _("Enable use of DDC"),
353             "saFlagP"           => _("Enable use of Pyzor"))
354           ));
355   }
356   */
358   /* For newer service management dialogs */
359   /*function getListEntry()
360   {
361     $this->updateStatusState();
362     $flag                   = $this->StatusFlag;
363     $fields['Status']       = $this->$flag;
364     $fields['Message']      = _("spamassassin");
365     $fields['AllowStart']   = true;
366     $fields['AllowStop']    = true;
367     $fields['AllowRestart'] = true;
368     $fields['AllowRemove']  = true;
369     $fields['AllowEdit']    = true;
370     return($fields);
371   }
373   function updateStatusState()
374   {
375     if(empty($this->StatusFlag)) return;
377     $attrs = array();
378     $flag = $this->StatusFlag;
379     $ldap = $this->config->get_ldap_link();
380     $ldap->cd($this->cn);
381     $ldap->cat($this->dn,array($flag));
382     if($ldap->count()){
383       $attrs = $ldap->fetch();
384     }
385     if(isset($attrs[$flag][0])){
386       $this->$flag = $attrs[$flag][0];
387     }
388   }
389   function action_hook($add_attrs= array())
390   {
391     /* Find postcreate entries for this class * /
392     $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
393     if ($command == "" && isset($this->config->data['TABS'])){
394       $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
395     }
396     if ($command != ""){
397       /* Walk through attribute list * /
398       foreach ($this->attributes as $attr){
399         if (!is_array($this->$attr)){
400           $command= preg_replace("/%$attr/", $this->$attr, $command);
401         }
402       }
403       $command= preg_replace("/%dn/", $this->dn, $command);
404       /* Additional attributes * /
405       foreach ($add_attrs as $name => $value){
406         $command= preg_replace("/%$name/", $value, $command);
407       }
409       /* If there are still some %.. in our command, try to fill these with some other class vars * /
410       if(preg_match("/%/",$command)){
411         $attrs = get_object_vars($this);
412         foreach($attrs as $name => $value){
413           if(!is_string($value)) continue;
414           $command= preg_replace("/%$name/", $value, $command);
415         }
416       }
418       if (check_command($command)){
419         @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
420             $command, "Execute");
422         exec($command);
423       } else {
424         $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
425         print_red ($message);
426       }
427     }
428   }
430   /* Directly save new status flag * /
431   function setStatus($value)
432   {
433     if($value == "none") return;
434     if(!$this->initially_was_account) return;
435     $ldap = $this->config->get_ldap_link();
436     $ldap->cd($this->dn);
437     $ldap->cat($this->dn,array("objectClass"));
438     if($ldap->count()){
440       $tmp = $ldap->fetch();
441       for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
442         $attrs['objectClass'][] = $tmp['objectClass'][$i];
443       }
444       $flag = $this->StatusFlag;
445       $attrs[$flag] = $value;
446       $this->$flag = $value;
447       $ldap->modify($attrs);
448       show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
449       $this->action_hook();
450     }
451   }
454   */
457 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
458 ?>