Code

Added branches container for old stuff
[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  = "This does something";
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     $error =false;
50     /******************
51       Handle options 
52      ******************/
54     $action = $server = $entry = "";
55     $types = array( 
56         "all_del"     => "del",
57         "all_hold"    => "hold",
58         "all_unhold"  => "unhold",
59         "all_requeue" => "requeue");
60     foreach($_POST as $name => $value){
61       foreach($types as $type => $target){
62         if(preg_match("/^".$type."/",$name)){
63           $entry  = $this->list_get_selected_items();
64           $action = $target;
65           break;
66         }
67       }
68       if(!empty($action)) break;
69     }
70   
72     $types = array("del","hold","unhold","header","requeue");
73     foreach($_POST as $name => $value){
74       foreach($types as $type){
75         if(preg_match("/^".$type."__/",$name)){
76           $action = $type;
77           $server = preg_replace("/^".$type."__[^_]*__([^_]*)_.*/","\\1",$name); 
78           $entry[$server][] = preg_replace("/^".$type."__([^_]*)__.*/","\\1",$name); 
79           break;
80         }
81       }
82       if(!empty($action)) break;
83     }
85     /* Send action for given mail id */
86     if(in_array($action,array("del","hold","unhold","requeue"))){
87       foreach($entry as $server => $entries){
88         $this->si_queue->send_queue_action($entries,$server,$action);
89       }
90     }
93     /******************
94       Display mail header
95      ******************/
97     if($action == "header"){
98       $server = key($entry);
99       $entry = $entry[$server];
101       /* Create table which displays the header informations */
102       $data = $this->si_queue->header($entry,$server);
103       $data = preg_replace("/([^\s]*:)/","\n\\1",$data);
104       $this->disp_header = $data;
105       if($this->si_queue->is_error()){
106         msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
107         $this->disp_header = FALSE;
108       }
109     }
111     /* Back is posted from the header display page */
112     if(isset($_POST['back'])){
113       $this->disp_header = false;
114     }
116     /* If there is a header in disp_header, then display it */
117     if($this->disp_header){
118       $smarty->assign("header",$this->disp_header);
119       return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
120     }
123     /******************
124       Query mailqueues 
125      ******************/
127     $within_minutes = -1;
128     if($this->Time != "nolimit"){
129       $within_minutes = 60*60*$this->Time;
130     }
132     if($this->Server == "all"){
133       $entries = array();
134       foreach($this->ServerList as $mac => $name){
135         if(!tests::is_mac($mac)) continue;
136         $entries = array_merge($entries,$this->si_queue->query_mailqueue($mac,$this->Search,$within_minutes));
137         if($this->si_queue->is_error()){
138           msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
139         }
140       }
141     }else{
142       $entries = $this->si_queue->query_mailqueue($this->Server,$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     }
148     /* Sort entries 
149      */ 
150     $data = array();
151     foreach($entries as $entry){
152       $data[uniqid($entry[$this->OrderBy])] = $entry;
153     }
155     /* Sort entries by given direction 
156      */
157     if($this->SortType == "down"){
158       uksort($data, 'strnatcasecmp');
159     }else{
160       uksort($data, 'strnatcasecmp');
161       $data = array_reverse($data);
162     }
164     $count = count($data);
165     $entries = array_slice($data,$this->Page,$this->range);
167     /* Add ServerName to results 
168      */
169     foreach($entries as $key => $data){
170       $entries[$key]['ServerName'] = $this->ServerList[$data['Server']];
171     }   
172  
173     /******************
174       create html output 
175      ******************/
177     $smarty->assign("all_ok"        , count($entries));
178     $smarty->assign("entries"       , $entries);
179     $smarty->assign("plug"          , "?plug=".$_GET['plug']);
181     $smarty->assign("r_stats"       , $this->getStats());
182     $smarty->assign("stats"         , array_flip($this->getStats()));
184     $smarty->assign("stat"          , $this->Stat);
185     $smarty->assign("p_server"      , $this->Server);
186     $smarty->assign("p_servers"     , $this->ServerList);
187     $smarty->assign("p_serverKeys"  , array_flip($this->ServerList));
188     $smarty->assign("p_time"        , $this->Time);
189     $smarty->assign("p_times"       , $this->getTimes());
190     $smarty->assign("p_timeKeys"    , array_flip($this->getTimes()));
191     $smarty->assign("search_for"    , $this->Search);
192     $smarty->assign("range_selector", range_selector($count, $this->Page, $this->range,"EntriesPerPage")); 
193     $smarty->assign("OrderBy"       , $this->OrderBy);
195     /* Display sort arrow */
196     if($this->SortType == "up"){
197       $smarty->assign("SortType","<img src='images/lists/sort-up.png' alt='"._("up")."' border='0'>");
198     }else{
199       $smarty->assign("SortType","<img src='images/lists/sort-down.png' alt='"._("down")."' border='0'>");
200     }
201     return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
202   }
205   /* return selectable server 
206    */
207   function getServer()
208   {
209     $ret= array("all"=>_("All"));
211     /* First of all, detect all servers that supports the mailqueue extension 
212         -If this fails, the mailqueue(s) can't be queried.
213      */
214     $hosts          = $this->si_queue->get_hosts_with_module("mailqueue_com");
215     $this->si_error = $this->si_queue->is_error();
216     if(!count($hosts)){
217       return(array());
218     }    
220     /* Create search filter and try to resolv mac to hostname 
221      */
222     $filter = "";
223     foreach($hosts as $mac){
224       $filter .= "(macAddress=".$mac.")";
225     }
226     $filter = "(&(objectClass=GOhard)(|".$filter."))";
227     $res = get_list($filter,"no_acls",$this->config->current['BASE'],
228         array("cn","macAddress"),GL_SUBSEARCH | GL_NO_ACL_CHECK); 
230     /* Create result array 
231      */
232     foreach($hosts as $mac){
233       $found = FALSE;
234       foreach($res as $entry){
235         if(preg_match("/^".normalizePreg($mac)."$/i",$entry['macAddress'][0])){
236           $ret[$mac] = $entry['cn'][0];
237           $found = TRUE;
238           break;
239         }
240       }
241       if(!$found){
242         $ret[$mac] = $mac;
243       }
244     }
245     $this->ServerList = $ret;
246   }
249   /* Return selectable times*/
250   function getTimes()
251   {
252     $ret = array();
253     $ret['nolimit']=_("no limit"); 
254     foreach(array(1,2,4,8,12,24,36,48) as $i){
255       if($i == 1){
256         $ret[$i] = $i."&nbsp;"._("hour");
257       }else{
258         $ret[$i] = $i."&nbsp;"._("hours");
259       }
260     }
261     return($ret);
262   }
265   /* Save post values*/
266   function save_object($save_current= FALSE)
267   {
268     /* Update amount of entries displayed */
269     if(isset($_POST['EntriesPerPage'])){
270       $this->range = $_POST['EntriesPerPage'];
271     }
273     if(isset($_POST['p_server']) && isset($this->ServerList[$_POST['p_server']])){
274       $this->Server = $_POST['p_server'];
275     }
277     if(isset($_POST['p_time'])){
278       $this->Time = $_POST['p_time'];
279     }
280     if(isset($_POST['search_for'])){
281       $this->Search = $_POST['search_for'];
282     }
283     if(isset($_POST['Stat'])){
284       $this->Stat = $_POST['Stat'];
285     }
286     if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
287       $this->Page = $_GET['start'];
288     }
290     if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
291       $old = $this->OrderBy;
292       $this->OrderBy = $_GET['sort'];
293       if($this->OrderBy == $old)
294       {
295         if($this->SortType== "up"){
296           $this->SortType = "down";
297         }else{
298           $this->SortType = "up";
299         }
300       }
301     }
303   }
305   /* Return stats */
306   function getStats()
307   {
308     return(array(
309           "all"     =>_("All"),
310           "hold"    =>_("Hold"),
311           "unhold"  =>_("Un hold"),
312           "active"  =>_("Active"),
313           "nonactive"  =>_("Not active")
314           ));
315   }
317   /* Return plugin informations for acl handling
318      #FIXME You can only read attributes within this report plugin */
319   static function plInfo()
320   {
321     return (array(
322         "plShortName"   => _("Mail queue"),
323         "plDescription" => _("Mail queue addon"),
324         "plSelfModify"  => FALSE,
325         "plDepends"     => array(),
326         "plPriority"    => 1,
327         "plSection"     => array("addon"),
328         "plCategory"    => array("mailqueue" => array("objectClass" => "none", "description" => _("Mail queue addon"))),
330         "plProvidedAcls" => array(
331             "unhold_all"      => _("Unhold all messages"),
332             "hold_all"        => _("Hold all messages"),
333             "del_all"         => _("Delete all messages"),
334             "requeue_all"     => _("Requeue all messages"),
335             "unhold"          => _("Unhold message"),
336             "hold"            => _("Hold message"),
337             "del"             => _("Delete message"),
338             "requeue"         => _("Requeue message"),
339             "query"           => _("Gathering queue data"),
340             "header"          => _("Get header information")
341           )
342         ));
343   }
345   function list_get_selected_items()
346   {
347     $ids = array();
348     foreach($_POST as $name => $value){
349       if(preg_match("/^selected_*/",$name)){
350         $server = preg_replace("/^selected_.*_/","",$name) ;
351         $ids[$server][] = preg_replace("/^selected_([^_]*)_.*$/","\\1",$name);
352       }
353     }
354     return($ids);
355   }
360 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
361 ?>