1 <?php
3 class parseMailQueue
4 {
5 var $s_dataToParse;
6 var $a_parsedData;
7 var $i_count;
9 /* Contructor
10 * $s_data specifies the data that shuold be parse
11 */
12 function parseMailQueue($s_data,$server)
13 {
14 $this->s_dataToParse = $s_data;
15 $this->a_parsedData = array();
16 $this->_parse($s_data,$server);
17 }
20 /* Remove all entries which are older than the last x hours
21 */
22 function OnlyDaysAgo($str)
23 {
24 /* Get current time */
25 $cur = time();
27 /* Only perform this filter, if the given parameter is valid */
28 if((is_numeric($str))&&($str != 0)){
30 /* hours are given as parameter */
31 $cur = $cur - ($str*(60*60));
33 /* Remove old entries */
34 foreach($this->a_parsedData as $key => $data){
35 if($data['Arrival'] < $cur){
36 unset($this->a_parsedData[$key]);
37 }
38 }
39 }
40 }
43 /* Only keep entries that contains the $filter
44 * in any of the given $fields
45 */
46 function Search($filter,$fields)
47 {
48 /* Go through all entries */
49 foreach($this->a_parsedData as $key => $data){
51 /* not found yet */
52 $found = false;
54 foreach($fields as $attr){
55 if(preg_match("/".str_replace("*",".*",$filter)."/i",$data[$attr])){
56 $found= true;
57 }
58 }
60 /* if nothing found, delete this entry */
61 if($found == false){
62 unset($this->a_parsedData[$key]);
63 }
64 }
65 }
67 /* Convert date from timestamp to human readable */
68 function CreateDate()
69 {
70 foreach($this->a_parsedData as $key => $data){
71 $this->a_parsedData[$key]['Arrival'] = date("d.m.Y H:i:s",$data['Arrival']);
72 }
73 }
75 /* Order by specified field */
76 function OrderBy($str = "Arrival",$type = "up" )
77 {
78 $tmp = array();
79 /* If the given field is not valid */
80 if(!in_array($str,array("MailID","Size","Sender","Recipient","Arrival","Error","Server"))){
81 return(false);
82 }
84 /* Size need special handling, cause it contains numbers
85 */
86 if($str == "Size"){
87 foreach($this->a_parsedData as $data){
88 $struse = "";
89 for($i = strlen($data['Size']); $i < 10 ; $i++ ){
90 $struse .="0";
91 }
92 $struse .= $data[$str].$data['MailID'].$data['Server'];
93 $tmp[$struse]= $data;
94 }
95 }else{
96 foreach($this->a_parsedData as $data){
97 $tmp[strtolower($data[$str]).$data['MailID']."-".$data['Server']]= $data;
98 }
99 }
100 ksort($tmp);
101 if($type != "up"){
102 $tmp = array_reverse($tmp);
103 }
104 $this->a_parsedData = array();
105 foreach($tmp as $data){
106 $this->a_parsedData[$data['MailID']."-".$data['Server']] = $data;
107 }
108 return(true);
109 }
111 function GetAll()
112 {
113 return($this->a_parsedData);
114 }
116 /* Checks if the given MailID exists */
117 function IDExists($id)
118 {
119 foreach($this->a_parsedData as $entry){
120 if($entry['MailID'] == $id) return(true);
121 }
122 return(false);
123 }
125 function parseAdditionalQueue($str, $server)
126 {
127 $this->_parse($str, $server);
128 }
130 /* This function parses the given data
131 * it creates an array with all given queue entries
132 */
133 function _parse($str, $server)
134 {
135 $i = 0; // Temp var
136 $entries = array(); // Contains an array with the raw data for every single entry
137 $s_tmp = ""; // Buffer
139 $s_mailID = ""; // Queue ID
140 $s_Size = ""; // Mail size
141 $s_Arrival = ""; // Arrival time
142 $s_Sender = ""; // Sender
143 $s_Recipient = ""; // Recipient
144 $s_Error = ""; // Occured error
146 /* Remove header
147 */
148 $this->s_dataToParse = preg_replace("/^.*------\n/","",$str);
150 /* Create array with single entries
151 */
152 $entries = split("\n\n",$this->s_dataToParse);
154 /* The last entry in this array is not realy an valid entry, its some kind of status.
155 * It would be something like this : -- 795 Kbytes in 124 Requests.
156 */
157 $this->i_count = (count($entries))-1;
159 for($i = 0 ; $i < $this->i_count; $i ++ ){
161 while(strstr($entries[$i]," ")){
162 $entries[$i] = str_replace(" "," ",$entries[$i]);
163 }
165 $s_buffer = split("\n",preg_replace("/[\\n\\r\\t]/s","\n",$entries[$i]));
167 /* Get mailID */
168 $tmp = split(" ",$s_buffer[0]);
170 /* Get values */
171 $s_mailID = $tmp[0];
172 $s_Size = $tmp[1];
173 $s_Sender = $tmp[6];
175 /* Parse time */
176 $tmp3 = split(":",$tmp[5]);
177 $tmp2 = strtotime($tmp[4]." ".$tmp[3]." ".date("Y"));
178 $s_Arrival= mktime($tmp3[0],$tmp3[1],$tmp3[2],date("d",$tmp2),date("m",$tmp2),date("Y",$tmp2));
180 $s_Error = $s_buffer[1];
181 $s_Recipient = $s_buffer[2];
183 /* Append data */
184 $this->a_parsedData[$s_mailID."-".$server]['Server'] = $server;
185 $this->a_parsedData[$s_mailID."-".$server]['MailID'] = $s_mailID;
186 $this->a_parsedData[$s_mailID."-".$server]['Size'] = $s_Size;
187 $this->a_parsedData[$s_mailID."-".$server]['Arrival'] = $s_Arrival;
188 $this->a_parsedData[$s_mailID."-".$server]['Sender'] = $s_Sender;
189 $this->a_parsedData[$s_mailID."-".$server]['Recipient'] = $s_Recipient;
190 $this->a_parsedData[$s_mailID."-".$server]['Error'] = $this->_parseError($s_Error);
191 }
192 return($this->a_parsedData);
193 }
195 /* Parse Error part of the entry */
196 function _parseError($str)
197 {
198 $str = trim(preg_replace("/[()]/","",$str));
199 $tmp2 = split(":",$str);
200 $tmp = array_reverse($tmp2);
201 $err = preg_replace("/#.*$/","",$tmp[0]);
202 $text = preg_replace("/said$/i","",trim($tmp2[0]));
203 return($err);
204 }
206 }
211 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
212 ?>