PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/standard/tags/release-1.0.4/library/Zend/Mail/Storage/Mbox.php

https://github.com/bhaumik25/zend-framework
PHP | 396 lines | 169 code | 48 blank | 179 comment | 27 complexity | 7bf6e65e41a4f56a4e3b6224e5ef88da MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to version 1.0 of the Zend Framework
  8. * license, that is bundled with this package in the file LICENSE.txt, and
  9. * is available through the world-wide-web at the following URL:
  10. * http://framework.zend.com/license/new-bsd. If you did not receive
  11. * a copy of the Zend Framework license and are unable to obtain it
  12. * through the world-wide-web, please send a note to license@zend.com
  13. * so we can mail you a copy immediately.
  14. *
  15. * @package Zend_Mail
  16. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /**
  20. * Zend_Loader
  21. * May be used in constructor, but commented out for now
  22. */
  23. // require_once 'Zend/Loader.php';
  24. /**
  25. * Zend_Mail_Storage_Abstract
  26. */
  27. require_once 'Zend/Mail/Storage/Abstract.php';
  28. /**
  29. * Zend_Mail_Message
  30. */
  31. require_once 'Zend/Mail/Message.php';
  32. /**
  33. * Zend_Mail_Storage_Exception
  34. */
  35. require_once 'Zend/Mail/Storage/Exception.php';
  36. /**
  37. * @package Zend_Mail
  38. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  39. * @license http://framework.zend.com/license/new-bsd New BSD License
  40. */
  41. class Zend_Mail_Storage_Mbox extends Zend_Mail_Storage_Abstract
  42. {
  43. /**
  44. * file handle to mbox file
  45. * @var null|resource
  46. */
  47. protected $_fh;
  48. /**
  49. * filename of mbox file for __wakeup
  50. * @var string
  51. */
  52. protected $_filename;
  53. /**
  54. * modification date of mbox file for __wakeup
  55. * @var int
  56. */
  57. protected $_filemtime;
  58. /**
  59. * start and end position of messages as array('start' => start, 'seperator' => headersep, 'end' => end)
  60. * @var array
  61. */
  62. protected $_positions;
  63. /**
  64. * Count messages all messages in current box
  65. *
  66. * @return int number of messages
  67. * @throws Zend_Mail_Storage_Exception
  68. */
  69. public function countMessages()
  70. {
  71. return count($this->_positions);
  72. }
  73. /**
  74. * Get a list of messages with number and size
  75. *
  76. * @param int|null $id number of message or null for all messages
  77. * @return int|array size of given message of list with all messages as array(num => size)
  78. */
  79. public function getSize($id = 0)
  80. {
  81. if ($id) {
  82. $pos = $this->_positions[$id - 1];
  83. return $pos['end'] - $pos['start'];
  84. }
  85. $result = array();
  86. foreach ($this->_positions as $num => $pos) {
  87. $result[$num + 1] = $pos['end'] - $pos['start'];
  88. }
  89. return $result;
  90. }
  91. /**
  92. * Get positions for mail message or throw exeption if id is invalid
  93. *
  94. * @param int $id number of message
  95. * @return array positions as in _positions
  96. * @throws Zend_Mail_Storage_Exception
  97. */
  98. protected function _getPos($id)
  99. {
  100. if (!isset($this->_positions[$id - 1])) {
  101. throw new Zend_Mail_Storage_Exception('id does not exist');
  102. }
  103. return $this->_positions[$id - 1];
  104. }
  105. /**
  106. * Fetch a message
  107. *
  108. * @param int $id number of message
  109. * @return Zend_Mail_Message
  110. * @throws Zend_Mail_Storage_Exception
  111. */
  112. public function getMessage($id)
  113. {
  114. $bodyLines = 0; // TODO: need a way to change that
  115. $message = $this->getRawHeader($id);
  116. // file pointer is after headers now
  117. if ($bodyLines) {
  118. $message .= "\n";
  119. while ($bodyLines-- && ftell($this->_fh) < $this->_positions[$id - 1]['end']) {
  120. $message .= fgets($this->_fh);
  121. }
  122. }
  123. return new $this->_messageClass(array('handler' => $this, 'id' => $id, 'headers' => $message));
  124. }
  125. /*
  126. * Get raw header of message or part
  127. *
  128. * @param int $id number of message
  129. * @param null|array|string $part path to part or null for messsage header
  130. * @param int $topLines include this many lines with header (after an empty line)
  131. * @return string raw header
  132. * @throws Zend_Mail_Protocol_Exception
  133. */
  134. public function getRawHeader($id, $part = null, $topLines = 0)
  135. {
  136. if ($part !== null) {
  137. // TODO: implement
  138. throw new Zend_Mail_Storage_Exception('not implemented');
  139. }
  140. $messagePos = $this->_getPos($id);
  141. // TODO: toplines
  142. return stream_get_contents($this->_fh, $messagePos['separator'] - $messagePos['start'], $messagePos['start']);
  143. }
  144. /*
  145. * Get raw content of message or part
  146. *
  147. * @param int $id number of message
  148. * @param null|array|string $part path to part or null for messsage content
  149. * @return string raw content
  150. * @throws Zend_Mail_Protocol_Exception
  151. */
  152. public function getRawContent($id, $part = null)
  153. {
  154. if ($part !== null) {
  155. // TODO: implement
  156. throw new Zend_Mail_Storage_Exception('not implemented');
  157. }
  158. $messagePos = $this->_getPos($id);
  159. return stream_get_contents($this->_fh, $messagePos['end'] - $messagePos['separator'], $messagePos['separator']);
  160. }
  161. /**
  162. * Create instance with parameters
  163. * Supported parameters are:
  164. * - filename filename of mbox file
  165. *
  166. * @param $params array mail reader specific parameters
  167. * @throws Zend_Mail_Storage_Exception
  168. */
  169. public function __construct($params)
  170. {
  171. if (!isset($params['filename']) /* || Zend_Loader::isReadable($params['filename']) */) {
  172. throw new Zend_Mail_Storage_Exception('no valid filename given in params');
  173. }
  174. $this->_openMboxFile($params['filename']);
  175. $this->_has['top'] = true;
  176. $this->_has['uniqueid'] = false;
  177. }
  178. /**
  179. * check if given file is a mbox file
  180. *
  181. * if $file is a resource its file pointer is moved after the first line
  182. *
  183. * @param resource|string $file stream resource of name of file
  184. * @param bool $fileIsString file is string or resource
  185. * @return bool file is mbox file
  186. */
  187. protected function _isMboxFile($file, $fileIsString = true)
  188. {
  189. if ($fileIsString) {
  190. $file = @fopen($file, 'r');
  191. if (!$file) {
  192. return false;
  193. }
  194. } else {
  195. fseek($file, 0);
  196. }
  197. $result = false;
  198. $line = fgets($file);
  199. if (strpos($line, 'From ') === 0) {
  200. $result = true;
  201. }
  202. if ($fileIsString) {
  203. @fclose($file);
  204. }
  205. return $result;
  206. }
  207. /**
  208. * open given file as current mbox file
  209. *
  210. * @param string $filename filename of mbox file
  211. * @return null
  212. * @throws Zend_Mail_Storage_Exception
  213. */
  214. protected function _openMboxFile($filename)
  215. {
  216. if ($this->_fh) {
  217. $this->close();
  218. }
  219. $this->_fh = @fopen($filename, 'r');
  220. if (!$this->_fh) {
  221. throw new Zend_Mail_Storage_Exception('cannot open mbox file');
  222. }
  223. $this->_filename = $filename;
  224. $this->_filemtime = filemtime($this->_filename);
  225. if (!$this->_isMboxFile($this->_fh, false)) {
  226. @fclose($this->_fh);
  227. throw new Zend_Mail_Storage_Exception('file is not a valid mbox format');
  228. }
  229. $messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0);
  230. while (($line = fgets($this->_fh)) !== false) {
  231. if (strpos($line, 'From ') === 0) {
  232. $messagePos['end'] = ftell($this->_fh) - strlen($line) - 2; // + newline
  233. if (!$messagePos['separator']) {
  234. $messagePos['separator'] = $messagePos['end'];
  235. }
  236. $this->_positions[] = $messagePos;
  237. $messagePos = array('start' => ftell($this->_fh), 'separator' => 0, 'end' => 0);
  238. }
  239. if (!$messagePos['separator'] && !trim($line)) {
  240. $messagePos['separator'] = ftell($this->_fh);
  241. }
  242. }
  243. $messagePos['end'] = ftell($this->_fh);
  244. if (!$messagePos['separator']) {
  245. $messagePos['separator'] = $messagePos['end'];
  246. }
  247. $this->_positions[] = $messagePos;
  248. }
  249. /**
  250. * Close resource for mail lib. If you need to control, when the resource
  251. * is closed. Otherwise the destructor would call this.
  252. *
  253. * @return void
  254. */
  255. public function close()
  256. {
  257. @fclose($this->_fh);
  258. $this->_positions = array();
  259. }
  260. /**
  261. * Waste some CPU cycles doing nothing.
  262. *
  263. * @return void
  264. */
  265. public function noop()
  266. {
  267. return true;
  268. }
  269. /**
  270. * stub for not supported message deletion
  271. *
  272. * @return null
  273. * @throws Zend_Mail_Storage_Exception
  274. */
  275. public function removeMessage($id)
  276. {
  277. throw new Zend_Mail_Storage_Exception('mbox is read-only');
  278. }
  279. /**
  280. * get unique id for one or all messages
  281. *
  282. * Mbox does not support unique ids (yet) - it's always the same as the message number.
  283. * That shouldn't be a problem, because we can't change mbox files. Therefor the message
  284. * number is save enough.
  285. *
  286. * @param int|null $id message number
  287. * @return array|string message number for given message or all messages as array
  288. * @throws Zend_Mail_Storage_Exception
  289. */
  290. public function getUniqueId($id = null)
  291. {
  292. if ($id) {
  293. // check if id exists
  294. $this->_getPos($id);
  295. return $id;
  296. }
  297. $range = range(1, $this->countMessages());
  298. return array_combine($range, $range);
  299. }
  300. /**
  301. * get a message number from a unique id
  302. *
  303. * I.e. if you have a webmailer that supports deleting messages you should use unique ids
  304. * as parameter and use this method to translate it to message number right before calling removeMessage()
  305. *
  306. * @param string $id unique id
  307. * @return int message number
  308. * @throws Zend_Mail_Storage_Exception
  309. */
  310. public function getNumberByUniqueId($id)
  311. {
  312. // check if id exists
  313. $this->_getPos($id);
  314. return $id;
  315. }
  316. /**
  317. * magic method for serialize()
  318. *
  319. * with this method you can cache the mbox class
  320. *
  321. * @return array name of variables
  322. */
  323. public function __sleep()
  324. {
  325. return array('_filename', '_positions', '_filemtime');
  326. }
  327. /**
  328. * magic method for unserialize()
  329. *
  330. * with this method you can cache the mbox class
  331. * for cache validation the mtime of the mbox file is used
  332. *
  333. * @return null
  334. * @throws Zend_Mail_Storage_Exception
  335. */
  336. public function __wakeup()
  337. {
  338. if ($this->_filemtime != @filemtime($this->_filename)) {
  339. $this->close();
  340. $this->_openMboxFile($this->_filename);
  341. } else {
  342. $this->_fh = @fopen($this->_filename, 'r');
  343. if (!$this->_fh) {
  344. throw new Zend_Mail_Storage_Exception('cannot open mbox file');
  345. }
  346. }
  347. }
  348. }