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";
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;
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 }
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 }
116 /* Add new trusted network */
117 if(isset($_POST['AddNewTrust'])){
118 $this->AddTrust($_POST['NewTrustName']);
119 }
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 }
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 }
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 {
250 if(!$this->is_account && $this->initially_was_account){
252 plugin::remove_from_parent();
254 /* Remove status flag, it is not a memeber of
255 this->attributes, so ensure that it is deleted too */
256 if(!empty($this->StatusFlag)){
257 $this->attrs[$this->StatusFlag] = array();
258 }
260 /* Check if this is a new entry ... add/modify */
261 $ldap = $this->config->get_ldap_link();
262 $ldap->cat($this->dn,array("objectClass"));
263 if($ldap->count()){
264 $ldap->cd($this->dn);
265 $ldap->modify($this->attrs);
266 }else{
267 $ldap->cd($this->dn);
268 $ldap->add($this->attrs);
269 }
270 show_ldap_error($ldap->get_error(), sprintf(_("Removing of server services/spamassassin with dn '%s' failed."),$this->dn));
271 $this->handle_post_events("remove");
272 }
273 }
276 function save()
277 {
278 if(!$this->is_account) return;
279 plugin::save();
281 /* Create Flags */
282 $this->attrs['saFlags'] = "";
283 foreach($this->Flags as $flag){
284 $var = "saFlags".$flag;
285 if($this->$var){
286 $this->attrs['saFlags'].=$flag;
287 }
288 }
290 /* Create trusted network entries */
291 $this->attrs['saTrustedNetworks'] = array();
292 foreach($this->TrustedNetworks as $net){
293 $this->attrs['saTrustedNetworks'][] = $net;
294 }
296 /* Rules */
297 $this->attrs['saRule'] = array();
298 foreach($this->Rules as $name => $rule){
299 $this->attrs['saRule'][] = $name.":".base64_encode($rule);
300 }
302 /* Check if this is a new entry ... add/modify */
303 $ldap = $this->config->get_ldap_link();
304 $ldap->cat($this->dn,array("objectClass"));
305 if($ldap->count()){
306 $ldap->cd($this->dn);
307 $ldap->modify($this->attrs);
308 }else{
309 $ldap->cd($this->dn);
310 $ldap->add($this->attrs);
311 }
312 if($this->initially_was_account){
313 $this->handle_post_events("modify");
314 }else{
315 $this->handle_post_events("add");
316 }
318 show_ldap_error($ldap->get_error(), sprintf(_("Saving of server services/spamassassin with dn '%s' failed."),$this->dn));
319 }
321 function check()
322 {
323 $message = plugin::check();
325 /* Check if required score is numeric */
326 if(!is_numeric($this->saRequiredScore)){
327 $message[] = _("Required score must be a numeric value.");
328 }
330 return($message);
331 }
334 function save_object()
335 {
336 if(isset($_POST['goSpamServer'])){
338 plugin::save_object();
340 /* Check flags */
341 foreach($this->Flags as $flag){
342 $var = "saFlags".$flag;
343 if(isset($_POST[$var])){
344 $this->$var = TRUE;
345 }else{
346 $this->$var = FALSE;
347 }
348 }
349 }
350 }
353 /* Return plugin informations for acl handling
354 function plInfo()
355 {
356 return (array(
357 "plShortName" => _("Spamassassin"),
358 "plDescription" => _("Spamassassin service"),
359 "plSelfModify" => FALSE,
360 "plDepends" => array(),
361 "plPriority" => 0,
362 "plSection" => array("administration"),
363 "plCategory" => array("server"),
364 "plProvidedAcls"=> array(
366 "saRewriteHeader" => _("Rewrite header"),
367 "saTrustedNetworks" => _("Trusted networks"),
368 "saRequiredScore" => _("Required score"),
369 "saRule" => _("Rules"),
371 "saFlagB" => _("Enable use of bayes filtering"),
372 "saFlagb" => _("Enabled bayes auto learning"),
373 "saFlagC" => _("Enable RBL checks"),
374 "saFlagR" => _("Enable use of Razor"),
375 "saFlagD" => _("Enable use of DDC"),
376 "saFlagP" => _("Enable use of Pyzor"))
377 ));
378 }
379 */
381 /* For newer service management dialogs */
382 function getListEntry()
383 {
384 $this->updateStatusState();
385 $flag = $this->StatusFlag;
386 $fields['Status'] = $this->$flag;
387 $fields['Message'] = _("Spamassassin");
388 $fields['AllowStart'] = true;
389 $fields['AllowStop'] = true;
390 $fields['AllowRestart'] = true;
391 $fields['AllowRemove'] = true;
392 $fields['AllowEdit'] = true;
393 return($fields);
394 }
396 function updateStatusState()
397 {
398 if(empty($this->StatusFlag)) return;
400 $attrs = array();
401 $flag = $this->StatusFlag;
402 $ldap = $this->config->get_ldap_link();
403 $ldap->cd($this->cn);
404 $ldap->cat($this->dn,array($flag));
405 if($ldap->count()){
406 $attrs = $ldap->fetch();
407 }
408 if(isset($attrs[$flag][0])){
409 $this->$flag = $attrs[$flag][0];
410 }
411 }
412 function action_hook($add_attrs= array())
413 {
414 /* Find postcreate entries for this class */
415 $command= search_config($this->config->data['MENU'], get_class($this), "ACTION_HOOK");
416 if ($command == "" && isset($this->config->data['TABS'])){
417 $command= search_config($this->config->data['TABS'], get_class($this), "ACTION_HOOK");
418 }
419 if ($command != ""){
420 /* Walk through attribute list */
421 foreach ($this->attributes as $attr){
422 if (!is_array($this->$attr)){
423 $command= preg_replace("/%$attr/", $this->$attr, $command);
424 }
425 }
426 $command= preg_replace("/%dn/", $this->dn, $command);
427 /* Additional attributes */
428 foreach ($add_attrs as $name => $value){
429 $command= preg_replace("/%$name/", $value, $command);
430 }
432 /* If there are still some %.. in our command, try to fill these with some other class vars */
433 if(preg_match("/%/",$command)){
434 $attrs = get_object_vars($this);
435 foreach($attrs as $name => $value){
436 if(!is_string($value)) continue;
437 $command= preg_replace("/%$name/", $value, $command);
438 }
439 }
441 if (check_command($command)){
442 @DEBUG (DEBUG_SHELL, __LINE__, __FUNCTION__, __FILE__,
443 $command, "Execute");
445 exec($command);
446 } else {
447 $message= sprintf(_("Command '%s', specified as ACTION_HOOK for plugin '%s' doesn't seem to exist."), $command, get_class($this));
448 print_red ($message);
449 }
450 }
451 }
453 /* Directly save new status flag */
454 function setStatus($value)
455 {
456 if($value == "none") return;
457 if(!$this->initially_was_account) return;
458 $ldap = $this->config->get_ldap_link();
459 $ldap->cd($this->dn);
460 $ldap->cat($this->dn,array("objectClass"));
461 if($ldap->count()){
463 $tmp = $ldap->fetch();
464 for($i = 0; $i < $tmp['objectClass']['count']; $i ++){
465 $attrs['objectClass'][] = $tmp['objectClass'][$i];
466 }
467 $flag = $this->StatusFlag;
468 $attrs[$flag] = $value;
469 $this->$flag = $value;
470 $ldap->modify($attrs);
471 show_ldap_error($ldap->get_error(), sprintf(_("Set status flag for server services/spamassassin with dn '%s' failed."),$this->dn));
472 $this->action_hook();
473 }
474 }
477 }
478 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
479 ?>