Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
flatfile.php
1 <?php
17 require_once 'Mail/Queue/Container.php';
18 
19 
29 class Mail_Queue_Container_flatfile extends Mail_Queue_Container
30 {
31 
36  var $dir;
37 
38 
49  function Mail_Queue_Container_flatfile($options)
50  {
51  if (!is_array($options) || !isset($options['dir'])) {
52  return new Mail_Queue_Error(MAILQUEUE_ERROR_NO_OPTIONS,
53  $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
54  'No queue directory specified!');
55  }
56  $this->dir = $options['dir'];
57  $this->setOption();
58  }
59 
60 
67  function _preload()
68  {
69  $this->_last_item = 0;
70  $this->queue_data = array(); //reset buffer
71 
72  $queue_files = $this->list_files($this->dir);
73  foreach ($queue_files as $index => $messageID) {
74  if ($this->limit && ($index + 1 > $this->limit)) break;
75 
76  $mail_array = $this->loadFromQueue($messageID);
77  $mail_body = new Mail_Queue_Body(
78  $mail_array['id'],
79  $mail_array['create_time'],
80  $mail_array['time_to_send'],
81  $mail_array['sent_time'],
82  $mail_array['id_user'],
83  $mail_array['ip'],
84  $mail_array['sender'],
85  $mail_array['recipient'],
86  unserialize($mail_array['headers']),
87  unserialize($mail_array['body']),
88  $mail_array['delete_after_send'],
89  $mail_array['try_sent']
90  );
91  if (is_a($mail_body, 'Mail_Queue_Body')) {
92  $this->queue_data[$this->_last_item] = $mail_body;
93  $this->_last_item++;
94  }
95  }
96 
97  return true;
98 
99  }//end _preload()
100 
101 
110  function list_files($dir)
111  {
112  if (!is_dir($dir)) return Array();
113 
114  $files = Array();
115  if ($handle = opendir($dir)) {
116  while (($file = readdir($handle)) !== false) {
117  if ($file == '.' || $file == '..') {
118  continue;
119  } else if (is_file($dir.'/'.$file)) {
120  $files[] = $file;
121  }
122  }
123  closedir($handle);
124  }
125  return $files;
126 
127  }//end list_files()
128 
129 
148  function put($time_to_send, $id_user, $ip, $sender,
149  $recipient, $headers, $body, $delete_after_send=true)
150  {
151  // accommodate changes made to PEAR Mail_Queue v 1.2.1
152  $to = $recipient;
153  if (method_exists($this, '_isSerialized')) {
154  if ($this->_isSerialized($recipient)) {
155  $to = unserialize($recipient);
156  }
157  }
158 
159  // uses user_id as the custom id of mail queue files
160  $header_array = unserialize($headers);
161  if (isset($header_array['custom_id'])) {
162  $custom_id = substr($header_array['custom_id'], 0, 113);
163  $id = md5(uniqid($custom_id));
164  unset($header_array['custom_id']);
165  $headers = serialize($header_array);
166  } else {
167  $id = md5(uniqid(''));
168  }
169 
170  $array = Array(
171  'id' => $id,
172  'time_to_send' => $time_to_send,
173  'id_user' => $id_user,
174  'ip' => $ip,
175  'sender' => $sender,
176  'recipient' => $to,
177  'headers' => $headers,
178  'body' => $body,
179  'delete_after_send' => $delete_after_send,
180  'try_sent' => 0,
181  'create_time' => date("Y-m-d G:i:s", time()),
182  'sent_time' => 0,
183  );
184 
185  $this->saveToQueue($id, $array);
186 
187  return $id;
188  }
189 
198  function loadFromQueue($id) {
199  if (is_file($this->dir.'/'.$id)) {
200  require $this->dir.'/'.$id;
201  }
202  return isset($mail_entry) ? $mail_entry : Array();
203  }
204 
214  function saveToQueue($id, $contents) {
215  $output = '<?php $mail_entry = '.var_export($contents, true).'; ?>';
216  $fp = fopen($this->dir.'/'.$id, 'w');
217  $return_value = fwrite($fp, $output);
218  fclose($fp);
219  return $return_value;
220  }
221 
222 
230  function countSend($mail)
231  {
232  if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
233  return new Mail_Queue_Error('Expected: Mail_Queue_Body class',
234  __FILE__, __LINE__);
235  }
236  $count = $mail->_try();
237 
238  $mail_array = $this->loadFromQueue($mail->getId());
239  if (!empty($mail_array)) {
240  $mail_array['try_sent'] = $count;
241  $this->saveToQueue($mail->getId(), $mail_array);
242  }
243 
244  return $count;
245  }
246 
247 
255  function setAsSent($mail)
256  {
257  if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
258  return new Mail_Queue_Error(MAILQUEUE_ERROR_UNEXPECTED,
259  $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
260  'Expected: Mail_Queue_Body class');
261  }
262 
263  $mail_array = $this->loadFromQueue($mail->getId());
264  if (!empty($mail_array)) {
265  $mail_array['sent_time'] = time();
266  $this->saveToQueue($mail->getId(), $mail_array);
267  }
268 
269  return true;
270  }
271 
272 
281  function deleteMail($id)
282  {
283  // move files to .queue/.sent folder instead of deleting them
284  $success = rename($this->dir.'/'.$id, $this->dir.'/sent/'.$id);
285 
286  if ($success === false) {
287  return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
288  $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
289  'Unable to delete mail from queue (MailID #'.$id.')');
290  }
291 
292  return true;
293  }
294 
295 }
296 ?>