Code

Backport from trunk
[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();
34     // Create statistic table entry
35     $this->initTime = microtime(TRUE);
36     stats::log('plugin', $class = get_class($this), $category = array($this->acl_category),  $action = 'open',
37             $amount = 1, $duration = (microtime(TRUE) - $this->initTime));
39   }
42   function execute()
43   {
44     /* Call parent execute */
45     plugin::execute();
47     /* Log view */
48     if(!$this->view_logged){
49       $this->view_logged = TRUE;
50       new log("view","mailqueue/".get_class($this),$this->dn);
51     }
53     $smarty= get_smarty();
54     $tmp = $this->plInfo();
55     foreach($tmp['plProvidedAcls'] as $name => $desc){
56       $smarty->assign($name."ACL",$this->getacl($name));
57       $smarty->assign($name."_W",$this->acl_is_writeable($name));
58     }
59     $error =false;
61     /******************
62       Handle options 
63      ******************/
65     $action = $server = $entry = "";
66     $types = array( 
67         "all_del"     => "del",
68         "all_hold"    => "hold",
69         "all_unhold"  => "unhold",
70         "all_requeue" => "requeue");
71     foreach($_POST as $name => $value){
72       foreach($types as $type => $target){
73         if(preg_match("/^".$type."/",$name) && $this->acl_is_writeable($target."All")){
74           $entry  = $this->list_get_selected_items();
75           $action = $target;
76           break;
77         }
78       }
79       if(!empty($action)) break;
80     }
82     $types = array("del","hold","unhold","header","requeue");
83     foreach($_POST as $name => $value){
84       foreach($types as $type){
85         if(preg_match("/^".$type."__/",$name) && $this->acl_is_writeable($type)){
86           $action = $type;
87           $server = preg_replace("/^".$type."__[^_]*__([^_]*)$/","\\1",$name); 
88           $entry[$server][] = preg_replace("/^".$type."__([^_]*)__.*/","\\1",$name); 
89           break;
90         }
91       }
92       if(!empty($action)) break;
93     }
95     /* Send action for given mail id */
96     if(in_array_strict($action,array("del","hold","unhold","requeue"))){
97       foreach($entry as $server => $entries){
98         $this->si_queue->send_queue_action($entries,$server,$action);
99       }
100     }
103     /******************
104       Display mail header
105      ******************/
107     if($action == "header"){
108       $server = key($entry);
109       $entry = $entry[$server];
111       /* Create table which displays the header informations */
112       $data = $this->si_queue->header($entry,$server);
113       $data = preg_replace("/([^\s]*:)/","\n\\1",$data);
114       $this->disp_header = $data;
115       if($this->si_queue->is_error()){
116         msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
117         $this->disp_header = FALSE;
118       }
119     }
121     /* Back is posted from the header display page */
122     if(isset($_POST['back'])){
123       $this->disp_header = false;
124     }
126     /* If there is a header in disp_header, then display it */
127     if($this->disp_header){
128       $smarty->assign("header",$this->disp_header);
129       return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
130     }
133     /******************
134       Query mailqueues 
135      ******************/
137     $entries = array();
138     if($this->acl_is_readable("query")){
139       $within_minutes = -1;
140       if($this->Time != "nolimit"){
141         $within_minutes = 60*60*$this->Time;
142       }
144       if($this->Server == "all"){
145         $entries = array();
146         foreach($this->ServerList as $mac => $name){
147           if(!tests::is_mac($mac)) continue;
148           $entries = array_merge($entries,$this->si_queue->query_mailqueue($mac,$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       }else{
154         $entries = $this->si_queue->query_mailqueue($this->Server,$this->Search,$within_minutes);
155         if($this->si_queue->is_error()){
156           msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
157         }
158       }
159     }
161     /* Sort entries 
162      */ 
163     $data = array();
164     foreach($entries as $entry){
165       $data[uniqid($entry[$this->OrderBy])] = $entry;
166     }
168     /* Sort entries by given direction 
169      */
170     if($this->SortType == "down"){
171       uksort($data, 'strnatcasecmp');
172     }else{
173       uksort($data, 'strnatcasecmp');
174       $data = array_reverse($data);
175     }
177     $count = count($data);
178     $entries = array_slice($data,$this->Page,$this->range);
180     /* Add ServerName to results 
181      */
182     foreach($entries as $key => $data){
183       $entries[$key]['ServerName'] = $this->ServerList[$data['Server']];
184     }   
185  
186     /******************
187       create html output 
188      ******************/
190     $smarty->assign("query_allowed",$this->acl_is_readable("query"));
191     $smarty->assign("all_ok"        , count($entries));
192     $smarty->assign("entries"       , $entries);
193     $smarty->assign("plug"          , "?plug=".$_GET['plug']);
195     $smarty->assign("r_stats"       , $this->getStats());
196     $smarty->assign("stats"         , array_flip($this->getStats()));
198     $smarty->assign("stat"          , $this->Stat);
199     $smarty->assign("p_server"      , set_post($this->Server));
200     $smarty->assign("p_servers"     , set_post($this->ServerList));
201     $smarty->assign("p_serverKeys"  , set_post(array_flip($this->ServerList)));
202     $smarty->assign("p_time"        , $this->Time);
203     $smarty->assign("p_times"       , $this->getTimes());
204     $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
205     $smarty->assign("search_for"    , set_post($this->Search));
206     $smarty->assign("range_selector", range_selector($count, $this->Page, $this->range,"EntriesPerPage")); 
207     $smarty->assign("OrderBy"       , set_post($this->OrderBy));
209     /* Display sort arrow */
210     if($this->SortType == "up"){
211       $smarty->assign("SortType","<img src='images/lists/sort-up.png' alt='"._("up")."' border='0'>");
212     }else{
213       $smarty->assign("SortType","<img src='images/lists/sort-down.png' alt='"._("down")."' border='0'>");
214     }
216     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
217   }
220   /* return selectable server 
221    */
222   function getServer()
223   {
224     $ret= array("all"=>_("All"));
226     /* First of all, detect all servers that supports the mailqueue extension 
227         -If this fails, the mailqueue(s) can't be queried.
228      */
229     $hosts          = $this->si_queue->get_hosts_with_module("mailqueue_com");
230     $this->si_error = $this->si_queue->is_error();
231     if(!count($hosts)){
232       return(array());
233     }    
235     /* Create search filter and try to resolv mac to hostname 
236      */
237     $filter = "";
238     foreach($hosts as $mac){
239       $filter .= "(macAddress=".$mac.")";
240     }
241     $filter = "(&(objectClass=GOhard)(|".$filter."))";
242     $res = get_list($filter,"no_acls",$this->config->current['BASE'],
243         array("cn","macAddress"),GL_SUBSEARCH | GL_NO_ACL_CHECK); 
245     /* Create result array 
246      */
247     foreach($hosts as $mac){
248       $found = FALSE;
249       foreach($res as $entry){
250         if(preg_match("/^".preg_quote($mac, '/')."$/i",$entry['macAddress'][0])){
251           $ret[$mac] = $entry['cn'][0];
252           $found = TRUE;
253           break;
254         }
255       }
256       if(!$found){
257         $ret[$mac] = $mac;
258       }
259     }
260     $this->ServerList = $ret;
261   }
264   /* Return selectable times*/
265   function getTimes()
266   {
267     $ret = array();
268     $ret['nolimit']=_("no limit"); 
269     foreach(array(1,2,4,8,12,24,36,48) as $i){
270       if($i == 1){
271         $ret[$i] = $i."&nbsp;"._("hour");
272       }else{
273         $ret[$i] = $i."&nbsp;"._("hours");
274       }
275     }
276     return($ret);
277   }
280   /* Save post values*/
281   function save_object($save_current= FALSE)
282   {
283     /* Update amount of entries displayed */
284     if(isset($_POST['EntriesPerPage'])){
285       $this->range = get_post('EntriesPerPage');
286     }
288     if(isset($_POST['p_server']) && isset($this->ServerList[$_POST['p_server']])){
289       $this->Server = get_post('p_server');
290     }
292     if(isset($_POST['p_time'])){
293       $this->Time = get_post('p_time');
294     }
295     if(isset($_POST['search_for'])){
296       $this->Search = get_post('search_for');
297     }
298     if(isset($_POST['Stat'])){
299       $this->Stat = get_post('Stat');
300     }
301     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
302       $this->Page = $_GET['start'];
303     }
305     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
306       $old = $this->OrderBy;
307       $this->OrderBy = $_GET['sort'];
308       if($this->OrderBy == $old)
309       {
310         if($this->SortType== "up"){
311           $this->SortType = "down";
312         }else{
313           $this->SortType = "up";
314         }
315       }
316     }
318   }
320   /* Return stats */
321   function getStats()
322   {
323     return(array(
324           "all"     =>_("All"),
325           "hold"    =>_("Hold"),
326           "unhold"  =>_("Release"),
327           "active"  =>_("Active"),
328           "nonactive"  =>_("Not active")
329           ));
330   }
332   /* Return plugin informations for acl handling
333      #FIXME You can only read attributes within this report plugin */
334   static function plInfo()
335   {
336     return (array(
337         "plShortName"   => _("Mail queue"),
338         "plDescription" => _("Mail queue add-on"),
339         "plSelfModify"  => FALSE,
340         "plDepends"     => array(),
341         "plPriority"    => 1,
342         "plSection"     => array("addon"),
343         "plCategory"    => array("mailqueue" => array("description" => _("Mail queue add-on"))),
345         "plProvidedAcls" => array(
346             "unholdAll"       => _("Release all messages"),
347             "holdAll"         => _("Hold all messages"),
348             "delAll"          => _("Delete all messages"),
349             "requeueAll"      => _("Re-queue all messages"),
350             "unhold"          => _("Release message"),
351             "hold"            => _("Hold message"),
352             "del"             => _("Delete message"),
353             "requeue"         => _("Re-queue message"),
354             "query"           => _("Gathering queue data"),
355             "header"          => _("Get header information")
356           )
357         ));
358   }
360   function list_get_selected_items()
361   {
362     $ids = array();
363     foreach($_POST as $name => $value){
364       if(preg_match("/^selected_*/",$name)){
365         $server = preg_replace("/^selected_.*_/","",$name) ;
366         $ids[$server][] = preg_replace("/^selected_([^_]*)_.*$/","\\1",$name);
367       }
368     }
369     return($ids);
370   }
375 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
376 ?>