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 }
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 $this->disp_header = $data;
104 if($this->si_queue->is_error()){
105 msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
106 $this->disp_header = FALSE;
107 }
108 }
110 /* Back is posted from the header display page */
111 if(isset($_POST['back'])){
112 $this->disp_header = false;
113 }
115 /* If there is a header in disp_header, then display it */
116 if($this->disp_header){
117 $smarty->assign("header",$this->disp_header);
118 return ($smarty->fetch (get_template_path('header.tpl', TRUE)));
119 }
122 /******************
123 Query mailqueues
124 ******************/
126 $within_minutes = -1;
127 if($this->Time != "nolimit"){
128 $within_minutes = 60*60*$this->Time;
129 }
131 if($this->Server == "all"){
132 $entries = array();
133 foreach($this->ServerList as $mac => $name){
134 if(!tests::is_mac($mac)) continue;
135 $entries = array_merge($entries,$this->si_queue->query_mailqueue(
136 $mac,$this->OrderBy,$this->SortType,$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(
143 $this->Server,$this->OrderBy,$this->SortType,$this->Search,$within_minutes);
144 if($this->si_queue->is_error()){
145 msg_dialog::display(_("Error"),msgPool::siError($this->si_queue->get_error()),ERROR_DIALOG);
146 }
147 }
149 $count = count($entries);
150 $entries = array_slice($entries,$this->Page,$this->range);
152 /* Add ServerName to results
153 */
154 foreach($entries as $key => $data){
155 $entries[$key]['ServerName'] = $this->ServerList[$data['Server']];
156 }
158 /******************
159 create html output
160 ******************/
162 $smarty->assign("all_ok" , count($entries));
163 $smarty->assign("entries" , $entries);
164 $smarty->assign("plug" , "?plug=".$_GET['plug']);
166 $smarty->assign("r_stats" , $this->getStats());
167 $smarty->assign("stats" , array_flip($this->getStats()));
169 $smarty->assign("stat" , $this->Stat);
170 $smarty->assign("p_server" , $this->Server);
171 $smarty->assign("p_servers" , $this->ServerList);
172 $smarty->assign("p_serverKeys" , array_flip($this->ServerList));
173 $smarty->assign("p_time" , $this->Time);
174 $smarty->assign("p_times" , $this->getTimes());
175 $smarty->assign("p_timeKeys" , array_flip($this->getTimes()));
176 $smarty->assign("search_for" , $this->Search);
177 $smarty->assign("range_selector", range_selector($count, $this->Page, $this->range,"EntriesPerPage"));
178 $smarty->assign("OrderBy" , $this->OrderBy);
180 /* Display sort arrow */
181 if($this->SortType == "up"){
182 $smarty->assign("SortType","<img src='images/lists/sort-up.png' alt='"._("up")."' border='0'>");
183 }else{
184 $smarty->assign("SortType","<img src='images/lists/sort-down.png' alt='"._("down")."' border='0'>");
185 }
186 return ($smarty->fetch (get_template_path('contents.tpl', TRUE)));
187 }
190 /* return selectable server
191 */
192 function getServer()
193 {
194 $ret= array("all"=>_("All"));
196 /* First of all, detect all servers that supports the mailqueue extension
197 -If this fails, the mailqueue(s) can't be queried.
198 */
199 $hosts = $this->si_queue->get_hosts_with_module("mailqueue_com");
200 $this->si_error = $this->si_queue->is_error();
201 if(!count($hosts)){
202 return(array());
203 }
205 /* Create search filter and try to resolv mac to hostname
206 */
207 $filter = "";
208 foreach($hosts as $mac){
209 $filter .= "(macAddress=".$mac.")";
210 }
211 $filter = "(&(objectClass=GOhard)(|".$filter."))";
212 $res = get_list($filter,"no_acls",$this->config->current['BASE'],
213 array("cn","macAddress"),GL_SUBSEARCH | GL_NO_ACL_CHECK);
215 /* Create result array
216 */
217 foreach($hosts as $mac){
218 $found = FALSE;
219 foreach($res as $entry){
220 if(preg_match("/^".normalizePreg($mac)."$/i",$entry['macAddress'][0])){
221 $ret[$mac] = $entry['cn'][0];
222 $found = TRUE;
223 break;
224 }
225 }
226 if(!$found){
227 $ret[$mac] = $mac;
228 }
229 }
230 $this->ServerList = $ret;
231 }
234 /* Return selectable times*/
235 function getTimes()
236 {
237 $ret = array();
238 $ret['nolimit']=_("no limit");
239 foreach(array(1,2,4,8,12,24,36,48) as $i){
240 if($i == 1){
241 $ret[$i] = $i." "._("hour");
242 }else{
243 $ret[$i] = $i." "._("hours");
244 }
245 }
246 return($ret);
247 }
250 /* Save post values*/
251 function save_object($save_current= FALSE)
252 {
253 /* Update amount of entries displayed */
254 if(isset($_POST['EntriesPerPage'])){
255 $this->range = $_POST['EntriesPerPage'];
256 }
258 if(isset($_POST['p_server']) && isset($this->ServerList[$_POST['p_server']])){
259 $this->Server = $_POST['p_server'];
260 }
262 if(isset($_POST['p_time'])){
263 $this->Time = $_POST['p_time'];
264 }
265 if(isset($_POST['search_for'])){
266 $this->Search = $_POST['search_for'];
267 }
268 if(isset($_POST['Stat'])){
269 $this->Stat = $_POST['Stat'];
270 }
271 if((isset($_GET['start']))&&(is_numeric($_GET['start']))&&($_GET['start']>=0)){
272 $this->Page = $_GET['start'];
273 }
275 if((isset($_GET['sort']))&&(!empty($_GET['sort']))){
276 $old = $this->OrderBy;
277 $this->OrderBy = $_GET['sort'];
278 if($this->OrderBy == $old)
279 {
280 if($this->SortType== "up"){
281 $this->SortType = "down";
282 }else{
283 $this->SortType = "up";
284 }
285 }
286 }
288 }
290 /* Return stats */
291 function getStats()
292 {
293 return(array(
294 "all" =>_("All"),
295 "hold" =>_("Hold"),
296 "unhold" =>_("Un hold"),
297 "active" =>_("Active"),
298 "nonactive" =>_("Not active")
299 ));
300 }
302 /* Return plugin informations for acl handling
303 #FIXME You can only read attributes within this report plugin */
304 static function plInfo()
305 {
306 return (array(
307 "plShortName" => _("Mail queue"),
308 "plDescription" => _("Mail queue addon"),
309 "plSelfModify" => FALSE,
310 "plDepends" => array(),
311 "plPriority" => 1,
312 "plSection" => array("addon"),
313 "plCategory" => array("mailqueue" => array("objectClass" => "none", "description" => _("Mail queue addon"))),
315 "plProvidedAcls" => array(
316 "unhold_all" => _("Unhold all messages"),
317 "hold_all" => _("Hold all messages"),
318 "del_all" => _("Delete all messages"),
319 "requeue_all" => _("Requeue all messages"),
320 "unhold" => _("Unhold message"),
321 "hold" => _("Hold message"),
322 "del" => _("Delete message"),
323 "requeue" => _("Requeue message"),
324 "query" => _("Gathering queue data"),
325 "header" => _("Get header information")
326 )
327 ));
328 }
330 function list_get_selected_items()
331 {
332 $ids = array();
333 foreach($_POST as $name => $value){
334 if(preg_match("/^selected_*/",$name)){
335 $server = preg_replace("/^selected_.*_/","",$name) ;
336 $ids[$server][] = preg_replace("/^selected_([^_]*)_.*$/","\\1",$name);
337 }
338 }
339 return($ids);
340 }
344 }
345 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
346 ?>