Code

Added requirement dependencies
[gosa.git] / gosa-plugins / mail / addons / mailqueue / class_mailqueue.inc
1 <?php
3 class mailqueue extends plugin
4 {
5   /* Definitions */
6   var $plHeadline     = "Mail queue";
7   var $plDescription  = "View and control the mailservers mail processing queue";
8   var $plIcon         = "plugins/mail/images/mailqueue.png";
10   /* attribute list for save action */
11   var $attributes     = array();
12   var $objectclasses  = array();
14   var $Server         = "all";
15   var $ServerList     = array(); // The list of all available servers.
16   var $Search         = "*";
17   var $Time           = 0;
18   var $Page           = 0;
19   var $Stat           = "all";
20   var $OrderBy        = "Arrival";
21   var $SortType       = "up";
22   var $disp_header    = false;
23   var $range          = 20;   
25   /* Logging detection */
26   var $view_logged    = FALSE;
28   function mailqueue(&$config, $dn= NULL)
29   {
30     $this->config   = &$config;
31     $this->si_queue = new si_mailqueue($this->config);
32     $this->getServer();
33   }
36   function execute()
37   {
38     /* Call parent execute */
39     plugin::execute();
41     /* Log view */
42     if(!$this->view_logged){
43       $this->view_logged = TRUE;
44       new log("view","mailqueue/".get_class($this),$this->dn);
45     }
47     $smarty= get_smarty();
48     $tmp = $this->plInfo();
49     foreach($tmp['plProvidedAcls'] as $name => $desc){
50       $smarty->assign($name."ACL",$this->getacl($name));
51       $smarty->assign($name."_W",$this->acl_is_writeable($name));
52     }
53     $error =false;
55     /******************
56       Handle options 
57      ******************/
59     $action = $server = $entry = "";
60     $types = array( 
61         "all_del"     => "del",
62         "all_hold"    => "hold",
63         "all_unhold"  => "unhold",
64         "all_requeue" => "requeue");
65     foreach($_POST as $name => $value){
66       foreach($types as $type => $target){
67         if(preg_match("/^".$type."/",$name) && $this->acl_is_writeable($target."All")){
68           $entry  = $this->list_get_selected_items();
69           $action = $target;
70           break;
71         }
72       }
73       if(!empty($action)) break;
74     }
76     $types = array("del","hold","unhold","header","requeue");
77     foreach($_POST as $name => $value){
78       foreach($types as $type){
79         if(preg_match("/^".$type."__/",$name) && $this->acl_is_writeable($type)){
80           $action = $type;
81           $server = preg_replace("/^".$type."__[^_]*__([^_]*)$/","\\1",$name); 
82           $entry[$server][] = preg_replace("/^".$type."__([^_]*)__.*/","\\1",$name); 
83           break;
84         }
85       }
86       if(!empty($action)) break;
87     }
89     /* Send action for given mail id */
90     if(in_array($action,array("del","hold","unhold","requeue"))){
91       foreach($entry as $server => $entries){
92         $this->si_queue->send_queue_action($entries,$server,$action);
93       }
94     }
97     /******************
98       Display mail header
99      ******************/
101     if($action == "header"){
102       $server = key($entry);
103       $entry = $entry[$server];
105       /* Create table which displays the header informations */
106       $data = $this->si_queue->header($entry,$server);
107       $data = preg_replace("/([^\s]*:)/","\n\\1",$data);
108       $this->disp_header = $data;
109       if($this->si_queue->is_error()){
110         msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
111         $this->disp_header = FALSE;
112       }
113     }
115     /* Back is posted from the header display page */
116     if(isset($_POST['back'])){
117       $this->disp_header = false;
118     }
120     /* If there is a header in disp_header, then display it */
121     if($this->disp_header){
122       $smarty->assign("header",$this->disp_header);
123       return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
124     }
127     /******************
128       Query mailqueues 
129      ******************/
131     $entries = array();
132     if($this->acl_is_readable("query")){
133       $within_minutes = -1;
134       if($this->Time != "nolimit"){
135         $within_minutes = 60*60*$this->Time;
136       }
138       if($this->Server == "all"){
139         $entries = array();
140         foreach($this->ServerList as $mac => $name){
141           if(!tests::is_mac($mac)) continue;
142           $entries = array_merge($entries,$this->si_queue->query_mailqueue($mac,$this->Search,$within_minutes));
143           if($this->si_queue->is_error()){
144             msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
145           }
146         }
147       }else{
148         $entries = $this->si_queue->query_mailqueue($this->Server,$this->Search,$within_minutes);
149         if($this->si_queue->is_error()){
150           msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
151         }
152       }
153     }
155     /* Sort entries 
156      */ 
157     $data = array();
158     foreach($entries as $entry){
159       $data[uniqid($entry[$this->OrderBy])] = $entry;
160     }
162     /* Sort entries by given direction 
163      */
164     if($this->SortType == "down"){
165       uksort($data, 'strnatcasecmp');
166     }else{
167       uksort($data, 'strnatcasecmp');
168       $data = array_reverse($data);
169     }
171     $count = count($data);
172     $entries = array_slice($data,$this->Page,$this->range);
174     /* Add ServerName to results 
175      */
176     foreach($entries as $key => $data){
177       $entries[$key]['ServerName'] = $this->ServerList[$data['Server']];
178     }   
179  
180     /******************
181       create html output 
182      ******************/
184     $smarty->assign("query_allowed",$this->acl_is_readable("query"));
185     $smarty->assign("all_ok"        , count($entries));
186     $smarty->assign("entries"       , $entries);
187     $smarty->assign("plug"          , "?plug=".$_GET['plug']);
189     $smarty->assign("r_stats"       , $this->getStats());
190     $smarty->assign("stats"         , array_flip($this->getStats()));
192     $smarty->assign("stat"          , $this->Stat);
193     $smarty->assign("p_server"      , $this->Server);
194     $smarty->assign("p_servers"     , $this->ServerList);
195     $smarty->assign("p_serverKeys"  , array_flip($this->ServerList));
196     $smarty->assign("p_time"        , $this->Time);
197     $smarty->assign("p_times"       , $this->getTimes());
198     $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
199     $smarty->assign("search_for"    , $this->Search);
200     $smarty->assign("range_selector", range_selector($count, $this->Page, $this->range,"EntriesPerPage")); 
201     $smarty->assign("OrderBy"       , $this->OrderBy);
203     /* Display sort arrow */
204     if($this->SortType == "up"){
205       $smarty->assign("SortType","<img src='images/lists/sort-up.png' alt='"._("up")."' border='0'>");
206     }else{
207       $smarty->assign("SortType","<img src='images/lists/sort-down.png' alt='"._("down")."' border='0'>");
208     }
210     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
211   }
214   /* return selectable server 
215    */
216   function getServer()
217   {
218     $ret= array("all"=>_("All"));
220     /* First of all, detect all servers that supports the mailqueue extension 
221         -If this fails, the mailqueue(s) can't be queried.
222      */
223     $hosts          = $this->si_queue->get_hosts_with_module("mailqueue_com");
224     $this->si_error = $this->si_queue->is_error();
225     if(!count($hosts)){
226       return(array());
227     }    
229     /* Create search filter and try to resolv mac to hostname 
230      */
231     $filter = "";
232     foreach($hosts as $mac){
233       $filter .= "(macAddress=".$mac.")";
234     }
235     $filter = "(&(objectClass=GOhard)(|".$filter."))";
236     $res = get_list($filter,"no_acls",$this->config->current['BASE'],
237         array("cn","macAddress"),GL_SUBSEARCH | GL_NO_ACL_CHECK); 
239     /* Create result array 
240      */
241     foreach($hosts as $mac){
242       $found = FALSE;
243       foreach($res as $entry){
244         if(preg_match("/^".preg_quote($mac, '/')."$/i",$entry['macAddress'][0])){
245           $ret[$mac] = $entry['cn'][0];
246           $found = TRUE;
247           break;
248         }
249       }
250       if(!$found){
251         $ret[$mac] = $mac;
252       }
253     }
254     $this->ServerList = $ret;
255   }
258   /* Return selectable times*/
259   function getTimes()
260   {
261     $ret = array();
262     $ret['nolimit']=_("no limit"); 
263     foreach(array(1,2,4,8,12,24,36,48) as $i){
264       if($i == 1){
265         $ret[$i] = $i."&nbsp;"._("hour");
266       }else{
267         $ret[$i] = $i."&nbsp;"._("hours");
268       }
269     }
270     return($ret);
271   }
274   /* Save post values*/
275   function save_object($save_current= FALSE)
276   {
277     /* Update amount of entries displayed */
278     if(isset($_POST['EntriesPerPage'])){
279       $this->range = $_POST['EntriesPerPage'];
280     }
282     if(isset($_POST['p_server']) && isset($this->ServerList[$_POST['p_server']])){
283       $this->Server = $_POST['p_server'];
284     }
286     if(isset($_POST['p_time'])){
287       $this->Time = $_POST['p_time'];
288     }
289     if(isset($_POST['search_for'])){
290       $this->Search = $_POST['search_for'];
291     }
292     if(isset($_POST['Stat'])){
293       $this->Stat = $_POST['Stat'];
294     }
295     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
296       $this->Page = $_GET['start'];
297     }
299     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
300       $old = $this->OrderBy;
301       $this->OrderBy = $_GET['sort'];
302       if($this->OrderBy == $old)
303       {
304         if($this->SortType== "up"){
305           $this->SortType = "down";
306         }else{
307           $this->SortType = "up";
308         }
309       }
310     }
312   }
314   /* Return stats */
315   function getStats()
316   {
317     return(array(
318           "all"     =>_("All"),
319           "hold"    =>_("Hold"),
320           "unhold"  =>_("Un hold"),
321           "active"  =>_("Active"),
322           "nonactive"  =>_("Not active")
323           ));
324   }
326   /* Return plugin informations for acl handling
327      #FIXME You can only read attributes within this report plugin */
328   static function plInfo()
329   {
330     return (array(
331         "plShortName"   => _("Mail queue"),
332         "plDescription" => _("Mail queue addon"),
333         "plSelfModify"  => FALSE,
334         "plDepends"     => array(),
335         "plPriority"    => 1,
336         "plSection"     => array("addon"),
337         "plCategory"    => array("mailqueue" => array("description" => _("Mail queue addon"))),
339         "plProvidedAcls" => array(
340             "unholdAll"       => _("Unhold all messages"),
341             "holdAll"         => _("Hold all messages"),
342             "delAll"          => _("Delete all messages"),
343             "requeueAll"      => _("Requeue all messages"),
344             "unhold"          => _("Unhold message"),
345             "hold"            => _("Hold message"),
346             "del"             => _("Delete message"),
347             "requeue"         => _("Requeue message"),
348             "query"           => _("Gathering queue data"),
349             "header"          => _("Get header information")
350           )
351         ));
352   }
354   function list_get_selected_items()
355   {
356     $ids = array();
357     foreach($_POST as $name => $value){
358       if(preg_match("/^selected_*/",$name)){
359         $server = preg_replace("/^selected_.*_/","",$name) ;
360         $ids[$server][] = preg_replace("/^selected_([^_]*)_.*$/","\\1",$name);
361       }
362     }
363     return($ids);
364   }
369 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
370 ?>