Code

Added apply button
[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(chkacl($this->acl,$attr)!=""){
84               $only_once = false;
85               #FIXME Please choose a better error msg, if release is released
86               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$attr));
87             }else{
88               $only_once = false;
89               $act = preg_replace("/_.*$/i","",$attr);
90               $r_cmd = preg_replace("/%action/" , $act           ,$this->mailQueueScript);
91               $r_cmd = preg_replace("/%server/" , $se_str         ,$r_cmd);
92               $r_cmd = preg_replace("/%id/"     , "ALL"           ,$r_cmd);
93               if($this->pass_cmd($r_cmd)==false){
94                 print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$r_cmd));
95               }
96             }
97           }
98         }
99       }        
101       /* Check single entry manipulation  posts */
102       $only_once = true;
104       /* act specifies the command to execute */
105       if(isset($_GET['act'])){
106         $opt = $_GET['act'];  
107   
108         /* The option to exec should be one of these */
109         if(in_array($opt,array("unhold","hold","del","requeue","query","header"))){
110           $only_once = false;
112           if(chkacl($this->acl,$opt)!=""){
113               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$opt));
114           }else{
115             /* Create cmd */
116             $r_cmd = preg_replace("/%action/" , $opt            ,$this->mailQueueScript);
117             $r_cmd = preg_replace("/%server/" , $this->Server   ,$r_cmd);
118             $r_cmd = preg_replace("/%id/"     , $_GET['id']     ,$r_cmd);
120             /* Execute cmd */
121             if(!$str = $this->pass_cmd($r_cmd)){
122               print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$r_cmd));
123             }else{
124             
125               /* Special handling for option='header' */
126               if($opt == "header"){
127           
128                 /* Create table which displays the header informations */
129                 $this->disp_header ="\n<table width='80%'>";
130                 foreach(split("\n",$str) as $line){
131                   $line = trim($line);
132                   if(empty($line)) {
133                     continue;
134                   }
135                   $this->disp_header .= "\n<tr>";
136                   $tmp0 = preg_replace("/:.*$/","",$line);
137                   $tmp1 = preg_replace("/^.*:/","",$line);
138                   $this->disp_header .= "\n<td style='background-color:#EEEEEE;'>".$tmp0."</td><td>".$tmp1."</td>"; 
139                   $this->disp_header .= "\n</tr>";
140                 }              
141                 $this->disp_header .= "\n</table>";
142               }
143             }
144           }
145         }
146       }        
149       /* Back is posted from the header display page */
150       if(isset($_POST['back'])){
151         $this->disp_header = false;
152       }
154       /* If there is a header in disp_header, then display it */
155       if($this->disp_header){
156         $smarty->assign("header",$this->disp_header);
157         return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
158       }
160       /* tell smarty to display the search results*/
161       $smarty->assign("all_ok"      , "true");
162       
163       /* A single server is selected */
164       if($this->Server != "all"){    
166         /* Create Query cmd */ 
167         $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
168         $q_cmd = preg_replace("/%server/" ,$this->Server,$q_cmd);
169         $q_cmd = preg_replace("/%id/"     ,"all"        ,$q_cmd);
171         /* Only display this if the query cmd is executeable */
172         if($str = @$this->pass_cmd ($q_cmd)){
173           /* Parse returned data */
174           $mailQueueParser = new parseMailQueue($str,$this->Server);
175         }else{
176           /* On error/ no return value / false return value */
177           print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
178           $error = true;
179         }
181       }else{
182         $mailQueueParser = NULL;
183         foreach($this->getServer() as $ServerID=>$ServerName){
184   
185           /* Don't query the server named all :) */
186           if($ServerID == "all") continue;
188           /* Prepare query cmd */
189           $q_cmd = preg_replace("/%action/" ,"query"      ,$this->mailQueueScript);
190           $q_cmd = preg_replace("/%server/" ,$ServerName  ,$q_cmd);
191           $q_cmd = preg_replace("/%id/"     ,"ALL"           ,$q_cmd);
193           /* Shell exec this cmd */
194           if($str = @$this->pass_cmd ($q_cmd)){
195     
196             /* If there is no parser available, create one  */
197             if($mailQueueParser == NULL){
198               $mailQueueParser = new parseMailQueue($str,$ServerID);
199             }else{
200               $mailQueueParser->parseAdditionalQueue($str,$ServerID);
201             }
202           /* On error/ no return value / false return value */
203           }else{
204             print_red(sprintf(_("Please check your 'gosa.conf' the given '%s' can't be executed."),$q_cmd));
205             $error = true;
206           }
207         }
209       }     
211       /* Check for existing servers 
212           objectClass=goMailServer is required at least for one server. 
213          Else display error   */
214       $server = $this->getServer();
215       if((count($server) == 1 ) && (isset($server['all']))){
216         print_red(_("There are no mail server specified."));
217         $error = true;
218       }
220       if(!$error){
222         /* Filter data with the given */
223         $mailQueueParser->OrderBy($this->OrderBy,$this->SortType);
224         $mailQueueParser->OnlyDaysAgo($this->Time);
225         $mailQueueParser->CreateDate();
226      
227         if($this->Stat == "hold"){
228           $mailQueueParser->Search(true,array("Hold"),true);
229         }
230         if($this->Stat == "unhold"){
231           $mailQueueParser->Search(false,array("Hold"),true);
232         }
233         if($this->Stat == "active"){
234           $mailQueueParser->Search(true,array("Active"),true);
235         }
236         if($this->Stat == "nonactive"){
237           $mailQueueParser->Search(false,array("Active"),true);
238         }
240         $mailQueueParser->Search($this->Search,array("MailID","Size","Sender","Recipient","Error","Arrival"));
242         /* */
243         $entries = $mailQueueParser->GetAll();
244   
245         if(count($entries) ==0 ){
246           $smarty->assign("all_ok",false);
247         }
248  
249         $smarty->assign("entries"       , array_slice($entries,$this->Page,$this->range));
250         $smarty->assign("plug"          , "?plug=".$_GET['plug']);
251         $smarty->assign("r_stats"       , $this->getStats());
252         $smarty->assign("stats"         , array_flip($this->getStats()));
253         $smarty->assign("stat"          , $this->Stat);
254         $smarty->assign("p_server"      , $this->Server);
255         $smarty->assign("p_servers"     , $this->getServer());
256         $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
257         $smarty->assign("p_time"        , $this->Time);
258         $smarty->assign("p_times"       , $this->getTimes());
259         $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
260         $smarty->assign("search_for"    , $this->Search);
261         $smarty->assign("range_selector", range_selector(count($entries), $this->Page, $this->range,"EntriesPerPage")); 
262         $smarty->assign("OrderBy"       , $this->OrderBy);
264         /* Display sort arrow */
265         if($this->SortType == "up"){
266           $smarty->assign("SortType","<img src='images/sort_up.png' alt='"._("up")."' border='0'>");
267         }else{
268           $smarty->assign("SortType","<img src='images/sort_down.png' alt='"._("down")."' border='0'>");
269         }
270       }
271     }
273     /* In case of an error */
274     if($error){  
275       $smarty->assign("all_ok"        , "false");
276       $smarty->assign("r_stats"       , $this->getStats());
277       $smarty->assign("stats"         , array_flip($this->getStats()));
278       $smarty->assign("stat"          , $this->Stat);
279       $smarty->assign("plug"          , "?plug=".$_GET['plug']);
280       $smarty->assign("p_server"      , $this->Server);
281       $smarty->assign("p_servers"     , $this->getServer());
282       $smarty->assign("p_serverKeys"  , array_flip($this->getServer()));
283       $smarty->assign("p_time"        , $this->Time);
284       $smarty->assign("p_times"       , $this->getTimes());
285       $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
286       $smarty->assign("search_for"    , $this->Search);
287       $smarty->assign("OrderBy"       , $this->OrderBy);
288     }
289     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
290   }
293   /* return selectable server */
294   function getServer()
295   {
296     $ret= array("all"=>_("All"));
297     $ldap = $this->config->get_ldap_link();
298     $ldap->cd($this->config->current['BASE']);    
299     $ldap->search("(objectClass=goMailServer)",array("cn"));
300     while($tmp = $ldap->fetch()){
301       $ret[$tmp['cn'][0]]= $tmp['cn'][0];
302     }
303     return($ret);
304   }
307   /* Return selectable times*/
308   function getTimes()
309   {
310     $ret = array();
311     $ret['nolimit']=_("no limit"); 
312     foreach(array(1,2,4,8,12,24,36,48) as $i){
313       if($i == 1){
314         $ret[$i] = $i."&nbsp;"._("hour");
315       }else{
316         $ret[$i] = $i."&nbsp;"._("hours");
317       }
318     }
319     return($ret);
320   }
323   /* Save post values*/
324   function save_object($save_current= FALSE)
325   {
326     if(isset($_POST['p_server'])){
327       $this->Server = $_POST['p_server'];
328     }
329     if(isset($_POST['p_time'])){
330       $this->Time = $_POST['p_time'];
331     }
332     if(isset($_POST['search_for'])){
333       $this->Search = $_POST['search_for'];
334     }
335     if(isset($_POST['Stat'])){
336       $this->Stat = $_POST['Stat'];
337     }
338     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
339       $this->Page = $_GET['start'];
340     }
342     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
343       $old = $this->OrderBy;
344       $this->OrderBy = $_GET['sort'];
345       if($this->OrderBy == $old)
346       {
347         if($this->SortType== "up"){
348           $this->SortType = "down";
349         }else{
350           $this->SortType = "up";
351         }
352       }
353     }
355   }
357   /* Return stats */
358   function getStats()
359   {
360     return(array(
361           "all"     =>_("All"),
362           "hold"    =>_("Hold"),
363           "unhold"  =>_("Un hold"),
364           "active"  =>_("Active"),
365           "nonactive"  =>_("Not active")
366           ));
367   }
370 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
371 ?>