Code

19268178dcd337f7d3d9a17ae27f344c38e7bc33
[gosa.git] / plugins / addons / mailqueue / class_mailqueue.inc
1 <?php
3 class mailqueue extends plugin
4 {
5   /* Definitions */
6   var $plHeadline= "Mail queue";
7   var $plDescription= "This does something";
9   /* attribute list for save action */
10   var $attributes     = array();
11   var $objectclasses  = array();
12   var $mailQueueScript= "";    
13   var $Server         = "all";
14   var $Time           = 0;
15   var $Search         = "*";
16   var $Page           = 0;
17   var $Stat           = "all";
18   var $OrderBy        = "Arrival";
19   var $SortType       = "up";
20   var $disp_header    = false;
21   var $range          = 20;   
23   function mailqueue($config, $dn= NULL)
24   {
25     $this->config           = $config;
26     if (isset($this->config->current['MAILQUEUESCRIPTPATH'])){
27       $this->mailQueueScript  = $this->config->current['MAILQUEUESCRIPTPATH'];    
28     }
30     if(isset($this->config->data['MAIN']['MAILQUEUESCRIPTPATH'])){
31        $this->mailQueueScript  = $this->config->data['MAIN']['MAILQUEUESCRIPTPATH'];
32     }
34     $this->Server           = "all";
35   }
38   function pass_cmd($str)
39   {
40 //    print_red($str);
41     return(shell_exec($str));
42   }
45   function execute()
46   {
47     /* Call parent execute */
48     plugin::execute();
50     if(isset($_POST['EntriesPerPage'])){
51       $this->range = $_POST['EntriesPerPage'];
52     }
54     $smarty= get_smarty();
55     $error =false;
57     if(empty($this->mailQueueScript)){
58       print_red(_("Please check your 'gosa.conf', there is no 'MAILQUEUESCRIPTPATH' specified."));
59       $error = true;
60     }else{
62       /* If we have more than one server selected (all), 
63          create a string with all servers separated by ', '
64        */ 
65       if($this->Server=="all"){
66         $se = $this->getServer();
68         unset($se['all']);
69         $se_str = "";
70         foreach($se as $server) {
71           $se_str .= $server." ";
72         }
73       }else{
74         /* We have only one server selected */
75         $se_str = $this->Server;
76       }
78       /* Check all post that will effect all entries */
79       $only_once = true;
80       foreach(array("unhold_all","hold_all","del_all","requeue_all") as $attr){
81         foreach($_POST as $name => $value){
82           if((preg_match("/".$attr."/",$name))&&($only_once)){
83             if(!$this->acl_is_readable($attr)){
84               $only_once = false;
85               print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),$attr));
86             }else{
87               $only_once = false;
88               $act = preg_replace("/_.*$/i","",$attr);
89               $r_cmd = preg_replace("/%action/" , $act           ,$this->mailQueueScript);
90               $r_cmd = preg_replace("/%server/" , $se_str         ,$r_cmd);
91               $r_cmd = preg_replace("/%id/"     , "ALL"           ,$r_cmd);
92               if($this->pass_cmd($r_cmd)==false){
93                 print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$r_cmd));
94               }
95             }
96           }
97         }
98       }        
100       /* Check single entry manipulation  posts */
101       $only_once = true;
103       /* act specifies the command to execute */
104       if(isset($_GET['act'])){
105         $opt = $_GET['act'];  
106   
107         /* The option to exec should be one of these */
108         if(in_array($opt,array("unhold","hold","del","requeue","query","header"))){
109           $only_once = false;
111           if(!$this->acl_is_readable($opt)){
112               print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),$opt));
113           }else{
114             /* Create cmd */
115             $r_cmd = preg_replace("/%action/" , $opt            ,$this->mailQueueScript);
116             $r_cmd = preg_replace("/%server/" , $this->Server   ,$r_cmd);
117             $r_cmd = preg_replace("/%id/"     , $_GET['id']     ,$r_cmd);
119             /* Execute cmd */
120             if(!$str = $this->pass_cmd($r_cmd)){
121               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$r_cmd));
122             }else{
123             
124               /* Special handling for option='header' */
125               if($opt == "header"){
126           
127                 /* Create table which displays the header informations */
128                 $this->disp_header ="\n<table width='80%'>";
129                 foreach(split("\n",$str) as $line){
130                   $line = trim($line);
131                   if(empty($line)) {
132                     continue;
133                   }
134                   $this->disp_header .= "\n<tr>";
135                   $tmp0 = preg_replace("/:.*$/","",$line);
136                   $tmp1 = preg_replace("/^.*:/","",$line);
137                   $this->disp_header .= "\n<td style='background-color:#EEEEEE;'>".$tmp0."</td><td>".$tmp1."</td>"; 
138                   $this->disp_header .= "\n</tr>";
139                 }              
140                 $this->disp_header .= "\n</table>";
141               }
142             }
143           }
144         }
145       }        
148       /* Back is posted from the header display page */
149       if(isset($_POST['back'])){
150         $this->disp_header = false;
151       }
153       /* If there is a header in disp_header, then display it */
154       if($this->disp_header){
155         $smarty->assign("header",$this->disp_header);
156         return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
157       }
159       /* tell smarty to display the search results*/
160       $smarty->assign("all_ok"      , "true");
161       
162       /* A single server is selected */
163       if($this->Server != "all"){    
165         /* Create Query cmd */ 
166         $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
167         $q_cmd = preg_replace("/%server/" ,$this->Server,$q_cmd);
168         $q_cmd = preg_replace("/%id/"     ,"all"        ,$q_cmd);
170         if(!$this->acl_is_readable("query")){
171           print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),"query"));
172           $mailQueueParser = new parseMailQueue("",$this->Server);
173         }else{
175           /* Only display this if the query cmd is executeable */
176           if($str = @$this->pass_cmd ($q_cmd)){
177             /* Parse returned data */
178             $mailQueueParser = new parseMailQueue($str,$this->Server);
179           }else{
180             /* On error/ no return value / false return value */
181             print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
182             $error = true;
183           }
184         }
185       }else{
186         $mailQueueParser = NULL;
187         foreach($this->getServer() as $ServerID=>$ServerName){
189           /* Don't query the server named all :) */
190           if($ServerID == "all") continue;
192           /* Prepare query cmd */
193           $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
194           $q_cmd = preg_replace("/%server/" ,$ServerName  ,$q_cmd);
195           $q_cmd = preg_replace("/%id/"     ,"ALL"           ,$q_cmd);
197           
198           if(!$this->acl_is_readable("query")){
199             print_red(sprintf(_("You do not have permission to execute the command '%s' on the mailqueue."),"query"));
200             $mailQueueParser = new parseMailQueue("",$this->Server);
201           }else{
203             /* Shell exec this cmd */
204             if($str = @$this->pass_cmd ($q_cmd)){
206               /* If there is no parser available, create one  */
207               if($mailQueueParser == NULL){
208                 $mailQueueParser = new parseMailQueue($str,$ServerID);
209               }else{
210                 $mailQueueParser->parseAdditionalQueue($str,$ServerID);
211               }
212               /* On error/ no return value / false return value */
213             }else{
214               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
215               $error = true;
216             }
217           }
218         }
219       }     
221       /* Check for existing servers 
222           objectClass=goMailServer is required at least for one server. 
223          Else display error   */
224       $server = $this->getServer();
225       if((count($server) == 1 ) && (isset($server['all']))){
226         print_red(_("There are no mail server specified."));
227         $error = true;
228       }
230       if(!$error){
232         /* Filter data with the given */
233         $mailQueueParser->OrderBy($this->OrderBy,$this->SortType);
234         $mailQueueParser->OnlyDaysAgo($this->Time);
235         $mailQueueParser->CreateDate();
236      
237         if($this->Stat == "hold"){
238           $mailQueueParser->Search(true,array("Hold"),true);
239         }
240         if($this->Stat == "unhold"){
241           $mailQueueParser->Search(false,array("Hold"),true);
242         }
243         if($this->Stat == "active"){
244           $mailQueueParser->Search(true,array("Active"),true);
245         }
246         if($this->Stat == "nonactive"){
247           $mailQueueParser->Search(false,array("Active"),true);
248         }
250         $mailQueueParser->Search($this->Search,array("MailID","Size","Sender","Recipient","Error","Arrival"));
252         /* */
253         $entries = $mailQueueParser->GetAll();
254   
255         if(count($entries) ==0 ){
256           $smarty->assign("all_ok",false);
257         }
258  
259         $smarty->assign("entries"       , array_slice($entries,$this->Page,$this->range));
260         $smarty->assign("plug"          , "?plug=".$_GET['plug']);
261         $smarty->assign("r_stats"       , $this->getStats());
262         $smarty->assign("stats"         , array_flip($this->getStats()));
263         $smarty->assign("stat"          , $this->Stat);
264         $smarty->assign("p_server"      , $this->Server);
265         $smarty->assign("p_servers"     , $this->getServer());
266         $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
267         $smarty->assign("p_time"        , $this->Time);
268         $smarty->assign("p_times"       , $this->getTimes());
269         $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
270         $smarty->assign("search_for"    , $this->Search);
271         $smarty->assign("range_selector", range_selector(count($entries), $this->Page, $this->range,"EntriesPerPage")); 
272         $smarty->assign("OrderBy"       , $this->OrderBy);
274         /* Display sort arrow */
275         if($this->SortType == "up"){
276           $smarty->assign("SortType","<img src='images/sort_up.png' alt='"._("up")."' border='0'>");
277         }else{
278           $smarty->assign("SortType","<img src='images/sort_down.png' alt='"._("down")."' border='0'>");
279         }
280       }
281     }
283     /* In case of an error */
284     if($error){  
285       $smarty->assign("all_ok"        , "false");
286       $smarty->assign("r_stats"       , $this->getStats());
287       $smarty->assign("stats"         , array_flip($this->getStats()));
288       $smarty->assign("stat"          , $this->Stat);
289       $smarty->assign("plug"          , "?plug=".$_GET['plug']);
290       $smarty->assign("p_server"      , $this->Server);
291       $smarty->assign("p_servers"     , $this->getServer());
292       $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
293       $smarty->assign("p_time"        , $this->Time);
294       $smarty->assign("p_times"       , $this->getTimes());
295       $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
296       $smarty->assign("search_for"    , $this->Search);
297       $smarty->assign("OrderBy"       , $this->OrderBy);
298     }
299     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
300   }
303   /* return selectable server */
304   function getServer()
305   {
306     $ret= array("all"=>_("All"));
307     $ldap = $this->config->get_ldap_link();
308     $ldap->cd($this->config->current['BASE']);    
309     $ldap->search("(objectClass=goMailServer)",array("cn"));
310     while($tmp = $ldap->fetch()){
311       $ret[$tmp['cn'][0]]= $tmp['cn'][0];
312     }
313     return($ret);
314   }
317   /* Return selectable times*/
318   function getTimes()
319   {
320     $ret = array();
321     $ret['nolimit']=_("no limit"); 
322     foreach(array(1,2,4,8,12,24,36,48) as $i){
323       if($i == 1){
324         $ret[$i] = $i."&nbsp;"._("hour");
325       }else{
326         $ret[$i] = $i."&nbsp;"._("hours");
327       }
328     }
329     return($ret);
330   }
333   /* Save post values*/
334   function save_object($save_current= FALSE)
335   {
336     if(isset($_POST['p_server'])){
337       $this->Server = $_POST['p_server'];
338     }
339     if(isset($_POST['p_time'])){
340       $this->Time = $_POST['p_time'];
341     }
342     if(isset($_POST['search_for'])){
343       $this->Search = $_POST['search_for'];
344     }
345     if(isset($_POST['Stat'])){
346       $this->Stat = $_POST['Stat'];
347     }
348     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
349       $this->Page = $_GET['start'];
350     }
352     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
353       $old = $this->OrderBy;
354       $this->OrderBy = $_GET['sort'];
355       if($this->OrderBy == $old)
356       {
357         if($this->SortType== "up"){
358           $this->SortType = "down";
359         }else{
360           $this->SortType = "up";
361         }
362       }
363     }
365   }
367   /* Return stats */
368   function getStats()
369   {
370     return(array(
371           "all"     =>_("All"),
372           "hold"    =>_("Hold"),
373           "unhold"  =>_("Un hold"),
374           "active"  =>_("Active"),
375           "nonactive"  =>_("Not active")
376           ));
377   }
379   /* Return plugin informations for acl handling
380      #FIXME You can only read attributes within this report plugin */
381   function plInfo()
382   {
383     return (array(
384         "plShortName"   => _("Mailqueue"),
385         "plDescription" => _("Mailqueue addon"),
386         "plSelfModify"  => FALSE,
387         "plDepends"     => array(),
388         "plPriority"    => 1,
389         "plSection"     => array("addon"),
390         "plCategory"    => array("mailqueue" => array("objectClass" => "none", "description" => _("Mail queue addon"))),
392         "plProvidedAcls" => array(
393             "unhold_all"      => _("Unhold all messages"),
394             "hold_all"        => _("Hold all messages"),
395             "del_all"         => _("Delete all messages"),
396             "requeue_all"     => _("Requeue all messages"),
397             "unhold"          => _("Unhold message"),
398             "hold"            => _("Hold message"),
399             "del"             => _("Delete message"),
400             "requeue"         => _("Requeue message"),
401             "query"           => _("Gathering queue data"),
402             "header"          => _("Get header information")
403           )
404         ));
405   }
410 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
411 ?>